-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
55 lines (47 loc) · 1.29 KB
/
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
#include "graphics.h"
#define WIDTH 640
#define HEIGHT 480
struct game_state {
int quit;
int color_idx;
uint32_t row_off;
};
int main(void) {
if (graphics_setup(WIDTH, HEIGHT) < 0) {
return 1;
}
struct game_state state = {
.quit = 0,
.color_idx = 2,
.row_off = 0,
};
while (!state.quit) {
int key;
while ((key = graphics_get_keypress())) {
switch (key) {
case ' ':
state.color_idx = (state.color_idx + 1) % 3;
break;
case 'q':
case ASCII_ESC:
state.quit = 1;
break;
}
}
uint32_t pixels[WIDTH*HEIGHT];
for (int row = 0; row < HEIGHT; row++) {
uint32_t actual_row = (row + state.row_off) % HEIGHT;
uint32_t color = (uint32_t)((double)row/HEIGHT*0xff) << (8*state.color_idx) | (0xffffffUL & ~(0xffUL << (8*state.color_idx)));
for (int col = 0; col < WIDTH; col++) {
pixels[actual_row*WIDTH + col] = color;
}
}
if (graphics_render(pixels) < 0) {
return 1;
}
state.row_off = (state.row_off + 1) % HEIGHT;
graphics_delay(1);
}
graphics_teardown();
return 0;
}