another stake at variable pre-initialisation - we lost preview logging in the course
authorkoda
Tue, 30 Oct 2012 02:22:54 +0100
changeset 7896 67217e6108fd
parent 7894 1e8e9400e9de
child 7897 5e7c0810f365
another stake at variable pre-initialisation - we lost preview logging in the course
hedgewars/hwengine.pas
hedgewars/uSound.pas
hedgewars/uUtils.pas
hedgewars/uVariables.pas
--- a/hedgewars/hwengine.pas	Mon Oct 29 20:37:57 2012 -0400
+++ b/hedgewars/hwengine.pas	Tue Oct 30 02:22:54 2012 +0100
@@ -39,6 +39,7 @@
 
 
 {$IFDEF HWLIBRARY}
+procedure preInitEverything();
 procedure initEverything(complete:boolean);
 procedure freeEverything(complete:boolean);
 procedure Game(gameArgs: PPChar); cdecl; export;
@@ -46,11 +47,12 @@
 
 implementation
 {$ELSE}
+procedure preInitEverything(); forward;
 procedure initEverything(complete:boolean); forward;
 procedure freeEverything(complete:boolean); forward;
 {$ENDIF}
 
-////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
 function DoTimer(Lag: LongInt): boolean;
 var s: shortstring;
 begin
@@ -138,7 +140,7 @@
         end;
 end;
 
-////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
 procedure MainLoop;
 var event: TSDL_Event;
     PrevTime, CurrTime: Longword;
@@ -310,28 +312,20 @@
 end;
 {$ENDIF}
 
-////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
 var p: TPathType;
     s: shortstring;
     i: LongInt;
 begin
 {$IFDEF HWLIBRARY}
-    initEverything(true);
+    preInitEverything();
     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
     ipcPort:= StrToInt(gameArgs[0]);
     cScreenWidth:= StrToInt(gameArgs[1]);
     cScreenHeight:= StrToInt(gameArgs[2]);
     cReducedQuality:= StrToInt(gameArgs[3]);
     cLocaleFName:= gameArgs[4];
-    // cFullScreen functionality is platform dependent, ifdef it if you need to modify it
-    cFullScreen:= false;
-    
-    if (Length(cLocaleFName) > 6) then
-        cLocale := Copy(cLocaleFName,1,5)
-    else
-        cLocale := Copy(cLocaleFName,1,2);
-        
     UserNick:= gameArgs[5];
     SetSound(gameArgs[6] = '1');
     SetMusic(gameArgs[7] = '1');
@@ -339,12 +333,8 @@
     PathPrefix:= gameArgs[9];
     UserPathPrefix:= '../Documents';
     recordFileName:= gameArgs[10];
-    cStereoMode:= smNone;
 {$ENDIF}
-    cMinScreenWidth:= min(cScreenWidth, cMinScreenWidth);
-    cMinScreenHeight:= min(cScreenHeight, cMinScreenHeight);
-    cOrigScreenWidth:= cScreenWidth;
-    cOrigScreenHeight:= cScreenHeight;
+    initEverything(true);
 
     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
     AddFileLog('Prefix: "' + PathPrefix +'"');
@@ -443,15 +433,20 @@
     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);
+///////////////////////////////////////////////////////////////////////////////
+// preInitEverything - init variables that are going to be ovewritten by arguments
+// initEverything - init variables only. Should be coupled by below
+// freeEverything - free above. Pay attention to the init/free order!
+procedure preInitEverything;
 begin
     Randomize();
 
+    uVariables.preInitModule;
+    uSound.preInitModule;
+end;
+
+procedure initEverything (complete:boolean);
+begin
     uUtils.initModule(complete);    // opens the debug file, must be the first
     uVariables.initModule;          // inits all global variables
     uConsole.initModule;            // opens stdout
@@ -528,12 +523,12 @@
     uUtils.freeModule;              // closes debug file
 end;
 
-////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
 var Preview: TPreview;
 begin
+    initEverything(false);
 {$IFDEF HWLIBRARY}
