65b88f2f5345a1967b63cfbe2a8ef7570611c17a
[rocksndiamonds.git] / src / game_sp / MainGameLoop.c
1 // ----------------------------------------------------------------------------
2 // MainGameLoop.c
3 // ----------------------------------------------------------------------------
4
5 #include "MainGameLoop.h"
6
7
8 boolean bPlaying;
9 int LeadOutCounter;
10 int ExitToMenuFlag;
11 boolean AutoScrollFlag;
12
13
14 // ==========================================================================
15 //                              SUBROUTINE
16 // Play a game/demo
17 // ==========================================================================
18
19 void subMainGameLoop_Init()
20 {
21   // This was a bug in the original Supaplex: sometimes red disks could not
22   // be released.  This happened if Murphy was killed DURING a red disk release
23   // and the next try started.
24
25   RedDiskReleasePhase = 0; // (re-)enable red disk release
26 }
27
28 void subMainGameLoop_Main(byte action, boolean warp_mode)
29 {
30   // ---------------------------------------------------------------------------
31   // --------------------- START OF GAME-BUSY LOOP -----------------------------
32   // ---------------------------------------------------------------------------
33
34   subProcessKeyboardInput(action);      // check keyboard, act on keys
35
36   // ---------------------------------------------------------------------------
37   //
38
39   subDoGameStuff();                     // do all game stuff
40
41   //
42   // ---------------------------------------------------------------------------
43
44   subRedDiskReleaseExplosion();         // Red Disk release and explode
45   subFollowUpExplosions();              // every explosion may cause up to 8 following explosions
46
47   subCalculateScreenScrollPos();        // calculate screen start addrs
48
49   if (AutoScrollFlag)
50     ScrollTowards(ScreenScrollXPos, ScreenScrollYPos);
51
52   TimerVar = TimerVar + 1;
53
54 #if 1
55   if (ExplosionShakeMurphy > 0)
56     ExplosionShakeMurphy--;
57 #endif
58
59 #if 1
60   if (ExitToMenuFlag == 1)
61   {
62     // happens when demo ends or when Murphy enters exit (to be checked)
63
64 #if 0
65     goto locExitMainGameLoop;
66 #endif
67   }
68 #else
69   if (ExitToMenuFlag == 1)
70     goto locExitMainGameLoop;
71 #endif
72
73   if (LeadOutCounter == 0) // no lead-out: game busy
74     return;
75
76   // ---------------------------------------------------------------------------
77   // ---------------------- END OF GAME-BUSY LOOP ------------------------------
78   // ---------------------------------------------------------------------------
79
80   LeadOutCounter = LeadOutCounter - 1;          // do more lead-out after quit
81
82   if (LeadOutCounter != 0)                      // lead-out not ready: more
83     return;
84
85   // lead-out done: exit now
86   // ---------------------- END OF GAME-BUSY LOOP (including lead-out) ---------
87
88 #if 0
89 locExitMainGameLoop:
90 #endif
91
92 #if 1
93   printf("::: locExitMainGameLoop reached [%d]\n", LeadOutCounter);
94   printf("::: [KillMurphyFlag == %d]\n", KillMurphyFlag);
95 #endif
96
97 #if 1
98   /* if the game is not won when reaching this point, then it is lost */
99   if (!game_sp.LevelSolved)
100     game_sp.GameOver = TRUE;
101 #endif
102 }
103
104 void subCalculateScreenScrollPos()
105 {
106 #if 1
107   int jump_pos = TILEX / 2;
108
109   /* handle wrap-around */
110   if (MurphyScreenXPos < -jump_pos)
111   {
112     MurphyScreenXPos = FieldWidth * TILEX + MurphyScreenXPos;
113     MurphyScreenYPos -= TILEY;
114   }
115   else if (MurphyScreenXPos >= FieldWidth * TILEX - jump_pos)
116   {
117     MurphyScreenXPos = MurphyScreenXPos - FieldWidth * TILEX;
118     MurphyScreenYPos += TILEY;
119   }
120 #endif
121
122   if (ExplosionShake != 0)
123   {
124     subGetRandomNumber();
125
126     // printf("::: ExplosionShake [%d]\n", FrameCounter);
127   }
128
129   ScreenScrollXPos = MurphyScreenXPos - (SCR_FIELDX / 2) * TILESIZE;
130   ScreenScrollYPos = MurphyScreenYPos - (SCR_FIELDY / 2) * TILESIZE;
131 }