Skip to content

Commit b3964fd

Browse files
committed
Applied formating patches and removed unecessary comment
1 parent 2594c16 commit b3964fd

File tree

2 files changed

+82
-91
lines changed

2 files changed

+82
-91
lines changed

src/displayapp/screens/Adder.cpp

+45-48
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#include "displayapp/DisplayApp.h"
22
#include "displayapp/screens/Adder.h"
3-
#include <cstdlib> // For std::rand
4-
#include <algorithm> // For std::max
3+
#include <cstdlib> // For std::rand
4+
#include <algorithm> // For std::max
55

66
using namespace Pinetime::Applications::Screens;
77

8-
Adder::Adder(Pinetime::Components::LittleVgl& lvgl, Controllers::FS& fs)
9-
: lvgl(lvgl), filesystem(fs) {
8+
Adder::Adder(Pinetime::Components::LittleVgl& lvgl, Controllers::FS& fs) : lvgl(lvgl), filesystem(fs) {
109
InitializeGame();
1110
}
1211

@@ -34,14 +33,17 @@ void Adder::InitializeGame() {
3433
InitializeBody();
3534
CreateLevel();
3635

37-
refreshTask = lv_task_create([](lv_task_t* task) {
36+
refreshTask = lv_task_create(
37+
[](lv_task_t* task) {
3838
auto* adder = static_cast<Adder*>(task->user_data);
3939
adder->Refresh();
4040
},
41-
AdderDelayInterval, LV_TASK_PRIO_MID, this);
41+
AdderDelayInterval,
42+
LV_TASK_PRIO_MID,
43+
this);
4244

4345
appReady = false;
44-
vTaskDelay(20);
46+
vTaskDelay(20);
4547
}
4648

4749
void Adder::Cleanup() {
@@ -86,7 +88,7 @@ void Adder::ResetGame() {
8688
GameOver();
8789
appReady = false;
8890
highScore = std::max(highScore, static_cast<unsigned int>(adderBody.size() - 2));
89-
data.HighScore=highScore;
91+
data.HighScore = highScore;
9092
SaveGame();
9193

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

104-
currentDirection = 1; // Start moving to the right
106+
currentDirection = 1; // Start moving to the right
105107
prevDirection = currentDirection;
106108
}
107109

@@ -147,8 +149,7 @@ bool Adder::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
147149
currentDirection = 1;
148150
break;
149151
case TouchEvents::LongTap:
150-
FullRedraw(); // Adjusted to method spelled as "FullRedraw"
151-
break;
152+
FullRedraw();
152153
default:
153154
break;
154155
}
@@ -163,26 +164,25 @@ bool Adder::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
163164
prevDirection = currentDirection;
164165
}
165166

166-
return true; // Return true to indicate the touch event was handled
167+
return true; // Return true to indicate the touch event was handled
167168
}
168169

