cleanup in initEverything and freeEverything
authorkoda
Sun, 28 Oct 2012 04:28:39 +0100
changeset 7850 fcbb024090a4
parent 7849 a12155461b34
child 7851 d4bda0201cae
cleanup in initEverything and freeEverything
hedgewars/hwengine.pas
hedgewars/uAILandMarks.pas
hedgewars/uAIMisc.pas
hedgewars/uCommands.pas
hedgewars/uGears.pas
hedgewars/uLandTexture.pas
hedgewars/uStats.pas
hedgewars/uTouch.pas
hedgewars/uVideoRec.pas
hedgewars/uWorld.pas
project_files/Android-build/gles11.pp
--- a/hedgewars/hwengine.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/hwengine.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -50,7 +50,7 @@
 procedure freeEverything(complete:boolean); forward;
 {$ENDIF}
 
-////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
 function DoTimer(Lag: LongInt): boolean;
 var s: shortstring;
 begin
@@ -61,9 +61,9 @@
         gsLandGen:
             begin
             GenMap;
-            uLandTexture.initModule;
-            UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false); 
-            uAILandMarks.initModule;
+            SetLandTexture;
+            UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false);
+            setAILandMarks;
             ParseCommand('sendlanddigest', true);
             GameState:= gsStart;
             end;
@@ -138,7 +138,7 @@
         end;
 end;
 
-///////////////////
+////////////////////////////////////////////////////////////////////////////////
 procedure MainLoop;
 var event: TSDL_Event;
     PrevTime, CurrTime: Longword;
@@ -310,7 +310,7 @@
 end;
 {$ENDIF}
 
-///////////////
+////////////////////////////////////////////////////////////////////////////////
 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
 var p: TPathType;
     s: shortstring;
@@ -443,40 +443,42 @@
     freeEverything(true);
 end;
 
+////////////////////////////////////////////////////////////////////////////////
+// As a rule of thumb, every module that is listed in either initEverything or 
+// freeEverything should come in pair, even if they are stubs. Only use this 
+// section for inialising variables and remeber that game args overwrite these,
+// so handle this section with care. Pay attention to the init/free order too! 
 procedure initEverything (complete:boolean);
 begin
     Randomize();
 
-    uUtils.initModule(complete);      // this opens the debug file, must be the first
-    uMisc.initModule;
-    uVariables.initModule;
-    uConsole.initModule;
-    uCommands.initModule;
-    uCommandHandlers.initModule;
+    uUtils.initModule(complete);    // opens the debug file, must be the first
+    uVariables.initModule;          // inits all global variables
+    uConsole.initModule;            // opens stdout
+    uCommands.initModule;           // helps below
+    uCommandHandlers.initModule;    // registers all messages from frontend
 
-    uLand.initModule;
-    uLandPainted.initModule;
-    uIO.initModule;
+    uLand.initModule;               // computes land
+    uLandPainted.initModule;        // computes drawn land
+    uIO.initModule;                 // sets up sockets
 
     if complete then
     begin
-{$IFDEF ANDROID}GLUnit.init;{$ENDIF}
+{$IFDEF ANDROID}GLUnit.initModule;{$ENDIF}
 {$IFDEF USE_TOUCH_INTERFACE}uTouch.initModule;{$ENDIF}
+{$IFDEF USE_VIDEO_RECORDING}uVideoRec.initModule;{$ENDIF}   //stub
         uAI.initModule;
-        //uAIActions does not need initialization
-        //uAIAmmoTests does not need initialization
         uAIMisc.initModule;
+        uAILandMarks.initModule;    //stub
         uAmmos.initModule;
+        uCaptions.initModule;
+
         uChat.initModule;
         uCollisions.initModule;
-        //uFloat does not need initialization
-        //uGame does not need initialization
         uGears.initModule;
-        uInputHandler.initModule;
-        //uLandGraphics does not need initialization
-        //uLandObjects does not need initialization
-        //uLandTemplates does not need initialization
-        //uLocale does not need initialization
+        uInputHandler.initModule;   //stub
+        uMisc.initModule;
+        uLandTexture.initModule;    //stub
         uScript.initModule;
         uSound.initModule;
         uStats.initModule;
