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
|
#include <games/snake/Snake.hpp>
#include <imgui.h>
Snake::Snake () {
m_IsPaused = false;
m_IsRunning = true;
m_DtInSecondsRemaining = 0.0f;
m_LastMillisecondsSinceT0 = SDL_GetTicks();
m_TilesPerSecond = 4.0f;
m_Direction = DIRECTION_RIGHT;
m_LastAdvancedDirection = DIRECTION_RIGHT;
m_MapWidth = 16;
m_MapHeight = 16;
assert(MAX_MAP_WIDTH <= 64); // m_BodyBitmap is uint64_t[]. We can't exceed that!
assert(MAX_MAP_HEIGHT <= 64);
assert(m_MapWidth <= MAX_MAP_WIDTH);
assert(m_MapHeight <= MAX_MAP_WIDTH);
m_Tail = 0;
m_Head = 1;
memset(m_BodyBitmap, 0, sizeof(m_BodyBitmap));
int32_t head_x = m_MapWidth / 2;
int32_t head_y = m_MapHeight / 2;
m_BodyPositions[0] = {head_x -1, head_y};
m_BodyPositions[1] = {head_x, head_y};
m_Rng = std::mt19937((std::random_device()()));
m_Dist = std::uniform_int_distribution<int32_t>(0, m_MapWidth * m_MapHeight - 3);
SpawnFood();
}
bool Snake::Update(std::vector<SDL_Event> &events, RenderGroup &render_group) {
uint64_t milliseconds_since_t0 = SDL_GetTicks();
uint64_t milliseconds_since_t0_last = m_LastMillisecondsSinceT0;
uint64_t dt_in_milliseconds = milliseconds_since_t0 - milliseconds_since_t0_last;
float dt_in_seconds = (float)dt_in_milliseconds / 1000.0f;
m_LastMillisecondsSinceT0 = milliseconds_since_t0;
V3F32 clear_color = V3F32(0.3f, 0.3f, 0.3f);
render_group.SetCameraSize(4.0f, 3.0f);
render_group.Clear(clear_color);
for (SDL_Event &event : events) {
if (!m_IsRunning) {
printf("event loop is running just false\n");
return false;
}
if (m_IsPaused) {
ProcessEventDuringPause(event);
}
else {
ProcessEventDuringResume(event);
}
}
if (!m_IsPaused) {
MaybeMoveSnake(dt_in_seconds);
}
Draw(render_group);
DoImgui();
return m_IsRunning;
}
void Snake::MaybeMoveSnake(float dt_in_seconds) {
float dt_in_seconds_to_use = m_DtInSecondsRemaining + dt_in_seconds;
float tiles_per_second = m_TilesPerSecond;
float seconds_per_tile = 1.0f / tiles_per_second;
while (dt_in_seconds_to_use > seconds_per_tile) {
V2I32 head_pos = m_BodyPositions[m_Head];
V2I32 tail_pos = m_BodyPositions[m_Tail];
// find head_pos
if (m_Direction == DIRECTION_UP) {
head_pos.y += 1;
}
else if (m_Direction == DIRECTION_DOWN) {
head_pos.y -= 1;
}
else if (m_Direction == DIRECTION_RIGHT) {
head_pos.x += 1;
}
else if (m_Direction == DIRECTION_LEFT) {
head_pos.x -= 1;
}
if ((head_pos.x < 0 || head_pos.x >= m_MapWidth) ||
(head_pos.y < 0 || head_pos.y >= m_MapHeight))
{
m_IsRunning = false;
return;
}
uint64_t head_bit = 1 << head_pos.x;
uint64_t body_bits = m_BodyBitmap[head_pos.y];
if (head_pos.y == tail_pos.y) {
body_bits &= ~(1 << tail_pos.x);
}
if (head_bit & body_bits) {
m_IsRunning = false;
return;
}
// advance head
int32_t max_positions = sizeof(m_BodyPositions) / sizeof(m_BodyPositions[0]);
m_Head += 1;
if (m_Head >= max_positions) {
m_Head = 0;
}
m_BodyPositions[m_Head] = head_pos;
m_BodyBitmap[head_pos.y] |= (1 << head_pos.x);
if (m_BodyPositions[m_Head] == m_FoodPosition) {
SpawnFood();
}
else {
// advance tail
V2I32 next_tail_pos = m_BodyPositions[m_Tail];
m_BodyBitmap[next_tail_pos.y] &= ~(1 << next_tail_pos.x);
m_Tail += 1;
if (m_Tail >= max_positions) {
m_Tail = 0;
}
}
m_LastAdvancedDirection = m_Direction;
dt_in_seconds_to_use -= seconds_per_tile;
}
m_DtInSecondsRemaining = dt_in_seconds_to_use;
}
void Snake::ProcessEventDuringPause(SDL_Event &event) {
switch (event.type) {
case SDL_EVENT_KEY_DOWN: {
if (event.key.key == SDLK_ESCAPE) {
m_IsPaused = false;
}
}
default:;
}
}
void Snake::ProcessEventDuringResume(SDL_Event &event) {
switch (event.type) {
case SDL_EVENT_KEY_DOWN: {
if (event.key.key == SDLK_UP) {
if (m_LastAdvancedDirection == DIRECTION_RIGHT ||
m_LastAdvancedDirection == DIRECTION_LEFT)
{
m_Direction = DIRECTION_UP;
}
}
else if (event.key.key == SDLK_DOWN) {
if (m_LastAdvancedDirection == DIRECTION_RIGHT ||
m_LastAdvancedDirection == DIRECTION_LEFT)
{
m_Direction = DIRECTION_DOWN;
}
}
else if (event.key.key == SDLK_RIGHT) {
if (m_LastAdvancedDirection == DIRECTION_UP ||
m_LastAdvancedDirection == DIRECTION_DOWN)
{
m_Direction = DIRECTION_RIGHT;
}
}
else if (event.key.key == SDLK_LEFT) {
if (m_LastAdvancedDirection == DIRECTION_UP ||
m_LastAdvancedDirection == DIRECTION_DOWN)
{
m_Direction = DIRECTION_LEFT;
}
}
else if (event.key.key == SDLK_ESCAPE) {
m_IsPaused = true;
}
}
default:;
}
}
void Snake::SpawnFood() {
int32_t bit0_counts[MAX_MAP_HEIGHT];
int32_t bit0_count_total = 0;
// count bits
for (int32_t y = 0; y < m_MapHeight; y++) {
int32_t bit1_count = 0;
uint64_t bitmap_row = m_BodyBitmap[y];
while (bitmap_row != 0) {
bitmap_row = bitmap_row & (bitmap_row - 1);
bit1_count += 1;
}
int32_t bit0_count = m_MapWidth - bit1_count;
bit0_counts[y] = bit0_count;
bit0_count_total += bit0_count;
}
if (bit0_count_total == 0) {
return;
}
m_Dist.param(std::uniform_int_distribution<int32_t>::param_type(0, bit0_count_total - 1));
int32_t bit0_index = m_Dist(m_Rng);
int32_t bit0_x = 0;
int32_t bit0_y = 0;
// find y
for (int32_t y = 0; y < m_MapHeight; y++) {
if (bit0_index < bit0_counts[y]) {
bit0_y = y;
break;
}
bit0_index -= bit0_counts[y];
}
// find x
uint64_t bitmap_row_not = ~m_BodyBitmap[bit0_y];
for (int32_t x = 0; x < m_MapWidth; x++) {
if (bitmap_row_not & 1) {
if (bit0_index == 0) {
bit0_x = x;
break;
}
bit0_index--;
}
bitmap_row_not >>= 1;
}
m_FoodPosition = {bit0_x, bit0_y};
}
void Snake::Draw(RenderGroup &render_group) {
float world_width = 4.0f;
float world_height = 3.0f;
float tile_size = (world_width / 2) / MAX_MAP_WIDTH;
float bodypart_size = 0.8f * tile_size;
float bodypart_offset = (tile_size - bodypart_size) / 2;
float map_view_width = tile_size * (float)m_MapWidth;
float map_view_height = tile_size * (float)m_MapHeight;
float map_x = (world_width - map_view_width) / 2;
float map_y = (world_height - map_view_height) / 2;
int32_t max_positions = sizeof(m_BodyPositions) / sizeof(m_BodyPositions[0]);
/* draw map background */
V3F32 map_world_pos = {map_x, map_y, 0.0f};
V2F32 map_world_dim = {map_view_width, map_view_height};
V3F32 bg_color = {0.0f, 0.0f, 0.0f};
render_group.PushRectangle(map_world_pos, map_world_dim, bg_color);
/* draw snake */
// 1) if tail > head: advance to end first
int32_t tail = m_Tail;
if (tail > m_Head) {
while (tail < max_positions) {
V3F32 local_pos = {
(float)m_BodyPositions[tail].x * tile_size + bodypart_offset,
(float)m_BodyPositions[tail].y * tile_size + bodypart_offset,
1.0f
};
V2F32 local_dim = {bodypart_size, bodypart_size};
V3F32 world_pos = {
map_world_pos.x + local_pos.x,
map_world_pos.y + local_pos.y,
1.0f
};
V2F32 world_dim = local_dim;
V3F32 color = {0.3f, 0.3f, 0.3f};
render_group.PushRectangle(world_pos, world_dim, color);
tail++;
}
tail = 0;
}
// 2) advance to head
while (tail <= m_Head) {
V3F32 local_pos = {
(float)m_BodyPositions[tail].x * tile_size + bodypart_offset,
(float)m_BodyPositions[tail].y * tile_size + bodypart_offset,
1.0f
};
V2F32 local_dim = {bodypart_size, bodypart_size};
V3F32 world_pos = {
map_world_pos.x + local_pos.x,
map_world_pos.y + local_pos.y,
1.0f
};
V2F32 world_dim = local_dim;
V3F32 color = {0.3f, 0.3f, 0.3f};
render_group.PushRectangle(world_pos, world_dim, color);
tail++;
}
/* draw food */
V3F32 pos = {
map_world_pos.x + (float)m_FoodPosition.x * tile_size + bodypart_offset,
map_world_pos.y + (float)m_FoodPosition.y * tile_size + bodypart_offset,
1.0f
};
V2F32 dim = {bodypart_size, bodypart_size};
V3F32 color = {0.3f, 0.6f, 0.4f};
render_group.PushRectangle(pos, dim, color);
}
void Snake::DoImgui() {
if (m_IsPaused) {
ImGui::Begin("SnakePause");
if (ImGui::Button("Resume")) {
m_IsPaused = false;
}
if (ImGui::Button("Exit")) {
m_IsRunning = false;
}
ImGui::End();
}
}
|