169-
170170
void Adder::UpdatePosition() {
171171
unsigned int newHead = adderBody.front() + currentDirection;
172-
Adder::MoveConsequence result = CheckMove(); // Fully qualify MoveConsequence
172+
Adder::MoveConsequence result = CheckMove();
173173

174174
switch (result) {
175-
case Adder::MoveConsequence::DEATH: // Fully qualify
175+
case Adder::MoveConsequence::DEATH:
176176
ResetGame();
177177
return;
178178

179-
case Adder::MoveConsequence::EAT: // Fully qualify
179+
case Adder::MoveConsequence::EAT:
180180
adderBody.push_front(newHead);
181181
CreateFood();
182182
UpdateScore(adderBody.size() - 2);
183183
break;
184184

185-
case Adder::MoveConsequence::MOVE: // Fully qualify
185+
case Adder::MoveConsequence::MOVE:
186186
adderBody.pop_back();
187187
adderBody.push_front(newHead);
188188
break;
@@ -195,33 +195,33 @@ void Adder::UpdatePosition() {
195195
Adder::MoveConsequence Adder::CheckMove() const {
196196
unsigned int newHead = adderBody.front() + currentDirection;
197197
if (newHead >= fieldSize) {
198-
return Adder::MoveConsequence::DEATH; // Fully qualify
198+
return Adder::MoveConsequence::DEATH;
199199
}
200200

201201
switch (field[newHead]) {
202202
case AdderField::BLANK:
203-
return Adder::MoveConsequence::MOVE; // Fully qualify
203+
return Adder::MoveConsequence::MOVE;
204204
case AdderField::FOOD:
205-
return Adder::MoveConsequence::EAT; // Fully qualify
205+
return Adder::MoveConsequence::EAT;
206206
default:
207-
return Adder::MoveConsequence::DEATH; // Fully qualify
207+
return Adder::MoveConsequence::DEATH;
208208
}
209209
}
210210

211211
void Adder::Refresh() {
212212
if (!appReady) {
213213
FullRedraw();
214214
CreateFood();
215-
vTaskDelay(1); //Required to let the OS draw the tile completely
215+
vTaskDelay(1); // Required to let the OS draw the tile completely
216216
UpdateScore(0);
217-
vTaskDelay(1); //Required to let the OS draw the tile completely
217+
vTaskDelay(1); // Required to let the OS draw the tile completely
218218
appReady = true;
219219
} else {
220220
UpdatePosition();
221221
UpdateSingleTile(adderBody.front() % fieldWidth, adderBody.front() / fieldWidth, LV_COLOR_YELLOW);
222-
vTaskDelay(1); //Required to let the OS draw the tile completely
222+
vTaskDelay(1); // Required to let the OS draw the tile completely
223223
UpdateSingleTile(adderBody.back() % fieldWidth, adderBody.back() / fieldWidth, LV_COLOR_BLACK);
224-
vTaskDelay(1); //Required to let the OS draw the tile completely
224+
vTaskDelay(1); // Required to let the OS draw the tile completely
225225
}
226226
}
227227

@@ -244,25 +244,23 @@ void Adder::FullRedraw() {
244244
break;
245245
}
246246
UpdateSingleTile(x, y, color);
247-
vTaskDelay(1); //Required to let the OS draw the tile completely
247+
vTaskDelay(1); // Required to let the OS draw the tile completely
248248
}
249249
}
250250
}
251251

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

261259
lvgl.FlushDisplay(&area, tileBuffer);
262260
}
263261

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

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

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

284284
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::None);
285285
lvgl.FlushDisplay(&area, digitBuffer);
286-
vTaskDelay(1); //Required to let the OS draw the tile completely
286+
vTaskDelay(1); // Required to let the OS draw the tile completely
287287
}
288288
}
289289
}
290290

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

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

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

308308
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::None);
309309
lvgl.FlushDisplay(&area, digitBuffer);
310-
vTaskDelay(20); // Small delay to allow display refresh
310+
vTaskDelay(20); // Small delay to allow display refresh
311311
}
312312

313313
// Update the high score if necessary
314314
unsigned int highScoreToWrite = (highScore > score) ? highScore : score;
315-
unsigned int highScoreDigits[] = {
316-
0,
317-
highScoreToWrite % 10,
318-
(highScoreToWrite % 100 - highScoreToWrite % 10) / 10,
319-
(highScoreToWrite - highScoreToWrite % 100) / 100
320-
};
315+
unsigned int highScoreDigits[] = {0,
316+
highScoreToWrite % 10,
317+
(highScoreToWrite % 100 - highScoreToWrite % 10) / 10,
318+
(highScoreToWrite - highScoreToWrite % 100) / 100};
321319

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

329327
lv_area_t area;
330-
area.x1 = 40 - 8 * i; // Adjust X to display digits
331-
area.y1 = 4; // Y-offset for High Score
328+
area.x1 = 40 - 8 * i; // Adjust X to display digits
329+
area.y1 = 4; // Y-offset for High Score
332330
area.x2 = area.x1 + 7;
333331
area.y2 = area.y1 + 7;
334332

335333
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::None);
336334
lvgl.FlushDisplay(&area, digitBuffer);
337-
vTaskDelay(20); // Small delay to allow display refresh
335+
vTaskDelay(20); // Small delay to allow display refresh
338336
}
339337

340338
// Save the high score if it has changed
341339
highScore = highScoreToWrite;
342340
}
343-

src/displayapp/screens/Adder.h

