1 // (_||_/
2 // ( ) Classe Menu
3 // ( o 0 )
4 //-OOO�--(_)---�OOO---------------------------------------
5 // Copyright (C) 2006 By Dominique Roux-Serret
6 // .OOOo oOOO. roux-serret@ifrance.com
7 //-( )------( )---------------------------------------
8 // ( ( ) / Le 23/03/2006
9 // (_) (_/
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 <iostream>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "preference.h"
29 #include "menu.h"
30 #include "sprite.h"
31 #include "ecran.h"
32 #include "mouse.h"
33 #include "audio.h"
34
35 #define PY 180
36
37 /*** Variables globales ***/
38 /**************************/
39 extern int Horloge;
40 extern int HorlogeAvant;
41 extern SDL_Surface *sdlVideo;
42 extern Sprite *Sprites;
43 extern Mouse Sourie;
44 extern Ecran Ec[2];
45 extern sPreference Pref;
46 extern Audio Sons;
47
48 static char Points[]=". . . . . . . . . . . . . .";
49 static struct mPy Menu_Py[20];
50
51 /*** Fait une attente pour 50fps ***/
52 /***********************************/
Sleeping()53 void Sleeping()
54 {
55 int delay;
56
57 if( (Horloge-HorlogeAvant) < (1000/60) ) {
58 delay=1000/60-(Horloge-HorlogeAvant);
59 SDL_Delay(delay);
60 Horloge=SDL_GetTicks();
61 }
62 }
63
64 /*** Constructeur et Destructeur ***/
65 /***********************************/
Menu()66 Menu::Menu() : PyE(0)
67 { }
68
~Menu()69 Menu::~Menu()
70 { }
71
72 /*** Ajoute une entr�e dans le tableau des boutons ***/
73 /*****************************************************/
AddBouton(int Num,e_Sprite NumSp,int X,int Y)74 void AddBouton(int Num,e_Sprite NumSp,int X,int Y)
75 {
76 int NumS=(int)NumSp;
77
78 Menu_Py[Num].DepX=X-Sprites[NumS].Dim[0].L/2;
79 Menu_Py[Num].DepY=Y-Sprites[NumS].Dim[0].H/2;
80 Menu_Py[Num].FinX=X+Sprites[NumS].Dim[0].L/2;
81 Menu_Py[Num].FinY=Y+Sprites[NumS].Dim[0].H/2;
82 Menu_Py[Num].Py=Num;
83 Menu_Py[Num].Valide=true;
84 }
85
86 /*** Change le vid�o ***/
87 /***********************/
ChangeVideo(void)88 void ChangeVideo(void)
89 {
90 SDL_VideoInfo *sdlVideoInfo;
91
92 // Teste la resolution video
93 sdlVideoInfo=(SDL_VideoInfo*)SDL_GetVideoInfo();
94 if(sdlVideoInfo->vfmt->BitsPerPixel==8) {
95 std::cerr <<"Impossible d'utiliser 8bits pour la vid�o !"<<std::endl;
96 exit(-1);
97 }
98
99 // Demande la resolution Video
100 #ifndef LINUX
101 int vOption;
102 if(Pref.FullScreen) vOption=SDL_SWSURFACE; // Bug acc� aux bits pour les cordes
103 else vOption=SDL_SWSURFACE|SDL_DOUBLEBUF;
104 #else
105 #ifndef __AMIGAOS4__
106 int vOption=SDL_SWSURFACE|SDL_DOUBLEBUF;
107 #else
108 int vOption=SDL_SWSURFACE;
109 #endif
110 #endif
111
112 if(Pref.FullScreen) vOption|=SDL_FULLSCREEN;
113 sdlVideo=SDL_SetVideoMode(800,600,sdlVideoInfo->vfmt->BitsPerPixel,vOption);
114 if(sdlVideo==NULL) {
115 std::cerr <<"Impossible de passer dans le mode vid�o 800x600 !"<<std::endl;
116 exit(-1);
117 }
118
119 SDL_ShowCursor(0); // Cache le curseur
120 }
121
122 /*** SDL Main Menu principale ***/
123 /********************************/
SDLMain(void)124 eMenu Menu::SDLMain(void)
125 {
126 int i;
127 char MCode[5]={0,0,0,0,0};
128 char key;
129
130 // Initialisations Divers
131 Sourie.Init(Menu_Py); // Initialise la sourie
132 PyE=0;
133
134 // Prend l'image du fond et fait l'affichage
135 Sprites[fond_menu].Affiche(400,300,0,Sprites[fmenu].Image[0]);
136 Sprites[menu].Affiche(400,340,0,Sprites[fmenu].Image[0]);
137 Sprites[titre].Affiche(400,65,0,Sprites[fmenu].Image[0]);
138 Sprites[copyright].Affiche(400,587,0,Sprites[fmenu].Image[0]);
139
140 AfficheText(400,229,T_play,Sprites[fmenu].Image[0]);
141 AddBouton(0,T_play,400,229);
142 AfficheText(400,306,T_scores,Sprites[fmenu].Image[0]);
143 AddBouton(1,T_scores,400,306);
144 AfficheText(400,384,T_moptions,Sprites[fmenu].Image[0]);
145 AddBouton(2,T_moptions,400,384);
146 AfficheText(400,461,T_quit,Sprites[fmenu].Image[0]);
147 AddBouton(3,T_quit,400,461);
148 Menu_Py[4].DepX=-1;
149
150 // Efface le fond
151 NumEc=0;
152 Ec[1].Cls(fmenu);
153 SDL_Flip(sdlVideo);
154 Ec[0].Cls(fmenu);
155
156 // Prend les evenements
157 do {
158 SDL_Event event;
159 while(SDL_PollEvent(&event)) {
160 Sourie.GetEvent(event,PyE); // Prend les evenements de la sourie
161 switch(event.type) {
162 case SDL_ACTIVEEVENT:
163 if(event.active.gain==1) {
164 NumEc=0;
165 Ec[1].Cls(fmenu);
166 SDL_Flip(sdlVideo);
167 Ec[0].Cls(fmenu);
168 }
169 break;
170 case SDL_KEYDOWN:
171 if(event.key.state==SDL_PRESSED) {
172 Sons.Play(sClic);
173 switch(event.key.keysym.sym) {
174 case SDLK_ESCAPE:
175 return mQuit;
176 case SDLK_UP:
177 PyE--;
178 if(PyE<0) PyE=3;
179 break;
180 case SDLK_DOWN:
181 PyE++;
182 if(PyE>3) PyE=0;
183 break;
184 case SDLK_F12: // Sauve un screenshot
185 SDL_SaveBMP(sdlVideo,"screenshot.bmp");
186 break;
187 case ' ':
188 case SDLK_RETURN:
189 case SDLK_KP_ENTER:
190 switch(PyE) {
191 case 0:
192 return mMenuNiveau;
193 case 1:
194 return mScore;
195 case 2:
196 return mOption;
197 case 3:
198 return mQuit;
199 }
200 break;
201 default:
202 key=event.key.keysym.unicode&0x7F; // Prend le caracataire correspondant � la touche
203 if(CharExiste(key)==true) { // Si la caractaire existe bien
204 for(i=2;i>=0;i--) MCode[i+1]=MCode[i]; // d�cale le code
205 MCode[0]=key;
206 if(strcmp(MCode,"tide")==0 || strcmp(MCode,"TIDE")==0) return mEdit; // Si editeur de niveau
207 }
208 }
209 }
210 break;
211 case SDL_QUIT:
212 return mQuit;
213 break;
214 }
215 }
216
217 // G�re les variables
218 HorlogeAvant=Horloge;
219 Horloge=SDL_GetTicks();
220 Sleeping();
221
222 // G�re l'Affichage
223 Ec[NumEc].Efface(fmenu);
224 Affiche_Main();
225 Sourie.Affiche(NumEc);
226
227 // Echange les buffets video
228 SDL_Flip(sdlVideo);
229 NumEc=(NumEc+1)&1;
230
231 } while(true);
232
233 return mQuit;
234 }
235
236 /*** SDL Main Menu Choix de la langue ***/
237 /****************************************/
SDLMain_Langue(void)238 eMenu Menu::SDLMain_Langue(void)
239 {
240 int NCol=1;
241 int NL;
242 int Ecart;
243 int i;
244 int x,y;
245 int OldLangue=Pref.Langue;
246
247 // Initialisations Divers
248 Sourie.Init(Menu_Py); // Initialise la sourie
249 PyE=Pref.Langue;
250 if(PyE==-1) PyE=1;
251
252 // Prend l'image du fond et fait l'affichage
253 Sprites[fond_menu].Affiche(400,300,0,Sprites[fmenu].Image[0]);
254
255 // Affiche les langues possibles
256 NCol=3;
257 if(Pref.NLangues%NCol==0) NL=Pref.NLangues/NCol;
258 else NL=Pref.NLangues/NCol+1;
259 Ecart=600/(NL+1);
260
261 for(i=0;i<Pref.NLangues;i++) {
262 x=(i/NL)*(800/3)+(800/6);
263 y=(i%NL)*Ecart+Ecart;
264
265 Sprites[T_Langue+i].Affiche(x,y,0,Sprites[fmenu].Image[0]);
266 AddBouton(i,(e_Sprite)(T_Langue+i),x,y);
267 }
268
269 Menu_Py[Pref.NLangues].DepX=-1;
270
271 // Efface le fond
272 NumEc=0;
273 Ec[1].Cls(fmenu);
274 SDL_Flip(sdlVideo);
275 Ec[0].Cls(fmenu);
276
277 // Prend les evenements
278 do {
279 SDL_Event event;
280 while(SDL_PollEvent(&event)) {
281 Sourie.GetEvent(event,PyE); // Prend les evenements de la sourie
282 switch(event.type) {
283 case SDL_ACTIVEEVENT:
284 if(event.active.gain==1) {
285 NumEc=0;
286 Ec[1].Cls(fmenu);
287 SDL_Flip(sdlVideo);
288 Ec[0].Cls(fmenu);
289 }
290 break;
291 case SDL_KEYDOWN:
292 if(event.key.state==SDL_PRESSED) {
293 Sons.Play(sClic);
294 switch(event.key.keysym.sym) {
295 case SDLK_ESCAPE:
296 if(Pref.Langue==-1) Pref.Langue=PyE;
297 return mMenu;
298 case SDLK_UP:
299 PyE--;
300 if(PyE<0) PyE=Pref.NLangues-1;
301 break;
302 case SDLK_DOWN:
303 PyE++;
304 if(PyE>=Pref.NLangues) PyE=0;
305 break;
306 case SDLK_LEFT:
307 if(PyE-NL>=0) PyE-=NL;
308 break;
309 case SDLK_RIGHT:
310 if(PyE+NL<Pref.NLangues) PyE+=NL;
311 break;
312 case SDLK_F12: // Sauve un screenshot
313 SDL_SaveBMP(sdlVideo,"screenshot.bmp");
314 break;
315 case ' ':
316 case SDLK_RETURN:
317 case SDLK_KP_ENTER:
318 Pref.Langue=PyE;
319 if(Pref.Langue!=OldLangue) LoadLangue();
320 return mMenu;
321 }
322 }
323 break;
324 case SDL_QUIT:
325 return mQuit;
326 break;
327 }
328 }
329
330 // G�re les variables
331 HorlogeAvant=Horloge;
332 Horloge=SDL_GetTicks();
333 Sleeping();
334
335 // G�re l'Affichage
336 Ec[NumEc].Efface(fmenu);
337
338 Affiche_Main_Centre();
339
340 Sourie.Affiche(NumEc);
341
342 // Echange les buffets video
343 SDL_Flip(sdlVideo);
344 NumEc=(NumEc+1)&1;
345
346 } while(true);
347
348 return mQuit;
349 }
350
351 /*** SDL Main Menu Choix des Options ***/
352 /***************************************/
InitMain_Options(void)353 void Menu::InitMain_Options(void)
354 {
355 // Initialisations Divers
356 Sourie.Init(Menu_Py); // Initialise la sourie
357 PyE=4;
358
359 // Prend l'image du fond et fait l'affichage
360 Sprites[fond_menu].Affiche(400,300,0,Sprites[fmenu].Image[0]);
361 Sprites[gmenu].Affiche(400,300,0,Sprites[fmenu].Image[0]);
362 Sprites[keys].Affiche(610,455,0,Sprites[fmenu].Image[0]);
363
364 AddBouton(0,bruitage,140,110);
365 AddBouton(1,music,160,200);
366
367 AddBouton(2,fscreen,190,300);
368 Menu_Py[2].DepX-=40;
369 Menu_Py[2].FinX=625+40+Sprites[fscreen].Dim[0].L/2;
370 Sprites[fscreen].Affiche(185,300,0,Sprites[fmenu].Image[0]);
371 Sprites[window].Affiche(625,300,0,Sprites[fmenu].Image[0]);
372
373 AddBouton(3,monde,180,400);
374
375 // Centre � gauche le text de menu
376 CentreM=120+Sprites[T_menu].Dim[0].L/2;
377 AfficheText(CentreM,490,T_menu,Sprites[fmenu].Image[0]);
378 AddBouton(4,T_menu,CentreM,490);
379
380 // Boutons des bruitages
381 Sprites[fleches].Affiche(250,110,1,Sprites[fmenu].Image[0]);
382 Sprites[fleches].Affiche(700,110,4,Sprites[fmenu].Image[0]);
383 Menu_Py[5].DepX=230;
384 Menu_Py[5].DepY=70;
385 Menu_Py[5].FinX=475;
386 Menu_Py[5].FinY=145;
387 Menu_Py[5].Py=5;
388 Menu_Py[5].Valide=true;
389
390 Menu_Py[6].DepX=476;
391 Menu_Py[6].DepY=70;
392 Menu_Py[6].FinX=720;
393 Menu_Py[6].FinY=145;
394 Menu_Py[6].Py=6;
395 Menu_Py[6].Valide=true;
396
397 // Boutons de musics
398 Sprites[fleches].Affiche(250,200,1,Sprites[fmenu].Image[0]);
399 Sprites[fleches].Affiche(700,200,4,Sprites[fmenu].Image[0]);
400 Menu_Py[7].DepX=230;
401 Menu_Py[7].DepY=155;
402 Menu_Py[7].FinX=475;
403 Menu_Py[7].FinY=245;
404 Menu_Py[7].Py=7;
405 Menu_Py[7].Valide=true;
406
407 Menu_Py[8].DepX=476;
408 Menu_Py[8].DepY=155;
409 Menu_Py[8].FinX=720;
410 Menu_Py[8].FinY=245;
411 Menu_Py[8].Py=8;
412 Menu_Py[8].Valide=true;
413
414 Menu_Py[9].DepX=-1;
415
416 // Efface le fond
417 NumEc=0;
418 Ec[1].Cls(fmenu);
419 SDL_Flip(sdlVideo);
420 Ec[0].Cls(fmenu);
421 }
422
423 /*** Gestion du menu Options ***/
424 /*******************************/
SDLMain_Options(void)425 eMenu Menu::SDLMain_Options(void)
426 {
427 int i,N;
428 int NumSp;
429
430 InitMain_Options(); // Pr�pare le menu
431
432 // Prend les evenements
433 do {
434 SDL_Event event;
435 while(SDL_PollEvent(&event)) {
436 Sourie.GetEvent(event,PyE); // Prend les evenements de la sourie
437 switch(event.type) {
438 case SDL_ACTIVEEVENT:
439 if(event.active.gain==1) {
440 NumEc=0;
441 Ec[1].Cls(fmenu);
442 SDL_Flip(sdlVideo);
443 Ec[0].Cls(fmenu);
444 }
445 break;
446 case SDL_KEYDOWN:
447 if(event.key.state==SDL_PRESSED) {
448 Sons.Play(sClic);
449 switch(event.key.keysym.sym) {
450 case SDLK_ESCAPE:
451 return mMenu;
452 case SDLK_LEFT:
453 switch(PyE) {
454 case 2:
455 if(Pref.FullScreen==false) {
456 Pref.FullScreen=true;
457 ChangeVideo();
458 InitMain_Options();
459 PyE=2;
460 }
461 break;
462 case 0:
463 case 5: // Diminue volume sons
464 case 6:
465 Pref.Volume-=SDL_MIX_MAXVOLUME/10.0;
466 if(Pref.Volume<0) Pref.Volume=0;
467 Sons.DoVolume();
468 Sons.Play(sLive);
469 break;
470 case 1:
471 case 7: // Diminue volume music
472 case 8:
473 Pref.VolumeM-=SDL_MIX_MAXVOLUME/10.0;
474 if(Pref.VolumeM<0) Pref.VolumeM=0;
475 Sons.DoVolume();
476 break;
477 }
478 break;
479 case SDLK_RIGHT:
480 switch(PyE) {
481 case 2:
482 if(Pref.FullScreen==true) {
483 Pref.FullScreen=false;
484 ChangeVideo();
485 InitMain_Options();
486 PyE=2;
487 }
488 break;
489 case 0:
490 case 5:
491 case 6:
492 Pref.Volume+=SDL_MIX_MAXVOLUME/10.0;
493 if(Pref.Volume>SDL_MIX_MAXVOLUME) Pref.Volume=SDL_MIX_MAXVOLUME;
494 Sons.DoVolume();
495 Sons.Play(sLive);
496 break;
497 case 1:
498 case 7:
499 case 8:
500 Pref.VolumeM+=SDL_MIX_MAXVOLUME/10.0;
501 if(Pref.VolumeM>SDL_MIX_MAXVOLUME) Pref.VolumeM=SDL_MIX_MAXVOLUME;
502 Sons.DoVolume();
503 break;
504 }
505 break;
506 case SDLK_UP:
507 PyE--;
508 if(PyE<0) PyE=4;
509 break;
510 case SDLK_DOWN:
511 PyE++;
512 if(PyE>=5) PyE=0;
513 break;
514 case SDLK_F12: // Sauve un screenshot
515 SDL_SaveBMP(sdlVideo,"screenshot.bmp");
516 break;
517 case ' ':
518 case SDLK_RETURN:
519 case SDLK_KP_ENTER:
520 switch(PyE) {
521 case 0:
522 case 1:
523 break;
524 case 2: // Type d'affichage
525 Pref.FullScreen=(Pref.FullScreen+1)&1;
526 ChangeVideo();
527 InitMain_Options();
528 PyE=2;
529 break;
530 case 3: // Choix de la langue
531 SDLMain_Langue();
532 InitMain_Options();
533 PyE=3;
534 break;
535 case 5: // Diminue volume sons
536 Pref.Volume-=SDL_MIX_MAXVOLUME/10.0;
537 if(Pref.Volume<0) Pref.Volume=0;
538 Sons.DoVolume();
539 Sons.Play(sLive);
540 break;
541 case 6:
542 Pref.Volume+=SDL_MIX_MAXVOLUME/10.0;
543 if(Pref.Volume>SDL_MIX_MAXVOLUME) Pref.Volume=SDL_MIX_MAXVOLUME;
544 Sons.DoVolume();
545 Sons.Play(sLive);
546 break;
547 case 7: // Diminue volume music
548 Pref.VolumeM-=SDL_MIX_MAXVOLUME/10.0;
549 if(Pref.VolumeM<0) Pref.VolumeM=0;
550 Sons.DoVolume();
551 break;
552 case 8:
553 Pref.VolumeM+=SDL_MIX_MAXVOLUME/10.0;
554 if(Pref.VolumeM>SDL_MIX_MAXVOLUME) Pref.VolumeM=SDL_MIX_MAXVOLUME;
555 Sons.DoVolume();
556 break;
557 default:
558 return mMenu;
559 }
560 }
561 }
562 break;
563 case SDL_QUIT:
564 return mQuit;
565 break;
566 }
567 }
568
569 // G�re les variables
570 HorlogeAvant=Horloge;
571 Horloge=SDL_GetTicks();
572 Sleeping();
573
574 // G�re l'Affichage
575 Ec[NumEc].Efface(fmenu);
576
577 if(Pref.FullScreen) {
578 Ec[NumEc].Affiche(fleches,1,350,300);
579 Ec[NumEc].Affiche(fleches,3,450,300);
580 }
581 else {
582 Ec[NumEc].Affiche(fleches,0,350,300);
583 Ec[NumEc].Affiche(fleches,4,450,300);
584 }
585
586 NumSp=(Horloge/30)%25;
587 Ec[NumEc].Affiche(bruitage,NumSp,150,110);
588 NumSp=(Horloge/30)%25;
589 Ec[NumEc].Affiche(music,NumSp,150,200);
590 NumSp=(Horloge/50)%50;
591 Ec[NumEc].Affiche(monde,NumSp,180,400);
592
593 N=(int)(Pref.Volume*10+1)/SDL_MIX_MAXVOLUME;
594 NumSp=(Horloge/50)%40+120;
595 for(i=0;i<N;i++) {
596 if(i==N-1) Ec[NumEc].Affiche(locomotive,NumSp,(690-300)/10*i+300,110);
597 else Ec[NumEc].Affiche(buches,NumSp,(690-300)/10*i+300,110);
598 }
599
600 N=(int)(Pref.VolumeM*10+1)/SDL_MIX_MAXVOLUME;
601 for(i=0;i<N;i++) {
602 if(i==N-1) Ec[NumEc].Affiche(locomotive,NumSp,(690-300)/10*i+300,200);
603 else Ec[NumEc].Affiche(buches,NumSp,(690-300)/10*i+300,200);
604 }
605
606 switch(PyE) {
607 case 3:
608 Affiche_Main(180);
609 break;
610 case 4:
611 Affiche_Main(CentreM);
612 break;
613 case 5:
614 PyE=0;
615 Affiche_Main();
616 PyE=5;
617 break;
618 case 6:
619 PyE=0;
620 Affiche_Main();
621 PyE=6;
622 break;
623 case 7:
624 PyE=1;
625 Affiche_Main();
626 PyE=7;
627 break;
628 case 8:
629 PyE=1;
630 Affiche_Main();
631 PyE=8;
632 break;
633 default:
634 Affiche_Main();
635 }
636
637 Sourie.Affiche(NumEc);
638
639 // Echange les buffets video
640 SDL_Flip(sdlVideo);
641 NumEc=(NumEc+1)&1;
642
643 } while(true);
644
645 return mQuit;
646 }
647
648 /*** SDL Main Menu Choix de la difficult� ***/
649 /********************************************/
SDLMain_Speed(void)650 eMenu Menu::SDLMain_Speed(void)
651 {
652 // Initialisations Divers
653 Sourie.Init(Menu_Py); // Initialise la sourie
654 PyE=1;
655
656 // Prend l'image du fond et fait l'affichage
657 Sprites[fond_menu].Affiche(400,300,0,Sprites[fmenu].Image[0]);
658 Sprites[menu].Affiche(400,340,0,Sprites[fmenu].Image[0]);
659 Sprites[titre].Affiche(400,65,0,Sprites[fmenu].Image[0]);
660
661 AfficheText(400,225,T_easy,Sprites[fmenu].Image[0]);
662 AddBouton(0,T_easy,400,225);
663 AfficheText(400,340,T_normal,Sprites[fmenu].Image[0]);
664 AddBouton(1,T_normal,400,340);
665 AfficheText(400,455,T_hard,Sprites[fmenu].Image[0]);
666 AddBouton(2,T_hard,400,455);
667 Menu_Py[3].DepX=-1;
668
669 // Efface le fond
670 NumEc=0;
671 Ec[1].Cls(fmenu);
672 SDL_Flip(sdlVideo);
673 Ec[0].Cls(fmenu);
674
675 // Prend les evenements
676 do {
677 SDL_Event event;
678 while(SDL_PollEvent(&event)) {
679 Sourie.GetEvent(event,PyE); // Prend les evenements de la sourie
680 switch(event.type) {
681 case SDL_ACTIVEEVENT:
682 if(event.active.gain==1) {
683 NumEc=0;
684 Ec[1].Cls(fmenu);
685 SDL_Flip(sdlVideo);
686 Ec[0].Cls(fmenu);
687 }
688 break;
689 case SDL_KEYDOWN:
690 if(event.key.state==SDL_PRESSED) {
691 Sons.Play(sClic);
692 switch(event.key.keysym.sym) {
693 case SDLK_ESCAPE:
694 return mMenu;
695 case SDLK_UP:
696 PyE--;
697 if(PyE<0) PyE=2;
698 break;
699 case SDLK_DOWN:
700 PyE++;
701 if(PyE>2) PyE=0;
702 break;
703 case SDLK_F12: // Sauve un screenshot
704 SDL_SaveBMP(sdlVideo,"screenshot.bmp");
705 break;
706 case ' ':
707 case SDLK_RETURN:
708 case SDLK_KP_ENTER:
709 switch(PyE) {
710 case 0:
711 Pref.Difficulte=Easy;
712 return mJeux;
713 case 1:
714 Pref.Difficulte=Normal;
715 return mJeux;
716 case 2:
717 Pref.Difficulte=Hard;
718 return mJeux;
719 }
720 break;
721 }
722 }
723 break;
724 case SDL_QUIT:
725 return mQuit;
726 break;
727 }
728 }
729
730 // G�re les variables
731 HorlogeAvant=Horloge;
732 Horloge=SDL_GetTicks();
733 Sleeping();
734
735 // G�re l'Affichage
736 Ec[NumEc].Efface(fmenu);
737 Affiche_Main();
738 Sourie.Affiche(NumEc);
739
740 // Echange les buffets video
741 SDL_Flip(sdlVideo);
742 NumEc=(NumEc+1)&1;
743
744 } while(true);
745
746 return mQuit;
747 }
748
749 /*** SDL Main Menu Choix du niveau ***/
750 /*************************************/
SDLMain_Niveau(void)751 eMenu Menu::SDLMain_Niveau(void)
752 {
753 // Initialisations Divers
754 Sourie.Init(Menu_Py); // Initialise la sourie
755 PyE=0;
756 Niv=Pref.NiveauMax;
757 Pref.Niveau=0;
758
759 if(Niv==0) return mMenuSpeed; // Inutile si la premi�re fois
760
761 // Prend l'image du fond et fait l'affichage
762 Sprites[fond_menu].Affiche(400,300,0,Sprites[fmenu].Image[0]);
763 Sprites[menu].Affiche(400,340,0,Sprites[fmenu].Image[0]);
764 Sprites[titre].Affiche(400,65,0,Sprites[fmenu].Image[0]);
765
766 AfficheText(400,225,T_new_game,Sprites[fmenu].Image[0]);
767 AddBouton(0,T_new_game,400,225);
768 AfficheText(400,320,T_old_level,Sprites[fmenu].Image[0]);
769 AddBouton(1,T_old_level,400,320);
770 AfficheText(400,455,T_menu,Sprites[fmenu].Image[0]);
771 AddBouton(2,T_menu,400,455);
772
773 AddBouton(3,fleches,330,380);
774 AddBouton(4,fleches,470,380);
775
776 Menu_Py[5].DepX=-1;
777
778 // Efface le fond
779 NumEc=0;
780 Ec[1].Cls(fmenu);
781 SDL_Flip(sdlVideo);
782 Ec[0].Cls(fmenu);
783
784 // Prend les evenements
785 do {
786 SDL_Event event;
787 while(SDL_PollEvent(&event)) {
788 Sourie.GetEvent(event,PyE); // Prend les evenements de la sourie
789 switch(event.type) {
790 case SDL_ACTIVEEVENT:
791 if(event.active.gain==1) {
792 NumEc=0;
793 Ec[1].Cls(fmenu);
794 SDL_Flip(sdlVideo);
795 Ec[0].Cls(fmenu);
796 }
797 break;
798 case SDL_KEYDOWN:
799 if(event.key.state==SDL_PRESSED) {
800 Sons.Play(sClic);
801 switch(event.key.keysym.sym) {
802 case SDLK_ESCAPE:
803 return mMenu;
804 case SDLK_UP:
805 PyE--;
806 if(PyE<0 || PyE>2) PyE=2;
807 break;
808 case SDLK_DOWN:
809 PyE++;
810 if(PyE>2) PyE=0;
811 break;
812 case SDLK_LEFT:
813 if(Niv>0) Niv--;
814 PyE=1;
815 break;
816 case SDLK_RIGHT:
817 if(Niv<Pref.NiveauMax) Niv++;
818 PyE=1;
819 break;
820 case SDLK_F12: // Sauve un screenshot
821 SDL_SaveBMP(sdlVideo,"screenshot.bmp");
822 break;
823 case ' ':
824 case SDLK_RETURN:
825 case SDLK_KP_ENTER:
826 switch(PyE) {
827 case 0:
828 return mMenuSpeed;
829 case 1:
830 Pref.Niveau=Niv;
831 return mMenuSpeed;
832 case 2:
833 return mMenu;
834 case 3:
835 if(Niv>0) Niv--;
836 break;
837 case 4:
838 if(Niv<Pref.NiveauMax) Niv++;
839 break;
840 }
841 break;
842 }
843 }
844 break;
845 case SDL_QUIT:
846 return mQuit;
847 break;
848 }
849 }
850
851 // G�re les variables
852 HorlogeAvant=Horloge;
853 Horloge=SDL_GetTicks();
854 Sleeping();
855
856 // G�re l'Affichage
857 Ec[NumEc].Efface(fmenu);
858 EffaceChiffre(400,380,999,Sprites[fmenu].Image[0]);
859
860 // Affiche les fl�ches
861 if(Niv>0) {
862 if(PyE==3) Ec[NumEc].Affiche(fleches,2,330,380);
863 else Ec[NumEc].Affiche(fleches,1,330,380);
864 }
865 else Ec[NumEc].Affiche(fleches,0,330,380);
866
867 if(Niv<Pref.NiveauMax) {
868 if(PyE==4) Ec[NumEc].Affiche(fleches,5,470,380);
869 else Ec[NumEc].Affiche(fleches,4,470,380);
870 }
871 else Ec[NumEc].Affiche(fleches,3,470,380);
872
873 AfficheChiffre(400,380,Niv+1);
874
875 if(PyE!=3 && PyE!=4) Affiche_Main();
876 Sourie.Affiche(NumEc);
877
878 // Echange les buffets video
879 SDL_Flip(sdlVideo);
880 NumEc=(NumEc+1)&1;
881
882 } while(true);
883
884 return mQuit;
885 }
886
887 /*** SDL Main questions sur les droits de l'homme ***/
888 /****************************************************/
889 #ifndef DCHILDREN
SDLMain_HR(void)890 eMenu Menu::SDLMain_HR(void)
891 {
892 int Fini=-1;
893 int N1,N2,Ordre;
894 SDL_Rect Position;
895
896 // InitialisationsDivers
897 Sourie.Init(Menu_Py); // Initialise la sourie
898 PyE=rand()%2;
899
900 // Choix de la question N1=reponse
901 N1=rand()%30;
902 do {
903 N2=rand()%30;
904 } while (N2==N1);
905 Ordre=rand()%2; // Si 0=bonne ordre, 1=inverse
906
907 // Prend l'image du fond et fait l'affichage
908 Position.x=Position.y=0;
909 Position.w=Sprites[fmenu].Dim[0].L;
910 Position.h=Sprites[fmenu].Dim[0].H;
911 SDL_BlitSurface(sdlVideo,&Position,Sprites[fmenu].Image[0],&Position);
912
913 Sprites[menu].Affiche(340,300,0,Sprites[fmenu].Image[0]);
914 Sprites[fond_hr].Affiche(340,74,0,Sprites[fmenu].Image[0]);
915 AfficheText(338,70,e_Sprite(T_question),Sprites[fmenu].Image[0]);
916
917 Sprites[locomotive].Affiche(115,110,rand()%320,Sprites[fmenu].Image[0]);
918 Sprites[deco].Affiche(100,160+(rand()%130),rand()%18,Sprites[fmenu].Image[0]);
919 Sprites[deco].Affiche(100,470-(rand()%130),rand()%18,Sprites[fmenu].Image[0]);
920 Sprites[deco].Affiche(580,100+(rand()%130),rand()%18,Sprites[fmenu].Image[0]);
921 Sprites[deco].Affiche(580,470-(rand()%130),rand()%18,Sprites[fmenu].Image[0]);
922
923 AfficheText(340,300,e_Sprite(T_art1+N1),Sprites[fmenu].Image[0]);
924
925 if(Ordre) {
926 AddBouton(0,e_Sprite(T_tart1+N1),240,492);
927 AddBouton(1,e_Sprite(T_tart1+N2),440,492);
928 }
929 else {
930 AddBouton(0,e_Sprite(T_tart1+N1),440,492);
931 AddBouton(1,e_Sprite(T_tart1+N2),240,492);
932 }
933 Menu_Py[0].DepY-=20;
934 Menu_Py[0].FinY+=20;
935 Menu_Py[1].DepY-=20;
936 Menu_Py[1].FinY+=20;
937
938 Menu_Py[2].DepX=-1;
939
940 // Efface le fond
941 NumEc=0;
942 Ec[1].Cls(fmenu);
943 SDL_Flip(sdlVideo);
944 Ec[0].Cls(fmenu);
945
946 // Prend les evenements
947 do {
948 SDL_Event event;
949 while(SDL_PollEvent(&event)) {
950 Sourie.GetEvent(event,PyE); // Prend les evenements de la sourie
951 switch(event.type) {
952 case SDL_ACTIVEEVENT:
953 if(event.active.gain==1) {
954 NumEc=0;
955 Ec[1].Cls(fmenu);
956 SDL_Flip(sdlVideo);
957 Ec[0].Cls(fmenu);
958 }
959 break;
960 case SDL_KEYDOWN:
961 if(Fini==-1 && event.key.state==SDL_PRESSED) {
962 Sons.Play(sClic);
963 switch(event.key.keysym.sym) {
964 case SDLK_ESCAPE:
965 return mJeux;
966 case SDLK_UP:
967 PyE--;
968 if(PyE<0) PyE=1;
969 break;
970 case SDLK_DOWN:
971 PyE++;
972 if(PyE>1) PyE=0;
973 break;
974 case SDLK_LEFT:
975 if(Ordre) PyE=0;
976 else PyE=1;
977 break;
978 case SDLK_RIGHT:
979 if(Ordre) PyE=1;
980 else PyE=0;
981 break;
982 case SDLK_F12: // Sauve un screenshot
983 SDL_SaveBMP(sdlVideo,"screenshot.bmp");
984 break;
985 case ' ':
986 case SDLK_RETURN:
987 case SDLK_KP_ENTER:
988 switch(PyE) {
989 case 0:
990 Sons.Play(sEnd);
991 Pref.Score+=50;
992 Fini=Horloge+2000;
993 break;
994 case 1:
995 Sons.Play(sLose);
996 Fini=Horloge+2000;
997 break;
998 }
999 break;
1000 }
1001 }
1002 break;
1003 case SDL_QUIT:
1004 return mQuit;
1005 break;
1006 }
1007 }
1008
1009 // Test si fini
1010 if(Fini!=-1 && Fini<Horloge) return mJeux;
1011
1012 // G�re les variables
1013 HorlogeAvant=Horloge;
1014 Horloge=SDL_GetTicks();
1015 Sleeping();
1016
1017 // G�re l'Affichage
1018 Ec[NumEc].Efface(fmenu);
1019
1020 if(Ordre) {
1021 Ec[NumEc].Affiche(fond_hrr,0,240,492);
1022 AfficheText(240,492,e_Sprite(T_tart1+N1));
1023 }
1024 else {
1025 Ec[NumEc].Affiche(fond_hrr,0,440,492);
1026 AfficheText(440,492,e_Sprite(T_tart1+N1));
1027 }
1028
1029 if(Fini==-1) {
1030 if(Ordre) {
1031 Ec[NumEc].Affiche(fond_hrr,0,440,492);
1032 AfficheText(440,492,e_Sprite(T_tart1+N2));
1033 if(PyE==0) Affiche_Main(240);
1034 else Affiche_Main(440);
1035 }
1036 else {
1037 Ec[NumEc].Affiche(fond_hrr,0,240,492);
1038 AfficheText(240,492,e_Sprite(T_tart1+N2));
1039 if(PyE==1) Affiche_Main(240);
1040 else Affiche_Main(440);
1041 }
1042 Sourie.Affiche(NumEc);
1043 }
1044
1045 // Echange les buffets video
1046 SDL_Flip(sdlVideo);
1047 NumEc=(NumEc+1)&1;
1048
1049 } while(true);
1050
1051 return mQuit;
1052 }
1053 #endif
1054
1055 /*** SDL Main dans la partie ***/
1056 /*******************************/
Affiche_InGame(void)1057 void Menu::Affiche_InGame(void)
1058 {
1059 SDL_Rect Position;
1060
1061 // InitialisationsDivers
1062 Sourie.Init(Menu_Py); // Initialise la sourie
1063 PyE=0;
1064
1065 // Prend l'image du fond et fait l'affichage
1066 Position.x=Position.y=0;
1067 Position.w=Sprites[fmenu].Dim[0].L;
1068 Position.h=Sprites[fmenu].Dim[0].H;
1069 SDL_BlitSurface(sdlVideo,&Position,Sprites[fmenu].Image[0],&Position);
1070
1071 Sprites[menu].Affiche(340,300,0,Sprites[fmenu].Image[0]);
1072
1073 AfficheText(340,185,T_continue,Sprites[fmenu].Image[0]);
1074 AddBouton(0,T_continue,340,185);
1075 AfficheText(340,300,T_moptions,Sprites[fmenu].Image[0]);
1076 AddBouton(1,T_moptions,340,300);
1077 AfficheText(340,415,T_exit_game,Sprites[fmenu].Image[0]);
1078 AddBouton(2,T_exit_game,340,415);
1079 Menu_Py[3].DepX=-1;
1080
1081 // Efface le fond
1082 NumEc=0;
1083 Ec[1].Cls(fmenu);
1084 SDL_Flip(sdlVideo);
1085 Ec[0].Cls(fmenu);
1086 }
1087
1088 /*** Gestion du menu dans le jeu ***/
1089 /***********************************/
SDLMain_InGame(void)1090 eMenu Menu::SDLMain_InGame(void)
1091 {
1092 Affiche_InGame();
1093
1094 // Prend les evenements
1095 do {
1096 SDL_Event event;
1097 while(SDL_PollEvent(&event)) {
1098 Sourie.GetEvent(event,PyE); // Prend les evenements de la sourie
1099 switch(event.type) {
1100 case SDL_ACTIVEEVENT:
1101 if(event.active.gain==1) {
1102 NumEc=0;
1103 Ec[1].Cls(fmenu);
1104 SDL_Flip(sdlVideo);
1105 Ec[0].Cls(fmenu);
1106 }
1107 break;
1108 case SDL_KEYDOWN:
1109 if(event.key.state==SDL_PRESSED) {
1110 Sons.Play(sClic);
1111 switch(event.key.keysym.sym) {
1112 case SDLK_ESCAPE:
1113 return mJeux;
1114 case SDLK_UP:
1115 PyE--;
1116 if(PyE<0) PyE=2;
1117 break;
1118 case SDLK_DOWN:
1119 PyE++;
1120 if(PyE>2) PyE=0;
1121 break;
1122 case SDLK_F12: // Sauve un screenshot
1123 SDL_SaveBMP(sdlVideo,"screenshot.bmp");
1124 break;
1125 case ' ':
1126 case SDLK_RETURN:
1127 case SDLK_KP_ENTER:
1128 switch(PyE) {
1129 case 0:
1130 return mJeux;
1131 case 1:
1132 SDLMain_Options();
1133 Affiche_InGame();
1134 PyE=1;
1135 break;
1136 case 2:
1137 return mScoreEdit;
1138 }
1139 break;
1140 }
1141 }
1142 break;
1143 case SDL_QUIT:
1144 return mQuit;
1145 break;
1146 }
1147 }
1148
1149 // G�re les variables
1150 HorlogeAvant=Horloge;
1151 Horloge=SDL_GetTicks();
1152 Sleeping();
1153
1154 // G�re l'Affichage
1155 Ec[NumEc].Efface(fmenu);
1156 Affiche_Main(340);
1157 Sourie.Affiche(NumEc);
1158
1159 // Echange les buffets video
1160 SDL_Flip(sdlVideo);
1161 NumEc=(NumEc+1)&1;
1162
1163 } while(true);
1164
1165 return mQuit;
1166 }
1167
1168 /*** SDL Main Menu Choix de la difficult� ***/
1169 /********************************************/
SDLMain_Score(bool EditScore)1170 eMenu Menu::SDLMain_Score(bool EditScore)
1171 {
1172 int i;
1173 int NEdit=-1;
1174 char Provi[256];
1175 int PosCur=0;
1176 char key;
1177
1178 // Cherche le num�ro du score � remplacer si edition des scores
1179 if(EditScore) {
1180 for(i=7;i>=0;i--) if(Pref.Sco[i].Score<Pref.Score) NEdit=i;
1181 if(NEdit==-1) return mMenu;
1182
1183 if(NEdit<7) // Si doit fair un d�calage
1184 for(i=7;i>NEdit;i--) {
1185 Pref.Sco[i].Score=Pref.Sco[i-1].Score;
1186 strcpy(Pref.Sco[i].Name,Pref.Sco[i-1].Name);
1187 }
1188
1189 // Efface le nouveau nom et met le score
1190 Pref.Sco[NEdit].Score=Pref.Score;
1191 Pref.Sco[NEdit].Name[0]=0;
1192 }
1193
1194 // Met la sourie sur tous l'ecran
1195 Sourie.Init(Menu_Py); // Initialise la sourie
1196 Menu_Py[0].DepX=0;
1197 Menu_Py[0].DepY=0;
1198 Menu_Py[0].FinX=800;
1199 Menu_Py[0].FinY=600;
1200 Menu_Py[0].Py=0;
1201 Menu_Py[0].Valide=true;
1202 Menu_Py[1].DepX=-1;
1203
1204 // Prend l'image du fond et fait l'affichage
1205 Sprites[fond_menu].Affiche(400,300,0,Sprites[fmenu].Image[0]);
1206
1207 // Affiche le titre et les commandes
1208 AfficheText(400,50,T_better_scores,Sprites[fmenu].Image[0]);
1209 AfficheText(400,550,T_press_any_key,Sprites[fmenu].Image[0]);
1210
1211 // Affiche les scores
1212 for(i=0;i<8;i++) {
1213 sprintf(Provi,"%u",i+1);
1214 AfficheString(70,120+i*(360/7),Provi,Sprites[fmenu].Image[0]);
1215
1216 if(EditScore==false || NEdit!=i) {
1217 if(Pref.Sco[i].Name[0]) AfficheString(140,120+i*(360/7),Pref.Sco[i].Name,Sprites[fmenu].Image[0]);
1218 else AfficheString(140,120+i*(360/7),Points,Sprites[fmenu].Image[0]);
1219 }
1220
1221 sprintf(Provi,"%i",Pref.Sco[i].Score);
1222 AfficheString(740-LongueurString(Provi),120+i*(360/7),Provi,Sprites[fmenu].Image[0]);
1223 }
1224
1225 // Efface le fond
1226 NumEc=0;
1227 Ec[1].Cls(fmenu);
1228 SDL_Flip(sdlVideo);
1229 Ec[0].Cls(fmenu);
1230
1231 // Prend les evenements
1232 do {
1233 SDL_Event event;
1234 while(SDL_PollEvent(&event)) {
1235 Sourie.GetEvent(event,PyE); // Prend les evenements de la sourie
1236 switch(event.type) {
1237 case SDL_ACTIVEEVENT:
1238 if(event.active.gain==1) {
1239 NumEc=0;
1240 Ec[1].Cls(fmenu);
1241 SDL_Flip(sdlVideo);
1242 Ec[0].Cls(fmenu);
1243 }
1244 break;
1245 case SDL_KEYDOWN: // Prend un touche au clavier
1246 if(event.key.state==SDL_PRESSED) {
1247 Sons.Play(sClic);
1248 if(EditScore==false && event.key.keysym.sym!=SDLK_F12) event.key.keysym.sym=SDLK_RETURN;
1249 switch(event.key.keysym.sym) {
1250 case SDLK_F12: // Sauve un screenshot
1251 SDL_SaveBMP(sdlVideo,"screenshot.bmp");
1252 break;
1253 case SDLK_ESCAPE: // Valide l'entr�e
1254 case SDLK_RETURN:
1255 case SDLK_KP_ENTER:
1256 return mMenu;
1257 case SDLK_BACKSPACE: // Fait un retour de chariot
1258 if(PosCur) {
1259 PosCur--;
1260 Pref.Sco[NEdit].Name[PosCur]=0;
1261 }
1262 break;
1263 default: // Prend les touches
1264 key=event.key.keysym.unicode&0x7F; // Prend le caracataire correspondant � la touche
1265 if(PosCur<79 && CharExiste(key)==true) { // Prend le caractaire
1266 Pref.Sco[NEdit].Name[PosCur]=key;
1267 Pref.Sco[NEdit].Name[PosCur+1]=0;
1268 if(LongueurString(Pref.Sco[NEdit].Name)<LSCOREMAX) { // Si longueur encore Ok
1269 PosCur++;
1270 }
1271 else {
1272 Pref.Sco[NEdit].Name[PosCur]=0; // Ne valide pas le caracataire
1273 }
1274 }
1275 break;
1276 }
1277 }
1278 break;
1279 case SDL_QUIT:
1280 return mQuit;
1281 }
1282 }
1283
1284 // G�re les variables
1285 HorlogeAvant=Horloge;
1286 Horloge=SDL_GetTicks();
1287 Sleeping();
1288
1289 // G�re l'Affichage
1290 Ec[NumEc].Efface(fmenu);
1291
1292 if(EditScore) { // G�re l'affiche pour l'�dition des scores
1293 Sprites[fmenu].EffaceCarre(140,120-360/14+NEdit*(360/7),140+LSCOREMAX,120-360/14+(NEdit+1)*(360/7),
1294 Sprites[fmenu].Image[0]);
1295 AfficheString(140,120+NEdit*(360/7),Pref.Sco[NEdit].Name);
1296
1297 i=(Horloge/50)%20; // Affiche les curseurs
1298 Ec[NumEc].Affiche(fleche_gauche,i,110,120+NEdit*(360/7));
1299 Ec[NumEc].Affiche(fleche_droite,i,180+LongueurString(Pref.Sco[NEdit].Name),120+NEdit*(360/7));
1300
1301 }
1302
1303 // Echange les buffets video
1304 SDL_Flip(sdlVideo);
1305 NumEc=(NumEc+1)&1;
1306
1307 } while(true);
1308
1309 return mQuit;
1310 }
1311
1312 /*** Affiche le menu Principale ***/
1313 /**********************************/
Affiche_Main(int Centre)1314 void Menu::Affiche_Main(int Centre)
1315 {
1316 int NumSp=(Horloge/50)%20;
1317 int x1=Menu_Py[PyE].DepX-25;
1318 int x2=(Centre-x1)+Centre;
1319 int y=(Menu_Py[PyE].FinY+Menu_Py[PyE].DepY)/2;
1320
1321 Ec[NumEc].Affiche(fleche_gauche,NumSp,x1,y);
1322 Ec[NumEc].Affiche(fleche_droite,NumSp,x2,y);
1323 }
1324
1325 /*** Centre les fl�ches sur le boutton ***/
1326 /*****************************************/
Affiche_Main_Centre()1327 void Menu::Affiche_Main_Centre()
1328 {
1329 int NumSp=(Horloge/50)%20;
1330 int x1=Menu_Py[PyE].DepX-5;
1331 int x2=Menu_Py[PyE].FinX+5;
1332 int y=(Menu_Py[PyE].FinY+Menu_Py[PyE].DepY)/2;
1333
1334 Ec[NumEc].Affiche(fleche_gauche,NumSp,x1,y);
1335 Ec[NumEc].Affiche(fleche_droite,NumSp,x2,y);
1336 }
1337