-
Notifications
You must be signed in to change notification settings - Fork 0
/
roots_of_life_game.c
755 lines (612 loc) · 21.4 KB
/
roots_of_life_game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#include <furi.h>
#include <gui/gui.h>
#include <input/input.h>
#include <stdlib.h>
#include <gui/view.h>
#include <notification/notification.h>
#include <notification/notification_messages.h>
#include "roots_of_life_game_icons.h"
#define TAG "RootsOfLife"
// Flipper
#define FLIPPER_LCD_WIDTH 128
#define FLIPPER_LCD_HEIGHT 64
// General
#define GROUND_HEIGHT 10
#define CELL_SIZE 3
#define FIELD_START_X 0
#define FIELD_START_Y (GROUND_HEIGHT + 1)
#define CELLS_X (FLIPPER_LCD_WIDTH / CELL_SIZE)
#define CELLS_Y ((FLIPPER_LCD_HEIGHT - GROUND_HEIGHT) / CELL_SIZE)
#define CELLS_TOTAL (CELLS_Y * CELLS_X)
#define CELL(Y, X) (Y * CELLS_X + X)
// Root Spawn
#define ROOT_SIZE_X 7
#define ROOT_SIZE_Y 7
#define ROOT(Y, X) ((Y)*ROOT_SIZE_X + (X))
#define SPAWN_DIRECTIONS 2
#define GROW_STEPS 4
#define GROW_SAME_DIRECTION_CHANCE 70
#define RANDOM_GROW_ATTEMPTS 4
#define RANDOM_GROW_CHANCE 50
// UI
#define BLINK_PERIOD 12
#define BLINK_HIDE_FRAMES 5
#define TREE_HEIGHT 10
#define PICKUP_FREQUENCY 10
// Game
#define REROLLS_MAX 5
#define SCORE_FACTOR 10
#define PICKUPS_MIN 1
#define PICKUPS_MAX 5
#define PICKUPS_POINTS_FACTOR 10
typedef enum { EventTypeTick, EventTypeKey } EventType;
typedef enum {
R_NONE = 0,
R_UP = 0b1000,
R_DOWN = 0b0100,
R_LEFT = 0b0010,
R_RIGHT = 0b0001
} Direction;
typedef enum { StageStart, StageRun, StageOver } GameStage;
typedef struct {
bool initialDraw;
GameStage stage;
int tick;
bool* filledCells;
char* cells;
bool* pickups;
int collectedPickups;
bool* filledRootBase;
char* rootBase;
int rootSizeX;
int rootSizeY;
bool* filledRoot;
char* root;
int pX, pY;
int rerolls;
int score;
FuriMutex* mutex;
} GameState;
typedef struct {
EventType type;
InputEvent input;
} GameEvent;
static Direction rand_dir() {
int r = rand() % 4;
return 1 << r;
}
static Direction reverse_dir(Direction dir) {
switch(dir) {
case R_UP:
return R_DOWN;
case R_DOWN:
return R_UP;
case R_LEFT:
return R_RIGHT;
case R_RIGHT:
return R_LEFT;
default:
return R_NONE;
}
}
static int rand_range(int min, int max) {
return min + rand() % (max - min);
}
static bool rand_chance(int chance) {
return (rand() % 100) < chance;
}
static bool has_intersection(char cellA, char cellB) {
return cellA & cellB;
}
static int root_index(GameState* state, int y, int x) {
return y * state->rootSizeX + x;
}
static void set_cell(GameState* state, int y, int x, char cellRoot) {
int c = CELL(y, x);
state->filledCells[c] = true;
state->cells[c] = cellRoot;
}
static void game_state_init(GameState* state) {
state->initialDraw = false;
state->tick = 0;
// Init field arrays
state->filledCells = (bool*)malloc(CELLS_TOTAL * sizeof(bool));
state->cells = (char*)malloc(CELLS_TOTAL * sizeof(char));
state->pickups = (bool*)malloc(CELLS_TOTAL * sizeof(char));
state->rootBase = (char*)malloc(ROOT_SIZE_X * ROOT_SIZE_Y * sizeof(char));
state->filledRootBase = (bool*)malloc(ROOT_SIZE_X * ROOT_SIZE_Y * sizeof(bool));
state->root = NULL;
state->filledRoot = NULL;
for(int i = 0; i < CELLS_TOTAL; i++) {
state->filledCells[i] = false;
state->cells[i] = R_NONE;
state->pickups[i] = false;
}
}
static void free_root(GameState* state) {
if(state->root) free(state->root);
if(state->filledRoot) free(state->filledRoot);
}
static void game_state_free(GameState* state) {
free(state->filledCells);
free(state->cells);
free(state->pickups);
free(state->rootBase);
free(state->filledRootBase);
free_root(state);
}
/*static bool has_root(GameState* state, int x, int y) {
return x >= 0 && x < ROOT_SIZE_X && y >= 0 && y < ROOT_SIZE_Y &&
state->filledRootBase[ROOT(y, x)];
}*/
static void generate_new_root(GameState* state) {
for(int i = 0; i < ROOT_SIZE_X * ROOT_SIZE_Y; i++) {
state->filledRootBase[i] = false;
state->rootBase[i] = R_NONE;
}
int cX = ROOT_SIZE_X / 2;
int cY = ROOT_SIZE_Y / 2;
int c = ROOT(cY, cX);
state->filledRootBase[c] = true;
for(int i = 0; i < SPAWN_DIRECTIONS; i++) {
int pX = cX, pY = cY;
Direction oldDir = rand_dir();
for(int g = 0; g < GROW_STEPS; g++) {
Direction dir = rand_chance(GROW_SAME_DIRECTION_CHANCE) ? oldDir : rand_dir();
oldDir = dir;
int nX = pX - (dir & R_LEFT ? 1 : 0) + (dir & R_RIGHT ? 1 : 0);
int nY = pY - (dir & R_UP ? 1 : 0) + (dir & R_DOWN ? 1 : 0);
if(nX < 0 || nY < 0 || nX >= ROOT_SIZE_X || nY >= ROOT_SIZE_Y) continue;
int n = ROOT(nY, nX);
state->filledRootBase[n] = true;
// Connect points
int p = ROOT(pY, pX);
state->rootBase[p] |= dir;
state->rootBase[n] |= reverse_dir(dir);
// Grow from new point
pX = nX;
pY = nY;
}
}
for(int y = 0; y < ROOT_SIZE_Y; y++) {
for(int x = 0; x < ROOT_SIZE_X; x++) {
int c = ROOT(y, x);
if(!state->filledRootBase[c]) continue;
/*
if(has_root(state, x - 1, y)) state->rootBase[c] |= R_LEFT;
if(has_root(state, x + 1, y)) state->rootBase[c] |= R_RIGHT;
if(has_root(state, x, y - 1)) state->rootBase[c] |= R_UP;
if(has_root(state, x, y + 1)) state->rootBase[c] |= R_DOWN;
*/
for(int r = 0; r < RANDOM_GROW_ATTEMPTS; r++) {
if(!rand_chance(RANDOM_GROW_CHANCE)) continue;
state->rootBase[c] |= rand_dir();
}
}
}
// Copy root to real root
int minX = cX, maxX = cX, minY = cY, maxY = cY;
for(int y = 0; y < ROOT_SIZE_Y; y++) {
for(int x = 0; x < ROOT_SIZE_X; x++) {
int r = ROOT(y, x);
if(!state->filledRootBase[r]) continue;
minX = MIN(minX, x);
maxX = MAX(maxX, x);
minY = MIN(minY, y);
maxY = MAX(maxY, y);
}
}
// Clone to real root
state->rootSizeX = maxX - minX + 1;
state->rootSizeY = maxY - minY + 1;
free_root(state);
state->root = (char*)malloc(state->rootSizeX * state->rootSizeY * sizeof(char));
state->filledRoot = (bool*)malloc(state->rootSizeX * state->rootSizeY * sizeof(bool));
for(int y = 0; y < state->rootSizeY; y++) {
for(int x = 0; x < state->rootSizeX; x++) {
int c = root_index(state, y, x);
int r = ROOT(y + minY, x + minX);
state->filledRoot[c] = state->filledRootBase[r];
state->root[c] = state->rootBase[r];
}
}
}
static bool in_borders(int x, int y) {
return x >= 0 && y >= 0 && x < CELLS_X && y < CELLS_Y;
}
static char get_cell(GameState* state, int x, int y) {
if(!in_borders(x, y)) return R_NONE;
return state->cells[CELL(y, x)];
}
static bool get_filled_cell(GameState* state, int x, int y) {
if(!in_borders(x, y)) return false;
return state->filledCells[CELL(y, x)];
}
static bool can_place_root(GameState* state) {
bool hasConnection = false;
for(int y = 0; y < state->rootSizeY; y++) {
for(int x = 0; x < state->rootSizeX; x++) {
int r = root_index(state, y, x);
if(!state->filledRoot[r]) {
continue;
}
char root = state->root[r];
int rY = y + state->pY;
int rX = x + state->pX;
// Check if colliding
if(get_filled_cell(state, rX, rY)) {
char cell = get_cell(state, rX, rY);
if(has_intersection(cell, root)) {
return false;
}
hasConnection = true;
}
// Check neighbours
hasConnection |= (root & R_RIGHT) && (get_cell(state, rX + 1, rY) & R_LEFT);
hasConnection |= (root & R_LEFT) && (get_cell(state, rX - 1, rY) & R_RIGHT);
hasConnection |= (root & R_UP) && (get_cell(state, rX, rY - 1) & R_DOWN);
hasConnection |= (root & R_DOWN) && (get_cell(state, rX, rY + 1) & R_UP);
}
}
return hasConnection;
}
static bool try_place_root(GameState* state) {
if(!can_place_root(state)) return false;
for(int y = 0; y < state->rootSizeY; y++) {
for(int x = 0; x < state->rootSizeX; x++) {
int r = root_index(state, y, x);
if(!state->filledRoot[r]) continue;
int rY = y + state->pY;
int rX = x + state->pX;
// Root may be out of borders in rare cases (after new cpawn changed its size), just ignore that part
if(in_borders(rX, rY)) {
int c = CELL(rY, rX);
state->filledCells[c] = true;
state->cells[c] |= state->root[r];
}
}
}
return true;
}
static void reset_level(GameState* state) {
state->stage = StageStart;
state->tick = 0;
for(int i = 0; i < CELLS_TOTAL; i++) {
state->filledCells[i] = false;
state->cells[i] = R_NONE;
}
generate_new_root(state);
// Starting cells
int midX = CELLS_X / 2;
set_cell(state, 0, midX, R_UP | R_DOWN);
set_cell(state, 1, midX, R_UP | R_DOWN | R_LEFT | R_RIGHT);
set_cell(state, 1, midX - 1, R_RIGHT | R_DOWN);
set_cell(state, 1, midX + 1, R_LEFT | R_DOWN);
set_cell(state, 2, midX, R_UP);
state->pX = midX;
state->pY = 4;
state->rerolls = REROLLS_MAX;
state->score = 0;
state->collectedPickups = 0;
for(int i = 0, n = rand_range(PICKUPS_MIN, PICKUPS_MAX); i < n; i++) {
int x = rand_range(0, CELLS_X);
int y = rand_range(0, CELLS_Y);
state->pickups[CELL(y, x)] = true;
}
}
static void recalculate_score(GameState* state) {
int score = 0;
for(int i = 0; i < CELLS_TOTAL; i++) {
if(state->filledCells[i]) score++;
}
for(int i = 0; i < CELLS_TOTAL; i++) {
if(!state->pickups[i] || !state->filledCells[i]) continue;
state->pickups[i] = false;
state->collectedPickups++;
state->rerolls++;
}
state->score = (score + state->collectedPickups * PICKUPS_POINTS_FACTOR) * SCORE_FACTOR;
}
static void draw_root_cell(Canvas* canvas, char root, int y, int x, bool isHidden) {
int posX = FIELD_START_X + x * CELL_SIZE + 1, posY = FIELD_START_Y + y * CELL_SIZE + 1;
canvas_draw_dot(canvas, posX, posY);
if(isHidden) {
canvas_set_color(canvas, ColorXOR);
}
if(root & R_UP) canvas_draw_dot(canvas, posX, posY - 1);
if(root & R_DOWN) canvas_draw_dot(canvas, posX, posY + 1);
if(root & R_LEFT) canvas_draw_dot(canvas, posX - 1, posY);
if(root & R_RIGHT) canvas_draw_dot(canvas, posX + 1, posY);
if(isHidden) {
canvas_set_color(canvas, ColorBlack);
}
}
static void draw_placed_roots(Canvas* canvas, GameState* state) {
for(int y = 0; y < CELLS_Y; y++) {
for(int x = 0; x < CELLS_X; x++) {
int c = CELL(y, x);
if(!state->filledCells[c]) continue;
draw_root_cell(canvas, state->cells[c], y, x, false);
}
}
}
static void draw_pickup(Canvas* canvas, GameState* state, int y, int x) {
int posX = FIELD_START_X + x * CELL_SIZE + 1, posY = FIELD_START_Y + y * CELL_SIZE + 1;
int stage = state->tick / PICKUP_FREQUENCY;
if(stage++ % 4 < 3) canvas_draw_dot(canvas, posX + 1, posY);
if(stage++ % 4 < 3) canvas_draw_dot(canvas, posX, posY + 1);
if(stage++ % 4 < 3) canvas_draw_dot(canvas, posX - 1, posY);
if(stage++ % 4 < 3) canvas_draw_dot(canvas, posX, posY - 1);
}
static void draw_pickups(Canvas* canvas, GameState* state) {
for(int y = 0; y < CELLS_Y; y++) {
for(int x = 0; x < CELLS_X; x++) {
int c = CELL(y, x);
if(!state->pickups[c]) continue;
draw_pickup(canvas, state, y, x);
}
}
}
static void draw_active_root(Canvas* canvas, GameState* state) {
bool isHidden = (state->tick % BLINK_PERIOD) < BLINK_HIDE_FRAMES;
for(int y = 0; y < state->rootSizeY; y++) {
for(int x = 0; x < state->rootSizeX; x++) {
int c = root_index(state, y, x);
if(!state->filledRoot[c]) continue;
int realX = x + state->pX;
int realY = y + state->pY;
draw_root_cell(canvas, state->root[c], realY, realX, isHidden);
}
}
}
#if DRAW_DEBUG
static void draw_generated_root(Canvas* canvas, GameState* state) {
bool isHidden = (state->tick % BLINK_PERIOD) < BLINK_HIDE_FRAMES;
for(int y = 0; y < ROOT_SIZE_Y; y++) {
for(int x = 0; x < ROOT_SIZE_X; x++) {
int c = ROOT(y, x);
if(!state->filledRootBase[c]) continue;
int realX = x + 1;
int realY = y + 1;
draw_root_cell(canvas, state->rootBase[c], realY, realX, isHidden);
}
}
}
#endif
static void draw_ground(Canvas* canvas, GameState* state) {
canvas_draw_line(canvas, 0, GROUND_HEIGHT, FLIPPER_LCD_WIDTH, GROUND_HEIGHT);
UNUSED(state);
}
static void draw_tree(Canvas* canvas, GameState* state) {
canvas_draw_icon(canvas, FLIPPER_LCD_WIDTH / 2 - 5, GROUND_HEIGHT - TREE_HEIGHT, &I_tree);
UNUSED(state);
}
static void draw_placement(Canvas* canvas, GameState* state) {
bool canPlace = can_place_root(state);
canvas_draw_icon(canvas, FLIPPER_LCD_WIDTH - 10, 0, canPlace ? &I_place_ok : &I_place_error);
}
static void draw_rerolls(Canvas* canvas, GameState* state) {
UNUSED(canvas);
UNUSED(state);
canvas_draw_icon(canvas, 0, 0, &I_root_reroll);
// Ugh
FuriString* tmp_string = furi_string_alloc();
furi_string_printf(tmp_string, "%d", MAX(0, state->rerolls));
canvas_draw_str(canvas, 11, 9, furi_string_get_cstr(tmp_string));
furi_string_free(tmp_string);
}
static void draw_score(Canvas* canvas, GameState* state) {
UNUSED(canvas);
UNUSED(state);
int x = FLIPPER_LCD_WIDTH / 2 + 15;
canvas_draw_icon(canvas, x, 0, &I_score);
// Ugh
FuriString* tmp_string = furi_string_alloc();
furi_string_printf(tmp_string, "%d", MAX(0, state->score));
canvas_draw_str(canvas, x + 11, 9, furi_string_get_cstr(tmp_string));
furi_string_free(tmp_string);
}
static void draw_gui(Canvas* canvas, GameState* state) {
draw_ground(canvas, state);
draw_tree(canvas, state);
draw_placement(canvas, state);
draw_rerolls(canvas, state);
draw_score(canvas, state);
}
static void draw_center_box(Canvas* canvas, int w2, int h2, int margin) {
int x = FLIPPER_LCD_WIDTH / 2 - w2;
int y = FLIPPER_LCD_HEIGHT / 2 - h2;
canvas_set_color(canvas, ColorWhite);
canvas_draw_box(
canvas, x - margin - 1, y - margin - 1, (w2 + margin + 1) * 2, (h2 + margin + 1) * 2);
canvas_set_color(canvas, ColorBlack);
canvas_draw_frame(canvas, x - margin, y - margin, (w2 + margin) * 2, (h2 + margin) * 2);
}
static void draw_start_ui(Canvas* canvas, GameState* state) {
int w2 = 40;
int margin = 3;
int h2 = 10;
draw_center_box(canvas, w2, h2, margin);
int x = FLIPPER_LCD_WIDTH / 2 - w2;
int y = FLIPPER_LCD_HEIGHT / 2 - h2;
canvas_draw_str(canvas, x + 1, y + 9, " Grow your roots ");
canvas_draw_str(canvas, x + 1, y + 18, "Press [OK] to start");
UNUSED(state);
}
static void draw_end_ui(Canvas* canvas, GameState* state) {
int w2 = 46;
int margin = 3;
int h2 = 15;
draw_center_box(canvas, w2, h2, margin);
int x = FLIPPER_LCD_WIDTH / 2 - w2;
int y = FLIPPER_LCD_HEIGHT / 2 - h2;
canvas_draw_str(canvas, x + 1, y + 9, " Game Over ");
FuriString* tmp_string = furi_string_alloc();
furi_string_printf(tmp_string, "You've got %d points", MAX(0, state->score));
canvas_draw_str(canvas, x + 1, y + 19, furi_string_get_cstr(tmp_string));
furi_string_free(tmp_string);
canvas_draw_str(canvas, x + 2, y + 29, "Press [OK] to restart");
int h = 13, w = 54;
canvas_set_color(canvas, ColorWhite);
canvas_draw_box(canvas, 0, FLIPPER_LCD_HEIGHT - h, w + 1, h + 1);
canvas_set_color(canvas, ColorBlack);
canvas_draw_frame(canvas, 0, FLIPPER_LCD_HEIGHT - h, w, h);
canvas_draw_str(canvas, 2, FLIPPER_LCD_HEIGHT - 3, "by @Xorboo");
UNUSED(state);
}
static void roots_draw_callback(Canvas* const canvas, void* ctx) {
furi_assert(ctx);
GameState* state = ctx;
furi_mutex_acquire(state->mutex, FuriWaitForever);
if(!state->initialDraw) {
state->initialDraw = true;
canvas_set_font(canvas, FontSecondary);
reset_level(state);
}
state->tick++;
draw_gui(canvas, state);
draw_placed_roots(canvas, state);
draw_pickups(canvas, state);
switch(state->stage) {
case StageStart:
draw_start_ui(canvas, state);
break;
case StageRun:
draw_active_root(canvas, state);
#if DRAW_DEBUG
draw_generated_root(canvas, state);
#endif
break;
case StageOver:
draw_end_ui(canvas, state);
break;
}
furi_mutex_release(state->mutex);
}
static void roots_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue);
GameEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
static void roots_update_timer_callback(FuriMessageQueue* event_queue) {
furi_assert(event_queue);
GameEvent event = {.type = EventTypeTick};
furi_message_queue_put(event_queue, &event, 0);
}
static void ProcessStartInput(GameState* state, InputKey key) {
if(key == InputKeyOk) {
state->stage = StageRun;
}
}
static void ProcessRunInput(GameState* state, InputKey key) {
switch(key) {
case InputKeyRight:
state->pX = MIN(state->pX + 1, CELLS_X - state->rootSizeX);
break;
case InputKeyLeft:
state->pX = MAX(state->pX - 1, 0);
break;
case InputKeyUp:
state->pY = MAX(state->pY - 1, 0);
break;
case InputKeyDown:
state->pY = MIN(state->pY + 1, CELLS_Y - state->rootSizeY);
break;
case InputKeyOk: {
bool rootPlaced = try_place_root(state);
if(rootPlaced) {
recalculate_score(state);
generate_new_root(state);
} else {
state->rerolls--;
if(state->rerolls >= 0) {
generate_new_root(state);
} else {
state->stage = StageOver;
}
}
break;
}
default:
break;
}
}
static void ProcessOverInput(GameState* state, InputKey key) {
if(key == InputKeyOk) {
state->stage = StageStart;
reset_level(state);
}
}
int32_t roots_of_life_game_app(void* p) {
FURI_LOG_D(TAG, "Starting game...");
UNUSED(p);
int32_t return_code = 0;
// Set random seed from interrR_UPts
srand(DWT->CYCCNT);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
GameState* state = malloc(sizeof(GameState));
game_state_init(state);
state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!state->mutex) {
FURI_LOG_E(TAG, "Cannot create mutex\r\n");
return_code = 255;
goto free_and_exit;
}
// Set system callbacks
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, roots_draw_callback, state);
view_port_input_callback_set(view_port, roots_input_callback, event_queue);
FuriTimer* timer =
furi_timer_alloc(roots_update_timer_callback, FuriTimerTypePeriodic, event_queue);
furi_timer_start(timer, furi_kernel_get_tick_frequency() / 22);
// Open GUI and register view_port
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
FURI_LOG_D(TAG, "Entering game loop...");
GameEvent event;
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
furi_mutex_acquire(state->mutex, FuriWaitForever);
if(event_status == FuriStatusOk) {
// Key events
if(event.type == EventTypeKey) {
FURI_LOG_D(TAG, "Got key: %d", event.input.key);
if(event.input.type == InputTypePress || event.input.type == InputTypeLong ||
event.input.type == InputTypeRepeat) {
if(event.input.key == InputKeyBack) {
processing = false;
}
switch(state->stage) {
case StageStart:
ProcessStartInput(state, event.input.key);
break;
case StageRun:
ProcessRunInput(state, event.input.key);
break;
case StageOver:
ProcessOverInput(state, event.input.key);
break;
}
}
}
} else {
// Event timeout
FURI_LOG_D(TAG, "furi_message_queue: Event timeout");
}
view_port_update(view_port);
furi_mutex_release(state->mutex);
}
furi_timer_free(timer);
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_NOTIFICATION);
view_port_free(view_port);
furi_mutex_free(state->mutex);
free_and_exit:
FURI_LOG_D(TAG, "Quitting game...");
game_state_free(state);
free(state);
furi_message_queue_free(event_queue);
return return_code;
}