+37-43
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ namespace Pinetime {
1616
constexpr unsigned int AdderVersion = 1;
1717

1818
struct AdderSave {
19-
unsigned int Level{0};
20-
unsigned int HighScore{0};
21-
unsigned int Version{AdderVersion};
19+
unsigned int Level {0};
20+
unsigned int HighScore {0};
21+
unsigned int Version {AdderVersion};
2222
};
2323

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

26-
2726
class Adder : public Screen {
2827

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

33-
3432
enum class MoveConsequence { DEATH, EAT, MOVE };
3533

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

45-
4643
Pinetime::Components::LittleVgl& lvgl;
4744
Controllers::FS& filesystem;
4845

4946
AdderSave data; // Game save data
50-
AdderField* field{nullptr};
47+
AdderField* field {nullptr};
5148

52-
lv_task_t* refreshTask{nullptr};
53-
lv_color_t* tileBuffer{nullptr};
49+
lv_task_t* refreshTask {nullptr};
50+
lv_color_t* tileBuffer {nullptr};
5451
lv_color_t digitBuffer[64];
5552

56-
unsigned int displayHeight{0};
57-
unsigned int displayWidth{0};
58-
unsigned int fieldWidth{0};
59-
unsigned int fieldHeight{0};
60-
unsigned int fieldSize{0};
61-
unsigned int highScore{2};
53+
unsigned int displayHeight {0};
54+
unsigned int displayWidth {0};
55+
unsigned int fieldWidth {0};
56+
unsigned int fieldHeight {0};
57+
unsigned int fieldSize {0};
58+
unsigned int highScore {2};
6259

63-
unsigned int fieldOffsetHorizontal{0};
64-
unsigned int fieldOffsetVertical{0};
60+
unsigned int fieldOffsetHorizontal {0};
61+
unsigned int fieldOffsetVertical {0};
6562

6663
std::list<unsigned int> adderBody;
6764
std::vector<unsigned int> blanks;
6865

69-
int prevDirection{0};
70-
int currentDirection{1};
66+
int prevDirection {0};
67+
int currentDirection {1};
7168

72-
bool appReady{false};
69+
bool appReady {false};
7370

7471
// Methods
7572
void InitializeGame();
@@ -80,44 +77,41 @@ namespace Pinetime {
8077
void InitializeBody();
8178
void CreateFood();
8279
void CreateLevel();
83-
80+
8481
void UpdatePosition();
8582
void FullRedraw();
8683
void UpdateSingleTile(unsigned int fieldX, unsigned int fieldY, lv_color_t color);
8784
void UpdateScore(unsigned int score);
88-
void HandleGameOver();
85+
void GameOver();
8986

9087
MoveConsequence CheckMove() const;
91-
92-
void Cleanup(); // Proper clean-up of allocated resources
93-
94-
void updateScore(unsigned int score); // Updates the score and high score
95-
void GameOver(); // Handles game-over logic
96-
97-
static constexpr const char DigitFont[10][8] = { // Font for digits 0-9
98-
{0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00}, // 0
99-
{0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00}, // 1
100-
{0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00}, // 2
101-
{0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00}, // 3
102-
{0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00}, // 4
103-
{0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00}, // 5
104-
{0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00}, // 6
105-
{0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00}, // 7
106-
{0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00}, // 8
107-
{0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00} // 9
88+
89+
static constexpr const char DigitFont[10][8] = {
90+
// Font for digits 0-9
91+
{0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00}, // 0
92+
{0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00}, // 1
93+
{0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00}, // 2
94+
{0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00}, // 3
95+
{0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00}, // 4
96+
{0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00}, // 5
97+
{0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00}, // 6
98+
{0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00}, // 7
99+
{0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00}, // 8
100+
{0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00} // 9
108101
};
109102
};
110-
} // namespace Screens
103+
} // namespace Screens
111104

112105
// Application Traits
113106
template <>
114107
struct AppTraits<Apps::Adder> {
115108
static constexpr Apps app = Apps::Adder;
116109
static constexpr const char* icon = "S";
110+
117111
static Screens::Screen* Create(AppControllers& controllers) {
118112
return new Screens::Adder(controllers.lvgl, controllers.filesystem);
119113
}
120114
};
121-
122-
} // namespace Applications
123-
} // namespace Pinetime
115+
116+
} // namespace Applications
117+
} // namespace Pinetime

0 commit comments

Comments
 (0)