Skip to content

Commit

Permalink
Applied formating patches and removed unecessary comment
Browse files Browse the repository at this point in the history
  • Loading branch information
pshobowale committed Jan 23, 2025
1 parent 2594c16 commit b3964fd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 91 deletions.
93 changes: 45 additions & 48 deletions src/displayapp/screens/Adder.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "displayapp/DisplayApp.h"
#include "displayapp/screens/Adder.h"
#include <cstdlib> // For std::rand
#include <algorithm> // For std::max
#include <cstdlib> // For std::rand
#include <algorithm> // For std::max

using namespace Pinetime::Applications::Screens;

Adder::Adder(Pinetime::Components::LittleVgl& lvgl, Controllers::FS& fs)
: lvgl(lvgl), filesystem(fs) {
Adder::Adder(Pinetime::Components::LittleVgl& lvgl, Controllers::FS& fs) : lvgl(lvgl), filesystem(fs) {
InitializeGame();
}

Expand Down Expand Up @@ -34,14 +33,17 @@ void Adder::InitializeGame() {
InitializeBody();
CreateLevel();

refreshTask = lv_task_create([](lv_task_t* task) {
refreshTask = lv_task_create(
[](lv_task_t* task) {
auto* adder = static_cast<Adder*>(task->user_data);
adder->Refresh();
},
AdderDelayInterval, LV_TASK_PRIO_MID, this);
AdderDelayInterval,
LV_TASK_PRIO_MID,
this);

appReady = false;
vTaskDelay(20);
vTaskDelay(20);
}

void Adder::Cleanup() {
Expand Down Expand Up @@ -86,7 +88,7 @@ void Adder::ResetGame() {
GameOver();
appReady = false;
highScore = std::max(highScore, static_cast<unsigned int>(adderBody.size() - 2));
data.HighScore=highScore;
data.HighScore = highScore;
SaveGame();

CreateLevel();
Expand All @@ -101,7 +103,7 @@ void Adder::InitializeBody() {
unsigned int startPosition = (fieldHeight / 2) * fieldWidth + fieldWidth / 2 + 2;
adderBody = {startPosition, startPosition - 1};

currentDirection = 1; // Start moving to the right
currentDirection = 1; // Start moving to the right
prevDirection = currentDirection;
}

Expand Down Expand Up @@ -147,8 +149,7 @@ bool Adder::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
currentDirection = 1;
break;
case TouchEvents::LongTap:
FullRedraw(); // Adjusted to method spelled as "FullRedraw"
break;
FullRedraw();
default:
break;
}
Expand All @@ -163,26 +164,25 @@ bool Adder::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
prevDirection = currentDirection;
}

return true; // Return true to indicate the touch event was handled
return true; // Return true to indicate the touch event was handled
}


void Adder::UpdatePosition() {
unsigned int newHead = adderBody.front() + currentDirection;
Adder::MoveConsequence result = CheckMove(); // Fully qualify MoveConsequence
Adder::MoveConsequence result = CheckMove();

switch (result) {
case Adder::MoveConsequence::DEATH: // Fully qualify
case Adder::MoveConsequence::DEATH:
ResetGame();
return;

case Adder::MoveConsequence::EAT: // Fully qualify
case Adder::MoveConsequence::EAT:
adderBody.push_front(newHead);
CreateFood();
UpdateScore(adderBody.size() - 2);
break;

case Adder::MoveConsequence::MOVE: // Fully qualify
case Adder::MoveConsequence::MOVE:
adderBody.pop_back();
adderBody.push_front(newHead);
break;
Expand All @@ -195,33 +195,33 @@ void Adder::UpdatePosition() {
Adder::MoveConsequence Adder::CheckMove() const {
unsigned int newHead = adderBody.front() + currentDirection;
if (newHead >= fieldSize) {
return Adder::MoveConsequence::DEATH; // Fully qualify
return Adder::MoveConsequence::DEATH;
}

switch (field[newHead]) {
case AdderField::BLANK:
return Adder::MoveConsequence::MOVE; // Fully qualify
return Adder::MoveConsequence::MOVE;
case AdderField::FOOD:
return Adder::MoveConsequence::EAT; // Fully qualify
return Adder::MoveConsequence::EAT;
default:
return Adder::MoveConsequence::DEATH; // Fully qualify
return Adder::MoveConsequence::DEATH;
}
}

void Adder::Refresh() {
if (!appReady) {
FullRedraw();
CreateFood();
vTaskDelay(1); //Required to let the OS draw the tile completely
vTaskDelay(1); // Required to let the OS draw the tile completely
UpdateScore(0);
vTaskDelay(1); //Required to let the OS draw the tile completely
vTaskDelay(1); // Required to let the OS draw the tile completely
appReady = true;
} else {
UpdatePosition();
UpdateSingleTile(adderBody.front() % fieldWidth, adderBody.front() / fieldWidth, LV_COLOR_YELLOW);
vTaskDelay(1); //Required to let the OS draw the tile completely
vTaskDelay(1); // Required to let the OS draw the tile completely
UpdateSingleTile(adderBody.back() % fieldWidth, adderBody.back() / fieldWidth, LV_COLOR_BLACK);
vTaskDelay(1); //Required to let the OS draw the tile completely
vTaskDelay(1); // Required to let the OS draw the tile completely
}
}

Expand All @@ -244,25 +244,23 @@ void Adder::FullRedraw() {
break;
}
UpdateSingleTile(x, y, color);
vTaskDelay(1); //Required to let the OS draw the tile completely
vTaskDelay(1); // Required to let the OS draw the tile completely
}
}
}

void Adder::UpdateSingleTile(unsigned int x, unsigned int y, lv_color_t color) {
std::fill(tileBuffer, tileBuffer + TileSize * TileSize, color);
lv_area_t area {
.x1 = static_cast<lv_coord_t>(x * TileSize + fieldOffsetHorizontal),
.y1 = static_cast<lv_coord_t>(y * TileSize + fieldOffsetVertical),
.x2 = static_cast<lv_coord_t>(x * TileSize + fieldOffsetHorizontal + TileSize - 1),
.y2 = static_cast<lv_coord_t>(y * TileSize + fieldOffsetVertical + TileSize - 1)
};
lv_area_t area {.x1 = static_cast<lv_coord_t>(x * TileSize + fieldOffsetHorizontal),
.y1 = static_cast<lv_coord_t>(y * TileSize + fieldOffsetVertical),
.x2 = static_cast<lv_coord_t>(x * TileSize + fieldOffsetHorizontal + TileSize - 1),
.y2 = static_cast<lv_coord_t>(y * TileSize + fieldOffsetVertical + TileSize - 1)};

lvgl.FlushDisplay(&area, tileBuffer);
}

void Adder::GameOver() {
unsigned int digits[] = { 7, 0, 5, 3 }; // Digits forming the "GAME OVER" display
unsigned int digits[] = {7, 0, 5, 3}; // Digits forming the "GAME OVER" display

// Determine offset based on field dimensions
unsigned int offset = fieldOffsetHorizontal > fieldOffsetVertical ? fieldOffsetHorizontal : fieldOffsetVertical;
Expand All @@ -272,7 +270,9 @@ void Adder::GameOver() {
for (unsigned int i = 0; i < 4; i++) {
for (unsigned int j = 0; j < 64; j++) {
// Map font bits into the display buffer
digitBuffer[63 - j] = (DigitFont[digits[i]][j / 8] & 1 << j % 8) ? LV_COLOR_WHITE : LV_COLOR_BLACK; // Bitmagic to rotate the Digits to look like Letters
digitBuffer[63 - j] = (DigitFont[digits[i]][j / 8] & 1 << j % 8)
? LV_COLOR_WHITE
: LV_COLOR_BLACK; // Bitmagic to rotate the Digits to look like Letters
}

lv_area_t area;
Expand All @@ -283,14 +283,14 @@ void Adder::GameOver() {

lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::None);
lvgl.FlushDisplay(&area, digitBuffer);
vTaskDelay(1); //Required to let the OS draw the tile completely
vTaskDelay(1); // Required to let the OS draw the tile completely
}
}
}

void Adder::UpdateScore(unsigned int score) {
// Extract individual digits of the score
unsigned int digits[] = { 0, score % 10, (score % 100 - score % 10) / 10, (score - score % 100) / 100 };
unsigned int digits[] = {0, score % 10, (score % 100 - score % 10) / 10, (score - score % 100) / 100};

// Render the score
for (unsigned int i = 0; i < 4; i++) {
Expand All @@ -300,24 +300,22 @@ void Adder::UpdateScore(unsigned int score) {
}

lv_area_t area;
area.x1 = displayWidth - 16 - 8 * i; // Adjust X to display digits
area.x1 = displayWidth - 16 - 8 * i; // Adjust X to display digits
area.y1 = 4; // Y-offset for Score
area.x2 = area.x1 + 7;
area.y2 = area.y1 + 7;

lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::None);
lvgl.FlushDisplay(&area, digitBuffer);
vTaskDelay(20); // Small delay to allow display refresh
vTaskDelay(20); // Small delay to allow display refresh
}

// Update the high score if necessary
unsigned int highScoreToWrite = (highScore > score) ? highScore : score;
unsigned int highScoreDigits[] = {
0,
highScoreToWrite % 10,
(highScoreToWrite % 100 - highScoreToWrite % 10) / 10,
(highScoreToWrite - highScoreToWrite % 100) / 100
};
unsigned int highScoreDigits[] = {0,
highScoreToWrite % 10,
(highScoreToWrite % 100 - highScoreToWrite % 10) / 10,
(highScoreToWrite - highScoreToWrite % 100) / 100};

// Render the high score
for (unsigned int i = 0; i < 4; i++) {
Expand All @@ -327,17 +325,16 @@ void Adder::UpdateScore(unsigned int score) {
}

lv_area_t area;
area.x1 = 40 - 8 * i; // Adjust X to display digits
area.y1 = 4; // Y-offset for High Score
area.x1 = 40 - 8 * i; // Adjust X to display digits
area.y1 = 4; // Y-offset for High Score
area.x2 = area.x1 + 7;
area.y2 = area.y1 + 7;

lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::None);
lvgl.FlushDisplay(&area, digitBuffer);
vTaskDelay(20); // Small delay to allow display refresh
vTaskDelay(20); // Small delay to allow display refresh
}

