2baa7435cc542dd2f258e55ad3a15b99b8971fe0
[rocksndiamonds.git] / src / game_sp / MainGameLoop.c
1 // ----------------------------------------------------------------------------
2 // MainGameLoop.c
3 // ----------------------------------------------------------------------------
4
5 #include "MainGameLoop.h"
6
7 // static char *VB_Name = "modMainGameLoop";
8
9 // --- Option Explicit
10
11 int GameLoopRunning;
12 boolean bPlaying;
13 int LeadOutCounter, EnterRepeatCounter;
14 int ForcedExitFlag;
15 int ExitToMenuFlag;
16 int SavedGameFlag;
17 boolean UserDragFlag;
18 boolean AutoScrollFlag;
19
20 // ==========================================================================
21 //                              SUBROUTINE
22 // Play a game/demo
23 // ==========================================================================
24
25 int subMainGameLoop_Init()
26 {
27   int subMainGameLoop;
28
29   // int al, bx;
30   // int bx;
31 #if 0
32   TickCountObject Clock;
33   currency LastFrame;
34 #endif
35
36   if (DemoFlag != 0)
37   {
38 #if 1
39     printf("::: playing demo ...\n");
40 #endif
41
42     // EP set level success byte: demo, not game
43     WasDemoFlag = 1;
44     EP_GameDemoVar0DAA = 0; // demo
45   }
46   else // loc_g_1836:
47   {
48 #if 1
49     printf("::: playing game ...\n");
50 #endif
51
52     // EP set level success byte: game, not demo
53     WasDemoFlag = 0;
54     EP_GameDemoVar0DAA = 1; // game
55   }
56
57   // RestartGameLoop:
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???
66   //    Wend
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
71   //  End If
72
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.
76
77   RedDiskReleasePhase = 0; // (re-)enable red disk release
78   UpdatedFlag = 0;
79   GameLoopRunning = 1;
80   LevelStatus = 0;
81
82   return subMainGameLoop;
83 }
84
85 int subMainGameLoop_Main()
86 {
87   int subMainGameLoop;
88   int bx;
89
90   // ----------------------------------------------------------------------------
91   // --------------------- START OF GAME-BUSY LOOP ------------------------------
92   // ----------------------------------------------------------------------------
93
94 #if 0
95 locRepeatMainGameLoop:                           // start repeating game loop
96 #endif
97
98   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99   // FS   synchronization
100   while (PauseMode != 0)
101   {
102     DoEvents();
103   }
104
105   do
106   {
107     DoEvents(); // user may klick on menus or move the window here ...
108   }
109 #if 1
110   while (0);
111 #else
112   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
113 #endif
114
115   //   never any additional code between here!
116 #if 0
117   LastFrame = Clock.TickNow(); // store the frame time
118 #endif
119   //   never any additional code between here!
120   if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
121     Stage.Blt();
122
123   // FS   end of synchronization
124   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
125 #if 1
126   if (EndFlag)
127   {
128     // (should never happen)
129
130     // printf("::: EndFlag == True\n");
131
132     goto locExitMainGameLoop;
133   }
134 #else
135   if (EndFlag)
136     goto locExitMainGameLoop;
137 #endif
138
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!
142
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
150   //    Data_SubRest = 10
151   //    Call subRestoreFancy
152   //    Call subDisplayLevel         ' Paint (Init) game field
153   //    Call subConvertToEasySymbols ' Convert to easy symbols
154   //  End If
155
156   // loc_g_186F:
157
158   subProcessKeyboardInput();                 // Check keyboard, act on keys
159
160   // 'HACK:
161   //  TimerVar = TimerVar + 1
162   //  DoEvents
163   //  GoTo loc_g_186F
164   // 'END HACK
165   // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
166
167   // ----------------------------------------------------------------------------
168   //
169
170 #if 0
171   printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
172 #endif
173
174   subDoGameStuff();                 // do all game stuff
175
176 #if 0
177   printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
178 #endif
179
180   //
181   // ----------------------------------------------------------------------------
182
183   //  Call subDisplayPlayingTime                 ' playing time on screen
184
185   subCheckRestoreRedDiskCountDisplay();    // Restore panel: red-disk hole
186
187   subRedDiskReleaseExplosion();       // Red Disk release and explode
188   subFollowUpExplosions();  // every explosion may cause up to 8 following explosions
189
190   bx = subCalculateScreenScrollPos();     // calculate screen start addrs
191
192   ScreenPosition = bx;
193
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)
198   {
199 #if 0
200     printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
201 #endif
202
203     ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
204   }
205
206 #if 1
207   if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
208   {
209     // (should never happen)
210
211     // printf("::: ForcedExitFlag == True\n");
212
213     goto locExitMainGameLoop;
214   }
215 #else
216   if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
217     goto locExitMainGameLoop;
218 #endif
219
220   TimerVar = TimerVar + 1;
221
222 #if 0
223   if (bCapturePane)
224     MainForm.SaveSnapshot(TimerVar);
225 #endif
226
227   //  If Not NoDisplayFlag Then
228   //    With MainForm.lblFrameCount
229   //      .Caption = TimerVar
230   //      .Refresh
231   //    End With
232   //  End If
233
234 #if 1
235   if (ExitToMenuFlag == 1)
236   {
237     // happens when demo ends or when Murphy enters exit (to be checked)
238
239 #if 0
240     printf("::: ExitToMenuFlag == True\n");
241 #endif
242
243 #if 0
244     goto locExitMainGameLoop;
245 #endif
246   }
247 #else
248   if (ExitToMenuFlag == 1)
249     goto locExitMainGameLoop;
250 #endif
251
252 #if 1
253   if (LeadOutCounter == 0) // no lead-out: game busy
254     return subMainGameLoop;
255 #else
256   if (LeadOutCounter == 0) // no lead-out: game busy
257     goto locRepeatMainGameLoop;
258 #endif
259
260   // ----------------------------------------------------------------------------
261   // ---------------------- END OF GAME-BUSY LOOP -------------------------------
262   // ----------------------------------------------------------------------------
263   LeadOutCounter = LeadOutCounter - 1;             // do more lead-out after quit
264
265 #if 0
266   printf("::: LeadOutCounter == %d\n", LeadOutCounter);
267 #endif
268
269 #if 1
270   if (LeadOutCounter != 0) // lead-out not ready: more
271     return subMainGameLoop;
272 #else
273   if (LeadOutCounter != 0) // lead-out not ready: more
274     goto locRepeatMainGameLoop;
275 #endif
276
277   // lead-out done: exit now
278   // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
279
280 locExitMainGameLoop:
281
282 #if 1
283   printf("::: locExitMainGameLoop reached [%d]\n", LeadOutCounter);
284 #endif
285
286
287
288 #if 1
289   return subMainGameLoop;
290 #endif
291
292
293
294   do
295   {
296     DoEvents(); // user may klick on menus or move the window here ...
297   }
298 #if 1
299   while (0);
300 #else
301   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
302 #endif
303
304   Stage.Blt(); // blit the last frame
305   GameLoopRunning = 0;
306
307 #if 0
308   MainForm.menStop_Click();
309   MainForm.PanelVisible = True;
310 #endif
311
312   // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
313   if (SavedGameFlag != 0) // after savegame: no update!
314   {
315     SavedGameFlag = 0;
316     return subMainGameLoop;
317   }
318
319   SavedGameFlag = 0;
320   if (UpdateTimeFlag == 0) // update time?
321     return subMainGameLoop;
322
323   if (UpdatedFlag == 0) // update playing time
324     subUpdatePlayingTime();
325
326   return subMainGameLoop;
327 } // subMainGameLoop
328
329 int subMainGameLoop()
330 {
331   int subMainGameLoop;
332
333   // int al, bx;
334   int bx;
335 #if 0
336   TickCountObject Clock;
337   currency LastFrame;
338 #endif
339
340   if (DemoFlag != 0)
341   {
342 #if 1
343     printf("::: playing demo ...\n");
344 #endif
345
346     // EP set level success byte: demo, not game
347     WasDemoFlag = 1;
348     EP_GameDemoVar0DAA = 0; // demo
349   }
350   else // loc_g_1836:
351   {
352 #if 1
353     printf("::: playing game ...\n");
354 #endif
355
356     // EP set level success byte: game, not demo
357     WasDemoFlag = 0;
358     EP_GameDemoVar0DAA = 1; // game
359   }
360
361   // RestartGameLoop:
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???
370   //    Wend
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
375   //  End If
376
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.
380
381   RedDiskReleasePhase = 0; // (re-)enable red disk release
382   UpdatedFlag = 0;
383   GameLoopRunning = 1;
384   LevelStatus = 0;
385
386   // ----------------------------------------------------------------------------
387   // --------------------- START OF GAME-BUSY LOOP ------------------------------
388   // ----------------------------------------------------------------------------
389
390 locRepeatMainGameLoop:                           // start repeating game loop
391
392   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
393   // FS   synchronization
394   while (PauseMode != 0)
395   {
396     DoEvents();
397   }
398
399   do
400   {
401     DoEvents(); // user may klick on menus or move the window here ...
402   }
403 #if 1
404   while (0);
405 #else
406   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
407 #endif
408
409   //   never any additional code between here!
410 #if 0
411   LastFrame = Clock.TickNow(); // store the frame time
412 #endif
413   //   never any additional code between here!
414   if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
415     Stage.Blt();
416
417   // FS   end of synchronization
418   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
419   if (EndFlag)
420     goto locExitMainGameLoop;
421
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!
425
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
433   //    Data_SubRest = 10
434   //    Call subRestoreFancy
435   //    Call subDisplayLevel         ' Paint (Init) game field
436   //    Call subConvertToEasySymbols ' Convert to easy symbols
437   //  End If
438
439   // loc_g_186F:
440
441   subProcessKeyboardInput();                 // Check keyboard, act on keys
442
443   // 'HACK:
444   //  TimerVar = TimerVar + 1
445   //  DoEvents
446   //  GoTo loc_g_186F
447   // 'END HACK
448   // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
449
450   // ----------------------------------------------------------------------------
451   //
452
453 #if 0
454   printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
455 #endif
456
457   subDoGameStuff();                 // do all game stuff
458
459 #if 0
460   printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
461 #endif
462
463   //
464   // ----------------------------------------------------------------------------
465
466   //  Call subDisplayPlayingTime                 ' playing time on screen
467
468   subCheckRestoreRedDiskCountDisplay();    // Restore panel: red-disk hole
469
470   subRedDiskReleaseExplosion();       // Red Disk release and explode
471   subFollowUpExplosions();  // every explosion may cause up to 8 following explosions
472
473   bx = subCalculateScreenScrollPos();     // calculate screen start addrs
474
475   ScreenPosition = bx;
476
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)
481   {
482 #if 0
483     printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
484 #endif
485
486     ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
487   }
488
489   if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
490     goto locExitMainGameLoop;
491
492   TimerVar = TimerVar + 1;
493
494 #if 0
495   if (bCapturePane)
496     MainForm.SaveSnapshot(TimerVar);
497 #endif
498
499   //  If Not NoDisplayFlag Then
500   //    With MainForm.lblFrameCount
501   //      .Caption = TimerVar
502   //      .Refresh
503   //    End With
504   //  End If
505   if (ExitToMenuFlag == 1)
506     goto locExitMainGameLoop;
507
508   if (LeadOutCounter == 0) // no lead-out: game busy
509     goto locRepeatMainGameLoop;
510
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;
517
518   // lead-out done: exit now
519   // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
520
521 locExitMainGameLoop:
522   do
523   {
524     DoEvents(); // user may klick on menus or move the window here ...
525   }
526 #if 1
527   while (0);
528 #else
529   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
530 #endif
531
532   Stage.Blt(); // blit the last frame
533   GameLoopRunning = 0;
534
535 #if 0
536   MainForm.menStop_Click();
537   MainForm.PanelVisible = True;
538 #endif
539
540   // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
541   if (SavedGameFlag != 0) // after savegame: no update!
542   {
543     SavedGameFlag = 0;
544     return subMainGameLoop;
545   }
546
547   SavedGameFlag = 0;
548   if (UpdateTimeFlag == 0) // update time?
549     return subMainGameLoop;
550
551   if (UpdatedFlag == 0) // update playing time
552     subUpdatePlayingTime();
553
554
555   return subMainGameLoop;
556 } // subMainGameLoop
557
558 void subUpdatePlayingTime()
559 {
560 }
561
562 int subCalculateScreenScrollPos()
563 {
564   int subCalculateScreenScrollPos;
565
566   int ax, Ay;
567
568   if (ExplosionShake != 0)
569   {
570     subGetRandomNumber();
571   }
572
573   {
574     ax = MainForm.picPane.Width / 2;
575     Ay = MainForm.picPane.Height / 2;
576   }
577
578 #if 1
579   ScreenScrollXPos = Stretch * (MurphyScreenXPos + 16) - ax;
580   ScreenScrollYPos = Stretch * (MurphyScreenYPos + 16) - Ay;
581 #else
582   ScreenScrollXPos = Stretch * (MurphyScreenXPos + 8) - ax;
583   ScreenScrollYPos = Stretch * (MurphyScreenYPos + 8) - Ay;
584 #endif
585
586 #if 0
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);
591 #endif
592
593   return subCalculateScreenScrollPos;
594 }