@@ -484,7 +486,6 @@
         uTeams.initModule;
         uVisualGears.initModule;
         uWorld.initModule;
-        uCaptions.initModule;
     end;
 end;
 
@@ -493,9 +494,9 @@
     if complete then
     begin
         WriteLnToConsole('Freeing resources...');
-        uAI.freeModule;
+        uAI.freeModule;             // AI things need to be freed first
+        uAIMisc.freeModule;         //stub
         uAILandMarks.freeModule;
-        uAIMisc.freeModule;         //stub
         uCaptions.freeModule;
         uWorld.freeModule;
         uVisualGears.freeModule;
@@ -504,22 +505,16 @@
         uStats.freeModule;          //stub
         uSound.freeModule;
         uScript.freeModule;
-        //uRandom does not need to be freed
-        //uLocale does not need to be freed
-        //uLandTemplates does not need to be freed
+        uMisc.freeModule;
         uLandTexture.freeModule;
-        //uLandObjects does not need to be freed
-        //uLandGraphics does not need to be freed
         uGears.freeModule;
-        //uGame does not need to be freed
-        //uFloat does not need to be freed
         uCollisions.freeModule;     //stub
         uChat.freeModule;
         uAmmos.freeModule;
-        //uAIAmmoTests does not need to be freed
-        //uAIActions does not need to be freed
-        uStore.freeModule;
+        uStore.freeModule;          // closes SDL
 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.freeModule;{$ENDIF}
+{$IFDEF USE_TOUCH_INTERFACE}uTouch.freeModule;{$ENDIF}  //stub
+{$IFDEF ANDROID}GLUnit.freeModule;{$ENDIF}
     end;
 
     uIO.freeModule;
@@ -528,13 +523,12 @@
 
     uCommandHandlers.freeModule;
     uCommands.freeModule;
-    uConsole.freeModule;
+    uConsole.freeModule;            // closes stdout
     uVariables.freeModule;
-    uUtils.freeModule;
-    uMisc.freeModule;           // uMisc closes the debug log.
+    uUtils.freeModule;              // closes debug file
 end;
 
-/////////////////////////
+////////////////////////////////////////////////////////////////////////////////
 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
 var Preview: TPreview;
 begin
@@ -557,7 +551,7 @@
 end;
 
 {$IFNDEF HWLIBRARY}
-/////////////////////
+////////////////////////////////////////////////////////////////////////////////
 procedure DisplayUsage;
 var i: LongInt;
 begin
@@ -583,7 +577,7 @@
     WriteLn(stdout, '');
 end;
 
-////////////////////
+////////////////////////////////////////////////////////////////////////////////
 {$INCLUDE "ArgParsers.inc"}
 
 procedure GetParams;
--- a/hedgewars/uAILandMarks.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/uAILandMarks.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -10,6 +10,7 @@
 function  checkMark(X, Y: LongInt; mark: byte) : boolean;
 procedure clearAllMarks;
 procedure clearMarks(mark: byte);
+procedure setAILandMarks;
 
 procedure initModule;
 procedure freeModule;
@@ -57,8 +58,7 @@
             marks[Y, X]:= marks[Y, X] and (not mark)
 end;
 
-
-procedure initModule;
+procedure setAILandMarks;
 begin
     WIDTH:= LAND_WIDTH shr gr;
     HEIGHT:= LAND_HEIGHT shr gr;
@@ -66,6 +66,10 @@
     SetLength(marks, HEIGHT, WIDTH);
 end;
 
+procedure initModule;
+begin
+end;
+
 procedure freeModule;
 begin
     SetLength(marks, 0, 0);
--- a/hedgewars/uAIMisc.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/uAIMisc.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -772,7 +772,6 @@
 
 procedure freeModule;
 begin
-
 end;
 
 end.
--- a/hedgewars/uCommands.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/uCommands.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -46,8 +46,7 @@
         Trusted, Rand: boolean;
         end;
 
-var
-    Variables: PVariable;
+var Variables: PVariable;
 
 procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean);
 begin
--- a/hedgewars/uGears.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/uGears.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -33,7 +33,7 @@
  *       effects are called "Visual Gears" and defined in the respective unit!
  *)
 interface
