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 | #include "snake_plugin.h"
#include "gate/mathematics.h"
#include "gate/randomgen.h"
#include "gate/graphics/drawing.h"
#include "gate/graphics/fonts.h"
#include "gate/times.h"
#include "gate/debugging.h"
typedef struct point_class
{
int x;
int y;
} point_t;
typedef struct apple_class
{
point_t coords;
gate_uint32_t score;
} apple_t;
typedef struct snake_class
{
point_t items[512];
gate_size_t length;
point_t direction;
unsigned grow;
gate_uint32_t score;
gate_uint32_t collisions;
gate_uint32_t loot;
} snake_t;
typedef struct snake_patch_class
{
unsigned x;
unsigned y;
unsigned width;
unsigned height;
} snake_patch_t;
struct world_class;
typedef struct world_class world_t;
typedef void(*set_world_pixel_func)(world_t* world, int x, int y, gate_color_t const* ptr_col, snake_patch_t* ptr_patch);
struct world_class
{
gate_rasterimage_t backbuffer;
gate_randomsession_t randomizer;
gate_int64_t move_interval_us;
gate_timecounter_t last_update;
int width;
int height;
int x_factor;
int y_factor;
int backbuffer_x1;
int backbuffer_y1;
int backbuffer_x2;
int backbuffer_y2;
set_world_pixel_func set_pixel;
snake_t snakes[1];
gate_size_t snakes_count;
apple_t apples[4];
gate_size_t apples_count;
gate_uint32_t apple_reset_ms;
gate_uint32_t next_apple_score;
gate_timecounter_t next_applegen;
gate_bool_t redraw_all_required;
};
static world_t world;
static gate_color_t const black = GATE_COLOR_BLACK;
static gate_color_t const white = GATE_COLOR_WHITE;
static gate_color_t const yellow = GATE_COLOR_YELLOW;
static gate_color_t const red = GATE_COLOR_RED;
static void reset_snake(snake_t* ptr_snake, int x, int y)
{
ptr_snake->length = 1;
ptr_snake->direction.x = 0;
ptr_snake->direction.y = 0;
ptr_snake->items[0].x = x;
ptr_snake->items[0].y = y;
ptr_snake->grow = 4;
}
void set_world_pixel_direct(world_t* world, int x, int y, gate_color_t const* ptr_col, snake_patch_t* ptr_patch)
{
const unsigned px = (unsigned)world->backbuffer_x1 + x;
const unsigned py = (unsigned)world->backbuffer_y1 + y;
gate_rasterimage_set_pixel(&world->backbuffer, px, py, ptr_col);
if (ptr_patch)
{
ptr_patch->x = px;
ptr_patch->y = py;
ptr_patch->width = 1;
ptr_patch->height = 1;
}
}
void set_world_pixel_warp(world_t* world, int x, int y, gate_color_t const* ptr_col, snake_patch_t* ptr_patch)
{
const unsigned px = (unsigned)world->backbuffer_x1 + x * world->x_factor;
const unsigned py = (unsigned)world->backbuffer_y1 + y * world->y_factor;
const unsigned px2 = (unsigned)world->backbuffer_x1 + (x + 1) * world->x_factor - 1;
const unsigned py2 = (unsigned)world->backbuffer_y1 + (y + 1) * world->y_factor - 1;
gate_draw_box(&world->backbuffer, px, py, px2, py2, *ptr_col);
if (ptr_patch)
{
ptr_patch->x = px;
ptr_patch->y = py;
ptr_patch->width = px2 - px + 1;
ptr_patch->height = py2 - py + 1;
}
}
static void recalc_world(world_t* world)
{
const int center_x = (int)(world->backbuffer.width / 2);
const int center_y = (int)(world->backbuffer.height / 2);
world->backbuffer_x1 = center_x - world->width / 2 * world->x_factor;
world->backbuffer_y1 = center_y - world->height / 2 * world->y_factor;
world->backbuffer_x2 = world->backbuffer_x1 + world->x_factor * world->width;
world->backbuffer_y2 = world->backbuffer_y1 + world->y_factor * world->height;
if ((world->x_factor == 1) && (world->y_factor == 1))
{
world->set_pixel = &set_world_pixel_direct;
}
else
{
world->set_pixel = &set_world_pixel_warp;
}
}
static void create_apples(world_t* world);
static gate_result_t gateframes_snake_create(gate_framebuffer_t* fb)
{
gate_result_t ret;
gate_framebuffer_info_t fb_info;
gate_uint16_t format_id;
gate_size_t ndx;
do
{
ret = gate_framebuffer_get_info(fb, &fb_info);
GATE_BREAK_IF_FAILED(ret);
switch (fb_info.bits_per_pixel)
{
case 8: format_id = GATE_IMAGE_PIXELFORMAT_PAL8; break;
case 15: format_id = GATE_IMAGE_PIXELFORMAT_RGB555; break;
case 16: format_id = GATE_IMAGE_PIXELFORMAT_RGB555; break;
case 24: format_id = GATE_IMAGE_PIXELFORMAT_RGB24; break;
case 32: format_id = GATE_IMAGE_PIXELFORMAT_RGBA; break;
default: format_id = GATE_IMAGE_PIXELFORMAT_DEFAULT; break;
}
if (NULL == gate_rasterimage_create(&world.backbuffer, format_id, fb_info.width, fb_info.height, NULL))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_randomgenerator_create(&world.randomizer);
if (GATE_FAILED(ret))
{
gate_rasterimage_release(&world.backbuffer);
break;
}
world.width = 260;
world.height = 180;
world.x_factor = fb_info.width / world.width;
world.y_factor = fb_info.height / world.height;
if (world.x_factor < world.y_factor)
{
world.y_factor = world.x_factor;
}
else
{
world.x_factor = world.y_factor;
}
world.move_interval_us = 25000;
world.snakes_count = 1;
for (ndx = 0; ndx != world.snakes_count; ++ndx)
{
reset_snake(&world.snakes[ndx], 150, 90 + (int)ndx * 5);
}
recalc_world(&world);
world.apple_reset_ms = 30000;
world.apples_count = sizeof(world.apples) / sizeof(world.apples[0]);
create_apples(&world);
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
static void snake_move(world_t* world)
{
gate_size_t ndx;
snake_t* ptr_snake;
for (ndx = 0; ndx != world->snakes_count; ++ndx)
{
ptr_snake = &world->snakes[ndx];
if (ptr_snake->length >= (sizeof(ptr_snake->items) / sizeof(ptr_snake->items[0])))
{
ptr_snake->length = (sizeof(ptr_snake->items) / sizeof(ptr_snake->items[0])) - 1;
}
gate_mem_move(&ptr_snake->items[1], &ptr_snake->items[0], ptr_snake->length * sizeof(point_t));
ptr_snake->items[0].x += ptr_snake->direction.x;
ptr_snake->items[0].y += ptr_snake->direction.y;
if (ptr_snake->grow > 0)
{
++ptr_snake->length;
--ptr_snake->grow;
}
}
}
static gate_bool_t points_equal(point_t const* p1, point_t const* p2)
{
return (p1->x == p2->x) && (p1->y == p2->y);
}
static gate_bool_t check_collision(world_t* world, point_t const* p, point_t const* excluded)
{
gate_size_t ndx;
gate_size_t item;
snake_t const* ptr_other;
point_t const* ptr_coord;
if ((p->x < 0) || (p->y < 0) || (p->x >= world->width) || (p->y >= world->height))
{
return true;
}
for (ndx = 0; ndx != world->snakes_count; ++ndx)
{
ptr_other = &world->snakes[ndx];
for (item = 0; item < ptr_other->length; ++item)
{
ptr_coord = &ptr_other->items[item];
if (points_equal(p, ptr_coord))
{
if (excluded == ptr_coord)
{
continue;
}
return true;
}
}
}
return false;
}
static void create_apples(world_t* world)
{
gate_randomsession_t r = NULL;
gate_size_t ndx;
point_t pnt;
gate_uint64_t rnd_num;
const gate_uint32_t next_score = ++world->next_apple_score;
gate_randomgenerator_create(&r);
for (ndx = 0; ndx != world->apples_count; ++ndx)
{
do
{
gate_randomgenerator_get_num(&r, &rnd_num);
pnt.x = (int)((unsigned int)rnd_num % world->width);
gate_randomgenerator_get_num(&r, &rnd_num);
pnt.y = (int)((unsigned int)rnd_num % world->height);
} while (check_collision(world, &pnt, NULL));
world->apples[ndx].coords = pnt;
world->apples[ndx].score = next_score * (gate_uint32_t)(ndx + 1);
}
gate_randomgenerator_destroy(&r);
}
static void snake_check(world_t* world)
{
gate_size_t ndx, ndx2;
snake_t* ptr_snake;
point_t const* p1;<--- The scope of the variable 'p1' can be reduced. [+]The scope of the variable 'p1' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
point_t const* p2;
for (ndx = 0; ndx != world->snakes_count; ++ndx)
{
ptr_snake = &world->snakes[ndx];
p1 = &ptr_snake->items[0];
if (check_collision(world, p1, p1))
{
world->redraw_all_required = true;
++ptr_snake->collisions;
reset_snake(ptr_snake, -1, -1);
ptr_snake->items[0].x = world->width / 2;
ptr_snake->items[0].y = world->height / 2;
}
else
{
for (ndx2 = 0; ndx2 != world->apples_count; ++ndx2)
{
p2 = &world->apples[ndx2].coords;
if ((p1->x == p2->x) && (p1->y == p2->y))
{
ptr_snake->score += world->apples[ndx2].score;
world->apples[ndx2].score = 0;
ptr_snake->grow += 8;
}
}
}
}
}
static gate_size_t snake_paint(world_t* world, gate_bool_t redraw_all, snake_patch_t* patches, gate_size_t patches_capacity)
{
gate_size_t ret = 0;
gate_size_t ndx, item;
snake_t const* ptr_snake;
point_t const* ptr_point;
apple_t const* ptr_apple;<--- The scope of the variable 'ptr_apple' can be reduced. [+]The scope of the variable 'ptr_apple' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
snake_patch_t* ptr_patch;
if (redraw_all)
{
world->redraw_all_required = false;
gate_rasterimage_clear(&world->backbuffer, &black);
gate_draw_rectangle(&world->backbuffer,
world->backbuffer_x1, world->backbuffer_y1,
world->backbuffer_x2, world->backbuffer_y2,
white);
for (ndx = 0; ndx != world->apples_count; ++ndx)
{
ptr_apple = &world->apples[ndx];
if (ptr_apple->score > 0)
{
ptr_patch = (ret < patches_capacity) ? &patches[ret++] : NULL;
world->set_pixel(world, ptr_apple->coords.x, ptr_apple->coords.y, &red, ptr_patch);
}
}
}
for (ndx = 0; ndx != world->snakes_count; ++ndx)
{
ptr_snake = &world->snakes[ndx];
if (ptr_snake->length < 1)
{
continue;
}
for (item = 1; item < ptr_snake->length - 1; ++item)
{
ptr_point = &ptr_snake->items[item];
/* ptr_patch = (ret < patches_capacity) ? &patches[ret++] : NULL; */
world->set_pixel(world, ptr_point->x, ptr_point->y, &yellow, NULL /* ptr_patch */);
}
ptr_point = &ptr_snake->items[ptr_snake->length - 1];
ptr_patch = (ret < patches_capacity) ? &patches[ret++] : NULL;
world->set_pixel(world, ptr_point->x, ptr_point->y, &black, ptr_patch);
ptr_point = &ptr_snake->items[0];
ptr_patch = (ret < patches_capacity) ? &patches[ret++] : NULL;
world->set_pixel(world, ptr_point->x, ptr_point->y, &yellow, ptr_patch);
}
return ret;
}
static gate_result_t gateframes_snake_destroy(gate_framebuffer_t* fb)
{
GATE_UNUSED_ARG(fb);
gate_randomgenerator_destroy(&world.randomizer);
gate_rasterimage_release(&world.backbuffer);
return GATE_RESULT_OK;
}
static gate_result_t gateframes_snake_render(gate_framebuffer_t* fb, gate_bool_t redraw_all)
{
snake_patch_t patches[32];
gate_size_t patches_used = 0;
gate_size_t ndx;
gate_rasterimage_t patch_image;
gate_result_t result = GATE_RESULT_OK;
snake_patch_t const* ptr_patch;<--- The scope of the variable 'ptr_patch' can be reduced. [+]The scope of the variable 'ptr_patch' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_bool_t complete_redraw = world.redraw_all_required || redraw_all;
world.redraw_all_required = false;
if (!complete_redraw)
{
patches_used = snake_paint(&world, false, patches, sizeof(patches) / sizeof(patches[0]));
for (ndx = 0; ndx != patches_used; ++ndx)
{
ptr_patch = &patches[ndx];
result = gate_rasterimage_subset(&patch_image, &world.backbuffer, ptr_patch->x, ptr_patch->y, ptr_patch->width, ptr_patch->height);
GATE_BREAK_IF_FAILED(result);
result = gate_framebuffer_set_image(fb, &patch_image, ptr_patch->x, ptr_patch->y);
gate_rasterimage_release(&patch_image);
GATE_BREAK_IF_FAILED(result);
}
if (GATE_SUCCEEDED(result))
{
return result;
}
else
{
GATE_DEBUG_BREAKPOINT;
}
/* error case: try redraw all*/
}
else
{
snake_paint(&world, true, NULL, 0);
}
/* redraw all */
return gate_framebuffer_set_image(fb, &world.backbuffer, 0, 0);
}
static gate_result_t gateframes_snake_handle_event(gate_framebuffer_t* fb, gate_framebuffer_event_t* evt,
gate_bool_t* render_frame, gate_uint32_t* out_next_event_timeout)
{
gate_result_t ret = GATE_RESULT_OK;
gate_timecounter_t now;
gate_int64_t timediff_us;
snake_t* ptr_snake = &world.snakes[0];
GATE_UNUSED_ARG(fb);
*render_frame = true;
switch (evt->event_type)
{
case GATE_FRAMEBUFFER_EVENT_KEY_DOWN:
{
switch (evt->event_data.key.key_code)
{
case GATE_KBD_KEY_8:
case GATE_KBD_KEY_UP:
if (ptr_snake->direction.y == 0)
{
ptr_snake->direction.x = 0;
ptr_snake->direction.y = -1;
}
break;
case GATE_KBD_KEY_2:
case GATE_KBD_KEY_DOWN:
if (ptr_snake->direction.y == 0)
{
ptr_snake->direction.x = 0;
ptr_snake->direction.y = +1;
}
break;
case GATE_KBD_KEY_4:
case GATE_KBD_KEY_LEFT:
if (ptr_snake->direction.x == 0)
{
ptr_snake->direction.x = -1;
ptr_snake->direction.y = 0;
}
break;
case GATE_KBD_KEY_6:
case GATE_KBD_KEY_RIGHT:
if (ptr_snake->direction.x == 0)
{
ptr_snake->direction.x = +1;
ptr_snake->direction.y = 0;
}
break;
}
break;
}
case GATE_FRAMEBUFFER_EVENT_KEY_UP:
{
break;
}
case GATE_FRAMEBUFFER_EVENT_KEY_PRESS:
{
break;
}
case GATE_FRAMEBUFFER_EVENT_POINTER_BUTTON_DOWN:
{
break;
}
case GATE_FRAMEBUFFER_EVENT_POINTER_BUTTON_UP:
{
break;
}
case GATE_FRAMEBUFFER_EVENT_POINTER_MOVE:
{
break;
}
case GATE_FRAMEBUFFER_EVENT_CANCEL:
{
break;
}
}
gate_timecounter_now(&now);
if (now > world.next_applegen)
{
world.redraw_all_required = true;
world.next_applegen = gate_timecounter_add(now, (gate_int64_t)world.apple_reset_ms * 1000);
create_apples(&world);
}
timediff_us = gate_timecounter_diff(now, world.last_update);
if ((timediff_us < 0) || (timediff_us >= world.move_interval_us))
{
world.last_update = now;
snake_move(&world);
snake_check(&world);
*render_frame = true;
*out_next_event_timeout = (gate_uint32_t)(world.move_interval_us / 1000);
}
else
{
*out_next_event_timeout = (gate_uint32_t)((world.move_interval_us - timediff_us) / 1000);
*render_frame = false;
}
return ret;
}
static gateframes_plugin_t snake_plugin =
{
&gateframes_snake_create,
&gateframes_snake_destroy,
&gateframes_snake_render,
&gateframes_snake_handle_event
};
gateframes_plugin_t* get_snake_plugin()
{
return &snake_plugin;
}
|