1 /* game.c */
2
3 # include "game.h"
4
game(SDL_Surface * screen,uint * state,uint * level)5 void game (SDL_Surface *screen, uint *state, uint *level) {
6
7 SDL_Surface *roundscreen = NULL;
8 SDL_Surface *blocks = NULL;
9 SDL_Surface *temp = NULL;
10 SDL_Surface *headgame = NULL;
11 SDL_Surface *fonts = NULL;
12 SDL_Surface *blackbox = NULL;
13 SDL_Surface *window = NULL;
14 SDL_Surface *doble = NULL;
15 SDL_Surface *gameoverscreen = NULL;
16 SDL_Surface *passscreen01 = NULL;
17 SDL_Surface *passscreen02 = NULL;
18 SDL_Surface *passscreen03 = NULL;
19 SDL_Surface *passscreen04 = NULL;
20 SDL_Surface *passscreen05 = NULL;
21 SDL_Surface *passscreen06 = NULL;
22 SDL_Surface *passscreen07 = NULL;
23 SDL_Surface *passscreen08 = NULL;
24 SDL_Surface *passscreen09 = NULL;
25 SDL_Surface *passscreen10 = NULL;
26
27 Mix_Music *bsogame;
28 Mix_Music *gameover;
29 Mix_Chunk *stageclear;
30 Mix_Chunk *giveup;
31 Mix_Chunk *key;
32 Mix_Chunk *kill;
33
34 int map[58][11][16];
35
36 uint step = 0;
37 uint waittime = 0;
38 uint fadecounter = 255;
39 int framerate = 0;
40 uint fademode = 0;
41 uint i = 0;
42 uint j = 0;
43 int round = (*level - 1);
44 int points = 0;
45 int counter = 0;
46 int animationtime = 0;
47 uint pausa = 0;
48 uint loadoninit = 0;
49 uint uplife = 0;
50 uint soundblock = 0;
51 uint grieltouch = 0;
52 uint fullscreench = 0;
53
54 /* load files */
55 temp = IMG_Load(DATADIR "/png/round.png");
56 roundscreen = SDL_DisplayFormat(temp);
57 SDL_FreeSurface(temp);
58 temp = IMG_Load(DATADIR "/png/blocks.png");
59 blocks = SDL_DisplayFormatAlpha(temp);
60 SDL_FreeSurface(temp);
61 temp = IMG_Load(DATADIR "/png/gamehead.png");
62 headgame = SDL_DisplayFormat(temp);
63 SDL_FreeSurface(temp);
64 temp = IMG_Load(DATADIR "/png/fonts.png");
65 fonts = SDL_DisplayFormatAlpha(temp);
66 SDL_FreeSurface(temp);
67 temp = IMG_Load(DATADIR "/png/blackbox.png");
68 blackbox = SDL_DisplayFormat(temp);
69 window = SDL_DisplayFormat(temp);
70 SDL_FreeSurface(temp);
71 temp = IMG_Load(DATADIR "/png/gameover.png");
72 gameoverscreen = SDL_DisplayFormat(temp);
73 SDL_FreeSurface(temp);
74 temp = IMG_Load(DATADIR "/png/passw1.png");
75 passscreen01 = SDL_DisplayFormat(temp);
76 SDL_FreeSurface(temp);
77 temp = IMG_Load(DATADIR "/png/passw2.png");
78 passscreen02 = SDL_DisplayFormat(temp);
79 SDL_FreeSurface(temp);
80 temp = IMG_Load(DATADIR "/png/passw3.png");
81 passscreen03 = SDL_DisplayFormat(temp);
82 SDL_FreeSurface(temp);
83 temp = IMG_Load(DATADIR "/png/passw4.png");
84 passscreen04 = SDL_DisplayFormat(temp);
85 SDL_FreeSurface(temp);
86 temp = IMG_Load(DATADIR "/png/passw5.png");
87 passscreen05 = SDL_DisplayFormat(temp);
88 SDL_FreeSurface(temp);
89 temp = IMG_Load(DATADIR "/png/passw6.png");
90 passscreen06 = SDL_DisplayFormat(temp);
91 SDL_FreeSurface(temp);
92 temp = IMG_Load(DATADIR "/png/passw7.png");
93 passscreen07 = SDL_DisplayFormat(temp);
94 SDL_FreeSurface(temp);
95 temp = IMG_Load(DATADIR "/png/passw8.png");
96 passscreen08 = SDL_DisplayFormat(temp);
97 SDL_FreeSurface(temp);
98 temp = IMG_Load(DATADIR "/png/passw9.png");
99 passscreen09 = SDL_DisplayFormat(temp);
100 SDL_FreeSurface(temp);
101 temp = IMG_Load(DATADIR "/png/passw10.png");
102 passscreen10 = SDL_DisplayFormat(temp);
103 SDL_FreeSurface(temp);
104 stageclear = Mix_LoadWAV(DATADIR "/music/stageclear.ogg");
105 gameover = Mix_LoadMUS(DATADIR "/music/gameover.ogg");
106 giveup = Mix_LoadWAV(DATADIR "/fx/fx_giveup.ogg");
107 key = Mix_LoadWAV(DATADIR "/fx/fx_key.ogg");
108 kill = Mix_LoadWAV(DATADIR "/fx/fx_kill.ogg");
109
110 /* load map data */
111 loaddata(map);
112
113 /* Enable repeat keys */
114 SDL_EnableKeyRepeat(30, 30);
115
116 /* init array */
117 struct hero griel = {
118 .score = 0,
119 .lifes = 3,
120 .object = 0,
121 .key = 0,
122 .x = 112,
123 .y = 48,
124 .positionx = 7,
125 .positiony = 1,
126 .direction = 2,
127 .locked = 0,
128 .movement = 0,
129 .endanimation = 0,
130 .deathanimation = 0,
131 .deathturns = 0,
132 .killed = 0,
133 .grial = 0,
134 };
135
136 SDL_Rect srcfonts = {0,128,8,8};
137 SDL_Rect desfonts = {136,96,8,8};
138 SDL_Rect srcblocks = {0,0,16,16};
139 SDL_Rect desblocks = {0,0,16,16};
140 SDL_Rect srctext = {0,0,256,0};
141
142 while (*state == 2) {
143 framerate = control_frames(1,0);
144 if (counter < 58)
145 counter ++;
146 else
147 counter = 0;
148 switch (step) {
149 case 0: /* Show round screen */
150 *level = round + 1;
151 desfonts.x = 144;
152 SDL_BlitSurface(roundscreen,NULL,window,NULL);
153 if (*level < 10) {
154 srcfonts.x = 0;
155 SDL_BlitSurface(fonts,&srcfonts,window,&desfonts);
156 srcfonts.x = *level * 8;
157 desfonts.x = 152;
158 SDL_BlitSurface(fonts,&srcfonts,window,&desfonts);
159 }
160 else {
161 srcfonts.x = (*level / 10) * 8;
162 SDL_BlitSurface(fonts,&srcfonts,window,&desfonts);
163 srcfonts.x = (*level - ((*level / 10) * 10)) * 8;
164 desfonts.x = 152;
165 SDL_BlitSurface(fonts,&srcfonts,window,&desfonts);
166 }
167 /* Apply transparency */
168 SDL_SetAlpha(blackbox,SDL_RLEACCEL|SDL_SRCALPHA,(Uint8)fadecounter);
169 SDL_BlitSurface(blackbox,NULL,window,NULL);
170 if ((fademode == 0) && (fadecounter == 255))
171 /* Disable push keys until game start */
172 SDL_EventState(SDL_KEYDOWN, SDL_IGNORE);
173 if ((fademode == 0) && (fadecounter > 0))
174 fadecounter -= 3;
175 if ((fademode == 1) && (fadecounter < 255))
176 fadecounter += 3;
177 if ((fademode == 0) && (fadecounter == 0))
178 fademode = 1;
179 if ((fademode == 1) && (fadecounter == 255))
180 waittime ++;
181 if (waittime == 60) {
182 step = 1;
183 waittime = 0;
184 loadoninit = 1;
185 fademode = 0;
186 SDL_EventState(SDL_KEYDOWN, SDL_ENABLE); /* Enable pushes keys */
187 }
188 break;
189 case 1: /* If stage starts now, load music */
190 if (loadoninit == 1) {
191 load_music(bsogame,round);
192 loadoninit = 0;
193 set_hero_init (&griel, round);
194 }
195 /* If hero have killed, reload map */
196 if (griel.killed == 1) {
197 loaddata(map);
198 griel.killed = 0;
199 }
200 /* When hero exits, play stage clear sound */
201 if (waittime == 1)
202 Mix_PlayChannel(-1,stageclear,0);
203 /* Checking score for extra life (every 5000 points), 4 lifes max. */
204 extralife (&griel, &uplife);
205 /* Show game screen */
206 SDL_BlitSurface(headgame,NULL,window,NULL);
207 /* Show hud */
208 show_hud (griel, fonts, window, blocks, round);
209 /* Show titles */
210 show_tiles (&griel, &animationtime, map, window, blocks, round, counter, key);
211 /* check for obstacles */
212 check_obstacles (&griel, round, map, kill, &grieltouch);
213 /* Griel touched ? */
214 if (grieltouch == 1)
215 *state = 3;
216 /* Show hero */
217 show_hero(&griel, counter, window, blocks, &round, &step, &waittime, &soundblock, giveup);
218 /* Key pressed */
219 controls(&griel,&fullscreench);
220 if (fullscreench == 1) {
221 SDL_WM_ToggleFullScreen (screen);
222 fullscreench = 0;
223 }
224 break;
225 case 2: /* gameover screen for 10 seconds */
226 if (waittime < 600) {
227 waittime ++;
228 SDL_BlitSurface(gameoverscreen,NULL,window,NULL);
229 if (waittime == 1)
230 Mix_PlayMusic(gameover, 0);
231 }
232 else {
233 *state = 0;
234 Mix_HaltMusic();
235 }
236 break;
237 case 3: /* show password info, with fade in & out */
238 if (round == 4)
239 SDL_BlitSurface(passscreen01,NULL,window,NULL);
240 if (round == 9)
241 SDL_BlitSurface(passscreen02,NULL,window,NULL);
242 if (round == 14)
243 SDL_BlitSurface(passscreen03,NULL,window,NULL);
244 if (round == 19)
245 SDL_BlitSurface(passscreen04,NULL,window,NULL);
246 if (round == 24)
247 SDL_BlitSurface(passscreen05,NULL,window,NULL);
248 if (round == 29)
249 SDL_BlitSurface(passscreen06,NULL,window,NULL);
250 if (round == 35)
251 SDL_BlitSurface(passscreen07,NULL,window,NULL);
252 if (round == 41)
253 SDL_BlitSurface(passscreen08,NULL,window,NULL);
254 if (round == 47)
255 SDL_BlitSurface(passscreen09,NULL,window,NULL);
256 if (round == 53)
257 SDL_BlitSurface(passscreen10,NULL,window,NULL);
258 SDL_SetAlpha(blackbox,SDL_RLEACCEL|SDL_SRCALPHA,(Uint8)fadecounter);
259 SDL_BlitSurface(blackbox,NULL,window,NULL);
260 if ((fademode == 0) && (fadecounter == 255))
261 SDL_EventState(SDL_KEYDOWN, SDL_IGNORE);
262 if ((fademode == 0) && (fadecounter > 0))
263 fadecounter -= 3;
264 if ((fademode == 1) && (fadecounter < 255))
265 fadecounter += 3;
266 if ((fademode == 0) && (fadecounter == 0))
267 waittime ++;
268 if (((fademode == 0) && (fadecounter == 0)) && (waittime == 600))
269 fademode = 1;
270 if (waittime > 599) {
271 round ++;
272 waittime = 0;
273 step = 0;
274 fademode = 0;
275 }
276 break;
277 }
278
279 /* Zoom 2x */
280 doble = zoomSurface(window,2,2,0);
281 SDL_BlitSurface(doble,NULL,screen,NULL);
282 SDL_Flip(screen);
283 SDL_FreeSurface(doble);
284 SDL_FreeSurface(screen);
285 control_frames(2,framerate);
286 }
287
288 /* Cleaning */
289 SDL_FreeSurface(roundscreen);
290 SDL_FreeSurface(blocks);
291 SDL_FreeSurface(headgame);
292 SDL_FreeSurface(fonts);
293 SDL_FreeSurface(blackbox);
294 SDL_FreeSurface(window);
295 SDL_FreeSurface(gameoverscreen);
296 SDL_FreeSurface(passscreen01);
297 SDL_FreeSurface(passscreen02);
298 SDL_FreeSurface(passscreen03);
299 SDL_FreeSurface(passscreen04);
300 SDL_FreeSurface(passscreen05);
301 SDL_FreeSurface(passscreen06);
302 SDL_FreeSurface(passscreen07);
303 SDL_FreeSurface(passscreen08);
304 SDL_FreeSurface(passscreen09);
305 SDL_FreeSurface(passscreen10);
306 /* Mix_FreeMusic(bsogame); */
307 Mix_FreeMusic(gameover);
308 Mix_FreeChunk(stageclear);
309 Mix_FreeChunk(giveup);
310 Mix_FreeChunk(key);
311 Mix_FreeChunk(kill);
312
313 }
314
show_tiles(struct hero * griel,int * animationtime,int map[][11][16],SDL_Surface * window,SDL_Surface * blocks,int round,int counter,Mix_Chunk * key)315 void show_tiles (struct hero *griel, int *animationtime, int map[][11][16], SDL_Surface *window, SDL_Surface *blocks, int round, int counter, Mix_Chunk *key) {
316
317 SDL_Rect srcblocks = {0,0,16,16};
318 SDL_Rect desblocks = {0,0,16,16};
319
320 int i = 0;
321 int j = 0;
322
323 for (i=0;i<11;i++) {
324 for (j=0;j<16;j++) {
325 if (map[round][i][j] > 0) {
326 if (((map[round][i][j] == 11) || (map[round][i][j] == 13) || (map[round][i][j] == 15)) && (counter > 29))
327 map[round][i][j] ++;
328 if (((map[round][i][j] == 12) || (map[round][i][j] == 14) || (map[round][i][j] == 16)) && (counter < 30))
329 map[round][i][j] --;
330 if ((map[round][i][j] == 17) && (counter > 29))
331 map[round][i][j] += 2;
332 if ((map[round][i][j] == 19) && (counter < 30))
333 map[round][i][j] -= 2;
334 if ((map[round][i][j] == 25) && (griel->key == 1)) { /* Got key, so open the door */
335 Mix_PlayChannel(-1,key,0);
336 griel->key = 0;
337 map[round][i][j] = 26;
338 }
339 if (map[round][i][j] == 35) { /* Pow tile 2 */
340 if (*animationtime < 15)
341 *animationtime += 1;
342 else {
343 map[round][i][j] = 0;
344 *animationtime = 0;
345 }
346 }
347 if (map[round][i][j] == 36) { /* Pow tile 1 */
348 if (*animationtime < 15)
349 *animationtime += 1;
350 else {
351 map[round][i][j] = 35;
352 *animationtime = 0;
353 }
354 }
355 srcblocks.x = (map[round][i][j] - ((map[round][i][j] / 10) * 10) - 1) * 16;
356 srcblocks.y = (map[round][i][j] / 10) * 16;
357 if (map[round][i][j] == 10) {
358 srcblocks.x = 144;
359 srcblocks.y = 0;
360 }
361 desblocks.x = j * 16;
362 desblocks.y = (i * 16) + 32;
363 SDL_BlitSurface(blocks,&srcblocks,window,&desblocks);
364 }
365 }
366 }
367
368 }
369
check_obstacles(struct hero * griel,int round,int map[][11][16],Mix_Chunk * kill,uint * grieltouch)370 void check_obstacles (struct hero *griel, int round, int map[][11][16], Mix_Chunk *kill, uint *grieltouch) {
371
372 int deleteobject = 0;
373 int target[2] = {0,0};
374
375 if (griel->locked == 1) {
376 /* Set block to analyze */
377 switch (griel->direction) {
378 case 1: target[0] = map[round][griel->positiony-1][griel->positionx];
379 if (griel->positiony > 1)
380 target[1] = map[round][griel->positiony-2][griel->positionx];
381 else
382 target[1] = 0;
383 break;
384 case 2: target[0] = map[round][griel->positiony+1][griel->positionx];
385 if (griel->positiony < 9)
386 target[1] = map[round][griel->positiony+2][griel->positionx];
387 else
388 target[1] = 0;
389 break;
390 case 3: target[0] = map[round][griel->positiony][griel->positionx-1];
391 if (griel->positionx > 1)
392 target[1] = map[round][griel->positiony][griel->positionx-2];
393 else
394 target[1] = 0;
395 break;
396 case 4: target[0] = map[round][griel->positiony][griel->positionx+1];
397 if (griel->positionx < 14)
398 target[1] = map[round][griel->positiony][griel->positionx+2];
399 else
400 target[1] = 0;
401 break;
402 }
403 /* Check if its a wall */
404 if (((target[0] > 0) && (target[0] < 4)) || ((target[0] > 7) && (target[0] < 11)) || ((target[0] > 34) && (target[0] < 37)) || ((target[0] > 40) && (target[0] < 46)) || (target[0] == 25) || (target[0] == 99)) {
405 griel->locked = 0;
406 /* griel->direction = 2; */
407 }
408 /* Check for "arrow" tile */
409 if ((target[0] > 3) && (target[0] < 8)) {
410 if ((griel->direction == 1) && (target[0] == 5)) {
411 griel->locked = 0;
412 /* griel->direction = 2; */
413 }
414 if ((griel->direction == 2) && (target[0] == 4)) {
415 griel->locked = 0;
416 /* griel->direction = 2; */
417 }
418 if ((griel->direction == 3) && (target[0] == 7)) {
419 griel->locked = 0;
420 /* griel->direction = 2; */
421 }
422 if ((griel->direction == 4) && (target[0] == 6)) {
423 griel->locked = 0;
424 /* griel->direction = 2; */
425 }
426 if ((griel->direction + 3 == target[0]) && ((target[1] > 20) && (target[1] < 24)) && (griel->object > 0)) {
427 /* Block movement if a object is after the arrow and Griel has a object */
428 griel->locked = 0;
429 /* griel->direction = 2; */
430 }
431 }
432 /* Check if its a enemy */
433 if ((target[0] == 11) || (target[0] == 12)) { /* Slim */
434 if (griel->object == 1) {
435 Mix_PlayChannel(-1,kill,0);
436 deleteobject = 1;
437 griel->object = 0;
438 griel->score += 10;
439 }
440 else {
441 griel->locked = 0;
442 /* griel->direction = 2; */
443 }
444 }
445 if ((target[0] == 13) || (target[0] == 14)) { /* Ghost */
446 if (griel->object == 2) {
447 deleteobject = 1;
448 Mix_PlayChannel(-1,kill,0);
449 griel->object = 0;
450 griel->score += 60;
451 }
452 else {
453 griel->locked = 0;
454 /* griel->direction = 2; */
455 }
456 }
457 if ((target[0] == 15) || (target[0] == 16)) { /* Ogre */
458 if (griel->object == 3) {
459 deleteobject = 1;
460 Mix_PlayChannel(-1,kill,0);
461 griel->object = 0;
462 griel->score += 30;
463 }
464 else {
465 griel->locked = 0;
466 /* griel->direction = 2; */
467 }
468 }
469 /* Check if its a object */
470 if ((target[0] > 20) && (target[0] < 24)) {
471 if (griel->object == 0) {
472 griel->object = target[0] - 20;
473 /* Add points to score */
474 griel->score += 50;
475 deleteobject = 1;
476 }
477 else {
478 griel->locked = 0;
479 /* griel->direction = 2; */
480 }
481 }
482 if (target[0] == 24) { /* Key */
483 griel->key = 1;
484 griel->score += 300; /* Add to score */
485 deleteobject = 1;
486 }
487 if (target[0] == 46) { /* Grial */
488 griel->grial = 1;
489 griel->score += 1000; /* Add to score */
490 deleteobject = 1;
491 *grieltouch = 1;
492 }
493 /* Deleting objects */
494 if (deleteobject == 1) {
495 switch (griel->direction) {
496 case 1: if (map[round][griel->positiony-1][griel->positionx] < 21) {
497 map[round][griel->positiony-1][griel->positionx] = 36;
498 griel->locked = 0;
499 /* griel->direction = 2; */
500 }
501 else
502 map[round][griel->positiony-1][griel->positionx] = 0;
503 break;
504 case 2: if (map[round][griel->positiony+1][griel->positionx] < 21) {
505 map[round][griel->positiony+1][griel->positionx] = 36;
506 griel->locked = 0;
507 /* griel->direction = 2; */
508 }
509 else
510 map[round][griel->positiony+1][griel->positionx] = 0;
511 break;
512 case 3: if (map[round][griel->positiony][griel->positionx-1] < 21) {
513 map[round][griel->positiony][griel->positionx-1] = 36;
514 griel->locked = 0;
515 /* griel->direction = 2; */
516 }
517 else
518 map[round][griel->positiony][griel->positionx-1] = 0;
519 break;
520 case 4: if (map[round][griel->positiony][griel->positionx+1] < 21) {
521 map[round][griel->positiony][griel->positionx+1] = 36;
522 griel->locked = 0;
523 /* griel->direction = 2; */
524 }
525 else
526 map[round][griel->positiony][griel->positionx+1] = 0;
527 break;
528 }
529 }
530 }
531
532 /* Contine moving if you are in arrow */
533 if (((map[round][griel->positiony][griel->positionx] > 3) && (map[round][griel->positiony][griel->positionx] < 8)) && (griel->movement == 0)) {
534 griel->locked = 1;
535 griel->direction = map[round][griel->positiony][griel->positionx] - 3;
536 }
537
538 /* Check for a open door */
539 if ((map[round][griel->positiony][griel->positionx] == 26) && (griel->locked == 0)) {
540 griel->direction = 5;
541 griel->locked = 1;
542 }
543
544 }
545
controls(struct hero * griel,uint * fullscreench)546 void controls (struct hero *griel, uint *fullscreench) {
547
548 SDL_Event keystroke;
549
550 while (SDL_PollEvent(&keystroke)) {
551 if (keystroke.type == SDL_QUIT)
552 exit(0);
553 if (keystroke.type == SDL_KEYDOWN) {
554 if (keystroke.key.keysym.sym == SDLK_ESCAPE) {
555 if ((griel->locked == 0) && (griel->deathanimation == 0)) {
556 griel->locked = 1;
557 griel->direction = 6;
558 }
559 }
560 if (keystroke.key.keysym.sym == SDLK_UP) {
561 if ((griel->locked == 0) && (griel->positiony > 0)) {
562 griel->locked = 1;
563 griel->direction = 1;
564 }
565 }
566 if (keystroke.key.keysym.sym == SDLK_DOWN) {
567 if ((griel->locked == 0) && (griel->positiony < 10)) {
568 griel->locked = 1;
569 griel->direction = 2;
570 }
571 }
572 if (keystroke.key.keysym.sym == SDLK_LEFT) {
573 if ((griel->locked == 0) && (griel->positionx > 0)) {
574 griel->locked = 1;
575 griel->direction = 3;
576 }
577 }
578 if (keystroke.key.keysym.sym == SDLK_RIGHT) {
579 if ((griel->locked == 0) && (griel->positionx < 15)) {
580 griel->locked = 1;
581 griel->direction = 4;
582 }
583 }
584 if (keystroke.key.keysym.sym == SDLK_f)
585 *fullscreench = 1;
586 if (keystroke.key.keysym.sym == SDLK_q)
587 exit(0);
588 }
589 }
590
591 }