// Save the high score if it has changed
highScore = highScoreToWrite;
}

80 changes: 37 additions & 43 deletions src/displayapp/screens/Adder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@ namespace Pinetime {
constexpr unsigned int AdderVersion = 1;

struct AdderSave {
unsigned int Level{0};
unsigned int HighScore{0};
unsigned int Version{AdderVersion};
unsigned int Level {0};
unsigned int HighScore {0};
unsigned int Version {AdderVersion};
};

enum class AdderField { UNDEFINED, BLANK, SOLID, BODY, FOOD };


class Adder : public Screen {

public:
Adder(Pinetime::Components::LittleVgl& lvgl, Pinetime::Controllers::FS& fs);
~Adder() override;


enum class MoveConsequence { DEATH, EAT, MOVE };

// Overridden functions
Expand All @@ -42,34 +40,33 @@ namespace Pinetime {
static constexpr unsigned int TileSize = 9;
static constexpr unsigned int AdderDelayInterval = 200;


Pinetime::Components::LittleVgl& lvgl;
Controllers::FS& filesystem;

AdderSave data; // Game save data
AdderField* field{nullptr};
AdderField* field {nullptr};

lv_task_t* refreshTask{nullptr};
lv_color_t* tileBuffer{nullptr};
lv_task_t* refreshTask {nullptr};
lv_color_t* tileBuffer {nullptr};
lv_color_t digitBuffer[64];

unsigned int displayHeight{0};
unsigned int displayWidth{0};
unsigned int fieldWidth{0};
unsigned int fieldHeight{0};
unsigned int fieldSize{0};
unsigned int highScore{2};
unsigned int displayHeight {0};
unsigned int displayWidth {0};
unsigned int fieldWidth {0};
unsigned int fieldHeight {0};
unsigned int fieldSize {0};
unsigned int highScore {2};

unsigned int fieldOffsetHorizontal{0};
unsigned int fieldOffsetVertical{0};
unsigned int fieldOffsetHorizontal {0};
unsigned int fieldOffsetVertical {0};

std::list<unsigned int> adderBody;
std::vector<unsigned int> blanks;

int prevDirection{0};
int currentDirection{1};
int prevDirection {0};
int currentDirection {1};

bool appReady{false};
bool appReady {false};

// Methods
void InitializeGame();
Expand All @@ -80,44 +77,41 @@ namespace Pinetime {
void InitializeBody();
void CreateFood();
void CreateLevel();

void UpdatePosition();
void FullRedraw();
void UpdateSingleTile(unsigned int fieldX, unsigned int fieldY, lv_color_t color);
void UpdateScore(unsigned int score);
void HandleGameOver();
void GameOver();

MoveConsequence CheckMove() const;

void Cleanup(); // Proper clean-up of allocated resources

void updateScore(unsigned int score); // Updates the score and high score
void GameOver(); // Handles game-over logic

static constexpr const char DigitFont[10][8] = { // Font for digits 0-9
{0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00}, // 0
{0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00}, // 1
{0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00}, // 2
{0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00}, // 3
{0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00}, // 4
{0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00}, // 5
{0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00}, // 6
{0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00}, // 7
{0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00}, // 8
{0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00} // 9

static constexpr const char DigitFont[10][8] = {
// Font for digits 0-9
{0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00}, // 0
{0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00}, // 1
{0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00}, // 2
{0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00}, // 3
{0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00}, // 4
{0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00}, // 5
{0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00}, // 6
{0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00}, // 7
{0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00}, // 8
{0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00} // 9
};
};
} // namespace Screens
} // namespace Screens

// Application Traits
template <>
struct AppTraits<Apps::Adder> {
static constexpr Apps app = Apps::Adder;
static constexpr const char* icon = "S";

static Screens::Screen* Create(AppControllers& controllers) {
return new Screens::Adder(controllers.lvgl, controllers.filesystem);
}
};
} // namespace Applications
} // namespace Pinetime

} // namespace Applications
} // namespace Pinetime

0 comments on commit b3964fd

Please sign in to comment.