rnd-20090724-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()
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     printf("::: playing demo ...\n");
39
40     // EP set level success byte: demo, not game
41     WasDemoFlag = 1;
42     EP_GameDemoVar0DAA = 0; // demo
43   }
44   else // loc_g_1836:
45   {
46     printf("::: playing game ...\n");
47
48     // EP set level success byte: game, not demo
49     WasDemoFlag = 0;
50     EP_GameDemoVar0DAA = 1; // game
51   }
52
53   // RestartGameLoop:
54   //  If RecordDemoFlag = 1 Then
55   //    RecordDemoFlag = 0 ' clear Demo Recording flag
56   //    Call subDisplayPlayingTime                 ' playing time on screen
57   //    ' Record key still pressed?' >= (Ctrl-)F1 and <= (Ctrl-)F10
58   //    While &H3B <= KeyScanCode7 And KeyScanCode7 <= &H44
59   //      ' yes -> wait until released
60   //      ' should we DoEvents here???? ... depends on how ... but yes!
61   //      ' ...or we can rather poll the keyboardstate inside this loop???
62   //    Wend
63   //    Call subInitGameConditions     ' Init game conditions (vars)
64   //    If MusicOnFlag = 0 Then Call subMusicInit
65   //    WasDemoFlag = 0          ' no demo anymore
66   //    EP_GameDemoVar0DAA = 1 ' force game
67   //  End If
68
69   // This was a bug in the original Supaplex: sometimes red disks could not
70   // be released.  This happened If Murphy was killed DURING a red disk release
71   // and the next try started.
72
73   RedDiskReleasePhase = 0; // (re-)enable red disk release
74   UpdatedFlag = 0;
75   GameLoopRunning = 1;
76   LevelStatus = 0;
77
78   // ----------------------------------------------------------------------------
79   // --------------------- START OF GAME-BUSY LOOP ------------------------------
80   // ----------------------------------------------------------------------------
81
82 locRepeatMainGameLoop:                           // start repeating game loop
83
84   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85   // FS   synchronization
86   while (PauseMode != 0)
87   {
88     DoEvents();
89   }
90
91   do
92   {
93     DoEvents(); // user may klick on menus or move the window here ...
94   }
95 #if 1
96   while (0);
97 #else
98   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
99 #endif
100
101   //   never any additional code between here!
102 #if 0
103   LastFrame = Clock.TickNow(); // store the frame time
104 #endif
105   //   never any additional code between here!
106   if (! NoDisplayFlag) // copy the BackBuffer(=Stage) to visible screen
107     Stage.Blt();
108
109   // FS   end of synchronization
110   // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111   if (EndFlag)
112     goto locExitMainGameLoop;
113
114   // If DemoFlag = 0 Then Call subCheckJoystick    ' check joystick
115   // bx = subCheckRightMouseButton()                ' check (right) mouse button
116   // If bx = 2 And LeadOutCounter < 1 Then KillMurphyFlag = 1 ' lead-out busy after quit? -> kill Murphy!
117
118   //  If DebugVersionFlag <> 0 Then ' debug mode on?
119   //    If Data_SubRest <> 0 Then Data_SubRest = Data_SubRest - 1
120   //    If keyEnter <> 0 Then GoTo loc_g_186F ' Was it the Enter key? -> yes--skip! No mouse!
121   //                                          ' fixes ENTER bug If no mouse driver!
122   //    If bx <> 1 Then GoTo loc_g_186F  ' Left button=Init game field
123   //                                    ' Also Enter If no mouse!
124   //    If Data_SubRest <> 0 Then GoTo loc_g_186F
125   //    Data_SubRest = 10
126   //    Call subRestoreFancy
127   //    Call subDisplayLevel         ' Paint (Init) game field
128   //    Call subConvertToEasySymbols ' Convert to easy symbols
129   //  End If
130
131   // loc_g_186F:
132
133   subProcessKeyboardInput();                 // Check keyboard, act on keys
134
135   // 'HACK:
136   //  TimerVar = TimerVar + 1
137   //  DoEvents
138   //  GoTo loc_g_186F
139   // 'END HACK
140   // If RecordDemoFlag = 1 Then GoTo RestartGameLoop
141
142   // ----------------------------------------------------------------------------
143   //
144
145   subDoGameStuff();                 // do all game stuff
146
147   //
148   // ----------------------------------------------------------------------------
149
150   //  Call subDisplayPlayingTime                 ' playing time on screen
151
152   subCheckRestoreRedDiskCountDisplay();    // Restore panel: red-disk hole
153
154   subRedDiskReleaseExplosion();       // Red Disk release and explode
155   subFollowUpExplosions();  // every explosion may cause up to 8 following explosions
156
157   bx = subCalculateScreenScrollPos();     // calculate screen start addrs
158
159   ScreenPosition = bx;
160
161   // Now new X and new Y are calculated, and bx = screen position = ScreenPosition
162   data_h_Ytmp = ScreenScrollYPos; // copy Y for next soft scroll
163   data_h_Xtmp = ScreenScrollXPos; // copy X for next soft scroll
164   if ((! UserDragFlag) && AutoScrollFlag)
165     ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
166
167   if (ForcedExitFlag != 0) // Forced Exit?' yes--exit!
168     goto locExitMainGameLoop;
169
170   TimerVar = TimerVar + 1;
171
172 #if 0
173   if (bCapturePane)
174     MainForm.SaveSnapshot(TimerVar);
175 #endif
176
177   //  If Not NoDisplayFlag Then
178   //    With MainForm.lblFrameCount
179   //      .Caption = TimerVar
180   //      .Refresh
181   //    End With
182   //  End If
183   if (ExitToMenuFlag == 1)
184     goto locExitMainGameLoop;
185
186   if (LeadOutCounter == 0) // no lead-out: game busy
187     goto locRepeatMainGameLoop;
188
189   // ----------------------------------------------------------------------------
190   // ---------------------- END OF GAME-BUSY LOOP -------------------------------
191   // ----------------------------------------------------------------------------
192   LeadOutCounter = LeadOutCounter - 1;             // do more lead-out after quit
193   if (LeadOutCounter != 0) // lead-out not ready: more
194     goto locRepeatMainGameLoop;
195
196   // lead-out done: exit now
197   // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ----------
198
199 locExitMainGameLoop:
200   do
201   {
202     DoEvents(); // user may klick on menus or move the window here ...
203   }
204 #if 1
205   while (0);
206 #else
207   while (Clock.TickDiffUS(LastFrame) < DeltaT); // wait till its time for the next frame
208 #endif
209
210   Stage.Blt(); // blit the last frame
211   GameLoopRunning = 0;
212
213 #if 0
214   MainForm.menStop_Click();
215   MainForm.PanelVisible = True;
216 #endif
217
218   // If DemoRecordingFlag <> 0 Then Call subCloseDemoRecordingFile ' Demo recording on? -> close opened demo file (w)
219   if (SavedGameFlag != 0) // after savegame: no update!
220   {
221     SavedGameFlag = 0;
222     return subMainGameLoop;
223   }
224
225   SavedGameFlag = 0;
226   if (UpdateTimeFlag == 0) // update time?
227     return subMainGameLoop;
228
229   if (UpdatedFlag == 0) // update playing time
230     subUpdatePlayingTime();
231
232
233   return subMainGameLoop;
234 } // subMainGameLoop
235
236 void subUpdatePlayingTime()
237 {
238 }
239
240 int subCalculateScreenScrollPos()
241 {
242   int subCalculateScreenScrollPos;
243
244   int ax, Ay;
245
246   if (ExplosionShake != 0)
247   {
248     subGetRandomNumber();
249   }
250
251   {
252     ax = MainForm.picPane.Width / 2;
253     Ay = MainForm.picPane.Height / 2;
254   }
255
256 #if 1
257   ScreenScrollXPos = Stretch * (MurphyScreenXPos + 16) - ax;
258   ScreenScrollYPos = Stretch * (MurphyScreenYPos + 16) - Ay;
259 #else
260   ScreenScrollXPos = Stretch * (MurphyScreenXPos + 8) - ax;
261   ScreenScrollYPos = Stretch * (MurphyScreenYPos + 8) - Ay;
262 #endif
263
264 #if 0
265   printf("::: MainGameLoop.c: subCalculateScreenScrollPos(): %d, %d [%d, %d] -> %d, %d\n",
266          MainForm.picPane.Width, MainForm.picPane.Height,
267          MurphyScreenXPos, MurphyScreenYPos,
268          ScreenScrollXPos, ScreenScrollYPos);
269 #endif
270
271   return subCalculateScreenScrollPos;
272 }