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