fixed some memory allocations to prevent valgrind warnings
[rocksndiamonds.git] / src / game_sp / Globals.c
index e34d19624341f42236c2ee46c5c67343ca8d900b..0ca6500049f0c3cb1e07bed3428eea1ddd7a4cd9 100644 (file)
@@ -5,6 +5,9 @@
 #include "Globals.h"
 
 
+static int *PlayField16Mem;
+static byte *PlayField8Mem;
+
 boolean LevelLoaded;
 
 boolean DemoAvailable;
@@ -14,24 +17,13 @@ int FieldWidth;             // standard size = 60
 int FieldHeight;       // standard size = 24
 int HeaderSize;                // standard size = 96
 int FieldMax, LevelMax;
-long FileMax;
 
-#if 1
-int PlayField16[SP_MAX_PLAYFIELD_SIZE + SP_HEADER_SIZE];
-byte PlayField8[SP_MAX_PLAYFIELD_SIZE + SP_HEADER_SIZE];
-byte DisPlayField[SP_MAX_PLAYFIELD_SIZE + SP_HEADER_SIZE];
-#else
 int *PlayField16;
 byte *PlayField8;
-byte *DisPlayField;
-#endif
+byte DisPlayField[SP_MAX_PLAYFIELD_SIZE + SP_HEADER_SIZE];
 
 int TimerVar;
-#if 1
 short RandomSeed;
-#else
-int RandomSeed;
-#endif
 
 int FreezeZonks;
 
@@ -44,7 +36,7 @@ int MurphyPosIndex, MurphyXPos, MurphyYPos;
 int MurphyScreenXPos, MurphyScreenYPos;
 int MurphyExplodePos, SplitMoveFlag, RedDiskReleaseMurphyPos;
 int KillMurphyFlag, MurphyMoveCounter;
-long YawnSleepCounter;
+int YawnSleepCounter;
 int MurphyVarFaceLeft;
 int ScratchGravity, GravityFlag;
 int RedDiskReleaseFlag, MovingPictureSequencePhase;
@@ -162,8 +154,10 @@ boolean isSnappingSequence(int sequence)
   }
 }
 
-void InitGlobals()
+void InitGlobals(void)
 {
+  InitPrecedingPlayfieldMemory();
+
   AutoScrollFlag = True;
   FreezeZonks = 0;
   LevelLoaded = False;
@@ -175,16 +169,23 @@ void InitGlobals()
   bPlaying = False;
   menBorder = False;
 
-#if 0
-  /* these defaults will be changed after reading a Supaplex level file */
-  PlayField8 = REDIM_1D(sizeof(byte), 0, FieldMax);
-  DisPlayField = REDIM_1D(sizeof(byte), 0, FieldMax);
-  PlayField16 = REDIM_1D(sizeof(int), 0, FieldMax);
+  // add preceding playfield buffer (as large as preceding memory area)
+  PlayField16Mem = checked_calloc((game_sp.preceding_buffer_size +
+                                  SP_MAX_PLAYFIELD_SIZE +
+                                  SP_HEADER_SIZE) * sizeof(int));
+  PlayField16 = &PlayField16Mem[game_sp.preceding_buffer_size];
+
+  // add preceding playfield buffer (as large as one playfield row)
+  PlayField8Mem = checked_calloc((SP_MAX_PLAYFIELD_WIDTH +
+                                 SP_MAX_PLAYFIELD_SIZE +
+                                 SP_HEADER_SIZE) * sizeof(byte));
+  PlayField8 = &PlayField8Mem[SP_MAX_PLAYFIELD_WIDTH];
+}
 
-  AnimationPosTable = REDIM_1D(sizeof(int), 0, LevelMax);
-  AnimationSubTable = REDIM_1D(sizeof(byte), 0, LevelMax);
-  TerminalState = REDIM_1D(sizeof(byte), 0, FieldMax);
-#endif
+void FreeGlobals(void)
+{
+  checked_free(PlayField16Mem);
+  checked_free(PlayField8Mem);
 }
 
 int GetSI(int X, int Y)
@@ -212,30 +213,16 @@ int GetStretchY(int si)
   return StretchWidth * (si / FieldWidth);
 }
 
-void PrepareLevel()
+void PrepareLevel(void)
 {
   copyInternalEngineVars_SP();
 
-#if 1
   SetDisplayRegion();
   SetScrollEdges();
-#endif
 
   LevelLoaded = True;
 }
 
-#if 0
-void Trace(char *Source, char *Message)
-{
-  printf("::: Trace: Source == '%s', Message == '%s'\n", Source, Message);
-}
-
-void ReportError(char *Source, char *Message)
-{
-  printf("::: ReportError: Source == '%s', Message == '%s'\n", Source, Message);
-}
-#endif
-
 int Min(int A, int B)
 {
   return (A < B ? A : B);