-uses SDLh, uConsts, uFloat, uTypes, uLandObjects;
+uses SDLh, uConsts, uFloat, uTypes;
 
 procedure initModule;
 procedure freeModule;
--- a/hedgewars/uLandTexture.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/uLandTexture.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -27,6 +27,7 @@
 procedure UpdateLandTexture(X, Width, Y, Height: LongInt; landAdded: boolean);
 procedure DrawLand(dX, dY: LongInt);
 procedure ResetLand;
+procedure SetLandTexture;
 
 implementation
 uses uConsts, GLunit, uTypes, uVariables, uTextures, uDebug, uRender;
@@ -183,7 +184,7 @@
 
 end;
 
-procedure initModule;
+procedure SetLandTexture;
 begin
     if (cReducedQuality and rqBlurryLand) = 0 then
         begin
@@ -199,6 +200,10 @@
     SetLength(LandTextures, LANDTEXARW, LANDTEXARH);
 end;
 
+procedure initModule;
+begin
+end;
+
 procedure ResetLand;
 var x, y: LongInt;
 begin
--- a/hedgewars/uStats.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/uStats.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -311,7 +311,6 @@
 
 procedure freeModule;
 begin
-
 end;
 
 end.
--- a/hedgewars/uTouch.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/uTouch.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -22,10 +22,11 @@
 
 interface
 
-uses SysUtils, uConsole, uVariables, SDLh, uFloat, uConsts, uCommands, uIO, GLUnit, uTypes, uCaptions, uAmmos, uWorld, uMobile;
+uses SysUtils, uConsole, uVariables, SDLh, uFloat, uConsts, uCommands, GLUnit, uTypes, uCaptions, uAmmos, uWorld, uMobile;
 
 
 procedure initModule;
+procedure freeModule;
 
 procedure ProcessTouch;
 procedure NewTurnBeginning;
@@ -643,5 +644,9 @@
     halfRectSize:= rectSize shl 1;
 end;
 
+procedure freeModule;
+begin
+end;
+
 begin
 end.
--- a/hedgewars/uVideoRec.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/uVideoRec.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -49,6 +49,7 @@
 procedure StopPreRecording;
 procedure SaveCameraPosition;
 
+procedure initModule;
 procedure freeModule;
 
 implementation
@@ -366,6 +367,10 @@
     BlockWrite(cameraFile, frame, 1);
 end;
 
+procedure initModule;
+begin
+end;
+
 procedure freeModule;
 begin
     if flagPrerecording then
--- a/hedgewars/uWorld.pas	Sun Oct 28 04:00:07 2012 +0100
+++ b/hedgewars/uWorld.pas	Sun Oct 28 04:28:39 2012 +0100
@@ -1892,19 +1892,12 @@
     stereoDepth:= 0;
     AMState:= AMHidden;
     isFirstFrame:= true;
+    stereoDepth:= stereoDepth; // avoid hint
 end;
 
 procedure freeModule;
 begin
-    stereoDepth:= stereoDepth; // avoid hint
-    FreeTexture(fpsTexture);
-    fpsTexture:= nil;
-    FreeTexture(timeTexture);
-    timeTexture:= nil;
-    FreeTexture(missionTex);
-    missionTex:= nil;
-    FreeTexture(recTexture);
-    recTexture:= nil;
+    ResetWorldTex();
 end;
 
 end.
--- a/project_files/Android-build/gles11.pp	Sun Oct 28 04:00:07 2012 +0100
+++ b/project_files/Android-build/gles11.pp	Sun Oct 28 04:28:39 2012 +0100
@@ -32,7 +32,8 @@
     gl.hh
 }
 
-  procedure init;
+  procedure initModule;
+  procedure freeModule;
 
   const
     External_library='GLESv1_CM'; {Setup as you need}
@@ -1106,15 +1107,14 @@
       pointer(glPointSizePointerOES):=GetProcAddress(hlib,'glPointSizePointerOES');
     end;
 
-procedure init;
+procedure initModule;
 begin
     Loadgles11('libGLESv1_CM.so');
 end;
 
-
-initialization
-  Loadgles11('gles11');
-finalization
+procedure freeModule;
+begin
   Freegles11;
+end;
 
 end.