hedgewars/hwengine.pas
changeset 7896 67217e6108fd
parent 7850 fcbb024090a4
child 7929 6e01c5134eb5
equal deleted inserted replaced
7894:1e8e9400e9de 7896:67217e6108fd
    37      {$IFDEF ANDROID}, GLUnit{$ENDIF}
    37      {$IFDEF ANDROID}, GLUnit{$ENDIF}
    38      ;
    38      ;
    39 
    39 
    40 
    40 
    41 {$IFDEF HWLIBRARY}
    41 {$IFDEF HWLIBRARY}
       
    42 procedure preInitEverything();
    42 procedure initEverything(complete:boolean);
    43 procedure initEverything(complete:boolean);
    43 procedure freeEverything(complete:boolean);
    44 procedure freeEverything(complete:boolean);
    44 procedure Game(gameArgs: PPChar); cdecl; export;
    45 procedure Game(gameArgs: PPChar); cdecl; export;
    45 procedure GenLandPreview(port: Longint); cdecl; export;
    46 procedure GenLandPreview(port: Longint); cdecl; export;
    46 
    47 
    47 implementation
    48 implementation
    48 {$ELSE}
    49 {$ELSE}
       
    50 procedure preInitEverything(); forward;
    49 procedure initEverything(complete:boolean); forward;
    51 procedure initEverything(complete:boolean); forward;
    50 procedure freeEverything(complete:boolean); forward;
    52 procedure freeEverything(complete:boolean); forward;
    51 {$ENDIF}
    53 {$ENDIF}
    52 
    54 
    53 ////////////////////////////////////////////////////////////////////////////////
    55 ///////////////////////////////////////////////////////////////////////////////
    54 function DoTimer(Lag: LongInt): boolean;
    56 function DoTimer(Lag: LongInt): boolean;
    55 var s: shortstring;
    57 var s: shortstring;
    56 begin
    58 begin
    57     DoTimer:= false;
    59     DoTimer:= false;
    58     inc(RealTicks, Lag);
    60     inc(RealTicks, Lag);
   136             AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)');
   138             AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)');
   137             end
   139             end
   138         end;
   140         end;
   139 end;
   141 end;
   140 
   142 
   141 ////////////////////////////////////////////////////////////////////////////////
   143 ///////////////////////////////////////////////////////////////////////////////
   142 procedure MainLoop;
   144 procedure MainLoop;
   143 var event: TSDL_Event;
   145 var event: TSDL_Event;
   144     PrevTime, CurrTime: Longword;
   146     PrevTime, CurrTime: Longword;
   145     isTerminated: boolean;
   147     isTerminated: boolean;
   146 {$IFDEF SDL13}
   148 {$IFDEF SDL13}
   308     end;
   310     end;
   309     StopVideoRecording();
   311     StopVideoRecording();
   310 end;
   312 end;
   311 {$ENDIF}
   313 {$ENDIF}
   312 
   314 
   313 ////////////////////////////////////////////////////////////////////////////////
   315 ///////////////////////////////////////////////////////////////////////////////
   314 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   316 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   315 var p: TPathType;
   317 var p: TPathType;
   316     s: shortstring;
   318     s: shortstring;
   317     i: LongInt;
   319     i: LongInt;
   318 begin
   320 begin
   319 {$IFDEF HWLIBRARY}
   321 {$IFDEF HWLIBRARY}
   320     initEverything(true);
   322     preInitEverything();
   321     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   323     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   322     ipcPort:= StrToInt(gameArgs[0]);
   324     ipcPort:= StrToInt(gameArgs[0]);
   323     cScreenWidth:= StrToInt(gameArgs[1]);
   325     cScreenWidth:= StrToInt(gameArgs[1]);
   324     cScreenHeight:= StrToInt(gameArgs[2]);
   326     cScreenHeight:= StrToInt(gameArgs[2]);
   325     cReducedQuality:= StrToInt(gameArgs[3]);
   327     cReducedQuality:= StrToInt(gameArgs[3]);
   326     cLocaleFName:= gameArgs[4];
   328     cLocaleFName:= gameArgs[4];
   327     // cFullScreen functionality is platform dependent, ifdef it if you need to modify it
       
   328     cFullScreen:= false;
       
   329     
       
   330     if (Length(cLocaleFName) > 6) then
       
   331         cLocale := Copy(cLocaleFName,1,5)
       
   332     else
       
   333         cLocale := Copy(cLocaleFName,1,2);
       
   334         
       
   335     UserNick:= gameArgs[5];
   329     UserNick:= gameArgs[5];
   336     SetSound(gameArgs[6] = '1');
   330     SetSound(gameArgs[6] = '1');
   337     SetMusic(gameArgs[7] = '1');
   331     SetMusic(gameArgs[7] = '1');
   338     cAltDamage:= gameArgs[8] = '1';
   332     cAltDamage:= gameArgs[8] = '1';
   339     PathPrefix:= gameArgs[9];
   333     PathPrefix:= gameArgs[9];
   340     UserPathPrefix:= '../Documents';
   334     UserPathPrefix:= '../Documents';
   341     recordFileName:= gameArgs[10];
   335     recordFileName:= gameArgs[10];
   342     cStereoMode:= smNone;
   336 {$ENDIF}
   343 {$ENDIF}
   337     initEverything(true);
   344     cMinScreenWidth:= min(cScreenWidth, cMinScreenWidth);
       
   345     cMinScreenHeight:= min(cScreenHeight, cMinScreenHeight);
       
   346     cOrigScreenWidth:= cScreenWidth;
       
   347     cOrigScreenHeight:= cScreenHeight;
       
   348 
   338 
   349     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   339     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   350     AddFileLog('Prefix: "' + PathPrefix +'"');
   340     AddFileLog('Prefix: "' + PathPrefix +'"');
   351     AddFileLog('UserPrefix: "' + UserPathPrefix +'"');
   341     AddFileLog('UserPrefix: "' + UserPathPrefix +'"');
   352     
   342     
   441 
   431 
   442     // clean up all the memory allocated
   432     // clean up all the memory allocated
   443     freeEverything(true);
   433     freeEverything(true);
   444 end;
   434 end;
   445 
   435 
   446 ////////////////////////////////////////////////////////////////////////////////
   436 ///////////////////////////////////////////////////////////////////////////////
   447 // As a rule of thumb, every module that is listed in either initEverything or 
   437 // preInitEverything - init variables that are going to be ovewritten by arguments
   448 // freeEverything should come in pair, even if they are stubs. Only use this 
   438 // initEverything - init variables only. Should be coupled by below
   449 // section for inialising variables and remeber that game args overwrite these,
   439 // freeEverything - free above. Pay attention to the init/free order!
   450 // so handle this section with care. Pay attention to the init/free order too! 
   440 procedure preInitEverything;
       
   441 begin
       
   442     Randomize();
       
   443 
       
   444     uVariables.preInitModule;
       
   445     uSound.preInitModule;
       
   446 end;
       
   447 
   451 procedure initEverything (complete:boolean);
   448 procedure initEverything (complete:boolean);
   452 begin
   449 begin
   453     Randomize();
       
   454 
       
   455     uUtils.initModule(complete);    // opens the debug file, must be the first
   450     uUtils.initModule(complete);    // opens the debug file, must be the first
   456     uVariables.initModule;          // inits all global variables
   451     uVariables.initModule;          // inits all global variables
   457     uConsole.initModule;            // opens stdout
   452     uConsole.initModule;            // opens stdout
   458     uCommands.initModule;           // helps below
   453     uCommands.initModule;           // helps below
   459     uCommandHandlers.initModule;    // registers all messages from frontend
   454     uCommandHandlers.initModule;    // registers all messages from frontend
   526     uConsole.freeModule;            // closes stdout
   521     uConsole.freeModule;            // closes stdout
   527     uVariables.freeModule;
   522     uVariables.freeModule;
   528     uUtils.freeModule;              // closes debug file
   523     uUtils.freeModule;              // closes debug file
   529 end;
   524 end;
   530 
   525 
   531 ////////////////////////////////////////////////////////////////////////////////
   526 ///////////////////////////////////////////////////////////////////////////////
   532 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
   527 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
   533 var Preview: TPreview;
   528 var Preview: TPreview;
   534 begin
   529 begin
       
   530     initEverything(false);
   535 {$IFDEF HWLIBRARY}
   531 {$IFDEF HWLIBRARY}
   536     initEverything(false);
       
   537     WriteLnToConsole('Preview connecting on port ' + inttostr(port));
   532     WriteLnToConsole('Preview connecting on port ' + inttostr(port));
   538     ipcPort:= port;
   533     ipcPort:= port;
   539     InitStepsFlags:= cifRandomize;
   534     InitStepsFlags:= cifRandomize;
   540 {$ENDIF}
   535 {$ENDIF}
   541     InitIPC;
   536     InitIPC;
   549     WriteLnToConsole('Preview sent, disconnect');
   544     WriteLnToConsole('Preview sent, disconnect');
   550     freeEverything(false);
   545     freeEverything(false);
   551 end;
   546 end;
   552 
   547 
   553 {$IFNDEF HWLIBRARY}
   548 {$IFNDEF HWLIBRARY}
   554 ////////////////////////////////////////////////////////////////////////////////
   549 ///////////////////////////////////////////////////////////////////////////////
   555 procedure DisplayUsage;
   550 procedure DisplayUsage;
   556 var i: LongInt;
   551 var i: LongInt;
   557 begin
   552 begin
   558     WriteLn(stdout, 'Wrong argument format: correct configurations is');
   553     WriteLn(stdout, 'Wrong argument format: correct configurations is');
   559     WriteLn(stdout, '');
   554     WriteLn(stdout, '');
   575         Write(stdout, ParamStr(i) + ' ');
   570         Write(stdout, ParamStr(i) + ' ');
   576         
   571         
   577     WriteLn(stdout, '');
   572     WriteLn(stdout, '');
   578 end;
   573 end;
   579 
   574 
   580 ////////////////////////////////////////////////////////////////////////////////
   575 ///////////////////////////////////////////////////////////////////////////////
   581 {$INCLUDE "ArgParsers.inc"}
   576 {$INCLUDE "ArgParsers.inc"}
   582 
   577 
   583 procedure GetParams;
   578 procedure GetParams;
   584 begin
   579 begin
   585     if (ParamCount < 3) then
   580     if (ParamCount < 3) then
   586         GameType:= gmtSyntax
   581         GameType:= gmtSyntax
   587     else
   582     else
   588         if (ParamCount = 3) and (ParamStr(3) = 'landpreview') then
   583         if (ParamCount = 3) and (ParamStr(3) = 'landpreview') then
   589             begin
   584             begin
   590             initEverything(false);
       
   591             ipcPort:= StrToInt(ParamStr(2));
   585             ipcPort:= StrToInt(ParamStr(2));
   592             GameType:= gmtLandPreview;
   586             GameType:= gmtLandPreview;
   593             exit;
       
   594             end
   587             end
   595         else
   588         else
   596             begin
   589             begin
   597             initEverything(true);
       
   598             if (ParamCount = 3) and (ParamStr(3) = '--stats-only') then
   590             if (ParamCount = 3) and (ParamStr(3) = '--stats-only') then
   599                 playReplayFileWithParameters()
   591                 playReplayFileWithParameters()
   600             else
   592             else
   601                 if ParamCount = cDefaultParamNum then
   593                 if ParamCount = cDefaultParamNum then
   602                     internalStartGameWithParameters()
   594                     internalStartGameWithParameters()
   607                 else
   599                 else
   608                     playReplayFileWithParameters();
   600                     playReplayFileWithParameters();
   609             end
   601             end
   610 end;
   602 end;
   611 
   603 
   612 ////////////////////////////////////////////////////////////////////////////////
   604 ///////////////////////////////////////////////////////////////////////////////
   613 /////////////////////////////// m a i n ////////////////////////////////////////
   605 /////////////////////////////////// m a i n ///////////////////////////////////
   614 ////////////////////////////////////////////////////////////////////////////////
   606 ///////////////////////////////////////////////////////////////////////////////
   615 begin
   607 begin
       
   608     preInitEverything();
   616     GetParams();
   609     GetParams();
   617     if (Length(cLocaleFName) > 6) then
       
   618         cLocale := Copy(cLocaleFName,1,5)
       
   619     else
       
   620         cLocale := Copy(cLocaleFName,1,2);
       
   621 
   610 
   622     if GameType = gmtLandPreview then
   611     if GameType = gmtLandPreview then
   623         GenLandPreview()
   612         GenLandPreview()
   624     else if GameType = gmtSyntax then
   613     else if GameType = gmtSyntax then
   625         DisplayUsage()
   614         DisplayUsage()