hedgewars/hwengine.pas
branch0.9.18
changeset 7884 60132c558c05
parent 7850 fcbb024090a4
child 7857 2bc61f8841a1
child 7896 67217e6108fd
--- a/hedgewars/hwengine.pas	Fri Oct 26 14:13:09 2012 +0100
+++ b/hedgewars/hwengine.pas	Mon Oct 29 21:46:50 2012 +0100
@@ -29,11 +29,13 @@
 program hwengine;
 {$ENDIF}
 
-uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler, uSound,
-     uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uLandTexture, uCollisions,
+uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler,
+     uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uAILandMarks, uLandTexture, uCollisions,
      SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
-     {$IFDEF SDL13}, uTouch{$ENDIF}{$IFDEF ANDROID}, GLUnit{$ENDIF}, uAILandMarks;
+     {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
+     {$IFDEF ANDROID}, GLUnit{$ENDIF}
+     ;
 
 
 {$IFDEF HWLIBRARY}
@@ -48,7 +50,7 @@
 procedure freeEverything(complete:boolean); forward;
 {$ENDIF}
 
-////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
 function DoTimer(Lag: LongInt): boolean;
 var s: shortstring;
 begin
@@ -59,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;
@@ -136,7 +138,7 @@
         end;
 end;
 
-///////////////////
+////////////////////////////////////////////////////////////////////////////////
 procedure MainLoop;
 var event: TSDL_Event;
     PrevTime, CurrTime: Longword;
@@ -308,15 +310,14 @@
 end;
 {$ENDIF}
 
-///////////////
+////////////////////////////////////////////////////////////////////////////////
 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
 var p: TPathType;
     s: shortstring;
     i: LongInt;
 begin
 {$IFDEF HWLIBRARY}
-    cBits:= 32;
-    cTimerInterval:= 8;
+    initEverything(true);
     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
     ipcPort:= StrToInt(gameArgs[0]);
     cScreenWidth:= StrToInt(gameArgs[1]);
@@ -345,7 +346,6 @@
     cOrigScreenWidth:= cScreenWidth;
     cOrigScreenHeight:= cScreenHeight;
 
-    initEverything(true);
     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
     AddFileLog('Prefix: "' + PathPrefix +'"');
     AddFileLog('UserPrefix: "' + UserPathPrefix +'"');
@@ -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 SDL13}uTouch.initModule;{$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,18 +523,17 @@
 
     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
+{$IFDEF HWLIBRARY}
     initEverything(false);
-{$IFDEF HWLIBRARY}
     WriteLnToConsole('Preview connecting on port ' + inttostr(port));
     ipcPort:= port;
     InitStepsFlags:= cifRandomize;
@@ -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;
@@ -591,16 +585,28 @@
     if (ParamCount < 3) then
         GameType:= gmtSyntax
     else
-        if (ParamCount = 3) and ((ParamStr(3) = '--stats-only') or (ParamStr(3) = 'landpreview')) then
-            internalSetGameTypeLandPreviewFromParameters()
-        else if ParamCount = cDefaultParamNum then
-            internalStartGameWithParameters()
+        if (ParamCount = 3) and (ParamStr(3) = 'landpreview') then
+            begin
+            initEverything(false);
+            ipcPort:= StrToInt(ParamStr(2));
+            GameType:= gmtLandPreview;
+            exit;
+            end
+        else
+            begin
+            initEverything(true);
+            if (ParamCount = 3) and (ParamStr(3) = '--stats-only') then
+                playReplayFileWithParameters()
+            else
+                if ParamCount = cDefaultParamNum then
+                    internalStartGameWithParameters()
 {$IFDEF USE_VIDEO_RECORDING}
-        else if ParamCount = cVideorecParamNum then
-            internalStartVideoRecordingWithParameters()
+                else if ParamCount = cVideorecParamNum then
+                    internalStartVideoRecordingWithParameters()
 {$ENDIF}
-        else
-            playReplayFileWithParameters();
+                else
+                    playReplayFileWithParameters();
+            end
 end;
 
 ////////////////////////////////////////////////////////////////////////////////