hedgewars/hwengine.pas
changeset 7687 c73fd8cfa7c0
parent 7613 ce6ead3327b2
parent 7615 b39beffcf05e
child 7857 2bc61f8841a1
equal deleted inserted replaced
7613:ce6ead3327b2 7687:c73fd8cfa7c0
    30 {$ENDIF}
    30 {$ENDIF}
    31 
    31 
    32 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler, uSound,
    32 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler, uSound,
    33      uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uLandTexture, uCollisions,
    33      uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uLandTexture, uCollisions,
    34      SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
    34      SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
       
    35      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    35      {$IFDEF SDL13}, uTouch{$ENDIF}{$IFDEF ANDROID}, GLUnit{$ENDIF}, uAILandMarks;
    36      {$IFDEF SDL13}, uTouch{$ENDIF}{$IFDEF ANDROID}, GLUnit{$ENDIF}, uAILandMarks;
       
    37 
    36 
    38 
    37 {$IFDEF HWLIBRARY}
    39 {$IFDEF HWLIBRARY}
    38 procedure initEverything(complete:boolean);
    40 procedure initEverything(complete:boolean);
    39 procedure freeEverything(complete:boolean);
    41 procedure freeEverything(complete:boolean);
    40 procedure Game(gameArgs: PPChar); cdecl; export;
    42 procedure Game(gameArgs: PPChar); cdecl; export;
    82             ScriptCall('onGameStart');
    84             ScriptCall('onGameStart');
    83             GameState:= gsGame;
    85             GameState:= gsGame;
    84             end;
    86             end;
    85         gsConfirm, gsGame:
    87         gsConfirm, gsGame:
    86             begin
    88             begin
    87             DrawWorld(Lag); // never place between ProcessKbd and DoGameTick - bugs due to /put cmd and isCursorVisible
    89             DrawWorld(Lag);
    88             DoGameTick(Lag);
    90             DoGameTick(Lag);
    89             ProcessVisualGears(Lag);
    91             ProcessVisualGears(Lag);
    90             end;
    92             end;
    91         gsChat:
    93         gsChat:
    92             begin
    94             begin
   102             exit(false);
   104             exit(false);
   103             end;
   105             end;
   104 
   106 
   105     SwapBuffers;
   107     SwapBuffers;
   106 
   108 
       
   109 {$IFDEF USE_VIDEO_RECORDING}
       
   110     if flagPrerecording then
       
   111         SaveCameraPosition;
       
   112 {$ENDIF}
       
   113 
   107     if flagMakeCapture then
   114     if flagMakeCapture then
   108         begin
   115         begin
   109         flagMakeCapture:= false;
   116         flagMakeCapture:= false;
   110         {$IFDEF PAS2C}
   117         {$IFDEF PAS2C}
   111         s:= 'hw';
   118         s:= '/Screenshots/hw';
   112         {$ELSE}
   119         {$ELSE}
   113         s:= 'hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
   120         s:= '/Screenshots/hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
   114         {$ENDIF}
   121         {$ENDIF}
   115 
   122 
       
   123         // flash
   116         playSound(sndShutter);
   124         playSound(sndShutter);
   117         
   125         ScreenFade:= sfFromWhite;
   118         if MakeScreenshot(s) then
   126         ScreenFadeValue:= sfMax;
       
   127         ScreenFadeSpeed:= 5;
       
   128 
       
   129         if MakeScreenshot(s, 1) then
   119             WriteLnToConsole('Screenshot saved: ' + s)
   130             WriteLnToConsole('Screenshot saved: ' + s)
   120         else
   131         else
   121             begin
   132             begin
   122             WriteLnToConsole('Screenshot failed.');
   133             WriteLnToConsole('Screenshot failed.');
   123             AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)');
   134             AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)');
   262         else SDL_Delay(1);
   273         else SDL_Delay(1);
   263         IPCCheckSock();
   274         IPCCheckSock();
   264     end;
   275     end;
   265 end;
   276 end;
   266 
   277 
       
   278 {$IFDEF USE_VIDEO_RECORDING}
       
   279 procedure RecorderMainLoop;
       
   280 var oldGameTicks, oldRealTicks, newGameTicks, newRealTicks: LongInt;
       
   281 begin
       
   282     if not BeginVideoRecording() then
       
   283         exit;
       
   284     DoTimer(0); // gsLandGen -> gsStart
       
   285     DoTimer(0); // gsStart -> gsGame
       
   286 
       
   287     if not LoadNextCameraPosition(newRealTicks, newGameTicks) then
       
   288         exit;
       
   289     fastScrolling:= true;
       
   290     DoGameTick(newGameTicks);
       
   291     fastScrolling:= false;
       
   292     oldRealTicks:= 0;
       
   293     oldGameTicks:= newGameTicks;
       
   294 
       
   295     while LoadNextCameraPosition(newRealTicks, newGameTicks) do
       
   296     begin
       
   297         IPCCheckSock();
       
   298         DoGameTick(newGameTicks - oldGameTicks);
       
   299         if GameState = gsExit then
       
   300             break;
       
   301         ProcessVisualGears(newRealTicks - oldRealTicks);
       
   302         DrawWorld(newRealTicks - oldRealTicks);
       
   303         EncodeFrame();
       
   304         oldRealTicks:= newRealTicks;
       
   305         oldGameTicks:= newGameTicks;
       
   306     end;
       
   307     StopVideoRecording();
       
   308 end;
       
   309 {$ENDIF}
       
   310 
   267 ///////////////
   311 ///////////////
   268 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   312 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   269 var p: TPathType;
   313 var p: TPathType;
   270     s: shortstring;
   314     s: shortstring;
   271     i: LongInt;
   315     i: LongInt;
   328 
   372 
   329     WriteToConsole('Init SDL_ttf... ');
   373     WriteToConsole('Init SDL_ttf... ');
   330     SDLTry(TTF_Init() <> -1, true);
   374     SDLTry(TTF_Init() <> -1, true);
   331     WriteLnToConsole(msgOK);
   375     WriteLnToConsole(msgOK);
   332 
   376 
   333     // show main window
   377 {$IFDEF USE_VIDEO_RECORDING}
   334     if cFullScreen then
   378     if GameType = gmtRecord then
   335         ParseCommand('fullscr 1', true)
   379         InitOffscreenOpenGL()
   336     else
   380     else
   337         ParseCommand('fullscr 0', true);
   381 {$ENDIF}
       
   382         begin            
       
   383         // show main window
       
   384         if cFullScreen then
       
   385             ParseCommand('fullscr 1', true)
       
   386         else
       
   387             ParseCommand('fullscr 0', true);
       
   388         end;
   338 
   389 
   339     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
   390     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
   340     InitKbdKeyTable();
   391     InitKbdKeyTable();
   341     AddProgress();
   392     AddProgress();
   342 
   393 
   369     s:= 'eproto ' + inttostr(cNetProtoVersion);
   420     s:= 'eproto ' + inttostr(cNetProtoVersion);
   370     SendIPCRaw(@s[0], Length(s) + 1); // send proto version
   421     SendIPCRaw(@s[0], Length(s) + 1); // send proto version
   371 
   422 
   372     InitTeams();
   423     InitTeams();
   373     AssignStores();
   424     AssignStores();
       
   425 
       
   426     if GameType = gmtRecord then
       
   427         SetSound(false);
       
   428 
   374     InitSound();
   429     InitSound();
   375 
   430 
   376     isDeveloperMode:= false;
   431     isDeveloperMode:= false;
   377     TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   432     TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   378     ParseCommand('rotmask', true);
   433     ParseCommand('rotmask', true);
   379     MainLoop();
   434 
       
   435 {$IFDEF USE_VIDEO_RECORDING}
       
   436     if GameType = gmtRecord then
       
   437         RecorderMainLoop()
       
   438     else
       
   439 {$ENDIF}
       
   440         MainLoop();
   380 
   441 
   381     // clean up all the memory allocated
   442     // clean up all the memory allocated
   382     freeEverything(true);
   443     freeEverything(true);
   383 end;
   444 end;
   384 
   445 
   456         uChat.freeModule;
   517         uChat.freeModule;
   457         uAmmos.freeModule;
   518         uAmmos.freeModule;
   458         //uAIAmmoTests does not need to be freed
   519         //uAIAmmoTests does not need to be freed
   459         //uAIActions does not need to be freed
   520         //uAIActions does not need to be freed
   460         uStore.freeModule;
   521         uStore.freeModule;
       
   522 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.freeModule;{$ENDIF}
   461     end;
   523     end;
   462 
   524 
   463     uIO.freeModule;
   525     uIO.freeModule;
   464     uLand.freeModule;
   526     uLand.freeModule;
   465     uLandPainted.freeModule;
   527     uLandPainted.freeModule;
   529     if (ParamCount < 3) then
   591     if (ParamCount < 3) then
   530         GameType:= gmtSyntax
   592         GameType:= gmtSyntax
   531     else
   593     else
   532         if (ParamCount = 3) and ((ParamStr(3) = '--stats-only') or (ParamStr(3) = 'landpreview')) then
   594         if (ParamCount = 3) and ((ParamStr(3) = '--stats-only') or (ParamStr(3) = 'landpreview')) then
   533             internalSetGameTypeLandPreviewFromParameters()
   595             internalSetGameTypeLandPreviewFromParameters()
       
   596         else if ParamCount = cDefaultParamNum then
       
   597             internalStartGameWithParameters()
       
   598 {$IFDEF USE_VIDEO_RECORDING}
       
   599         else if ParamCount = cVideorecParamNum then
       
   600             internalStartVideoRecordingWithParameters()
       
   601 {$ENDIF}
   534         else
   602         else
   535             if (ParamCount = cDefaultParamNum) then
   603             playReplayFileWithParameters();
   536                 internalStartGameWithParameters()
       
   537             else
       
   538                 playReplayFileWithParameters();
       
   539 end;
   604 end;
   540 
   605 
   541 ////////////////////////////////////////////////////////////////////////////////
   606 ////////////////////////////////////////////////////////////////////////////////
   542 /////////////////////////////// m a i n ////////////////////////////////////////
   607 /////////////////////////////// m a i n ////////////////////////////////////////
   543 ////////////////////////////////////////////////////////////////////////////////
   608 ////////////////////////////////////////////////////////////////////////////////