rnd-20091019-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     printf("::: ExitToMenuFlag == True\n");
240
241     goto locExitMainGameLoop;
242   }
243 #else
244   if (ExitToMenuFlag == 1)
245     goto locExitMainGameLoop;
246 #endif
247
248 #if 1
249   if (LeadOutCounter == 0) // no lead-out: game busy
250     return subMainGameLoop;
251 #else
252   if (LeadOutCounter == 0) // no lead-out: game busy
253     goto locRepeatMainGameLoop;
254 #endif
255
256   // ----------------------------------------------------------------------------
257   // ---------------------- END OF GAME-BUSY LOOP -------------------------------
258   // ----------------------------------------------------------------------------
259   LeadOutCounter = LeadOutCounter - 1;             // do more lead-out after quit
260 #if 1
261   if (LeadOutCounter != 0) // lead-out not ready: more
262     return subMainGameLoop;
263 #else
264   if (LeadOutCounter != 0) // lead-out not ready: more
265     goto locRepeatMainGameLoop;
266 #endif
267
268   // lead-out done: exit now
269   // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
270
271 locExitMainGameLoop:
272
273 #if 0
274   printf("::: locExitMainGameLoop reached [%d]\n", LeadOutCounter);
275 #endif
276
277   do
278   {
279     DoEvents(); // user may klick on menus or move the window here ...
280   }
281 #if 1
282   while (0);
283 #else
284   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
285 #endif
286
287   Stage.Blt(); // blit the last frame
288   GameLoopRunning = 0;
289
290 #if 0
291   MainForm.menStop_Click();
292   MainForm.PanelVisible = True;
293 #endif
294
295   // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
296   if (SavedGameFlag != 0) // after savegame: no update!
297   {
298     SavedGameFlag = 0;
299     return subMainGameLoop;
300   }
301
302   SavedGameFlag = 0;
303   if (UpdateTimeFlag == 0) // update time?
304     return subMainGameLoop;
305
306   if (UpdatedFlag == 0) // update playing time
307     subUpdatePlayingTime();
308
309
310   return subMainGameLoop;
311 } // subMainGameLoop
312
313 int subMainGameLoop()
314 {
315   int subMainGameLoop;
316
317   // int al, bx;
318   int bx;
319 #if 0
320   TickCountObject Clock;
321   currency LastFrame;
322 #endif
323
324   if (DemoFlag != 0)
325   {
326 #if 1
327     printf("::: playing demo ...\n");
328 #endif
329
330     // EP set level success byte: demo, not game
331     WasDemoFlag = 1;
332     EP_GameDemoVar0DAA = 0; // demo
333   }
334   else // loc_g_1836:
335   {
336 #if 1
337     printf("::: playing game ...\n");
338 #endif
339
340     // EP set level success byte: game, not demo
341     WasDemoFlag = 0;
342     EP_GameDemoVar0DAA = 1; // game
343   }
344
345   // RestartGameLoop:
346   //  If RecordDemoFlag = 1 Then
347   //    RecordDemoFlag = 0 ' clear Demo Recording flag
348   //    Call subDisplayPlayingTime                 ' playing time on screen
349   //    ' Record key still pressed?' >= (Ctrl-)F1 and <= (Ctrl-)F10
350   //    While &H3B <= KeyScanCode7 And KeyScanCode7 <= &H44
351   //      ' yes -> wait until released
352   //      ' should we DoEvents here???? ... depends on how ... but yes!
353   //      ' ...or we can rather poll the keyboardstate inside this loop???
354   //    Wend
355   //    Call subInitGameConditions     ' Init game conditions (vars)
356   //    If MusicOnFlag = 0 Then Call subMusicInit
357   //    WasDemoFlag = 0          ' no demo anymore
358   //    EP_GameDemoVar0DAA = 1 ' force game
359   //  End If
360
361   // This was a bug in the original Supaplex: sometimes red disks could not
362   // be released.  This happened If Murphy was killed DURING a red disk release
363   // and the next try started.
364
365   RedDiskReleasePhase = 0; // (re-)enable red disk release
366   UpdatedFlag = 0;
367   GameLoopRunning = 1;
368   LevelStatus = 0;
369
370   // ----------------------------------------------------------------------------
371   // --------------------- START OF GAME-BUSY LOOP ------------------------------
372   // ----------------------------------------------------------------------------
373
374 locRepeatMainGameLoop:                           // start repeating game loop
375
376   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
377   // FS   synchronization
378   while (PauseMode != 0)
379   {
380     DoEvents();
381   }
382
383   do
384   {
385     DoEvents(); // user may klick on menus or move the window here ...
386   }
387 #if 1
388   while (0);
389 #else
390   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
391 #endif
392
393   //   never any additional code between here!
394 #if 0
395   LastFrame = Clock.TickNow(); // store the frame time
396 #endif
397   //   never any additional code between here!
398   if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
399     Stage.Blt();
400
401   // FS   end of synchronization
402   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
403   if (EndFlag)
404     goto locExitMainGameLoop;
405
406   // If DemoFlag = 0 Then Call subCheckJoystick    ' check joystick
407   // bx = subCheckRightMouseButton()                ' check (right) mouse button
408   // If bx = 2 And LeadOutCounter < 1 Then KillMurphyFlag = 1 ' lead-out busy after quit? -> kill Murphy!
409
410   //  If DebugVersionFlag <> 0 Then ' debug mode on?
411   //    If Data_SubRest <> 0 Then Data_SubRest = Data_SubRest - 1
412   //    If keyEnter <> 0 Then GoTo loc_g_186F ' Was it the Enter key? -> yes--skip! No mouse!
413   //                                          ' fixes ENTER bug If no mouse driver!
414   //    If bx <> 1 Then GoTo loc_g_186F  ' Left button=Init game field
415   //                                    ' Also Enter If no mouse!
416   //    If Data_SubRest <> 0 Then GoTo loc_g_186F
417   //    Data_SubRest = 10
418   //    Call subRestoreFancy
419   //    Call subDisplayLevel         ' Paint (Init) game field
420   //    Call subConvertToEasySymbols ' Convert to easy symbols
421   //  End If
422
423   // loc_g_186F:
424
425   subProcessKeyboardInput();                 // Check keyboard, act on keys
426
427   // 'HACK:
428   //  TimerVar = TimerVar + 1
429   //  DoEvents
430   //  GoTo loc_g_186F
431   // 'END HACK
432   // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
433
434   // ----------------------------------------------------------------------------
435   //
436
437 #if 0
438   printf("::: >>>>>>>>>> MainGameLoop.c: subDoGameStuff() START\n");
439 #endif
440
441   subDoGameStuff();                 // do all game stuff
442
443 #if 0
444   printf("::: <<<<<<<<<< MainGameLoop.c: subDoGameStuff() END\n");
445 #endif
446
447   //
448   // ----------------------------------------------------------------------------
449
450   //  Call subDisplayPlayingTime                 ' playing time on screen
451
452   subCheckRestoreRedDiskCountDisplay();    // Restore panel: red-disk hole
453
454   subRedDiskReleaseExplosion();       // Red Disk release and explode
455   subFollowUpExplosions();  // every explosion may cause up to 8 following explosions
456
457   bx = subCalculateScreenScrollPos();     // calculate screen start addrs
458
459   ScreenPosition = bx;
460
461   // Now new X and new Y are calculated, and bx = screen position = ScreenPosition
462   data_h_Ytmp = ScreenScrollYPos; // copy Y for next soft scroll
463   data_h_Xtmp = ScreenScrollXPos; // copy X for next soft scroll
464   if ((! UserDragFlag) && AutoScrollFlag)
465   {
466 #if 0
467     printf("::: MainGameLoop.c: subMainGameLoop(): %d, %d\n", ScreenScrollXPos, ScreenScrollYPos);
468 #endif
469
470     ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
471   }
472
473   if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
474     goto locExitMainGameLoop;
475
476   TimerVar = TimerVar + 1;
477
478 #if 0
479   if (bCapturePane)
480     MainForm.SaveSnapshot(TimerVar);
481 #endif
482
483   //  If Not NoDisplayFlag Then
484   //    With MainForm.lblFrameCount
485   //      .Caption = TimerVar
486   //      .Refresh
487   //    End With
488   //  End If
489   if (ExitToMenuFlag == 1)
490     goto locExitMainGameLoop;
491
492   if (LeadOutCounter == 0) // no lead-out: game busy
493     goto locRepeatMainGameLoop;
494
495   // ----------------------------------------------------------------------------
496   // ---------------------- END OF GAME-BUSY LOOP -------------------------------
497   // ----------------------------------------------------------------------------
498   LeadOutCounter = LeadOutCounter - 1;             // do more lead-out after quit
499   if (LeadOutCounter != 0) // lead-out not ready: more
500     goto locRepeatMainGameLoop;
501
502   // lead-out done: exit now
503   // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
504
505 locExitMainGameLoop:
506   do
507   {
508     DoEvents(); // user may klick on menus or move the window here ...
509   }
510 #if 1
511   while (0);
512 #else
513   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
514 #endif
515
516   Stage.Blt(); // blit the last frame
517   GameLoopRunning = 0;
518
519 #if 0
520   MainForm.menStop_Click();
521   MainForm.PanelVisible = True;
522 #endif
523
524   // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
525   if (SavedGameFlag != 0) // after savegame: no update!
526   {
527     SavedGameFlag = 0;
528     return subMainGameLoop;
529   }
530
531   SavedGameFlag = 0;
532   if (UpdateTimeFlag == 0) // update time?
533     return subMainGameLoop;
534
535   if (UpdatedFlag == 0) // update playing time
536     subUpdatePlayingTime();
537
538
539   return subMainGameLoop;
540 } // subMainGameLoop
541
542 void subUpdatePlayingTime()
543 {
544 }
545
546 int subCalculateScreenScrollPos()
547 {
548   int subCalculateScreenScrollPos;
549
550   int ax, Ay;
551
552   if (ExplosionShake != 0)
553   {
554     subGetRandomNumber();
555   }
556
557   {
558     ax = MainForm.picPane.Width / 2;
559     Ay = MainForm.picPane.Height / 2;
560   }
561
562 #if 1
563   ScreenScrollXPos = Stretch * (MurphyScreenXPos + 16) - ax;
564   ScreenScrollYPos = Stretch * (MurphyScreenYPos + 16) - Ay;
565 #else
566   ScreenScrollXPos = Stretch * (MurphyScreenXPos + 8) - ax;
567   ScreenScrollYPos = Stretch * (MurphyScreenYPos + 8) - Ay;
568 #endif
569
570 #if 0
571   printf("::: MainGameLoop.c: subCalculateScreenScrollPos(): %d, %d [%d, %d] -> %d, %d\n",
572          MainForm.picPane.Width, MainForm.picPane.Height,
573          MurphyScreenXPos, MurphyScreenYPos,
574          ScreenScrollXPos, ScreenScrollYPos);
575 #endif
576
577   return subCalculateScreenScrollPos;
578 }