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 // ----------------------------------------------------------------------------
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(); // 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;
329 int subMainGameLoop()
336 TickCountObject Clock;
343 printf("::: playing demo ...\n");
346 // EP set level success byte: demo, not game
348 EP_GameDemoVar0DAA = 0; // demo
353 printf("::: playing game ...\n");
356 // EP set level success byte: game, not demo
358 EP_GameDemoVar0DAA = 1; // game
362 // If RecordDemoFlag = 1 Then
363 // RecordDemoFlag = 0 ' clear Demo Recording flag
364 // Call subDisplayPlayingTime ' playing time on screen
365 // ' Record key still pressed?' >= (Ctrl-)F1 and <= (Ctrl-)F10
366 // While &H3B <= KeyScanCode7 And KeyScanCode7 <= &H44
367 // ' yes -> wait until released
368 // ' should we DoEvents here???? ... depends on how ... but yes!
369 // ' ...or we can rather poll the keyboardstate inside this loop???
371 // Call subInitGameConditions ' Init game conditions (vars)
372 // If MusicOnFlag = 0 Then Call subMusicInit
373 // WasDemoFlag = 0 ' no demo anymore
374 // EP_GameDemoVar0DAA = 1 ' force game
377 // This was a bug in the original Supaplex: sometimes red disks could not
378 // be released. This happened If Murphy was killed DURING a red disk release
379 // and the next try started.
381 RedDiskReleasePhase = 0; // (re-)enable red disk release
386 // ----------------------------------------------------------------------------
387 // --------------------- START OF GAME-BUSY LOOP ------------------------------
388 // ----------------------------------------------------------------------------
390 locRepeatMainGameLoop: // start repeating game loop
392 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
393 // FS synchronization
394 while (PauseMode != 0)
401 DoEvents(); // user may klick on menus or move the window here ...
406 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
409 // never any additional code between here!
411 LastFrame = Clock.TickNow(); // store the frame time
413 // never any additional code between here!
414 if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
417 // FS end of synchronization
418 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
420 goto locExitMainGameLoop;
422 // If DemoFlag = 0 Then Call subCheckJoystick ' check joystick
423 // bx = subCheckRightMouseButton() ' check (right) mouse button
424 // If bx = 2 And LeadOutCounter < 1 Then KillMurphyFlag = 1 ' lead-out busy after quit? -> kill Murphy!
426 // If DebugVersionFlag <> 0 Then ' debug mode on?
427 // If Data_SubRest <> 0 Then Data_SubRest = Data_SubRest - 1
428 // If keyEnter <> 0 Then GoTo loc_g_186F ' Was it the Enter key? -> yes--skip! No mouse!
429 // ' fixes ENTER bug If no mouse driver!
430 // If bx <> 1 Then GoTo loc_g_186F ' Left button=Init game field
431 // ' Also Enter If no mouse!
432 // If Data_SubRest <> 0 Then GoTo loc_g_186F
434 // Call subRestoreFancy
435 // Call subDisplayLevel ' Paint (Init) game field
436 // Call subConvertToEasySymbols ' Convert to easy symbols
441 subProcessKeyboardInput(); // Check keyboard, act on keys
444 // TimerVar = TimerVar + 1
448 // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
450 // ----------------------------------------------------------------------------
454 printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
457 subDoGameStuff(); // do all game stuff
460 printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
464 // ----------------------------------------------------------------------------
466 // Call subDisplayPlayingTime ' playing time on screen
468 subCheckRestoreRedDiskCountDisplay(); // Restore panel: red-disk hole
470 subRedDiskReleaseExplosion(); // Red Disk release and explode
471 subFollowUpExplosions(); // every explosion may cause up to 8 following explosions
473 bx = subCalculateScreenScrollPos(); // calculate screen start addrs
477 // Now new X and new Y are calculated, and bx = screen position = ScreenPosition
478 data_h_Ytmp = ScreenScrollYPos; // copy Y for next soft scroll
479 data_h_Xtmp = ScreenScrollXPos; // copy X for next soft scroll
480 if ((! UserDragFlag) && AutoScrollFlag)
483 printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
486 ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
489 if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
490 goto locExitMainGameLoop;
492 TimerVar = TimerVar + 1;
496 MainForm.SaveSnapshot(TimerVar);
499 // If Not NoDisplayFlag Then
500 // With MainForm.lblFrameCount
501 // .Caption = TimerVar
505 if (ExitToMenuFlag == 1)
506 goto locExitMainGameLoop;
508 if (LeadOutCounter == 0) // no lead-out: game busy
509 goto locRepeatMainGameLoop;
511 // ----------------------------------------------------------------------------
512 // ---------------------- END OF GAME-BUSY LOOP -------------------------------
513 // ----------------------------------------------------------------------------
514 LeadOutCounter = LeadOutCounter - 1; // do more lead-out after quit
515 if (LeadOutCounter != 0) // lead-out not ready: more
516 goto locRepeatMainGameLoop;
518 // lead-out done: exit now
519 // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
524 DoEvents(); // user may klick on menus or move the window here ...
529 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
532 Stage.Blt(); // blit the last frame
536 MainForm.menStop_Click();
537 MainForm.PanelVisible = True;
540 // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
541 if (SavedGameFlag != 0) // after savegame: no update!
544 return subMainGameLoop;
548 if (UpdateTimeFlag == 0) // update time?
549 return subMainGameLoop;
551 if (UpdatedFlag == 0) // update playing time
552 subUpdatePlayingTime();
555 return subMainGameLoop;
558 void subUpdatePlayingTime()
562 int subCalculateScreenScrollPos()
564 int subCalculateScreenScrollPos;
568 if (ExplosionShake != 0)
570 subGetRandomNumber();
574 ax = MainForm.picPane.Width / 2;
575 Ay = MainForm.picPane.Height / 2;
579 ScreenScrollXPos = Stretch * (MurphyScreenXPos + 16) - ax;
580 ScreenScrollYPos = Stretch * (MurphyScreenYPos + 16) - Ay;
582 ScreenScrollXPos = Stretch * (MurphyScreenXPos + 8) - ax;
583 ScreenScrollYPos = Stretch * (MurphyScreenYPos + 8) - Ay;
587 printf("::: MainGameLoop.c: subCalculateScreenScrollPos(): %d, %d [%d, %d] -> %d, %d\n",
588 MainForm.picPane.Width, MainForm.picPane.Height,
589 MurphyScreenXPos, MurphyScreenYPos,
590 ScreenScrollXPos, ScreenScrollYPos);
593 return subCalculateScreenScrollPos;