1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
5 #include "MainGameLoop.h"
7 // static char *VB_Name = "modMainGameLoop";
13 int LeadOutCounter, EnterRepeatCounter;
18 boolean AutoScrollFlag;
20 // ==========================================================================
23 // ==========================================================================
25 int subMainGameLoop_Init()
32 TickCountObject Clock;
39 printf("::: playing demo ...\n");
42 // EP set level success byte: demo, not game
44 EP_GameDemoVar0DAA = 0; // demo
49 printf("::: playing game ...\n");
52 // EP set level success byte: game, not demo
54 EP_GameDemoVar0DAA = 1; // game
58 // If RecordDemoFlag = 1 Then
59 // RecordDemoFlag = 0 ' clear Demo Recording flag
60 // Call subDisplayPlayingTime ' playing time on screen
61 // ' Record key still pressed?' >= (Ctrl-)F1 and <= (Ctrl-)F10
62 // While &H3B <= KeyScanCode7 And KeyScanCode7 <= &H44
63 // ' yes -> wait until released
64 // ' should we DoEvents here???? ... depends on how ... but yes!
65 // ' ...or we can rather poll the keyboardstate inside this loop???
67 // Call subInitGameConditions ' Init game conditions (vars)
68 // If MusicOnFlag = 0 Then Call subMusicInit
69 // WasDemoFlag = 0 ' no demo anymore
70 // EP_GameDemoVar0DAA = 1 ' force game
73 // This was a bug in the original Supaplex: sometimes red disks could not
74 // be released. This happened If Murphy was killed DURING a red disk release
75 // and the next try started.
77 RedDiskReleasePhase = 0; // (re-)enable red disk release
82 return subMainGameLoop;
85 int subMainGameLoop_Main(byte action, boolean warp_mode)
90 // ----------------------------------------------------------------------------
91 // --------------------- START OF GAME-BUSY LOOP ------------------------------
92 // ----------------------------------------------------------------------------
95 locRepeatMainGameLoop: // start repeating game loop
98 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100 while (PauseMode != 0)
107 DoEvents(); // user may klick on menus or move the window here ...
112 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
115 // never any additional code between here!
117 LastFrame = Clock.TickNow(); // store the frame time
119 // never any additional code between here!
120 if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
123 // FS end of synchronization
124 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128 // (should never happen)
130 // printf("::: EndFlag == True\n");
132 goto locExitMainGameLoop;
136 goto locExitMainGameLoop;
139 // If DemoFlag = 0 Then Call subCheckJoystick ' check joystick
140 // bx = subCheckRightMouseButton() ' check (right) mouse button
141 // If bx = 2 And LeadOutCounter < 1 Then KillMurphyFlag = 1 ' lead-out busy after quit? -> kill Murphy!
143 // If DebugVersionFlag <> 0 Then ' debug mode on?
144 // If Data_SubRest <> 0 Then Data_SubRest = Data_SubRest - 1
145 // If keyEnter <> 0 Then GoTo loc_g_186F ' Was it the Enter key? -> yes--skip! No mouse!
146 // ' fixes ENTER bug If no mouse driver!
147 // If bx <> 1 Then GoTo loc_g_186F ' Left button=Init game field
148 // ' Also Enter If no mouse!
149 // If Data_SubRest <> 0 Then GoTo loc_g_186F
151 // Call subRestoreFancy
152 // Call subDisplayLevel ' Paint (Init) game field
153 // Call subConvertToEasySymbols ' Convert to easy symbols
158 subProcessKeyboardInput(action); // Check keyboard, act on keys
161 // TimerVar = TimerVar + 1
165 // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
167 // ----------------------------------------------------------------------------
171 printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
174 subDoGameStuff(); // do all game stuff
177 printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
181 // ----------------------------------------------------------------------------
183 // Call subDisplayPlayingTime ' playing time on screen
185 subCheckRestoreRedDiskCountDisplay(); // Restore panel: red-disk hole
187 subRedDiskReleaseExplosion(); // Red Disk release and explode
188 subFollowUpExplosions(); // every explosion may cause up to 8 following explosions
190 bx = subCalculateScreenScrollPos(); // calculate screen start addrs
194 // Now new X and new Y are calculated, and bx = screen position = ScreenPosition
195 data_h_Ytmp = ScreenScrollYPos; // copy Y for next soft scroll
196 data_h_Xtmp = ScreenScrollXPos; // copy X for next soft scroll
197 if ((! UserDragFlag) && AutoScrollFlag)
200 printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
203 ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
207 if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
209 // (should never happen)
211 // printf("::: ForcedExitFlag == True\n");
213 goto locExitMainGameLoop;
216 if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
217 goto locExitMainGameLoop;
220 TimerVar = TimerVar + 1;
224 MainForm.SaveSnapshot(TimerVar);
227 // If Not NoDisplayFlag Then
228 // With MainForm.lblFrameCount
229 // .Caption = TimerVar
235 if (ExitToMenuFlag == 1)
237 // happens when demo ends or when Murphy enters exit (to be checked)
240 printf("::: ExitToMenuFlag == True\n");
244 goto locExitMainGameLoop;
248 if (ExitToMenuFlag == 1)
249 goto locExitMainGameLoop;
253 if (LeadOutCounter == 0) // no lead-out: game busy
254 return subMainGameLoop;
256 if (LeadOutCounter == 0) // no lead-out: game busy
257 goto locRepeatMainGameLoop;
260 // ----------------------------------------------------------------------------
261 // ---------------------- END OF GAME-BUSY LOOP -------------------------------
262 // ----------------------------------------------------------------------------
263 LeadOutCounter = LeadOutCounter - 1; // do more lead-out after quit
266 printf("::: LeadOutCounter == %d\n", LeadOutCounter);
270 if (LeadOutCounter != 0) // lead-out not ready: more
271 return subMainGameLoop;
273 if (LeadOutCounter != 0) // lead-out not ready: more
274 goto locRepeatMainGameLoop;
277 // lead-out done: exit now
278 // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
283 printf("::: locExitMainGameLoop reached [%d]\n", LeadOutCounter);
289 return subMainGameLoop;
296 DoEvents(); // user may klick on menus or move the window here ...
301 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
304 Stage.Blt(); // blit the last frame
308 MainForm.menStop_Click();
309 MainForm.PanelVisible = True;
312 // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
313 if (SavedGameFlag != 0) // after savegame: no update!
316 return subMainGameLoop;
320 if (UpdateTimeFlag == 0) // update time?
321 return subMainGameLoop;
323 if (UpdatedFlag == 0) // update playing time
324 subUpdatePlayingTime();
326 return subMainGameLoop;
331 int subMainGameLoop()
338 TickCountObject Clock;
345 printf("::: playing demo ...\n");
348 // EP set level success byte: demo, not game
350 EP_GameDemoVar0DAA = 0; // demo
355 printf("::: playing game ...\n");
358 // EP set level success byte: game, not demo
360 EP_GameDemoVar0DAA = 1; // game
364 // If RecordDemoFlag = 1 Then
365 // RecordDemoFlag = 0 ' clear Demo Recording flag
366 // Call subDisplayPlayingTime ' playing time on screen
367 // ' Record key still pressed?' >= (Ctrl-)F1 and <= (Ctrl-)F10
368 // While &H3B <= KeyScanCode7 And KeyScanCode7 <= &H44
369 // ' yes -> wait until released
370 // ' should we DoEvents here???? ... depends on how ... but yes!
371 // ' ...or we can rather poll the keyboardstate inside this loop???
373 // Call subInitGameConditions ' Init game conditions (vars)
374 // If MusicOnFlag = 0 Then Call subMusicInit
375 // WasDemoFlag = 0 ' no demo anymore
376 // EP_GameDemoVar0DAA = 1 ' force game
379 // This was a bug in the original Supaplex: sometimes red disks could not
380 // be released. This happened If Murphy was killed DURING a red disk release
381 // and the next try started.
383 RedDiskReleasePhase = 0; // (re-)enable red disk release
388 // ----------------------------------------------------------------------------
389 // --------------------- START OF GAME-BUSY LOOP ------------------------------
390 // ----------------------------------------------------------------------------
392 locRepeatMainGameLoop: // start repeating game loop
394 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
395 // FS synchronization
396 while (PauseMode != 0)
403 DoEvents(); // user may klick on menus or move the window here ...
408 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
411 // never any additional code between here!
413 LastFrame = Clock.TickNow(); // store the frame time
415 // never any additional code between here!
416 if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
419 // FS end of synchronization
420 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
422 goto locExitMainGameLoop;
424 // If DemoFlag = 0 Then Call subCheckJoystick ' check joystick
425 // bx = subCheckRightMouseButton() ' check (right) mouse button
426 // If bx = 2 And LeadOutCounter < 1 Then KillMurphyFlag = 1 ' lead-out busy after quit? -> kill Murphy!
428 // If DebugVersionFlag <> 0 Then ' debug mode on?
429 // If Data_SubRest <> 0 Then Data_SubRest = Data_SubRest - 1
430 // If keyEnter <> 0 Then GoTo loc_g_186F ' Was it the Enter key? -> yes--skip! No mouse!
431 // ' fixes ENTER bug If no mouse driver!
432 // If bx <> 1 Then GoTo loc_g_186F ' Left button=Init game field
433 // ' Also Enter If no mouse!
434 // If Data_SubRest <> 0 Then GoTo loc_g_186F
436 // Call subRestoreFancy
437 // Call subDisplayLevel ' Paint (Init) game field
438 // Call subConvertToEasySymbols ' Convert to easy symbols
443 subProcessKeyboardInput(); // Check keyboard, act on keys
446 // TimerVar = TimerVar + 1
450 // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
452 // ----------------------------------------------------------------------------
456 printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
459 subDoGameStuff(); // do all game stuff
462 printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
466 // ----------------------------------------------------------------------------
468 // Call subDisplayPlayingTime ' playing time on screen
470 subCheckRestoreRedDiskCountDisplay(); // Restore panel: red-disk hole
472 subRedDiskReleaseExplosion(); // Red Disk release and explode
473 subFollowUpExplosions(); // every explosion may cause up to 8 following explosions
475 bx = subCalculateScreenScrollPos(); // calculate screen start addrs
479 // Now new X and new Y are calculated, and bx = screen position = ScreenPosition
480 data_h_Ytmp = ScreenScrollYPos; // copy Y for next soft scroll
481 data_h_Xtmp = ScreenScrollXPos; // copy X for next soft scroll
482 if ((! UserDragFlag) && AutoScrollFlag)
485 printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
488 ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
491 if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
492 goto locExitMainGameLoop;
494 TimerVar = TimerVar + 1;
498 MainForm.SaveSnapshot(TimerVar);
501 // If Not NoDisplayFlag Then
502 // With MainForm.lblFrameCount
503 // .Caption = TimerVar
507 if (ExitToMenuFlag == 1)
508 goto locExitMainGameLoop;
510 if (LeadOutCounter == 0) // no lead-out: game busy
511 goto locRepeatMainGameLoop;
513 // ----------------------------------------------------------------------------
514 // ---------------------- END OF GAME-BUSY LOOP -------------------------------
515 // ----------------------------------------------------------------------------
516 LeadOutCounter = LeadOutCounter - 1; // do more lead-out after quit
517 if (LeadOutCounter != 0) // lead-out not ready: more
518 goto locRepeatMainGameLoop;
520 // lead-out done: exit now
521 // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
526 DoEvents(); // user may klick on menus or move the window here ...
531 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
534 Stage.Blt(); // blit the last frame
538 MainForm.menStop_Click();
539 MainForm.PanelVisible = True;
542 // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
543 if (SavedGameFlag != 0) // after savegame: no update!
546 return subMainGameLoop;
550 if (UpdateTimeFlag == 0) // update time?
551 return subMainGameLoop;
553 if (UpdatedFlag == 0) // update playing time
554 subUpdatePlayingTime();
557 return subMainGameLoop;
562 void subUpdatePlayingTime()
566 int subCalculateScreenScrollPos()
568 int subCalculateScreenScrollPos;
572 if (ExplosionShake != 0)
574 subGetRandomNumber();
578 ax = MainForm.picPane.Width / 2;
579 Ay = MainForm.picPane.Height / 2;
583 ScreenScrollXPos = Stretch * (MurphyScreenXPos + 16) - ax;
584 ScreenScrollYPos = Stretch * (MurphyScreenYPos + 16) - Ay;
586 ScreenScrollXPos = Stretch * (MurphyScreenXPos + 8) - ax;
587 ScreenScrollYPos = Stretch * (MurphyScreenYPos + 8) - Ay;
591 printf("::: MainGameLoop.c: subCalculateScreenScrollPos(): %d, %d [%d, %d] -> %d, %d\n",
592 MainForm.picPane.Width, MainForm.picPane.Height,
593 MurphyScreenXPos, MurphyScreenYPos,
594 ScreenScrollXPos, ScreenScrollYPos);
597 return subCalculateScreenScrollPos;