-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlife.cpp
More file actions
184 lines (155 loc) · 5.07 KB
/
life.cpp
File metadata and controls
184 lines (155 loc) · 5.07 KB
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
#include "life.h"
#include "graphics.h"
static bool Life::run(Display& disp, Controller *controllers, uint8_t controller_count) {
Life game = Life(disp, controllers, controller_count);
return game.play();
}
static MenuOption Life::menuOption() {
static const char PROGMEM graphic[] =
"__________"
"__________"
"__8_______"
"___88_____"
"__88______"
"_______8__"
"_____8_8__"
"______88__"
"__________"
"__________";
return MenuOption(graphic, Life::setPalette, Life::run);
}
Life::Life(Display& disp, Controller *controllers, uint8_t controller_count):
InputProcessor(controllers, controller_count),
disp(disp), paused(true), cursor_x(disp.width / 2), cursor_y(disp.height / 2) {
button_conf[Controller::Button::b] = { .initial = 0, .subsequent = 0};
button_conf[Controller::Button::a] = { .initial = 0, .subsequent = 0};
button_conf[Controller::Button::select] = { .initial = 0, .subsequent = 0};
button_conf[Controller::Button::start] = { .initial = 0, .subsequent = 0};
button_conf[Controller::Button::down] = { .initial = 200, .subsequent = 100};
button_conf[Controller::Button::right] = { .initial = 200, .subsequent = 100};
button_conf[Controller::Button::up] = { .initial = 200, .subsequent = 100};
button_conf[Controller::Button::left] = { .initial = 200, .subsequent = 100};
}
static void Life::setPalette(Display& disp) {
disp.palette[0] = RGB(0, 0, 0); // still dead
disp.palette[1] = RGB(1, 1, 1); // newly dead
disp.palette[2] = RGB(8, 8, 8); // newly alive
disp.palette[3] = RGB(6, 6, 6); // still alive
// With cursor
disp.palette[4] = RGB(0, 0, 20); // still dead
disp.palette[5] = RGB(1, 1, 20); // newly dead
disp.palette[6] = RGB(4, 4, 20); // newly alive
disp.palette[7] = RGB(2, 2, 18); // still alive
// Menu color
disp.palette[8] = RGB(6, 4, 12);
}
bool Life::play() {
Life::setPalette(disp);
move_cursor(cursor_x, cursor_y, 0);
disp.refresh();
unsigned long last_cycle = millis();
unsigned long cycle_rate = 300;
while (true) {
unsigned long now = millis();
bool exit_game = handle_input(now);
if (exit_game) {
break;
}
if (!paused && now > last_cycle + cycle_rate) {
cycle();
last_cycle = now;
}
disp.refresh();
delay(1);
}
return true;
}
bool Life::handle_button_down(Controller::Button button, uint8_t controller_index) {
switch (button) {
case Controller::Button::start: {
return true;
}
case Controller::Button::select: {
paused = !paused;
break;
}
case Controller::Button::up: {
move_cursor(cursor_x, cursor_y - 1, controller_index);
break;
}
case Controller::Button::down: {
move_cursor(cursor_x, cursor_y + 1, controller_index);
break;
}
case Controller::Button::left: {
move_cursor(cursor_x - 1, cursor_y, controller_index);
break;
}
case Controller::Button::right: {
move_cursor(cursor_x + 1, cursor_y, controller_index);
break;
}
case Controller::Button::a: {
set_cell(cursor_x, cursor_y, true);
break;
}
case Controller::Button::b: {
set_cell(cursor_x, cursor_y, false);
break;
}
}
return false;
}
void Life::move_cursor(int8_t x, int8_t y, uint8_t controller_index) {
uint8_t pixel = disp.get_pixel(cursor_x, cursor_y);
pixel &= ~(1 << 2);
disp.set_pixel(cursor_x, cursor_y, pixel);
cursor_x = (x + disp.width) % disp.width;
cursor_y = (y + disp.height) % disp.height;
pixel = disp.get_pixel(cursor_x, cursor_y);
pixel |= 1 << 2;
disp.set_pixel(cursor_x, cursor_y, pixel);
if (controllers[controller_index].handler_states[Controller::Button::a].is_pressed()) {
set_cell(cursor_x, cursor_y, true);
} else if (controllers[controller_index].handler_states[Controller::Button::b].is_pressed()) {
set_cell(cursor_x, cursor_y, false);
}
}
void Life::set_cell(uint8_t x, uint8_t y, bool value) {
byte cell = disp.get_pixel(x, y);
cell = value ? cell | 2 : cell & ~2;
disp.set_pixel(x, y, cell);
}
void Life::cycle() {
struct Offset {
short x;
short y;
};
Offset neighbors[] = {
{-1, -1}, {0, -1}, {1, -1},
{-1, 0}, {1, 0},
{-1, 1}, {0, 1}, {1, 1},
};
// 3 is 00000011
for (uint8_t x = 0; x < disp.width; x++) {
for (uint8_t y = 0; y < disp.height; y++) {
byte cell = disp.get_pixel(x, y);
cell = ((cell & 3) >> 1) | (cell & ~3);
disp.set_pixel(x, y, cell);
}
}
for (uint8_t x = 0; x < disp.width; x++) {
for (uint8_t y = 0; y < disp.height; y++) {
uint8_t alive_neighbors = 0;
for (uint8_t i = 0; i < sizeof(neighbors) / sizeof(*neighbors); i++) {
if (disp.get_pixel((x + neighbors[i].x + disp.width) % disp.width, (y + neighbors[i].y + disp.height) % disp.height) & 1) {
alive_neighbors++;
}
}
byte cell = disp.get_pixel(x, y);
bool alive = cell & 1;
alive = alive_neighbors == 3 || (alive && alive_neighbors == 2);
disp.set_pixel(x, y, cell | (alive ? 1 : 0) << 1);
}
}
}