1 #include "PuyoStarter.h"
2 #include "PuyoView.h"
3 
4 #include "SDL_Painter.h"
5 #include "IosImgProcess.h"
6 
7 extern SDL_Painter painter;
8 
9 IIM_Surface *grid;
10 IIM_Surface *perso[2];
11 int currentPerso;
12 IIM_Surface *live[4];
13 IIM_Surface *speedImg;
14 IIM_Surface *speedBlackImg;
15 IIM_Surface *gameScreen;
16 IIM_Surface *shrinkingPuyo[5][5];
17 IIM_Surface *explodingPuyo[5][5];
18 extern IIM_Surface *bigNeutral;
19 IIM_Surface *puyoCircle[32];
20 extern IIM_Surface *puyoShadow;
21 IIM_Surface *puyoEye[3];
22 extern IIM_Surface *puyoEyes;
23 IIM_Surface *puyoEyesSwirl[4];
24 extern IIM_Surface *puyoFaces[5][16];
25 
26 int gameLevel;
27 int GAME_ACCEL = 1250;
28 static const int NB_PERSO_STATE = 2;
29 
30 const char *p1name;
31 const char *p2name;
32 
33 extern Menu *menu_pause;
34 static char *BACKGROUND[NB_MUSIC_THEME] = { "Background.jpg", "BackgroundDark.jpg" };
35 extern IIM_Surface *background, *fallingBlue, *fallingRed, *fallingGreen, *fallingViolet, *fallingYellow, *neutral;
36 
loadShrinkXplode2(int i,float dec)37 static void loadShrinkXplode2(int i, float dec)
38 {
39     for (int j=1;j<=4;++j)
40     {
41         shrinkingPuyo[j-1][i] = iim_surface_shift_hue(shrinkingPuyo[j-1][3],dec);
42         explodingPuyo[j-1][i] = iim_surface_shift_hue(explodingPuyo[j-1][3],dec);
43     }
44 }
45 
loadShrinkXplode(void)46 static void loadShrinkXplode(void)
47 {
48     for (int j=1;j<=4;++j)
49     {
50         char f[20];
51         sprintf(f,"Shrink%d.png", j);
52         shrinkingPuyo[j-1][3] = IIM_Load_DisplayFormatAlpha(f);
53         sprintf(f,"Explode%d.png", j);
54         explodingPuyo[j-1][3] = IIM_Load_DisplayFormatAlpha(f);
55     }
56 
57     loadShrinkXplode2(0,-65.0f);
58     loadShrinkXplode2(1,100.0f);
59     loadShrinkXplode2(2,-150.0f);
60     loadShrinkXplode2(4,140.0f);
61 }
62 
63 
stopRender()64 void PuyoStarter::stopRender()
65 {
66     this->stopRendering = true;
67     iim_surface_convert_to_gray(painter.gameScreen);
68 }
69 
70 
restartRender()71 void PuyoStarter::restartRender()
72 {
73     this->stopRendering = false;
74     painter.redrawAll();
75 }
76 
draw()77 void PuyoStarter::draw()
78 {
79     if (stopRendering) {
80         SDL_BlitSurface(painter.gameScreen->surf,NULL,display,NULL);
81     }
82     else {
83         SDL_Rect drect;
84 
85         areaA->render();
86         areaB->render();
87 
88         drect.x = 21;
89         drect.y = -1;
90         drect.w = grid->w;
91         drect.h = grid->h;
92         painter.requestDraw(grid, &drect);
93         drect.x = 407;
94         drect.y = -1;
95         drect.w = grid->w;
96         drect.h = grid->h;
97         painter.requestDraw(grid, &drect);
98 
99         areaA->renderNeutral();
100         areaB->renderNeutral();
101         if ((randomPlayer)&&(currentPerso>=0))
102         {
103             drect.x = 320 - perso[currentPerso]->w/2;
104             drect.y = 280 - perso[currentPerso]->h/2;
105             drect.w = perso[currentPerso]->w;
106             drect.h = perso[currentPerso]->h;
107             painter.requestDraw(perso[currentPerso], &drect);
108         }
109 
110         if ((randomPlayer)&&(lives>=0)&&(lives<=3))
111         {
112             drect.x = painter.gameScreen->w / 2 - live[lives]->w / 2;
113             drect.y = 436;
114             drect.w = live[lives]->w;
115             drect.h = live[lives]->h;
116             painter.requestDraw(live[lives], &drect);
117         }
118 
119         painter.draw(painter.gameScreen->surf);
120     }
121     char text[256];
122     if (!randomPlayer)
123     {
124         sprintf(text, "Win %d", score1);
125         SoFont_CenteredString_XY (commander->smallFont, display,
126                                   50, 460,   text, NULL);
127         sprintf(text, "Win %d", score2);
128         SoFont_CenteredString_XY (commander->smallFont, display,
129                                   590, 460, text, NULL);
130     }
131 
132     SoFont *font = (stopRendering?commander->darkFont:commander->menuFont);
133     SoFont_CenteredString_XY (font, display, 510, 460,   p1name, NULL);
134     SoFont_CenteredString_XY (font, display, 130, 460,   p2name, NULL);
135 
136     int gameSpeedCpy = gameSpeed;
137     if (gameSpeed == 1) gameSpeedCpy = 0;
138 
139     SDL_Rect speedRect;
140     speedRect.x = 0;
141     speedRect.w = speedImg->w;
142     speedRect.h = (20 - gameSpeedCpy) * 6;
143     speedRect.y = speedImg->h - speedRect.h;
144 
145     SDL_Rect drect;
146     drect.x = 320 - speedRect.w / 2;
147     drect.y = 170 - speedRect.h;
148     drect.w = speedRect.w;
149     drect.h = speedRect.h;
150 
151     SDL_Rect speedBlackRect = speedRect;
152     SDL_Rect drectBlack     = drect;
153 
154     speedBlackRect.h = speedImg->h - speedRect.h;
155     speedBlackRect.y = 0;
156     drectBlack.y = 50;
157     drectBlack.h = speedBlackRect.h;
158 
159     SDL_BlitSurface(speedBlackImg->surf,&speedBlackRect,display,&drectBlack);
160     if (stopRendering)
161         SDL_BlitSurface(speedBlackImg->surf,&speedRect,display,&drect);
162     else
163         SDL_BlitSurface(speedImg->surf,&speedRect,display,&drect);
164 
165     SoFont *fontBl = NULL;
166     if ((blinkingPointsA % 2) == 0)
167         fontBl = commander->smallFont;
168     else
169         fontBl = commander->menuFont;
170 
171     sprintf(text, "<< %d", attachedGameA->getPoints());
172     SoFont_CenteredString_XY (fontBl, display,
173                               300, 380,   text, NULL);
174 
175     if ((blinkingPointsB % 2) == 0)
176         fontBl = commander->smallFont;
177     else
178         fontBl = commander->menuFont;
179 
180     sprintf(text, "%d >>", attachedGameB->getPoints());
181     SoFont_CenteredString_XY (fontBl, display,
182                               340, 395, text, NULL);
183 }
184 
185 
PuyoStarter(PuyoCommander * commander,bool aiLeft,int aiLevel,IA_Type aiType,int theme)186 PuyoStarter::PuyoStarter(PuyoCommander *commander, bool aiLeft, int aiLevel, IA_Type aiType, int theme)
187 {
188     this->stopRendering = false;
189     this->paused = false;
190     tickCounts = 0;
191     this->commander = commander;
192 
193     blinkingPointsA = 0;
194     blinkingPointsB = 0;
195     savePointsA = 0;
196     savePointsB = 0;
197 
198     background    = IIM_Load_DisplayFormat(BACKGROUND[theme]);
199 
200     painter.backGround = background;
201     if (painter.gameScreen == NULL)
202     {
203         SDL_PixelFormat *fmt = background->surf->format;
204         SDL_Surface *tmp = SDL_CreateRGBSurface(background->surf->flags,
205                                                 background->w, background->h, 32,
206                                                 fmt->Rmask, fmt->Gmask,
207                                                 fmt->Bmask, fmt->Amask);
208         gameScreen = painter.gameScreen = IIM_RegisterImg(SDL_DisplayFormat(tmp), false);
209         SDL_FreeSurface(tmp);
210     }
211     painter.redrawAll(painter.gameScreen->surf);
212 
213     static bool firstTime = true;
214     if (firstTime) {
215         fallingViolet = IIM_Load_DisplayFormatAlpha("v0.png");
216         fallingRed    = iim_surface_shift_hue(fallingViolet, 100.0f);
217         fallingBlue   = iim_surface_shift_hue(fallingViolet, -65.0f);
218         fallingGreen  = iim_surface_shift_hue(fallingViolet, -150.0f);
219         fallingYellow = iim_surface_shift_hue(fallingViolet, 140.0f);
220         neutral       = IIM_Load_DisplayFormatAlpha("Neutral.png");
221         bigNeutral    = IIM_Load_DisplayFormatAlpha("BigNeutral.png");
222         speedImg      = IIM_Load_DisplayFormatAlpha("speed.png");
223         speedBlackImg = IIM_Load_DisplayFormatAlpha("speed_black.png");
224 
225         IIM_Surface * tmpsurf = IIM_Load_DisplayFormatAlpha("circle.png");
226         for (int i = 0 ; i < 32 ; i++)
227             puyoCircle[i] = iim_surface_set_value(tmpsurf,sin(3.14f/2.0f+i*3.14f/64.0f)*0.6f+0.2f);
228         IIM_Free(tmpsurf);
229         loadShrinkXplode();
230 
231         puyoShadow = IIM_Load_DisplayFormatAlpha("Shadow.png");
232         puyoEye[0] = IIM_Load_DisplayFormatAlpha("eye0.png");
233         puyoEye[1] = IIM_Load_DisplayFormatAlpha("eye1.png");
234         puyoEye[2] = IIM_Load_DisplayFormatAlpha("eye2.png");
235         puyoEyes = puyoEye[0];
236         puyoEyesSwirl[0] = IIM_Load_DisplayFormatAlpha("twirleye0.png");
237         puyoEyesSwirl[1] = IIM_Load_DisplayFormatAlpha("twirleye1.png");
238         puyoEyesSwirl[2] = IIM_Load_DisplayFormatAlpha("twirleye2.png");
239         puyoEyesSwirl[3] = IIM_Load_DisplayFormatAlpha("twirleye3.png");
240 
241         puyoFaces[0][0] = IIM_Load_DisplayFormatAlpha("v0.png");
242         puyoFaces[0][1] = IIM_Load_DisplayFormatAlpha("v1a.png");
243         puyoFaces[0][2] = IIM_Load_DisplayFormatAlpha("v1b.png");
244         puyoFaces[0][3] = IIM_Load_DisplayFormatAlpha("v1c.png");
245         puyoFaces[0][4] = IIM_Load_DisplayFormatAlpha("v1d.png");
246         puyoFaces[0][5] = IIM_Load_DisplayFormatAlpha("v2ab.png");
247         puyoFaces[0][6] = IIM_Load_DisplayFormatAlpha("v2ac.png");
248         puyoFaces[0][7] = IIM_Load_DisplayFormatAlpha("v2ad.png");
249         puyoFaces[0][8] = IIM_Load_DisplayFormatAlpha("v2bc.png");
250         puyoFaces[0][9] = IIM_Load_DisplayFormatAlpha("v2bd.png");
251         puyoFaces[0][10] = IIM_Load_DisplayFormatAlpha("v2cd.png");
252         puyoFaces[0][11] = IIM_Load_DisplayFormatAlpha("v3abc.png");
253         puyoFaces[0][12] = IIM_Load_DisplayFormatAlpha("v3abd.png");
254         puyoFaces[0][13] = IIM_Load_DisplayFormatAlpha("v3acd.png");
255         puyoFaces[0][14] = IIM_Load_DisplayFormatAlpha("v3bcd.png");
256         puyoFaces[0][15] = IIM_Load_DisplayFormatAlpha("v4abcd.png");
257 
258         live[0] = IIM_Load_DisplayFormatAlpha("0live.png");
259         live[1] = IIM_Load_DisplayFormatAlpha("1live.png");
260         live[2] = IIM_Load_DisplayFormatAlpha("2live.png");
261         live[3] = IIM_Load_DisplayFormatAlpha("3live.png");
262 
263         for (int i = 0 ; i < 16 ; i++) {
264             puyoFaces[1][i] = iim_surface_shift_hue(puyoFaces[0][i], 100);
265         }
266         for (int i = 0 ; i < 16 ; i++) {
267             puyoFaces[2][i] = iim_surface_shift_hue(puyoFaces[0][i], -65);
268         }
269         for (int i = 0 ; i < 16 ; i++) {
270             puyoFaces[3][i] = iim_surface_shift_hue(puyoFaces[0][i], -150);
271         }
272         for (int i = 0 ; i < 16 ; i++) {
273             puyoFaces[4][i] = iim_surface_shift_hue(puyoFaces[0][i], 140);
274         }
275 
276         grid          = IIM_Load_DisplayFormatAlpha("grid.png");
277         firstTime = false;
278     }
279 
280     if (fallingBlue == NULL)
281     {
282         fprintf(stderr, "IMG_Load error:%s\n", SDL_GetError());
283         exit(-1);
284     }
285 
286     areaA = new PuyoView(&attachedRandom, 1 + CSIZE, BSIZE-TSIZE, CSIZE + PUYODIMX*TSIZE + FSIZE, BSIZE+ESIZE);
287     areaB = new PuyoView(&attachedRandom, 1 + CSIZE + PUYODIMX*TSIZE + DSIZE, BSIZE-TSIZE, CSIZE + PUYODIMX*TSIZE + DSIZE - FSIZE - TSIZE, BSIZE+ESIZE);
288 
289     attachedGameA = areaA->getAttachedGame();
290     attachedGameB = areaB->getAttachedGame();
291 
292     if (aiLeft) {
293         randomPlayer = new PuyoIA(aiType, aiLevel, areaA);
294         perso[0] = IIM_Load_DisplayFormatAlpha("perso1_1.png");
295         perso[1] = IIM_Load_DisplayFormatAlpha("perso1_2.png");
296     }
297     else {
298         randomPlayer = 0;
299         perso[0] = NULL;
300     }
301 
302     areaA->setEnemyGame(attachedGameB);
303     areaB->setEnemyGame(attachedGameA);
304 }
305 
~PuyoStarter()306 PuyoStarter::~PuyoStarter()
307 {
308     delete areaA;
309     delete areaB;
310     delete attachedGameA;
311     delete attachedGameB;
312     //	SDL_FreeSurface(fallingBlue);
313 }
314 
315 
316 
317 
318 #define FPKEY_REPEAT        7
319 #define FPKEY_DELAY         5
320 
321 #define FPKEY_keyNumber     10
322 
323 #define FPKEY_P1_Down       0
324 #define FPKEY_P1_Left       1
325 #define FPKEY_P1_Right      2
326 #define FPKEY_P1_TurnLeft   3
327 #define FPKEY_P1_TurnRight  4
328 #define FPKEY_P2_Down       5
329 #define FPKEY_P2_Left       6
330 #define FPKEY_P2_Right      7
331 #define FPKEY_P2_TurnLeft   8
332 #define FPKEY_P2_TurnRight  9
333 
334 #define repeatCondition(A) keysDown[A]++; if (((keysDown[A]-FPKEY_DELAY)>0) && ((keysDown[A]-FPKEY_DELAY)%FPKEY_REPEAT == 0))
335 
336 
run(int _score1,int _score2,int lives,int point1,int point2)337 void PuyoStarter::run(int _score1, int _score2, int lives, int point1, int point2)
338 {
339     this->lives = lives;
340     this->score1 = _score1;
341     this->score2 = _score2;
342     SDL_Rect drect;
343     SDL_Event event;
344     int quit = 0;
345     SDL_EnableUNICODE(1);
346     int keysDown[FPKEY_keyNumber] = {0,0,0,0,0,0,0,0,0,0};
347 
348     gameSpeed = 20;
349     attachedGameB->points = point1;
350     attachedGameA->points = point2;
351 
352     if (!randomPlayer) {
353         int sc1=score1,sc2=score2;
354         while ((sc1>0)&&(sc2>0)) { sc1--; sc2--; }
355         attachedGameA->increaseNeutralPuyos(sc1 * PUYODIMX);
356         attachedGameB->increaseNeutralPuyos(sc2 * PUYODIMX);
357         attachedGameA->dropNeutrals();
358         attachedGameB->dropNeutrals();
359         attachedGameA->cycle();
360         attachedGameB->cycle();
361     }
362 
363     while (!quit) {
364         bool left_danger = (attachedGameA->getMaxColumnHeight() > PUYODIMY - 5);
365         bool right_danger = (attachedGameB->getMaxColumnHeight() > PUYODIMY - 5);
366         bool danger = left_danger || right_danger;
367         bool gameover = (!attachedGameA->isGameRunning() || !attachedGameB->isGameRunning());
368 
369         /*if (paused)
370             audio_music_start(0);
371         else */if (gameover)
372             audio_music_start(3);
373         else if (danger)
374             audio_music_start(2);
375         else
376             audio_music_start(1);
377 
378         if (left_danger)
379             currentPerso = 0;
380         else
381             currentPerso = 1;
382 
383         while (SDL_PollEvent(&event) == 1)
384         {
385             if (attachedGameA->isGameRunning() &&
386                 attachedGameB->isGameRunning()) {
387                 if (!paused) {
388 
389                     /* Check for usual events */
390                     GameControlEvent controlEvent;
391                     getControlEvent(event, &controlEvent);
392 
393                     if (controlEvent.isUp)
394                     {
395                         switch (controlEvent.gameEvent) {
396                             case GameControlEvent::kPlayer1Down:
397                                 if (randomPlayer == 0) keysDown[FPKEY_P1_Down] = 0;
398                                 break;
399                             case GameControlEvent::kPlayer1Left:
400                                 if (randomPlayer == 0) keysDown[FPKEY_P1_Left] = 0;
401                                 break;
402                             case GameControlEvent::kPlayer1Right:
403                                 if (randomPlayer == 0) keysDown[FPKEY_P1_Right] = 0;
404                                 break;
405                             case GameControlEvent::kPlayer1TurnLeft:
406                                 if (randomPlayer == 0) keysDown[FPKEY_P1_TurnLeft] = 0;
407                                 break;
408                             case GameControlEvent::kPlayer1TurnRight:
409                                 if (randomPlayer == 0) keysDown[FPKEY_P1_TurnRight] = 0;
410                                 break;
411 
412                             case GameControlEvent::kPlayer2Down:
413                                 keysDown[FPKEY_P2_Down] = 0;
414                                 break;
415                             case GameControlEvent::kPlayer2Left:
416                                 keysDown[FPKEY_P2_Left] = 0;
417                                 break;
418                             case GameControlEvent::kPlayer2Right:
419                                 keysDown[FPKEY_P2_Right] = 0;
420                                 break;
421                             case GameControlEvent::kPlayer2TurnLeft:
422                                 keysDown[FPKEY_P2_TurnLeft] = 0;
423                                 break;
424                             case GameControlEvent::kPlayer2TurnRight:
425                                 keysDown[FPKEY_P2_TurnRight] = 0;
426                                 break;
427                         }
428                     }
429                     else {
430                         switch (controlEvent.gameEvent) {
431                             case GameControlEvent::kPlayer1Left:
432                                 if (randomPlayer == 0) {
433                                     areaA->moveLeft();
434                                     if (attachedGameA->isEndOfCycle()) keysDown[FPKEY_P1_Left] = 0;
435                                     else keysDown[FPKEY_P1_Left]++;
436                                 }
437                                 break;
438                             case GameControlEvent::kPlayer1Right:
439                                 if (randomPlayer == 0) {
440                                     areaA->moveRight();
441                                     if (attachedGameA->isEndOfCycle()) keysDown[FPKEY_P1_Right] = 0;
442                                     else keysDown[FPKEY_P1_Right]++;
443                                 }
444                                 break;
445                             case GameControlEvent::kPlayer1TurnLeft:
446                                 if (randomPlayer == 0) {
447                                     areaA->rotateLeft();
448                                     if (attachedGameA->isEndOfCycle()) keysDown[FPKEY_P1_TurnLeft] = 0;
449                                     else keysDown[FPKEY_P1_TurnLeft]++;
450                                 }
451                                 break;
452                             case GameControlEvent::kPlayer1TurnRight:
453                                 if (randomPlayer == 0) {
454                                     areaA->rotateRight();
455                                     if (attachedGameA->isEndOfCycle()) keysDown[FPKEY_P1_TurnRight] = 0;
456                                     else keysDown[FPKEY_P1_TurnRight]++;
457                                 }
458                                 break;
459                             case GameControlEvent::kPlayer1Down:
460                                 if (randomPlayer == 0) {
461                                     //attachedGameA->cycle(); desact flobo
462                                     if (attachedGameA->isEndOfCycle()) keysDown[FPKEY_P1_Down] = 0;
463                                     else keysDown[FPKEY_P1_Down]++;
464                                 }
465                                 break;
466 
467                             case GameControlEvent::kPlayer2Left:
468                                 areaB->moveLeft();
469                                 if (attachedGameB->isEndOfCycle()) keysDown[FPKEY_P2_Left] = 0;
470                                 else keysDown[FPKEY_P2_Left]++;
471                                 break;
472                             case GameControlEvent::kPlayer2Right:
473                                 areaB->moveRight();
474                                 if (attachedGameB->isEndOfCycle()) keysDown[FPKEY_P2_Right] = 0;
475                                 else keysDown[FPKEY_P2_Right]++;
476                                 break;
477                             case GameControlEvent::kPlayer2TurnLeft:
478                                 areaB->rotateLeft();
479                                 if (attachedGameB->isEndOfCycle()) keysDown[FPKEY_P2_TurnLeft] = 0;
480                                 else keysDown[FPKEY_P2_TurnLeft]++;
481                                 break;
482                             case GameControlEvent::kPlayer2TurnRight:
483                                 areaB->rotateRight();
484                                 if (attachedGameB->isEndOfCycle()) keysDown[FPKEY_P2_TurnRight] = 0;
485                                 else keysDown[FPKEY_P2_TurnRight]++;
486                                 break;
487                             case GameControlEvent::kPlayer2Down:
488                                 //attachedGameB->cycle(); desact flobo
489                                 if (attachedGameB->isEndOfCycle()) keysDown[FPKEY_P2_Down] = 0;
490                                 else keysDown[FPKEY_P2_Down]++;
491                                 break;
492                             default:
493                                 break;
494                         }
495                     }
496 
497                     switch (event.type) {
498                       case SDL_USEREVENT:
499                         if (randomPlayer)
500                           randomPlayer->cycle();
501                         if (event.user.code == 1) {
502                           drect.x = 0;
503                           drect.y = 0;
504                           drect.w = 640;
505                           drect.h = 480;
506 
507                           if (attachedGameA->isEndOfCycle()) {
508                             keysDown[FPKEY_P1_Down] = 0;
509                             keysDown[FPKEY_P1_TurnLeft] = 0;
510                             keysDown[FPKEY_P1_TurnRight] = 0;
511                           }
512 
513                           areaA->cycleGame(); // a voir
514 
515                           if (attachedGameB->isEndOfCycle()) {
516                             keysDown[FPKEY_P2_Down] = 0;
517                             keysDown[FPKEY_P2_TurnLeft] = 0;
518                             keysDown[FPKEY_P2_TurnRight] = 0;
519                           }
520                           areaB->cycleGame(); // a voir
521 
522                           switch (gameLevel)
523                           {
524                             case 1:
525                               attachedGameB->points += 1;
526                               attachedGameA->points += 1;
527                               break;
528                             case 2:
529                               attachedGameB->points += 5;
530                               attachedGameA->points += 5;
531                               break;
532                             case 3:
533                               attachedGameB->points += 10;
534                               attachedGameA->points += 10;
535                               break;
536                           }
537 
538                           if (attachedGameA->getPoints()/50000 > savePointsA/50000)
539                             blinkingPointsA = 10;
540                           if (attachedGameB->getPoints()/50000 > savePointsB/50000)
541                             blinkingPointsB = 10;
542 
543                           if (blinkingPointsA > 0)
544                             blinkingPointsA--;
545                           if (blinkingPointsB > 0)
546                             blinkingPointsB--;
547 
548                           savePointsB = attachedGameB->getPoints();
549                           savePointsA = attachedGameA->getPoints();
550 
551                           if (savePointsA < 50000) blinkingPointsA=0;
552                           if (savePointsB < 50000) blinkingPointsB=0;
553 
554                         } else {
555                           if (keysDown[FPKEY_P2_Down]) {
556                             if (attachedGameB->isEndOfCycle())
557                               keysDown[FPKEY_P2_Down] = 0;
558                             else
559                               areaB->cycleGame(); // a voir
560                           }
561                           if (keysDown[FPKEY_P2_Left]) {
562                             repeatCondition(FPKEY_P2_Left) areaB->moveLeft();
563                           }
564                           if (keysDown[FPKEY_P2_Right]) {
565                             repeatCondition(FPKEY_P2_Right) areaB->moveRight();
566                           }
567                           if (keysDown[FPKEY_P2_TurnLeft]) {
568                             repeatCondition(FPKEY_P2_TurnLeft) areaB->rotateLeft();
569                             if (attachedGameB->isEndOfCycle())
570                               keysDown[FPKEY_P2_TurnLeft] = 0;
571                           }
572                           if (keysDown[FPKEY_P2_TurnRight]) {
573                             repeatCondition(FPKEY_P2_TurnRight) areaB->rotateRight();
574                             if (attachedGameB->isEndOfCycle())
575                               keysDown[FPKEY_P2_TurnRight] = 0;
576                           }
577 
578                           if (keysDown[FPKEY_P1_Down]) {
579                             if (attachedGameA->isEndOfCycle())
580                               keysDown[FPKEY_P1_Down] = 0;
581                             else
582                               areaA->cycleGame(); // a voir
583                           }
584                           if (keysDown[FPKEY_P1_Left]) {
585                             repeatCondition(FPKEY_P1_Left) areaA->moveLeft();
586                             if (attachedGameA->isEndOfCycle())
587                               keysDown[FPKEY_P1_Left] = 0;
588                           }
589                           if (keysDown[FPKEY_P1_Right]) {
590                             repeatCondition(FPKEY_P1_Right) areaA->moveRight();
591                             if (attachedGameA->isEndOfCycle())
592                               keysDown[FPKEY_P1_Right] = 0;
593                           }
594                           if (keysDown[FPKEY_P1_TurnLeft]) {
595                             repeatCondition(FPKEY_P1_TurnLeft) areaA->rotateLeft();
596                             if (attachedGameA->isEndOfCycle())
597                               keysDown[FPKEY_P1_TurnLeft] = 0;
598                           }
599                           if (keysDown[FPKEY_P1_TurnRight]) {
600                             repeatCondition(FPKEY_P1_TurnRight) areaA->rotateRight();
601                             if (attachedGameA->isEndOfCycle())
602                               keysDown[FPKEY_P1_TurnRight] = 0;
603                           }
604                         }
605                         break;
606                       case SDL_KEYDOWN:
607                         /* check for cheat-codes */
608                         static int cheatcode = 0;
609                         if (event.key.keysym.sym == SDLK_k) cheatcode  = 0;
610                         if (event.key.keysym.sym == SDLK_i) cheatcode += 1;
611                         if (event.key.keysym.sym == SDLK_e) cheatcode += 10;
612                         if (event.key.keysym.sym == SDLK_f) cheatcode += 100;
613                         if (event.key.keysym.sym == SDLK_t) cheatcode += 1000;
614                         if (event.key.keysym.sym == SDLK_l) cheatcode += 10000;
615                         if (cheatcode == 31111) {
616                           attachedGameA->increaseNeutralPuyos(PUYODIMX * 12);
617                           attachedGameA->dropNeutrals();
618                           attachedGameA->increaseNeutralPuyos(PUYODIMX * 12);
619                         }
620                       default:
621                         break;
622                     }
623                 } // !Paused
624                     else {
625                         GameControlEvent controlEvent;
626                         getControlEvent(event, &controlEvent);
627                         switch (controlEvent.cursorEvent) {
628                             case GameControlEvent::kUp:
629                                 menu_prev_item(menu_pause);
630                                 break;
631                             case GameControlEvent::kDown:
632                                 menu_next_item(menu_pause);
633                                 break;
634                             case GameControlEvent::kStart:
635                                 if (menu_active_is(menu_pause, kContinueGame)) {
636                                     paused = false;
637                                     restartRender();
638                                     menu_hide(menu_pause);
639                                 }
640                                 if (menu_active_is(menu_pause, kAbortGame)) {
641                                     if (menu_active_is(commander->gameOverMenu, "YES"))
642                                         menu_next_item(commander->gameOverMenu);
643                                     quit = 1;
644                                     menu_hide(menu_pause);
645                                 }
646                                 if (menu_active_is(menu_pause, kOptions)) {
647                                     menu_hide (menu_pause);
648                                     commander->optionMenuLoop(this);
649                                     menu_show (menu_pause);
650                                 }
651                                 break;
652                         }
653                     }
654             } else // Not GameIsRunning
655             {
656                 if (randomPlayer) {
657                     if (rightPlayerWin()) {
658                         if ((_score2 == 7) && (rightPlayerWin()))
659                             commander->gameOverMenu = commander->finishedMenu;
660                         else
661                             commander->gameOverMenu = commander->nextLevelMenu;
662                     }
663                     else {
664                         if (lives == 0) {
665                             commander->gameOverMenu = commander->gameOver1PMenu;
666                         }
667                         else {
668                             commander->gameOverMenu = commander->looserMenu;
669                         }
670                     }
671                 }
672                 else {
673                     commander->gameOverMenu = commander->gameOver2PMenu;
674                 }
675 
676                 if (!menu_visible(commander->gameOverMenu)) {
677 
678                     if (leftPlayerWin()) score1 = _score1 + 1;
679                     else if (rightPlayerWin()) score2 = _score2 + 1;
680 
681                     if (commander->gameOverMenu == commander->gameOver2PMenu) {
682                         char winner[256];
683                         char score[256];
684                         sprintf(winner,"%d Wins!!!",(leftPlayerWin()?1:2));
685                         sprintf(score, "%d - %d", score1, score2);
686                         menu_set_value(commander->gameOverMenu, kPlayer, winner);
687                         menu_set_value(commander->gameOverMenu, kScore,  score);
688                     }
689                     else if (commander->gameOverMenu == commander->nextLevelMenu) {
690                         char level[256];
691                         extern char *AI_NAMES[];
692                         sprintf(level, "Stage %d... Vs %s", score2+1, AI_NAMES[score2]);
693                         menu_set_value(commander->gameOverMenu, kNextLevel, level);
694                     }
695                     else if (commander->gameOverMenu == commander->looserMenu) {
696                         char level[256];
697                         char cont[256];
698                         sprintf(level, "Stage %d... Vs %s", score2+1, p2name);
699                         sprintf(cont, "%d Left", lives);
700                         menu_set_value(commander->gameOverMenu, kCurrentLevel, level);
701                         menu_set_value(commander->gameOverMenu, kContinueLeft, cont);
702                     }
703                     else if (commander->gameOverMenu == commander->gameOver1PMenu) {
704                         char level[256];
705                         sprintf(level, "Stage %d... Vs %s", score2+1, p2name);
706                         menu_set_value(commander->gameOverMenu, kYouGotToLevel, level);
707                     }
708                     commander->showGameOver();
709                     stopRender();
710                 } // GameOver Visible
711             }
712                 GameControlEvent controlEvent2;
713                 getControlEvent(event, &controlEvent2);
714                 switch (controlEvent2.cursorEvent) {
715                     case GameControlEvent::kStart:
716                         if (gameover)
717                         {
718                             if (menu_active_is(commander->gameOverMenu, "NO"))
719                                 menu_next_item(commander->gameOverMenu);
720                             quit = 1;
721                         }
722                         break;
723                     case GameControlEvent::kBack:
724                         if (!gameover) {
725                             if (!paused) {
726                                 menu_show(menu_pause);
727                                 paused = true;
728                                 stopRender();
729                             }
730                             else {
731                                 paused = false;
732                                 restartRender();
733                                 menu_hide(menu_pause);
734                             }
735                         }
736                         else {
737                             if (menu_active_is(commander->gameOverMenu, "NO"))
738                                 menu_next_item(commander->gameOverMenu);
739                             quit = 1;
740                         }
741                         break;
742                     default:
743                         break;
744                 }
745                 if(event.type == SDL_QUIT) {
746                     if (menu_active_is(commander->gameOverMenu, "YES"))
747                         menu_next_item(commander->gameOverMenu);
748                     quit = 1;
749                     exit(0);
750                 }
751         }
752             commander->updateAll(this);
753 
754             if (!paused) {
755                 areaA->cycleAnimation();
756                 areaB->cycleAnimation();
757 
758                 tickCounts++;
759                 event.user.type = SDL_USEREVENT;
760                 event.user.code = 0;
761                 SDL_PushEvent(&event);
762                 // Vitesse du jeu
763                 if (tickCounts % (gameSpeed + 5 * (3 - gameLevel)) == 0)
764                 {
765                     event.user.type = SDL_USEREVENT;
766                     event.user.code = 1;
767                     SDL_PushEvent(&event);
768                     if (!paused) {
769                         if ((tickCounts > GAME_ACCEL) && (gameSpeed > 1)) {
770                             tickCounts = 0;
771                             gameSpeed--;
772                         }
773                     }
774                 }
775             }
776 
777     }
778 	commander->hideGameOver();
779 	if (randomPlayer) {
780             for (int i=0; i<NB_PERSO_STATE; ++i)
781                 IIM_Free(perso[i]);
782 	}
783 	SDL_SetClipRect(display,NULL);
784 }
785