-    initEverything(false);
     WriteLnToConsole('Preview connecting on port ' + inttostr(port));
     ipcPort:= port;
     InitStepsFlags:= cifRandomize;
@@ -551,7 +546,7 @@
 end;
 
 {$IFNDEF HWLIBRARY}
-////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
 procedure DisplayUsage;
 var i: LongInt;
 begin
@@ -577,7 +572,7 @@
     WriteLn(stdout, '');
 end;
 
-////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
 {$INCLUDE "ArgParsers.inc"}
 
 procedure GetParams;
@@ -587,14 +582,11 @@
     else
         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
@@ -609,15 +601,12 @@
             end
 end;
 
-////////////////////////////////////////////////////////////////////////////////
-/////////////////////////////// m a i n ////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
+/////////////////////////////////// m a i n ///////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
 begin
+    preInitEverything();
     GetParams();
-    if (Length(cLocaleFName) > 6) then
-        cLocale := Copy(cLocaleFName,1,5)
-    else
-        cLocale := Copy(cLocaleFName,1,2);
 
     if GameType = gmtLandPreview then
         GenLandPreview()
--- a/hedgewars/uSound.pas	Mon Oct 29 20:37:57 2012 -0400
+++ b/hedgewars/uSound.pas	Tue Oct 30 02:22:54 2012 +0100
@@ -35,6 +35,7 @@
 interface
 uses SDLh, uConsts, uTypes, SysUtils;
 
+procedure preInitModule;
 procedure initModule;
 procedure freeModule;
 
@@ -577,6 +578,13 @@
     MuteAudio;
 end;
 
+procedure preInitModule;
+begin
+    isMusicEnabled:= true;
+    isSoundEnabled:= true;
+    cInitVolume:= 100;
+end;
+
 procedure initModule;
 var t: LongInt;
     i: TSound;
@@ -586,12 +594,9 @@
 
     MusicFN:='';
     Mus:= nil;
-    isMusicEnabled:= true;
-    isSoundEnabled:= true;
     isAudioMuted:= false;
     isSEBackup:= isSoundEnabled;
     Volume:= 0;
-    cInitVolume:= 100;
     defVoicepack:= AskForVoicepack('Default');
 
     for i:= Low(TSound) to High(TSound) do
--- a/hedgewars/uUtils.pas	Mon Oct 29 20:37:57 2012 -0400
+++ b/hedgewars/uUtils.pas	Tue Oct 30 02:22:54 2012 +0100
@@ -73,7 +73,7 @@
 procedure WriteLn(var f: textfile; s: shortstring);
 {$ENDIF}
 
-procedure initModule(isGame: boolean);
+procedure initModule(isNotPreview: boolean);
 procedure freeModule;
 
 
@@ -401,14 +401,14 @@
 end;
 {$ENDIF}
 
-procedure initModule(isGame: boolean);
+procedure initModule(isNotPreview: boolean);
 {$IFDEF DEBUGFILE}
 var logfileBase: shortstring;
 {$IFNDEF MOBILE}var i: LongInt;{$ENDIF}
 {$ENDIF}
 begin
 {$IFDEF DEBUGFILE}
-    if isGame then
+    if isNotPreview then
     begin
         if GameType = gmtRecord then
             logfileBase:= 'rec'
@@ -422,7 +422,7 @@
 {$ENDIF}
 {$I-}
 {$IFDEF MOBILE}
-    {$IFDEF IPHONEOS} Assign(f,'../Documents/hw-' + logfileBase + '.log'); {$ENDIF}
+    {$IFDEF IPHONEOS} Assign(f, UserPathPrefix + '/hw-' + logfileBase + '.log'); {$ENDIF}
     {$IFDEF ANDROID} Assign(f,pathPrefix + '/' + logfileBase + '.log'); {$ENDIF}
     Rewrite(f);
 {$ELSE}
@@ -450,8 +450,6 @@
 
 procedure freeModule;
 begin
