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
|
/*
* catch.c
*
* Created on: 25.01.2020
* Author: victor
*/
#include "catch.h"
SDL_Surface* screen;
bool run;
SDL_Rect sdl_rect, sdl_rect2;
// key handler stuff
bool keyPressed[SDLK_LAST];
// the images
SDL_Surface* image_chargebar, *image_chargebar_dark, *image_cat, *image_bird;
struct Cat cat;
struct Bird bird[BIRD_COUNT];
int state, menu_state, score;
int main(int argc, char* argv[]) {
memset(keyPressed,0,sizeof(keyPressed));
srand(435334);
// initialize SDL
if(SDL_Init(SDL_INIT_VIDEO) == -1) {
printf("Could not initialize SDL! Error: %s", SDL_GetError());
return -1;
}
// create a window
screen = SDL_SetVideoMode(640, 480, 8, SDL_DOUBLEBUF/* | SDL_FULLSCREEN*/);
if(!screen) {
printf("Could not create a SDL window! Error: %s", SDL_GetError());
return -1;
}
// initialize the font renderer
Font_Init();
// initialize the pidgin animation
Pidgin_Init();
// load the images
SDL_RWops* rwops = SDL_RWFromConstMem(chargebar_bmp, sizeof(chargebar_bmp) / sizeof(char));
image_chargebar = SDL_LoadBMP_RW(rwops,1);
rwops = SDL_RWFromConstMem(chargebar_dark_bmp, sizeof(chargebar_dark_bmp) / sizeof(char));
image_chargebar_dark = SDL_LoadBMP_RW(rwops,1);
rwops = SDL_RWFromConstMem(cat_bmp, sizeof(cat_bmp) / sizeof(char));
image_cat = SDL_LoadBMP_RW(rwops,1);
SDL_SetColorKey(image_cat, SDL_SRCCOLORKEY, SDL_MapRGB(image_cat->format, 255, 0, 255));
// set the correct physics flags for the cat
cat.jumping = true;
// start the event loop
run = true;
state = 1;
while(run) {
long deltaTime = clock();
// process all sdl events
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
run = false;
break;
case SDL_KEYDOWN:
keyPressed[event.key.keysym.sym] = true;
break;
case SDL_KEYUP:
keyPressed[event.key.keysym.sym] = false;
break;
}
}
// quit if the esc key was pressed
if(keyPressed[SDLK_ESCAPE]) run = false;
// run the game routine
gameRoutine();
// draw the sky
sdl_rect.x = 0;
sdl_rect.y = 0;
sdl_rect.w = screen->w;
sdl_rect.h = screen->h-FLOOR_HEIGHT;
SDL_FillRect(screen,&sdl_rect,SDL_MapRGB(screen->format,0,0,200));
// draw the floor
sdl_rect.y = sdl_rect.h;
sdl_rect.h = FLOOR_HEIGHT;
SDL_FillRect(screen,&sdl_rect,SDL_MapRGB(screen->format,0,200,000));
// draw the cat
cat.x = screen->w/2 - image_cat->w/2;
sdl_rect.x = cat.x;
sdl_rect.y = cat.y;
SDL_BlitSurface(image_cat, 0, screen, &sdl_rect);
if(state == STATE_MAIN_MENU) {
// draw the main menu
sdl_rect.x = 285;
sdl_rect.h = 18;
sdl_rect.y = 195 + menu_state * 20;
sdl_rect.w = 42;
SDL_FillRect(screen,&sdl_rect,SDL_MapRGB(screen->format,204,133,0));
Font_DrawString(screen, 290, 200, "Play");
Font_DrawString(screen, 290, 220, "Quit");
}
else
if(state == STATE_GAME) {
// draw the birds
Pidgin_IncrementFrame();
for(int i = 0; i<BIRD_COUNT; i++) {
if(bird[i].type == BIRD_TYPE_PIDGIN) {
draw_Pidgin(screen, bird[i].x, bird[i].y);
}
else if(bird[i].type == BIRD_TYPE_DEAD_PIDGIN) {
sdl_rect.x = bird[i].x;
sdl_rect.y = bird[i].y;
SDL_BlitSurface(image_pidgin_dead, 0, screen, &sdl_rect);
}
}
// draw the chargebar
int chargeBarSplitPosition;
if(!cat.jumping && cat.downwardForce == 0) {
chargeBarSplitPosition = CHARGEBAR_WIDTH;
}
else
if(cat.downwardForce == 0)
chargeBarSplitPosition = 0;
else
if(cat.downwardForce < 0)
chargeBarSplitPosition = CHARGEBAR_WIDTH / -(MAXIMUM_JUMP_FORCE / cat.downwardForce);
else
chargeBarSplitPosition = CHARGEBAR_WIDTH / (MAXIMUM_JUMP_FORCE / cat.downwardForce);
sdl_rect.x = 0;
sdl_rect.y = 0;
sdl_rect.w = chargeBarSplitPosition;
sdl_rect.h = 1;
sdl_rect2.x = CHARGEBAR_POS_X;
sdl_rect2.y = CHARGEBAR_POS_Y;
// draw it 16 times
for(int i = 0; i<15;i++) {
SDL_BlitSurface(image_chargebar, &sdl_rect, screen, &sdl_rect2);
sdl_rect2.y++;
}
// draw the grayed out part of the bar
sdl_rect.x = chargeBarSplitPosition;
sdl_rect.y = 0;
sdl_rect.w = CHARGEBAR_WIDTH-chargeBarSplitPosition;
sdl_rect2.x = CHARGEBAR_POS_X+chargeBarSplitPosition;
sdl_rect2.y = CHARGEBAR_POS_Y;
// draw it 16 times
for(int i = 0; i<15;i++) {
SDL_BlitSurface(image_chargebar_dark, &sdl_rect, screen, &sdl_rect2);
sdl_rect2.y++;
}
// draw the score
char score_s[50]; // TODO this could create a buffer overflow
sprintf(score_s, "SCORE0: %d", score);
int length = strlen(score_s) + 1;
Font_DrawString(screen, screen->w - length * 8, 5, score_s);
}
//Font_DrawString(screen, 0,5, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstufwxyz\n 01234567890: +");
Font_DrawString(screen, screen->h-10, 5, "");
// draw the screen
SDL_Flip(screen);
// get the delta time
deltaTime = clock() - deltaTime;
deltaTime /= CLOCKS_PER_SEC / 1000;
deltaTime = 50 - deltaTime;
// wait 50 milliseconds
if(deltaTime>0)
usleep(deltaTime);
}
// dispose the surfaces
SDL_FreeSurface(screen);
SDL_FreeSurface(image_chargebar);
SDL_FreeSurface(image_chargebar_dark);
//SDL_FreeSurface(screen);
SDL_Quit();
// close the program
return 0;
}
void gameRoutine() {
if(keyPressed[SDLK_BACKSPACE]) state = STATE_MAIN_MENU;
bool charge = keyPressed[SDLK_SPACE] && (state == STATE_GAME);
// do the physics calculation
if(cat.jumping) {
cat.downwardForce+=0.03;
cat.y+=cat.downwardForce;
// the cat has touched the floor
if(cat.y>screen->h-FLOOR_HEIGHT-CAT_HEIGHT) {
cat.y = screen->h-FLOOR_HEIGHT-CAT_HEIGHT;
cat.jumping = false;
cat.downwardForce = 0;
}
} else {
//the player accumulated some energy and let go of the space bar
if(!charge && cat.downwardForce<0) {
cat.jumping = true;
}
else if(charge) {
if(cat.downwardForce > -MAXIMUM_JUMP_FORCE)
cat.downwardForce-=0.02;
}
}
if(state == STATE_MAIN_MENU) {
if(keyPressed[SDLK_UP] && menu_state > 0)
menu_state--;
if(keyPressed[SDLK_DOWN] && menu_state < 1)
menu_state++;
if(keyPressed[SDLK_RETURN]) {
keyPressed[SDLK_RETURN] = false; // reset the key
if(menu_state == 0) { // start button
score = 0;
state = STATE_GAME;
}
else
if(menu_state == 1) { // quit button
run = false;
}
}
}
else
// move the birds in the game state
if(state == STATE_GAME) {
for(int i = 0; i<BIRD_COUNT; i++) {
// if the selected bird exists, move it
if(bird[i].type == BIRD_TYPE_PIDGIN) {
bird[i].x+=0.5;
// if the bird left the screen, reset it
if(bird[i].x > screen->w) {
bird[i].type = BIRD_TYPE_NONE;
}
else {
// check if the bird collided with the cat
if(checkCollision(cat.x, cat.y, image_cat->w, image_cat->h, bird[i].x, bird[i].y, image_cat->w, image_cat->h)) {
bird[i].type = BIRD_TYPE_DEAD_PIDGIN;
score+=10;
}
}
} else if(bird[i].type == BIRD_TYPE_DEAD_PIDGIN) {
bird[i].y+=0.5;
if(bird[i].y > screen->h-FLOOR_HEIGHT) {
bird[i].type = BIRD_TYPE_NONE;
}
} else {
// there should be a 1 in 10 chance for a bird to spawn
if(getRandomInt(20) == 1) {
bird[i].x = -150 + getRandomInt(100);
bird[i].y = 10 + getRandomInt(screen->h - FLOOR_HEIGHT - 40);
bird[i].type = BIRD_TYPE_PIDGIN;
}
}
}
}
}
bool checkCollision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
if(y1 > y2 + h2) return false;
if(y2 > y1 + h1) return false;
if(x1 > x2 + w2) return false;
if(x2 > x1 + w1) return false;
return true;
}
int getRandomInt(int limit) {
return rand() % (limit+1);
}
|