/
flappy.c
190 lines (169 loc) · 3.24 KB
/
flappy.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
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
// 2014 by nucular, MIT license
// tigcc -O2 -Wall flappy.c extgraph/extgraph.a -DLC_LANG=LC_ENGLISH
#include "flappy.h"
// configuration headers
#include "locale.h"
#include "config.h"
#include <tigcclib.h>
#include "graphics.c"
#include "menu.c"
#include "game.c"
#include "gameover.c"
// allocate and set up stuff
void init()
{
// start the random number generator
randomize();
// allocate our graphic buffers
buffermem = malloc(LCD_WIDTH * LCD_HEIGHT * 2);
// not enough RAM
if (!buffermem)
error(LC_MEMORY_ERROR_TEXT);
lightbuffer = buffermem;
darkbuffer = buffermem + LCD_WIDTH * LCD_HEIGHT;
// quit if we can't switch to grayscale
if (!GrayOn())
error(LC_GRAYSCALE_ERROR_TEXT);
// clear the screen
ClrScr();
// get the grayscale plane pointers
lightplane = GrayGetPlane(LIGHT_PLANE);
darkplane = GrayGetPlane(DARK_PLANE);
}
// clean up and exit gracefully
void deinit()
{
if (buffermem)
free(buffermem);
// turn this off or we'd crash the calc
GrayOff();
// for good measure
ClrScr();
}
// generic error handler
void error(char* text)
{
switchgs(GS_NONE);
deinit();
DlgMessage(LC_ERROR, text, BT_OK, BT_NONE);
exit(1);
}
// entry point
void _main(void)
{
// some variables for the main loop
unsigned int lasttick = 0;
void *keyqueue = kbd_queue();
unsigned short key;
// initiate all the stuff
init();
// start at the menu
switchgs(GS_MENU);
// main loop
while (gs != GS_NONE)
{
// run at 20 fps
if (FiftyMsecTick != lasttick)
{
// Check for keypresses
if (!OSdequeue(&key, keyqueue))
{
if (key == KEY_ON)
off();
else if (key == KEY_QUIT)
switchgs(GS_NONE);
else
{
switch (gs)
{
case GS_MENU:
if (key == KEY_ENTER)
switchgs(GS_GAME);
else if (key == KEY_ESC)
switchgs(GS_NONE);
break;
case GS_GAME:
if (key == KEY_ENTER)
game_flap();
else if (key == KEY_ESC)
switchgs(GS_MENU);
break;
case GS_GAMEOVER:
if (key == KEY_ENTER)
switchgs(GS_GAME);
else if (key == KEY_ESC)
switchgs(GS_MENU);
break;
default:
break;
}
}
}
// draw to the buffers
GrayClearScreen2B(lightbuffer, darkbuffer);
switch (gs)
{
case GS_MENU:
menu_update();
menu_draw();
break;
case GS_GAME:
game_update();
game_draw();
break;
case GS_GAMEOVER:
gameover_update();
game_draw();
gameover_draw();
break;
default:
break;
}
// flip the buffers
FastCopyScreen(darkbuffer, darkplane);
FastCopyScreen(lightbuffer, lightplane);
lasttick = FiftyMsecTick;
framecounter++;
}
}
// important!
deinit();
}
// switch between gamestates
void switchgs(enum GameState newgs)
{
FastClearScreen_R(lightbuffer);
FastClearScreen_R(darkbuffer);
birdframe = 0;
// deinitialize
switch (gs)
{
case GS_MENU:
menu_deinit(newgs);
break;
case GS_GAME:
game_deinit(newgs);
break;
case GS_GAMEOVER:
gameover_deinit(newgs);
break;
default:
break;
}
// initialize
switch (newgs)
{
case GS_MENU:
menu_init(gs);
break;
case GS_GAME:
game_init(gs);
break;
case GS_GAMEOVER:
gameover_init(gs);
break;
default:
break;
}
gs = newgs;
}