rnd-20091020-1-src
[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     goto locExitMainGameLoop;
244   }
245 #else
246   if (ExitToMenuFlag == 1)
247     goto locExitMainGameLoop;
248 #endif
249
250 #if 1
251   if (LeadOutCounter == 0) // no lead-out: game busy
252     return subMainGameLoop;
253 #else
254   if (LeadOutCounter == 0) // no lead-out: game busy
255     goto locRepeatMainGameLoop;
256 #endif
257
258   // ----------------------------------------------------------------------------
259   // ---------------------- END OF GAME-BUSY LOOP -------------------------------
260   // ----------------------------------------------------------------------------
261   LeadOutCounter = LeadOutCounter - 1;             // do more lead-out after quit
262 #if 1
263   if (LeadOutCounter != 0) // lead-out not ready: more
264     return subMainGameLoop;
265 #else
266   if (LeadOutCounter != 0) // lead-out not ready: more
267     goto locRepeatMainGameLoop;
268 #endif
269
270   // lead-out done: exit now
271   // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
272
273 locExitMainGameLoop:
274
275
276
277 #if 1
278   return subMainGameLoop;
279 #endif
280
281
282
283 #if 0
284   printf("::: locExitMainGameLoop reached [%d]\n", LeadOutCounter);
285 #endif
286
287   do
288   {
289     DoEvents(); // user may klick on menus or move the window here ...
290   }
291 #if 1
292   while (0);
293 #else
294   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
295 #endif
296
297   Stage.Blt(); // blit the last frame
298   GameLoopRunning = 0;
299
300 #if 0
301   MainForm.menStop_Click();
302   MainForm.PanelVisible = True;
303 #endif
304
305   // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
306   if (SavedGameFlag != 0) // after savegame: no update!
307   {
308     SavedGameFlag = 0;
309     return subMainGameLoop;
310   }
311
312   SavedGameFlag = 0;
313   if (UpdateTimeFlag == 0) // update time?
314     return subMainGameLoop;
315
316   if (UpdatedFlag == 0) // update playing time
317     subUpdatePlayingTime();
318
319   return subMainGameLoop;
320 } // subMainGameLoop
321
322 int subMainGameLoop()
323 {
324   int subMainGameLoop;
325
326   // int al, bx;
327   int bx;
328 #if 0
329   TickCountObject Clock;
330   currency LastFrame;
331 #endif
332
333   if (DemoFlag != 0)
334   {
335 #if 1
336     printf("::: playing demo ...\n");
337 #endif
338
339     // EP set level success byte: demo, not game
340     WasDemoFlag = 1;
341     EP_GameDemoVar0DAA = 0; // demo
342   }
343   else // loc_g_1836:
344   {
345 #if 1
346     printf("::: playing game ...\n");
347 #endif
348
349     // EP set level success byte: game, not demo
350     WasDemoFlag = 0;
351     EP_GameDemoVar0DAA = 1; // game
352   }
353
354   // RestartGameLoop:
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???
363   //    Wend
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
368   //  End If
369
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.
373
374   RedDiskReleasePhase = 0; // (re-)enable red disk release
375   UpdatedFlag = 0;
376   GameLoopRunning = 1;
377   LevelStatus = 0;
378
379   // ----------------------------------------------------------------------------
380   // --------------------- START OF GAME-BUSY LOOP ------------------------------
381   // ----------------------------------------------------------------------------
382
383 locRepeatMainGameLoop:                           // start repeating game loop
384
385   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
386   // FS   synchronization
387   while (PauseMode != 0)
388   {
389     DoEvents();
390   }
391
392   do
393   {
394     DoEvents(); // user may klick on menus or move the window here ...
395   }
396 #if 1
397   while (0);
398 #else
399   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
400 #endif
401
402   //   never any additional code between here!
403 #if 0
404   LastFrame = Clock.TickNow(); // store the frame time
405 #endif
406   //   never any additional code between here!
407   if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
408     Stage.Blt();
409
410   // FS   end of synchronization
411   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
412   if (EndFlag)
413     goto locExitMainGameLoop;
414
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!
418
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
426   //    Data_SubRest = 10
427   //    Call subRestoreFancy
428   //    Call subDisplayLevel         ' Paint (Init) game field
429   //    Call subConvertToEasySymbols ' Convert to easy symbols
430   //  End If
431
432   // loc_g_186F:
433
434   subProcessKeyboardInput();                 // Check keyboard, act on keys
435
436   // 'HACK:
437   //  TimerVar = TimerVar + 1
438   //  DoEvents
439   //  GoTo loc_g_186F
440   // 'END HACK
441   // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
442
443   // ----------------------------------------------------------------------------
444   //
445
446 #if 0
447   printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
448 #endif
449
450   subDoGameStuff();                 // do all game stuff
451
452 #if 0
453   printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
454 #endif
455
456   //
457   // ----------------------------------------------------------------------------
458
459   //  Call subDisplayPlayingTime                 ' playing time on screen
460
461   subCheckRestoreRedDiskCountDisplay();    // Restore panel: red-disk hole
462
463   subRedDiskReleaseExplosion();       // Red Disk release and explode
464   subFollowUpExplosions();  // every explosion may cause up to 8 following explosions
465
466   bx = subCalculateScreenScrollPos();     // calculate screen start addrs
467
468   ScreenPosition = bx;
469
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)
474   {
475 #if 0
476     printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
477 #endif
478
479     ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
480   }
481
482   if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
483     goto locExitMainGameLoop;
484
485   TimerVar = TimerVar + 1;
486
487 #if 0
488   if (bCapturePane)
489     MainForm.SaveSnapshot(TimerVar);
490 #endif
491
492   //  If Not NoDisplayFlag Then
493   //    With MainForm.lblFrameCount
494   //      .Caption = TimerVar
495   //      .Refresh
496   //    End With
497   //  End If
498   if (ExitToMenuFlag == 1)
499     goto locExitMainGameLoop;
500
501   if (LeadOutCounter == 0) // no lead-out: game busy
502     goto locRepeatMainGameLoop;
503
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;
510
511   // lead-out done: exit now
512   // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
513
514 locExitMainGameLoop:
515   do
516   {
517     DoEvents(); // user may klick on menus or move the window here ...
518   }
519 #if 1
520   while (0);
521 #else
522   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
523 #endif
524
525   Stage.Blt(); // blit the last frame
526   GameLoopRunning = 0;
527
528 #if 0
529   MainForm.menStop_Click();
530   MainForm.PanelVisible = True;
531 #endif
532
533   // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
534   if (SavedGameFlag != 0) // after savegame: no update!
535   {
536     SavedGameFlag = 0;
537     return subMainGameLoop;
538   }
539
540   SavedGameFlag = 0;
541   if (UpdateTimeFlag == 0) // update time?
542     return subMainGameLoop;
543
544   if (UpdatedFlag == 0) // update playing time
545     subUpdatePlayingTime();
546
547
548   return subMainGameLoop;
549 } // subMainGameLoop
550
551 void subUpdatePlayingTime()
552 {
553 }
554
555 int subCalculateScreenScrollPos()
556 {
557   int subCalculateScreenScrollPos;
558
559   int ax, Ay;
560
561   if (ExplosionShake != 0)
562   {
563     subGetRandomNumber();
564   }
565
566   {
567     ax = MainForm.picPane.Width / 2;
568     Ay = MainForm.picPane.Height / 2;
569   }
570
571 #if 1
572   ScreenScrollXPos = Stretch * (MurphyScreenXPos + 16) - ax;
573   ScreenScrollYPos = Stretch * (MurphyScreenYPos + 16) - Ay;
574 #else
575   ScreenScrollXPos = Stretch * (MurphyScreenXPos + 8) - ax;
576   ScreenScrollYPos = Stretch * (MurphyScreenYPos + 8) - Ay;
577 #endif
578
579 #if 0
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);
584 #endif
585
586   return subCalculateScreenScrollPos;
587 }