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");
243 goto locExitMainGameLoop;
246 if (ExitToMenuFlag == 1)
247 goto locExitMainGameLoop;
251 if (LeadOutCounter == 0) // no lead-out: game busy
252 return subMainGameLoop;
254 if (LeadOutCounter == 0) // no lead-out: game busy
255 goto locRepeatMainGameLoop;
258 // ----------------------------------------------------------------------------
259 // ---------------------- END OF GAME-BUSY LOOP -------------------------------
260 // ----------------------------------------------------------------------------
261 LeadOutCounter = LeadOutCounter - 1; // do more lead-out after quit
263 if (LeadOutCounter != 0) // lead-out not ready: more
264 return subMainGameLoop;
266 if (LeadOutCounter != 0) // lead-out not ready: more
267 goto locRepeatMainGameLoop;
270 // lead-out done: exit now
271 // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
278 return subMainGameLoop;
284 printf("::: locExitMainGameLoop reached [%d]\n", LeadOutCounter);
289 DoEvents(); // user may klick on menus or move the window here ...
294 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
297 Stage.Blt(); // blit the last frame
301 MainForm.menStop_Click();
302 MainForm.PanelVisible = True;
305 // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
306 if (SavedGameFlag != 0) // after savegame: no update!
309 return subMainGameLoop;
313 if (UpdateTimeFlag == 0) // update time?
314 return subMainGameLoop;
316 if (UpdatedFlag == 0) // update playing time
317 subUpdatePlayingTime();
319 return subMainGameLoop;
322 int subMainGameLoop()
329 TickCountObject Clock;
336 printf("::: playing demo ...\n");
339 // EP set level success byte: demo, not game
341 EP_GameDemoVar0DAA = 0; // demo
346 printf("::: playing game ...\n");
349 // EP set level success byte: game, not demo
351 EP_GameDemoVar0DAA = 1; // game
355 // If RecordDemoFlag = 1 Then
356 // RecordDemoFlag = 0 ' clear Demo Recording flag
357 // Call subDisplayPlayingTime ' playing time on screen
358 // ' Record key still pressed?' >= (Ctrl-)F1 and <= (Ctrl-)F10
359 // While &H3B <= KeyScanCode7 And KeyScanCode7 <= &H44
360 // ' yes -> wait until released
361 // ' should we DoEvents here???? ... depends on how ... but yes!
362 // ' ...or we can rather poll the keyboardstate inside this loop???
364 // Call subInitGameConditions ' Init game conditions (vars)
365 // If MusicOnFlag = 0 Then Call subMusicInit
366 // WasDemoFlag = 0 ' no demo anymore
367 // EP_GameDemoVar0DAA = 1 ' force game
370 // This was a bug in the original Supaplex: sometimes red disks could not
371 // be released. This happened If Murphy was killed DURING a red disk release
372 // and the next try started.
374 RedDiskReleasePhase = 0; // (re-)enable red disk release
379 // ----------------------------------------------------------------------------
380 // --------------------- START OF GAME-BUSY LOOP ------------------------------
381 // ----------------------------------------------------------------------------
383 locRepeatMainGameLoop: // start repeating game loop
385 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
386 // FS synchronization
387 while (PauseMode != 0)
394 DoEvents(); // user may klick on menus or move the window here ...
399 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
402 // never any additional code between here!
404 LastFrame = Clock.TickNow(); // store the frame time
406 // never any additional code between here!
407 if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
410 // FS end of synchronization
411 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
413 goto locExitMainGameLoop;
415 // If DemoFlag = 0 Then Call subCheckJoystick ' check joystick
416 // bx = subCheckRightMouseButton() ' check (right) mouse button
417 // If bx = 2 And LeadOutCounter < 1 Then KillMurphyFlag = 1 ' lead-out busy after quit? -> kill Murphy!
419 // If DebugVersionFlag <> 0 Then ' debug mode on?
420 // If Data_SubRest <> 0 Then Data_SubRest = Data_SubRest - 1
421 // If keyEnter <> 0 Then GoTo loc_g_186F ' Was it the Enter key? -> yes--skip! No mouse!
422 // ' fixes ENTER bug If no mouse driver!
423 // If bx <> 1 Then GoTo loc_g_186F ' Left button=Init game field
424 // ' Also Enter If no mouse!
425 // If Data_SubRest <> 0 Then GoTo loc_g_186F
427 // Call subRestoreFancy
428 // Call subDisplayLevel ' Paint (Init) game field
429 // Call subConvertToEasySymbols ' Convert to easy symbols
434 subProcessKeyboardInput(); // Check keyboard, act on keys
437 // TimerVar = TimerVar + 1
441 // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
443 // ----------------------------------------------------------------------------
447 printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
450 subDoGameStuff(); // do all game stuff
453 printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
457 // ----------------------------------------------------------------------------
459 // Call subDisplayPlayingTime ' playing time on screen
461 subCheckRestoreRedDiskCountDisplay(); // Restore panel: red-disk hole
463 subRedDiskReleaseExplosion(); // Red Disk release and explode
464 subFollowUpExplosions(); // every explosion may cause up to 8 following explosions
466 bx = subCalculateScreenScrollPos(); // calculate screen start addrs
470 // Now new X and new Y are calculated, and bx = screen position = ScreenPosition
471 data_h_Ytmp = ScreenScrollYPos; // copy Y for next soft scroll
472 data_h_Xtmp = ScreenScrollXPos; // copy X for next soft scroll
473 if ((! UserDragFlag) && AutoScrollFlag)
476 printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
479 ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
482 if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
483 goto locExitMainGameLoop;
485 TimerVar = TimerVar + 1;
489 MainForm.SaveSnapshot(TimerVar);
492 // If Not NoDisplayFlag Then
493 // With MainForm.lblFrameCount
494 // .Caption = TimerVar
498 if (ExitToMenuFlag == 1)
499 goto locExitMainGameLoop;
501 if (LeadOutCounter == 0) // no lead-out: game busy
502 goto locRepeatMainGameLoop;
504 // ----------------------------------------------------------------------------
505 // ---------------------- END OF GAME-BUSY LOOP -------------------------------
506 // ----------------------------------------------------------------------------
507 LeadOutCounter = LeadOutCounter - 1; // do more lead-out after quit
508 if (LeadOutCounter != 0) // lead-out not ready: more
509 goto locRepeatMainGameLoop;
511 // lead-out done: exit now
512 // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
517 DoEvents(); // user may klick on menus or move the window here ...
522 while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
525 Stage.Blt(); // blit the last frame
529 MainForm.menStop_Click();
530 MainForm.PanelVisible = True;
533 // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
534 if (SavedGameFlag != 0) // after savegame: no update!
537 return subMainGameLoop;
541 if (UpdateTimeFlag == 0) // update time?
542 return subMainGameLoop;
544 if (UpdatedFlag == 0) // update playing time
545 subUpdatePlayingTime();
548 return subMainGameLoop;
551 void subUpdatePlayingTime()
555 int subCalculateScreenScrollPos()
557 int subCalculateScreenScrollPos;
561 if (ExplosionShake != 0)
563 subGetRandomNumber();
567 ax = MainForm.picPane.Width / 2;
568 Ay = MainForm.picPane.Height / 2;
572 ScreenScrollXPos = Stretch * (MurphyScreenXPos + 16) - ax;
573 ScreenScrollYPos = Stretch * (MurphyScreenYPos + 16) - Ay;
575 ScreenScrollXPos = Stretch * (MurphyScreenXPos + 8) - ax;
576 ScreenScrollYPos = Stretch * (MurphyScreenYPos + 8) - Ay;
580 printf("::: MainGameLoop.c: subCalculateScreenScrollPos(): %d, %d [%d, %d] -> %d, %d\n",
581 MainForm.picPane.Width, MainForm.picPane.Height,
582 MurphyScreenXPos, MurphyScreenYPos,
583 ScreenScrollXPos, ScreenScrollYPos);
586 return subCalculateScreenScrollPos;