forked from eugene-kirzhanov/flipper-zero-2048-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_controller_drawer.c
164 lines (130 loc) · 5.81 KB
/
game_controller_drawer.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
#define __game_controller_c
#include "game_controller.h"
#include <furi.h>
#include <gui/gui.h>
#include "digits.h"
#define CELL_INNER_SIZE 14
#define FRAME_LEFT 10
#define FRAME_TOP 1
#define FRAME_SIZE 61
static const char* popup_menu_items[] = {"Resume", "New Game"};
static void game_controller_draw_game(const GameController* gamectrl, Canvas* const canvas);
static void game_controller_draw_menu(const GameController* gamectrl, Canvas* const canvas);
static void game_controller_draw_game_over(const GameController* gamectrl, Canvas* const canvas);
static void ui_draw_table(Canvas* canvas, const GameBoardTable table);
static void ui_draw_stats(Canvas* const canvas, const GameState*);
static void ui_draw_digit(Canvas* canvas, uint8_t row, uint8_t column, uint8_t value);
static void ui_draw_popup_background(Canvas* const canvas);
void game_controller_draw(const GameController* gamectrl, Canvas* const canvas) {
canvas_clear(canvas);
game_controller_draw_game(gamectrl, canvas);
switch(gamectrl->ui_state) {
case UIStateMenu:
game_controller_draw_menu(gamectrl, canvas);
break;
case UIStateGameOver:
game_controller_draw_game_over(gamectrl, canvas);
break;
default:
break;
}
}
static void game_controller_draw_game(const GameController* gamectrl, Canvas* const canvas) {
ui_draw_table(canvas, gamectrl->state.board.table);
ui_draw_stats(canvas, &gamectrl->state);
}
static void game_controller_draw_menu(const GameController* gamectrl, Canvas* const canvas) {
ui_draw_popup_background(canvas);
canvas_set_color(canvas, ColorWhite);
canvas_draw_rbox(canvas, 28, 16, 72, 32, 4);
canvas_set_color(canvas, ColorBlack);
canvas_draw_rframe(canvas, 28, 16, 72, 32, 4);
for(uint8_t i = 0; i < MENU_ITEMS_COUNT; i++) {
if(i == gamectrl->selected_menu_item) {
canvas_set_color(canvas, ColorBlack);
canvas_draw_box(canvas, 34, 20 + 12 * i, 60, 12);
}
canvas_set_color(canvas, i == gamectrl->selected_menu_item ? ColorWhite : ColorBlack);
canvas_draw_str_aligned(
canvas, 64, 26 + 12 * i, AlignCenter, AlignCenter, popup_menu_items[i]);
}
}
static void game_controller_draw_game_over(const GameController* gamectrl, Canvas* const canvas) {
ui_draw_popup_background(canvas);
bool record_broken = gamectrl->state.board.score > gamectrl->state.top_score;
canvas_set_color(canvas, ColorWhite);
canvas_draw_rbox(canvas, 14, 12, 100, 40, 4);
canvas_set_color(canvas, ColorBlack);
canvas_draw_line(canvas, 14, 26, 114, 26);
canvas_draw_rframe(canvas, 14, 12, 100, 40, 4);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 15, AlignCenter, AlignTop, "Game Over");
canvas_set_font(canvas, FontSecondary);
if(record_broken) {
canvas_draw_str_aligned(canvas, 64, 29, AlignCenter, AlignTop, "New Top Score!!!");
} else {
canvas_draw_str_aligned(canvas, 64, 29, AlignCenter, AlignTop, "Your Score");
}
uint8_t bufSize = 12;
char buf[bufSize];
memset(buf, 0, bufSize);
snprintf(buf, sizeof(buf), "%lu", gamectrl->state.board.score);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 48, AlignCenter, AlignBottom, buf);
}
static void ui_draw_digit(Canvas* canvas, uint8_t row, uint8_t column, uint8_t value) {
if(value == 0) return;
uint8_t left = FRAME_LEFT + 1 + (column * (CELL_INNER_SIZE + 1));
uint8_t top = FRAME_TOP + 1 + (row * (CELL_INNER_SIZE + 1));
for(uint8_t r = 0; r < CELL_INNER_SIZE; r++) {
for(uint8_t c = 0; c < CELL_INNER_SIZE; c++) {
if(digits[value - 1][r][c] == 1) {
canvas_draw_dot(canvas, left + c, top + r);
}
}
}
}
static void ui_draw_table(Canvas* canvas, const GameBoardTable table) {
canvas_draw_frame(canvas, FRAME_LEFT, FRAME_TOP, FRAME_SIZE, FRAME_SIZE);
uint8_t offs = FRAME_LEFT + CELL_INNER_SIZE + 1;
for(uint8_t i = 0; i < CELLS_COUNT - 1; i++) {
canvas_draw_line(canvas, offs, FRAME_TOP + 1, offs, FRAME_TOP + FRAME_SIZE - 2);
offs += CELL_INNER_SIZE + 1;
}
offs = FRAME_TOP + CELL_INNER_SIZE + 1;
for(uint8_t i = 0; i < CELLS_COUNT - 1; i++) {
canvas_draw_line(canvas, FRAME_LEFT + 1, offs, FRAME_LEFT + FRAME_SIZE - 2, offs);
offs += CELL_INNER_SIZE + 1;
}
for(uint8_t row = 0; row < CELLS_COUNT; row++) {
for(uint8_t column = 0; column < CELLS_COUNT; column++) {
ui_draw_digit(canvas, row, column, table[row][column]);
}
}
}
static void ui_draw_stats(Canvas* const canvas, const GameState* state) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 128, FRAME_TOP, AlignRight, AlignTop, "Score");
canvas_draw_str_aligned(canvas, 128, FRAME_TOP + 20, AlignRight, AlignTop, "Moves");
canvas_draw_str_aligned(canvas, 128, FRAME_TOP + 40, AlignRight, AlignTop, "Top Score");
uint8_t bufSize = 12;
char buf[bufSize];
canvas_set_font(canvas, FontSecondary);
snprintf(buf, sizeof(buf), "%lu", state->board.score);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 128, FRAME_TOP + 10, AlignRight, AlignTop, buf);
memset(buf, 0, bufSize);
snprintf(buf, sizeof(buf), "%lu", state->board.moves);
canvas_draw_str_aligned(canvas, 128, FRAME_TOP + 30, AlignRight, AlignTop, buf);
memset(buf, 0, bufSize);
snprintf(buf, sizeof(buf), "%lu", state->top_score);
canvas_draw_str_aligned(canvas, 128, FRAME_TOP + 50, AlignRight, AlignTop, buf);
}
static void ui_draw_popup_background(Canvas* const canvas) {
canvas_set_color(canvas, ColorWhite);
for(uint8_t x = 0; x < 128; x += 2) {
for(uint8_t y = 0; y < 64; y++) {
canvas_draw_dot(canvas, x + (y % 2 == 1 ? 0 : 1), y);
}
}
}