hedgewars/hwengine.pas
changeset 7628 bc7b1d228a2c
parent 7615 b39beffcf05e
child 7687 c73fd8cfa7c0
child 7837 3e031b3b33e6
--- a/hedgewars/hwengine.pas	Thu Aug 30 12:47:41 2012 -0400
+++ b/hedgewars/hwengine.pas	Thu Aug 30 13:02:19 2012 -0400
@@ -30,9 +30,11 @@
 {$ENDIF}
 
 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler, uSound,
-     uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions,
+     uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uLandTexture, uCollisions,
      SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
-     {$IFDEF SDL13}, uTouch{$ENDIF}{$IFDEF ANDROID}, GLUnit{$ENDIF};
+     {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
+     {$IFDEF SDL13}, uTouch{$ENDIF}{$IFDEF ANDROID}, GLUnit{$ENDIF}, uAILandMarks;
+
 
 {$IFDEF HWLIBRARY}
 procedure initEverything(complete:boolean);
@@ -57,6 +59,9 @@
         gsLandGen:
             begin
             GenMap;
+            uLandTexture.initModule;
+            UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false); 
+            uAILandMarks.initModule;
             ParseCommand('sendlanddigest', true);
             GameState:= gsStart;
             end;
@@ -81,7 +86,7 @@
             end;
         gsConfirm, gsGame:
             begin
-            DrawWorld(Lag); // never place between ProcessKbd and DoGameTick - bugs due to /put cmd and isCursorVisible
+            DrawWorld(Lag);
             DoGameTick(Lag);
             ProcessVisualGears(Lag);
             end;
@@ -101,18 +106,27 @@
 
     SwapBuffers;
 
+{$IFDEF USE_VIDEO_RECORDING}
+    if flagPrerecording then
+        SaveCameraPosition;
+{$ENDIF}
+
     if flagMakeCapture then
         begin
         flagMakeCapture:= false;
         {$IFDEF PAS2C}
-        s:= 'hw';
+        s:= '/Screenshots/hw';
         {$ELSE}
-        s:= 'hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
+        s:= '/Screenshots/hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
         {$ENDIF}
 
+        // flash
         playSound(sndShutter);
-        
-        if MakeScreenshot(s) then
+        ScreenFade:= sfFromWhite;
+        ScreenFadeValue:= sfMax;
+        ScreenFadeSpeed:= 5;
+
+        if MakeScreenshot(s, 1) then
             WriteLnToConsole('Screenshot saved: ' + s)
         else
             begin
@@ -261,6 +275,39 @@
     end;
 end;
 
+{$IFDEF USE_VIDEO_RECORDING}
+procedure RecorderMainLoop;
+var oldGameTicks, oldRealTicks, newGameTicks, newRealTicks: LongInt;
+begin
+    if not BeginVideoRecording() then
+        exit;
+    DoTimer(0); // gsLandGen -> gsStart
+    DoTimer(0); // gsStart -> gsGame
+
+    if not LoadNextCameraPosition(newRealTicks, newGameTicks) then
+        exit;
+    fastScrolling:= true;
+    DoGameTick(newGameTicks);
+    fastScrolling:= false;
+    oldRealTicks:= 0;
+    oldGameTicks:= newGameTicks;
+
+    while LoadNextCameraPosition(newRealTicks, newGameTicks) do
+    begin
+        IPCCheckSock();
+        DoGameTick(newGameTicks - oldGameTicks);
+        if GameState = gsExit then
+            break;
+        ProcessVisualGears(newRealTicks - oldRealTicks);
+        DrawWorld(newRealTicks - oldRealTicks);
+        EncodeFrame();
+        oldRealTicks:= newRealTicks;
+        oldGameTicks:= newGameTicks;
+    end;
+    StopVideoRecording();
+end;
+{$ENDIF}
+
 ///////////////
 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
 var p: TPathType;
@@ -327,11 +374,18 @@
     SDLTry(TTF_Init() <> -1, true);
     WriteLnToConsole(msgOK);
 
-    // show main window
-    if cFullScreen then
-        ParseCommand('fullscr 1', true)
+{$IFDEF USE_VIDEO_RECORDING}
+    if GameType = gmtRecord then
+        InitOffscreenOpenGL()
     else
-        ParseCommand('fullscr 0', true);
+{$ENDIF}
+        begin            
+        // show main window
+        if cFullScreen then
+            ParseCommand('fullscr 1', true)
+        else
+            ParseCommand('fullscr 0', true);
+        end;
 
     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
     InitKbdKeyTable();
@@ -368,12 +422,22 @@
 
     InitTeams();
     AssignStores();
+
+    if GameType = gmtRecord then
+        SetSound(false);
+
     InitSound();
 
     isDeveloperMode:= false;
     TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
     ParseCommand('rotmask', true);
-    MainLoop();
+
+{$IFDEF USE_VIDEO_RECORDING}
+    if GameType = gmtRecord then
+        RecorderMainLoop()
+    else
+{$ENDIF}
+        MainLoop();
 
     // clean up all the memory allocated
     freeEverything(true);
@@ -412,9 +476,7 @@
         //uLandGraphics does not need initialization
         //uLandObjects does not need initialization
         //uLandTemplates does not need initialization
-        uLandTexture.initModule;
         //uLocale does not need initialization
-        uRandom.initModule;
         uScript.initModule;
         uSound.initModule;
         uStats.initModule;
@@ -432,6 +494,7 @@
     begin
         WriteLnToConsole('Freeing resources...');
         uAI.freeModule;
+        uAILandMarks.freeModule;
         uAIMisc.freeModule;         //stub
         uCaptions.freeModule;
         uWorld.freeModule;
@@ -441,7 +504,7 @@
         uStats.freeModule;          //stub
         uSound.freeModule;
         uScript.freeModule;
-        uRandom.freeModule;         //stub
+        //uRandom does not need to be freed
         //uLocale does not need to be freed
         //uLandTemplates does not need to be freed
         uLandTexture.freeModule;
@@ -456,6 +519,7 @@
         //uAIAmmoTests does not need to be freed
         //uAIActions does not need to be freed
         uStore.freeModule;
+{$IFDEF USE_VIDEO_RECORDING}uVideoRec.freeModule;{$ENDIF}
     end;
 
     uIO.freeModule;
@@ -529,11 +593,14 @@
     else
         if (ParamCount = 3) and ((ParamStr(3) = '--stats-only') or (ParamStr(3) = 'landpreview')) then
             internalSetGameTypeLandPreviewFromParameters()
+        else if ParamCount = cDefaultParamNum then
+            internalStartGameWithParameters()
+{$IFDEF USE_VIDEO_RECORDING}
+        else if ParamCount = cVideorecParamNum then
+            internalStartVideoRecordingWithParameters()
+{$ENDIF}
         else
-            if (ParamCount = cDefaultParamNum) then
-                internalStartGameWithParameters()
-            else
-                playReplayFileWithParameters();
+            playReplayFileWithParameters();
 end;
 
 ////////////////////////////////////////////////////////////////////////////////