Debian Codegraph @

src/game.cc

raw symbols SCIP
1 // (_||_/
2 // ( )
3 // ( o 0 )
4 //-OOO°--(_)---°OOO---------------------------------------
5 // Copyright (C) 2006 By Dominique Roux-Serret
6 // .OOOo oOOO. [email protected]
7 //-( )------( )---------------------------------------
8 // ( ( ) / Copyright (C) 2023 By Johnny Jazeix
9 // (_) (_/ [email protected]
10
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 or version 3 of the License.
14
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19
20 // You should have received a copy of the GNU General Public License along
21 // with this program; if not, write to the Free Software Foundation, Inc.,
22 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 #include <SDL2/SDL_events.h> // for SDL_PollEvent, SDL_PRESSED, SDL_Event
25 #include <SDL2/SDL_keycode.h> // for SDLK_DOWN, SDLK_LEFT, SDLK_RIGHT, SDLK_UP
26 #include <SDL2/SDL_render.h> // for SDL_RenderPresent, SDL_RenderClear
27 #include <SDL2/SDL_timer.h> // for SDL_GetTicks
28 #include <SDL2/SDL_video.h> // for SDL_WINDOWEVENT_ENTER, SDL_WINDOWEVENT...
29 #include <cstdlib>
30
31 #include "game.h"
32 #include "gamepad.h"
33 #include "screen.h"
34 #include "menu.h"
35 #include "sprite.h"
36 #include "level.h"
37 #include "utils.h"
38 #include "audio.h"
39
40 /*** Variables globales ***/
41 /**************************/
42 extern SDL_Renderer *sdlRenderer;
43
44 extern Sprite *Sprites;
45 extern sNewPreference Pref;
46
47 extern int Horloge;
48 extern int HorlogeAvant;
49
50 extern Screen Ec;
51
52 extern Level level;
53
54 static int NumRail[] = { -1, -1, -1, 0, -1, 1, 2, 3, -1, 4, 5, 6, 7, 8, 9, 10 };
55
56 int MasqueK; // Masque pour les touches de déplacement
57
58 /*** Constructeur et Destructeur ***/
59 /***********************************/
60 Game::Game(Audio &sounds, Gamepad &gamepad) :
61 m_sounds(sounds), m_gamepad(gamepad), Lo(m_sounds)
62 {
63 Touche[0] = D_Top;
64 Touche[1] = D_Bottom;
65 Touche[2] = D_Left;
66 Touche[3] = D_Right;
67 }
68
69 /*** SDL Main ***/
70 /****************/
71 eMenu Game::SDLMain()
72 {
73 eMenu mRet;
74 int NumN = Pref.Niveau;
75
76 Help = true;
77 Load(NumN); // Charge le tableau
78 SDL_RenderPresent(sdlRenderer);
79 Ec.CleanSpriteAndScreen(fjeu);
80 Pause = true;
81
82 Horloge = SDL_GetTicks(); // Prend l'horloge
83 DureeJeu = 0;
84 Key = 0;
85
86 // Met le options de départ du joueur
87 Pref.NVie = N_VIES_DEP;
88 Pref.Score = 0;
89
90 // Prend les evenements
91 do {
92 bool doScreenshot = false;
93 SDL_RenderClear(sdlRenderer);
94 SDL_Event event;
95 while (SDL_PollEvent(&event)) {
96 m_gamepad.GetEvent(event); // Handle gamepad
97
98 switch (event.type) {
99 case SDL_WINDOWEVENT:
100 if (event.window.event == SDL_WINDOWEVENT_ENTER) {
101 }
102 else if (event.window.event == SDL_WINDOWEVENT_LEAVE) { // Si désactive l'ecran
103 Pause = true; // Met en Pause
104 }
105 break;
106 case SDL_MOUSEBUTTONDOWN:
107 if (event.button.state == SDL_PRESSED) {
108 // Add a Pause event if we click on the top right loco
109 int const Px = event.button.x;
110 int const Py = event.button.y;
111 if (Px >= 680 && Py <= 90) {
112 mRet = m_menu->SDLMain_InGame();
113 if (mRet == mGame) {
114 DrawLevel(NumN);
115 SDL_RenderPresent(sdlRenderer);
116 Pause = true;
117 }
118 else {
119 return mRet;
120 }
121 }
122 else if (Pause) {
123 Pause = false;
124 }
125 else {
126 TourneFleche();
127 }
128 }
129 break;
130 case SDL_KEYDOWN:
131 if (event.key.state == SDL_PRESSED) {
132 if (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_AC_BACK) {
133 mRet = m_menu->SDLMain_InGame();
134 if (mRet == mGame) {
135 DrawLevel(NumN);
136 SDL_RenderPresent(sdlRenderer);
137 Pause = true;
138 }
139 else {
140 return mRet;
141 }
142 }
143 else {
144 Key = event.key.keysym.sym;
145
146 if (event.key.repeat == 0) {
147 // The text says: "press any key to start"
148 // so we allow any key press (except Escape)
149 // to start the game
150 if (Pause) {
151 Pause = false;
152 break;
153 }
154 switch (Key) {
155 case SDLK_UP:
156 BufTouche(D_Top);
157 MasqueK |= 1;
158 break;
159 case SDLK_DOWN:
160 BufTouche(D_Bottom);
161 MasqueK |= 2;
162 break;
163 case SDLK_LEFT:
164 BufTouche(D_Left);
165 MasqueK |= 4;
166 break;
167 case SDLK_RIGHT:
168 BufTouche(D_Right);
169 MasqueK |= 8;
170 break;
171 case SDLK_F12: // Save screenshot
172 doScreenshot = true;
173 break;
174 case 'p':
175 case 'P':
176 if (Pause == false) {
177 Pause = true;
178 }
179 break;
180 case ' ':
181 if (Pause) {
182 Pause = false;
183 }
184 break;
185 case 'h':
186 case 'H':
187 Help = !Help;
188 break;
189 }
190 }
191 }
192 break;
193 case SDL_KEYUP:
194 switch (event.key.keysym.sym) {
195 case SDLK_UP:
196 MasqueK &= 14;
197 break;
198 case SDLK_DOWN:
199 MasqueK &= 13;
200 break;
201 case SDLK_LEFT:
202 MasqueK &= 11;
203 break;
204 case SDLK_RIGHT:
205 MasqueK &= 7;
206 break;
207 default:
208 break;
209 }
210 if (!MasqueK) {
211 Key = 0;
212 }
213 break;
214 case SDL_QUIT:
215 return mQuit;
216 break;
217 }
218 }
219 }
220
221 // Gère les Horloges et la pose
222 HorlogeAvant = Horloge;
223 Horloge = SDL_GetTicks();
224 Sleeping();
225 if (Pause == true || Lo.Mort > Horloge) {
226 HorlogeAvant = Horloge;
227 }
228 DureeJeu += Horloge - HorlogeAvant;
229
230 // Fait l'affichage
231 DrawLevel(NumN);
232 AfficheEcran();
233 SDL_RenderPresent(sdlRenderer);
234
235 if (doScreenshot) {
236 Utils::doScreenshot(sdlRenderer);
237 }
238
239 // Fait avancer la loco
240 if (Lo.Mort == -1 && Pause == false) {
241 Lo.Avance(Horloge - HorlogeAvant, DureeJeu, Touche, T);
242 }
243
244 // Test la fin d'une partie
245 if (Lo.Mort > -1 && Lo.Mort < Horloge) { // Si est Mort test si doit continuer ou quitter
246 if (Pref.NVie < 0) {
247 return mScoreEdit; // Si mort fini
248 }
249 if (Lo.Gagne) {
250 #ifndef DCHILDREN
251 if (Pref.HumanRightsQuiz && m_menu->SDLMain_HR() == mQuit) {
252 return mQuit;
253 }
254 #endif
255 NumN++;
256 if (level.N == NumN) {
257 Pref.Score += Pref.NVie * 100;
258 return mScoreEdit;
259 }
260 if (Pref.NiveauMax[Pref.Difficulte] < NumN) {
261 Pref.NiveauMax[Pref.Difficulte] = NumN;
262 }
263 }
264 m_sounds.NextMusic();
265 DureeJeu = 0;
266 Pause = true;
267 Key = MasqueK = 0;
268 Load(NumN);
269 }
270
271 } while (true);
272
273 return mQuit;
274 }
275
276 /*** Charge un tableau ***/
277 /*************************/
278 bool Game::Load(int NivN)
279 {
280 int i;
281
282 Pref.Niveau = NivN;
283
284 // Recopie le tableau
285 for (i = 0; i < LT * HT; i++) {
286 T[i] = (int)level.T[NivN].T[i];
287 }
288
289 // Laisse ou efface la vie suivant le niveau
290 switch (Pref.Difficulte) {
291 case Easy:
292 i = 5;
293 break;
294 case Normal:
295 i = 9;
296 break;
297 default:
298 i = 15;
299 }
300 if ((rand() % i) != 0) {
301 for (i = 0; i < LT * HT; i++) {
302 if (T[i] == C_Live) {
303 T[i] = C_Rail;
304 }
305 }
306 }
307
308 // Initialise la locomotive
309 Lo.Init(level.T[NivN].DepX + level.T[NivN].DepY * LT, level.T[NivN].DepDir);
310 BufTouche(level.T[NivN].DepDir);
311 MasqueK = 0;
312
313 // Met la vitesse suivant difficulté
314 switch (Pref.Difficulte) {
315 case Easy:
316 Pref.Vitesse = Pref.VitesseMoy = VITESSE_MIN;
317 break;
318 case Hard:
319 Pref.Vitesse = Pref.VitesseMoy = VITESSE_MAX;
320 break;
321 default:
322 Pref.Vitesse = Pref.VitesseMoy = VITESSE_MOY;
323 }
324
325 return DrawLevel(NivN);
326 }
327
328 /*** Dessine le fond de l'ecran de jeu ***/
329 /*****************************************/
330 bool Game::DrawLevel(int NivN)
331 {
332 int i, x, y, m, cx, cy;
333
334 // Fabrique le fond du jeu
335 Sprites[fond].Affiche(400, 300, 0, Sprites[fjeu].Image[0]);
336
337 // Affiche le circuit
338 for (i = 0; i < LT * HT; i++) {
339 if (T[i] >= C_Rail && T[i] < C_Fin) {
340 y = i / LT * D_Case + D_Case / 2;
341 x = i % LT * D_Case + D_Case / 2;
342
343 m = 0;
344 cx = i % LT;
345 cy = i / LT;
346 if (cy > 0 && T[i - LT] >= C_Rail && T[i - LT] < C_Fin) {
347 m += 8;
348 }
349 if (cy < HT - 1 && T[i + LT] >= C_Rail && T[i + LT] < C_Fin) {
350 m += 4;
351 }
352 if (cx > 0 && T[i - 1] >= C_Rail && T[i - 1] <= C_Fin) {
353 m += 2;
354 }
355 if (cx < LT - 1 && T[i + 1] >= C_Rail && T[i + 1] <= C_Fin) {
356 m += 1;
357 }
358
359 Sprites[rail].Affiche(x, y, NumRail[m], Sprites[fjeu].Image[0]);
360 }
361 }
362
363 // Affiche les décorations
364 #ifndef DCHILDREN
365 for (i = 0; i < level.T[NivN].NDeco; i++) {
366 Sprites[deco].Affiche(level.T[NivN].Deco[i].x, level.T[NivN].Deco[i].y, level.T[NivN].Deco[i].NumSpr,
367 Sprites[fjeu].Image[0]);
368 }
369 #endif
370
371 // Affiche les textes suivant la langue
372 AfficheText(740, 110, T_level, Sprites[fjeu].Image[0]);
373 AfficheText(740, 180, T_score, Sprites[fjeu].Image[0]);
374 AfficheText(740, 260, T_options, Sprites[fjeu].Image[0]);
375 AfficheText(740, 340, T_lives, Sprites[fjeu].Image[0]);
376
377 AfficheChiffre(740, 140, Pref.Niveau + 1, Sprites[fjeu].Image[0]);
378
379 return true;
380 }
381
382 /*** Fait tourner la fleche d'une simple touche ***/
383 /**************************************************/
384 void Game::TourneFleche()
385 {
386 int To = Touche[0];
387 bool Cherche = false;
388 int const x = Lo.PInter % LT;
389 int const y = Lo.PInter / LT;
390
391 do {
392 // Fait toucher la direction dans le sens des aiguilles d'une montre
393 // et test si la nouvelle direction est possible
394 switch (To) {
395 case D_Top:
396 To = D_Right;
397 if (Lo.PEntree != D_Right && x + 1 < LT && T[Lo.PInter + 1] != 0) {
398 Cherche = true;
399 }
400 break;
401 case D_Right:
402 To = D_Bottom;
403 if (Lo.PEntree != D_Bottom && y + 1 < HT && T[Lo.PInter + LT] != 0) {
404 Cherche = true;
405 }
406 break;
407 case D_Bottom:
408 To = D_Left;
409 if (Lo.PEntree != D_Left && x > 0 && T[Lo.PInter - 1] != 0) {
410 Cherche = true;
411 }
412 break;
413 case D_Left:
414 To = D_Top;
415 if (Lo.PEntree != D_Top && y > 0 && T[Lo.PInter - LT] != 0) {
416 Cherche = true;
417 }
418 break;
419 }
420 } while (Cherche == false);
421
422 BufTouche(To); // Mémorise la nouvelle touche par defaut.
423 }
424
425 /*** Mémorise une touche dans le buffet des touches ***/
426 /******************************************************/
427 void Game::BufTouche(int Tou)
428 {
429 int n = 0;
430
431 // Favorise la touche
432 while (Touche[n] != Tou) {
433 n++; // Prend position de la touche
434 }
435
436 if (n) { // Si changement doit faire un décalage
437 while (n) {
438 Touche[n] = Touche[n - 1];
439 n--;
440 }
441 Touche[0] = Tou; // Mémorise la touche
442 }
443
444 // Cherche son oposé
445 switch (Tou) {
446 case D_Top:
447 Tou = D_Bottom;
448 break;
449 case D_Bottom:
450 Tou = D_Top;
451 break;
452 case D_Left:
453 Tou = D_Right;
454 break;
455 case D_Right:
456 Tou = D_Left;
457 break;
458 }
459
460 // Défavorise son oposé.
461 n = 3;
462 while (Touche[n] != Tou) {
463 n--; // Prend position de la touche
464 }
465
466 if (n < 3) { // Si changement doit faire un décalage
467 while (n < 3) {
468 Touche[n] = Touche[n + 1];
469 n++;
470 }
471 Touche[3] = Tou; // Mémorise la touche
472 }
473 }
474
475 /*** Test les directions possibles pour les fleches ***/
476 /******************************************************/
477 int Game::TestFleche(int Haut, int Bas, int Gauche, int Droite)
478 {
479 int i;
480 int const x = Lo.PInter % LT;
481 int const y = Lo.PInter / LT;
482
483 for (i = 0; i < 4; i++) {
484 switch (Touche[i]) {
485 case D_Top:
486 if (y > 0 && Haut != -1 && T[Lo.PInter - LT] != 0) {
487 return Haut;
488 }
489 break;
490 case D_Bottom:
491 if (y + 1 < HT && Bas != -1 && T[Lo.PInter + LT] != 0) {
492 return Bas;
493 }
494 break;
495 case D_Left:
496 if (x > 0 && Gauche != -1 && T[Lo.PInter - 1] != 0) {
497 return Gauche;
498 }
499 break;
500 case D_Right:
501 if (x + 1 < LT && Droite != -1 && T[Lo.PInter + 1] != 0) {
502 return Droite;
503 }
504 break;
505 }
506 }
507 return 0;
508 }
509
510 /*** Affiche un ecran du jeu ***/
511 /*******************************/
512 void Game::AfficheEcran()
513 {
514 int i;
515 int ndir = 0;
516
517 // Prépare pour nouvelle Affichage
518 Ec.ClearSprite(fjeu);
519
520 // Fait nouvelle Affichage
521 Lo.Display(Ec); // Affiche la loco
522
523 if (Lo.PInter != -1 && Help) { // Affiche la fleche sur la futur intersection
524 switch (Lo.PEntree) {
525 case D_Left:
526 ndir = TestFleche(0, 2, -1, 1);
527 break;
528 case D_Right:
529 ndir = TestFleche(8, 6, 7, -1);
530 break;
531 case D_Top:
532 ndir = TestFleche(-1, 4, 5, 3);
533 break;
534 case D_Bottom:
535 ndir = TestFleche(10, -1, 9, 11);
536 break;
537 }
538
539 Ec.PrintSprite(dir, ndir, (Lo.PInter % LT) * D_Case + D_Case / 2, (Lo.PInter / LT) * D_Case + D_Case / 2);
540 }
541
542 // Affiche les options
543 for (i = 0; i < LT * HT; i++) {
544 switch (T[i]) {
545 case C_Wagon: // Si un loco
546 Ec.PrintSprite(wagon, (DureeJeu * 40 / 1000 + i * 7) % 50, i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2);
547 break;
548 case C_Allonge: // Si plus long
549 Ec.PrintSprite(pluslong, (DureeJeu * 40 / 1000 + i * 7) % 50, i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2);
550 break;
551 case C_Reduit: // Si plus court
552 Ec.PrintSprite(pluscourt, (DureeJeu * 40 / 1000 + i * 7) % 50, i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2);
553 break;
554 case C_Speed: // Si plus vite
555 Ec.PrintSprite(vitesse, (DureeJeu * 40 / 1000 + i * 7) % 50, i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2);
556 break;
557 case C_Live: // Si une vie
558 Ec.PrintSprite(vie, (DureeJeu * 40 / 1000 + i * 7) % 50, i % LT * D_Case + D_Case / 2, i / LT * D_Case + D_Case / 2);
559 break;
560 }
561 }
562
563 // Si en pose demande une touche
564 if (Pause) {
565 Ec.PrintText(T_press_any_key, LT * D_Case / 2, 300);
566 }
567
568 // Affiche tableau de bord
569 Ec.PrintOptions(Pref.NVie, Pref.Score);
570 if (Pref.EcartWagon < ECARTWAGON_MOY) {
571 Ec.PrintSprite(pluscourt, (DureeJeu * 40 / 1000) % 50, 715, 295);
572 }
573 if (Pref.EcartWagon > ECARTWAGON_MOY) {
574 Ec.PrintSprite(pluslong, (DureeJeu * 40 / 1000) % 50, 715, 295);
575 }
576 if (Pref.VitesseMoy > Pref.Vitesse) {
577 Ec.PrintSprite(vitesse, (DureeJeu * 40 / 1000 + 7) % 50, 765, 295);
578 }
579 }