-recordFileName:= '';
-
 {$IFDEF DEBUGFILE}
     writeln(f, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
     flush(f);
--- a/hedgewars/uVariables.pas	Mon Oct 29 20:37:57 2012 -0400
+++ b/hedgewars/uVariables.pas	Tue Oct 30 02:22:54 2012 +0100
@@ -2532,44 +2532,63 @@
     trmsg:   array[TMsgStrId]  of ansistring;   // message of the event
     trgoal:  array[TGoalStrId] of ansistring;   // message of the goal
 
+procedure preInitModule;
 procedure initModule;
 procedure freeModule;
 
 implementation
 
 
-procedure initModule;
+procedure preInitModule;
 begin
-    // initialisation flags - they are going to be overwritten by args or by msgs
+    // initialisation flags - they are going to be overwritten by program args
+
     cScreenWidth    := 1024;
     cScreenHeight   := 768;
     cBits           := 32;
-    ipcPort         := 0;
-    cFullScreen     := false;
-    cLocaleFName    := 'en.txt';
-    cLocale         := 'en';
+    cShowFPS        := false;
+    cAltDamage      := true;
     cTimerInterval  := 8;
-    PathPrefix      := './';
-    UserPathPrefix  := './';
-    cShowFPS        := false;
-    cFlattenFlakes  := false;
-    cFlattenClouds  := false;
-    cAltDamage      := true;
     cReducedQuality := rqNone;
+    cLocaleFName    := 'en.txt';
+    cFullScreen     := false;
+
+    UserPathPrefix  := '';
+    ipcPort         := 0;
     UserNick        := '';
-    recordFileName  := '';
-    cScriptName     := '';
-    cReadyDelay     := 5000;
     cStereoMode     := smNone;
     GrayScale       := false;
+    PathPrefix      := './';
+    GameType        := gmtLocal;
 
-    cFlattenFlakes  := false;
-    cFlattenClouds  := false;
-    cOnlyStats      := False;
-    lastVisualGearByUID:= nil;
-    lastGearByUID:= nil;
-    
-    Pathz:= cPathz;
+{$IFDEF USE_VIDEO_RECORDING}
+    RecPrefix          := '';
+    cAVFormat          := '';
+    cVideoCodec        := '';
+    cVideoFramerateNum := 0;
+    cVideoFramerateDen := 0;
+    cVideoQuality      := 0;
+    cAudioCodec        := '';
+{$ENDIF}
+end;
+
+procedure initModule;
+begin
+
+    if (Length(cLocaleFName) > 6) then
+        cLocale := Copy(cLocaleFName,1,5)
+    else
+        cLocale := Copy(cLocaleFName,1,2);
+
+    cFlattenFlakes      := false;
+    cFlattenClouds      := false;
+    cOnlyStats          := False;
+    lastVisualGearByUID := nil;
+    lastGearByUID       := nil;
+    recordFileName      := '';
+    cReadyDelay         := 5000;
+    Pathz               := cPathz;
+
         {*  REFERENCE
       4096 -> $FFFFF000
       2048 -> $FFFFF800
@@ -2679,7 +2698,6 @@
     ReadyTimeLeft   := 0;
     
     disableLandBack := false;
-
     ScreenFade      := sfNone;
 
     // those values still are not perfect
@@ -2699,14 +2717,13 @@
     vobSDVelocity:= 15;
     vobSDFallSpeed:= 250;
 
-    cMinScreenWidth    := 640;
-    cMinScreenHeight   := 480;
-    cScreenWidth       := 1024;
-    cScreenHeight      := 768;
-    cOrigScreenWidth   := 1024;
-    cOrigScreenHeight  := 768;
-    cNewScreenWidth    := 1024;
-    cNewScreenHeight   := 768;
+    cMinScreenWidth:= min(cScreenWidth, 640);
+    cMinScreenHeight:= min(cScreenHeight, 480);
+    cOrigScreenWidth:= cScreenWidth;
+    cOrigScreenHeight:= cScreenHeight;
+
+    cNewScreenWidth    := cScreenWidth;
+    cNewScreenHeight   := cScreenHeight;
     cScreenResizeDelay := 0;
 
     LuaGoals:= '';