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()
90 // ----------------------------------------------------------------------------
91 // --------------------- START OF GAME-BUSY LOOP ------------------------------
92 // ----------------------------------------------------------------------------
94 locRepeatMainGameLoop: // start repeating game loop
96 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
98 while (PauseMode != 0)
105 DoEvents(); // user may klick on menus or move the window here ...
110 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
113 // never any additional code between here!
115 LastFrame = Clock.TickNow(); // store the frame time
117 // never any additional code between here!
118 if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
121 // FS end of synchronization
122 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
124 goto locExitMainGameLoop;
126 // If DemoFlag = 0 Then Call subCheckJoystick ' check joystick
127 // bx = subCheckRightMouseButton() ' check (right) mouse button
128 // If bx = 2 And LeadOutCounter < 1 Then KillMurphyFlag = 1 ' lead-out busy after quit? -> kill Murphy!
130 // If DebugVersionFlag <> 0 Then ' debug mode on?
131 // If Data_SubRest <> 0 Then Data_SubRest = Data_SubRest - 1
132 // If keyEnter <> 0 Then GoTo loc_g_186F ' Was it the Enter key? -> yes--skip! No mouse!
133 // ' fixes ENTER bug If no mouse driver!
134 // If bx <> 1 Then GoTo loc_g_186F ' Left button=Init game field
135 // ' Also Enter If no mouse!
136 // If Data_SubRest <> 0 Then GoTo loc_g_186F
138 // Call subRestoreFancy
139 // Call subDisplayLevel ' Paint (Init) game field
140 // Call subConvertToEasySymbols ' Convert to easy symbols
145 subProcessKeyboardInput(); // Check keyboard, act on keys
148 // TimerVar = TimerVar + 1
152 // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
154 // ----------------------------------------------------------------------------
158 printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
161 subDoGameStuff(); // do all game stuff
164 printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
168 // ----------------------------------------------------------------------------
170 // Call subDisplayPlayingTime ' playing time on screen
172 subCheckRestoreRedDiskCountDisplay(); // Restore panel: red-disk hole
174 subRedDiskReleaseExplosion(); // Red Disk release and explode
175 subFollowUpExplosions(); // every explosion may cause up to 8 following explosions
177 bx = subCalculateScreenScrollPos(); // calculate screen start addrs
181 // Now new X and new Y are calculated, and bx = screen position = ScreenPosition
182 data_h_Ytmp = ScreenScrollYPos; // copy Y for next soft scroll
183 data_h_Xtmp = ScreenScrollXPos; // copy X for next soft scroll
184 if ((! UserDragFlag) && AutoScrollFlag)
187 printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
190 ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
193 if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
194 goto locExitMainGameLoop;
196 TimerVar = TimerVar + 1;
200 MainForm.SaveSnapshot(TimerVar);
203 // If Not NoDisplayFlag Then
204 // With MainForm.lblFrameCount
205 // .Caption = TimerVar
209 if (ExitToMenuFlag == 1)
210 goto locExitMainGameLoop;
212 if (LeadOutCounter == 0) // no lead-out: game busy
213 goto locRepeatMainGameLoop;
215 // ----------------------------------------------------------------------------
216 // ---------------------- END OF GAME-BUSY LOOP -------------------------------
217 // ----------------------------------------------------------------------------
218 LeadOutCounter = LeadOutCounter - 1; // do more lead-out after quit
219 if (LeadOutCounter != 0) // lead-out not ready: more
220 goto locRepeatMainGameLoop;
222 // lead-out done: exit now
223 // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
228 DoEvents(); // user may klick on menus or move the window here ...
233 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
236 Stage.Blt(); // blit the last frame
240 MainForm.menStop_Click();
241 MainForm.PanelVisible = True;
244 // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
245 if (SavedGameFlag != 0) // after savegame: no update!
248 return subMainGameLoop;
252 if (UpdateTimeFlag == 0) // update time?
253 return subMainGameLoop;
255 if (UpdatedFlag == 0) // update playing time
256 subUpdatePlayingTime();
259 return subMainGameLoop;
262 int subMainGameLoop()
269 TickCountObject Clock;
276 printf("::: playing demo ...\n");
279 // EP set level success byte: demo, not game
281 EP_GameDemoVar0DAA = 0; // demo
286 printf("::: playing game ...\n");
289 // EP set level success byte: game, not demo
291 EP_GameDemoVar0DAA = 1; // game
295 // If RecordDemoFlag = 1 Then
296 // RecordDemoFlag = 0 ' clear Demo Recording flag
297 // Call subDisplayPlayingTime ' playing time on screen
298 // ' Record key still pressed?' >= (Ctrl-)F1 and <= (Ctrl-)F10
299 // While &H3B <= KeyScanCode7 And KeyScanCode7 <= &H44
300 // ' yes -> wait until released
301 // ' should we DoEvents here???? ... depends on how ... but yes!
302 // ' ...or we can rather poll the keyboardstate inside this loop???
304 // Call subInitGameConditions ' Init game conditions (vars)
305 // If MusicOnFlag = 0 Then Call subMusicInit
306 // WasDemoFlag = 0 ' no demo anymore
307 // EP_GameDemoVar0DAA = 1 ' force game
310 // This was a bug in the original Supaplex: sometimes red disks could not
311 // be released. This happened If Murphy was killed DURING a red disk release
312 // and the next try started.
314 RedDiskReleasePhase = 0; // (re-)enable red disk release
319 // ----------------------------------------------------------------------------
320 // --------------------- START OF GAME-BUSY LOOP ------------------------------
321 // ----------------------------------------------------------------------------
323 locRepeatMainGameLoop: // start repeating game loop
325 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
326 // FS synchronization
327 while (PauseMode != 0)
334 DoEvents(); // user may klick on menus or move the window here ...
339 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
342 // never any additional code between here!
344 LastFrame = Clock.TickNow(); // store the frame time
346 // never any additional code between here!
347 if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
350 // FS end of synchronization
351 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
353 goto locExitMainGameLoop;
355 // If DemoFlag = 0 Then Call subCheckJoystick ' check joystick
356 // bx = subCheckRightMouseButton() ' check (right) mouse button
357 // If bx = 2 And LeadOutCounter < 1 Then KillMurphyFlag = 1 ' lead-out busy after quit? -> kill Murphy!
359 // If DebugVersionFlag <> 0 Then ' debug mode on?
360 // If Data_SubRest <> 0 Then Data_SubRest = Data_SubRest - 1
361 // If keyEnter <> 0 Then GoTo loc_g_186F ' Was it the Enter key? -> yes--skip! No mouse!
362 // ' fixes ENTER bug If no mouse driver!
363 // If bx <> 1 Then GoTo loc_g_186F ' Left button=Init game field
364 // ' Also Enter If no mouse!
365 // If Data_SubRest <> 0 Then GoTo loc_g_186F
367 // Call subRestoreFancy
368 // Call subDisplayLevel ' Paint (Init) game field
369 // Call subConvertToEasySymbols ' Convert to easy symbols
374 subProcessKeyboardInput(); // Check keyboard, act on keys
377 // TimerVar = TimerVar + 1
381 // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
383 // ----------------------------------------------------------------------------
387 printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
390 subDoGameStuff(); // do all game stuff
393 printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
397 // ----------------------------------------------------------------------------
399 // Call subDisplayPlayingTime ' playing time on screen
401 subCheckRestoreRedDiskCountDisplay(); // Restore panel: red-disk hole
403 subRedDiskReleaseExplosion(); // Red Disk release and explode
404 subFollowUpExplosions(); // every explosion may cause up to 8 following explosions
406 bx = subCalculateScreenScrollPos(); // calculate screen start addrs
410 // Now new X and new Y are calculated, and bx = screen position = ScreenPosition
411 data_h_Ytmp = ScreenScrollYPos; // copy Y for next soft scroll
412 data_h_Xtmp = ScreenScrollXPos; // copy X for next soft scroll
413 if ((! UserDragFlag) && AutoScrollFlag)
416 printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
419 ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
422 if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
423 goto locExitMainGameLoop;
425 TimerVar = TimerVar + 1;
429 MainForm.SaveSnapshot(TimerVar);
432 // If Not NoDisplayFlag Then
433 // With MainForm.lblFrameCount
434 // .Caption = TimerVar
438 if (ExitToMenuFlag == 1)
439 goto locExitMainGameLoop;
441 if (LeadOutCounter == 0) // no lead-out: game busy
442 goto locRepeatMainGameLoop;
444 // ----------------------------------------------------------------------------
445 // ---------------------- END OF GAME-BUSY LOOP -------------------------------
446 // ----------------------------------------------------------------------------
447 LeadOutCounter = LeadOutCounter - 1; // do more lead-out after quit
448 if (LeadOutCounter != 0) // lead-out not ready: more
449 goto locRepeatMainGameLoop;
451 // lead-out done: exit now
452 // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
457 DoEvents(); // user may klick on menus or move the window here ...
462 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
465 Stage.Blt(); // blit the last frame
469 MainForm.menStop_Click();
470 MainForm.PanelVisible = True;
473 // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
474 if (SavedGameFlag != 0) // after savegame: no update!
477 return subMainGameLoop;
481 if (UpdateTimeFlag == 0) // update time?
482 return subMainGameLoop;
484 if (UpdatedFlag == 0) // update playing time
485 subUpdatePlayingTime();
488 return subMainGameLoop;
491 void subUpdatePlayingTime()
495 int subCalculateScreenScrollPos()
497 int subCalculateScreenScrollPos;
501 if (ExplosionShake != 0)
503 subGetRandomNumber();
507 ax = MainForm.picPane.Width / 2;
508 Ay = MainForm.picPane.Height / 2;
512 ScreenScrollXPos = Stretch * (MurphyScreenXPos + 16) - ax;
513 ScreenScrollYPos = Stretch * (MurphyScreenYPos + 16) - Ay;
515 ScreenScrollXPos = Stretch * (MurphyScreenXPos + 8) - ax;
516 ScreenScrollYPos = Stretch * (MurphyScreenYPos + 8) - Ay;
520 printf("::: MainGameLoop.c: subCalculateScreenScrollPos(): %d, %d [%d, %d] -> %d, %d\n",
521 MainForm.picPane.Width, MainForm.picPane.Height,
522 MurphyScreenXPos, MurphyScreenYPos,
523 ScreenScrollXPos, ScreenScrollYPos);
526 return subCalculateScreenScrollPos;