1
1
#include " displayapp/DisplayApp.h"
2
2
#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
5
5
6
6
using namespace Pinetime ::Applications::Screens;
7
7
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) {
10
9
InitializeGame ();
11
10
}
12
11
@@ -34,14 +33,17 @@ void Adder::InitializeGame() {
34
33
InitializeBody ();
35
34
CreateLevel ();
36
35
37
- refreshTask = lv_task_create ([](lv_task_t * task) {
36
+ refreshTask = lv_task_create (
37
+ [](lv_task_t * task) {
38
38
auto * adder = static_cast <Adder*>(task->user_data );
39
39
adder->Refresh ();
40
40
},
41
- AdderDelayInterval, LV_TASK_PRIO_MID, this );
41
+ AdderDelayInterval,
42
+ LV_TASK_PRIO_MID,
43
+ this );
42
44
43
45
appReady = false ;
44
- vTaskDelay (20 );
46
+ vTaskDelay (20 );
45
47
}
46
48
47
49
void Adder::Cleanup () {
@@ -86,7 +88,7 @@ void Adder::ResetGame() {
86
88
GameOver ();
87
89
appReady = false ;
88
90
highScore = std::max (highScore, static_cast <unsigned int >(adderBody.size () - 2 ));
89
- data.HighScore = highScore;
91
+ data.HighScore = highScore;
90
92
SaveGame ();
91
93
92
94
CreateLevel ();
@@ -101,7 +103,7 @@ void Adder::InitializeBody() {
101
103
unsigned int startPosition = (fieldHeight / 2 ) * fieldWidth + fieldWidth / 2 + 2 ;
102
104
adderBody = {startPosition, startPosition - 1 };
103
105
104
- currentDirection = 1 ; // Start moving to the right
106
+ currentDirection = 1 ; // Start moving to the right
105
107
prevDirection = currentDirection;
106
108
}
107
109
@@ -147,8 +149,7 @@ bool Adder::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
147
149
currentDirection = 1 ;
148
150
break ;
149
151
case TouchEvents::LongTap:
150
- FullRedraw (); // Adjusted to method spelled as "FullRedraw"
151
- break ;
152
+ FullRedraw ();
152
153
default :
153
154
break ;
154
155
}
@@ -163,26 +164,25 @@ bool Adder::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
163
164
prevDirection = currentDirection;
164
165
}
165
166
166
- return true ; // Return true to indicate the touch event was handled
167
+ return true ; // Return true to indicate the touch event was handled
167
168
}
168
169
169
-
170
170
void Adder::UpdatePosition () {
171
171
unsigned int newHead = adderBody.front () + currentDirection;
172
- Adder::MoveConsequence result = CheckMove (); // Fully qualify MoveConsequence
172
+ Adder::MoveConsequence result = CheckMove ();
173
173
174
174
switch (result) {
175
- case Adder::MoveConsequence::DEATH: // Fully qualify
175
+ case Adder::MoveConsequence::DEATH:
176
176
ResetGame ();
177
177
return ;
178
178
179
- case Adder::MoveConsequence::EAT: // Fully qualify
179
+ case Adder::MoveConsequence::EAT:
180
180
adderBody.push_front (newHead);
181
181
CreateFood ();
182
182
UpdateScore (adderBody.size () - 2 );
183
183
break ;
184
184
185
- case Adder::MoveConsequence::MOVE: // Fully qualify
185
+ case Adder::MoveConsequence::MOVE:
186
186
adderBody.pop_back ();
187
187
adderBody.push_front (newHead);
188
188
break ;
@@ -195,33 +195,33 @@ void Adder::UpdatePosition() {
195
195
Adder::MoveConsequence Adder::CheckMove () const {
196
196
unsigned int newHead = adderBody.front () + currentDirection;
197
197
if (newHead >= fieldSize) {
198
- return Adder::MoveConsequence::DEATH; // Fully qualify
198
+ return Adder::MoveConsequence::DEATH;
199
199
}
200
200
201
201
switch (field[newHead]) {
202
202
case AdderField::BLANK:
203
- return Adder::MoveConsequence::MOVE; // Fully qualify
203
+ return Adder::MoveConsequence::MOVE;
204
204
case AdderField::FOOD:
205
- return Adder::MoveConsequence::EAT; // Fully qualify
205
+ return Adder::MoveConsequence::EAT;
206
206
default :
207
- return Adder::MoveConsequence::DEATH; // Fully qualify
207
+ return Adder::MoveConsequence::DEATH;
208
208
}
209
209
}
210
210
211
211
void Adder::Refresh () {
212
212
if (!appReady) {
213
213
FullRedraw ();
214
214
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
216
216
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
218
218
appReady = true ;
219
219
} else {
220
220
UpdatePosition ();
221
221
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
223
223
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
225
225
}
226
226
}
227
227
@@ -244,25 +244,23 @@ void Adder::FullRedraw() {
244
244
break ;
245
245
}
246
246
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
248
248
}
249
249
}
250
250
}
251
251
252
252
void Adder::UpdateSingleTile (unsigned int x, unsigned int y, lv_color_t color) {
253
253
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 )};
260
258
261
259
lvgl.FlushDisplay (&area, tileBuffer);
262
260
}
263
261
264
262
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
266
264
267
265
// Determine offset based on field dimensions
268
266
unsigned int offset = fieldOffsetHorizontal > fieldOffsetVertical ? fieldOffsetHorizontal : fieldOffsetVertical;
@@ -272,7 +270,9 @@ void Adder::GameOver() {
272
270
for (unsigned int i = 0 ; i < 4 ; i++) {
273
271
for (unsigned int j = 0 ; j < 64 ; j++) {
274
272
// 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
276
276
}
277
277
278
278
lv_area_t area;
@@ -283,14 +283,14 @@ void Adder::GameOver() {
283
283
284
284
lvgl.SetFullRefresh (Components::LittleVgl::FullRefreshDirections::None);
285
285
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
287
287
}
288
288
}
289
289
}
290
290
291
291
void Adder::UpdateScore (unsigned int score) {
292
292
// 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 };
294
294
295
295
// Render the score
296
296
for (unsigned int i = 0 ; i < 4 ; i++) {
@@ -300,24 +300,22 @@ void Adder::UpdateScore(unsigned int score) {
300
300
}
301
301
302
302
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
304
304
area.y1 = 4 ; // Y-offset for Score
305
305
area.x2 = area.x1 + 7 ;
306
306
area.y2 = area.y1 + 7 ;
307
307
308
308
lvgl.SetFullRefresh (Components::LittleVgl::FullRefreshDirections::None);
309
309
lvgl.FlushDisplay (&area, digitBuffer);
310
- vTaskDelay (20 ); // Small delay to allow display refresh
310
+ vTaskDelay (20 ); // Small delay to allow display refresh
311
311
}
312
312
313
313
// Update the high score if necessary
314
314
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 };
321
319
322
320
// Render the high score
323
321
for (unsigned int i = 0 ; i < 4 ; i++) {
@@ -327,17 +325,16 @@ void Adder::UpdateScore(unsigned int score) {
327
325
}
328
326
329
327
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
332
330
area.x2 = area.x1 + 7 ;
333
331
area.y2 = area.y1 + 7 ;
334
332
335
333
lvgl.SetFullRefresh (Components::LittleVgl::FullRefreshDirections::None);
336
334
lvgl.FlushDisplay (&area, digitBuffer);
337
- vTaskDelay (20 ); // Small delay to allow display refresh
335
+ vTaskDelay (20 ); // Small delay to allow display refresh
338
336
}
339
337
340
338
// Save the high score if it has changed
341
339
highScore = highScoreToWrite;
342
340
}
343
-
0 commit comments