hedgewars/hwengine.pas
branchwebgl
changeset 8026 4a4f21070479
parent 8018 091293bc974f
child 8096 453917e94e55
equal deleted inserted replaced
8023:7de85783b823 8026:4a4f21070479
    17  *)
    17  *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 {$IFDEF WIN32}
    21 {$IFDEF WIN32}
    22 {$R res/hwengine.rc}
    22 {$R hwengine.rc}
    23 {$ENDIF}
    23 {$ENDIF}
    24 
    24 
    25 {$IFDEF HWLIBRARY}
    25 {$IFDEF HWLIBRARY}
    26 unit hwengine;
    26 unit hwengine;
    27 interface
    27 interface
    28 {$ELSE}
    28 {$ELSE}
    29 program hwengine;
    29 program hwengine;
    30 {$ENDIF}
    30 {$ENDIF}
    31 
    31 
    32 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler,
    32 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler,
    33      uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uAILandMarks, uLandTexture, uCollisions,
    33      uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uLandTexture, uCollisions, uAILandMarks,
    34      SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
    34      SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted, uFloat
    35      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    35      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
       
    36      {$IFDEF WEBGL}, uWeb{$ENDIF}
    36      {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
    37      {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
    37      {$IFDEF ANDROID}, GLUnit{$ENDIF}
    38      {$IFDEF ANDROID}, GLUnit{$ENDIF}
    38      ;
    39      ;
    39 
       
    40 
    40 
    41 {$IFDEF HWLIBRARY}
    41 {$IFDEF HWLIBRARY}
    42 procedure preInitEverything();
    42 procedure preInitEverything();
    43 procedure initEverything(complete:boolean);
    43 procedure initEverything(complete:boolean);
    44 procedure freeEverything(complete:boolean);
    44 procedure freeEverything(complete:boolean);
    50 procedure preInitEverything(); forward;
    50 procedure preInitEverything(); forward;
    51 procedure initEverything(complete:boolean); forward;
    51 procedure initEverything(complete:boolean); forward;
    52 procedure freeEverything(complete:boolean); forward;
    52 procedure freeEverything(complete:boolean); forward;
    53 {$ENDIF}
    53 {$ENDIF}
    54 
    54 
       
    55 {$IFDEF WEBGL}
       
    56 procedure playFile(path: PChar); forward;
       
    57 function isEngineRunning():Integer; forward;
       
    58 procedure shutdown();forward;
       
    59 function getRealTicks():Integer; forward;
       
    60 procedure mainhook(); forward;
       
    61 var
       
    62     args: array[0..3] of PChar;
       
    63     PrevTime, CurrTime: LongInt;
       
    64     isTerminated: boolean;
       
    65     prevFocusState: boolean;
       
    66     isRunning : boolean;
       
    67 {$ENDIF}
       
    68 
    55 ///////////////////////////////////////////////////////////////////////////////
    69 ///////////////////////////////////////////////////////////////////////////////
    56 function DoTimer(Lag: LongInt): boolean;
    70 function DoTimer(Lag: LongInt): boolean;
    57 var s: shortstring;
    71 var s : shortstring;
    58 begin
    72 begin
    59     DoTimer:= false;
    73     DoTimer:= false;
    60     inc(RealTicks, Lag);
    74     inc(RealTicks, Lag);
    61 
    75 
    62     case GameState of
    76     case GameState of
    88             ScriptCall('onGameStart');
   102             ScriptCall('onGameStart');
    89             GameState:= gsGame;
   103             GameState:= gsGame;
    90             end;
   104             end;
    91         gsConfirm, gsGame:
   105         gsConfirm, gsGame:
    92             begin
   106             begin
    93             DrawWorld(Lag);
   107 {$IFDEF WEBGL}
       
   108             drawworld_hook();
       
   109 {$ELSE}
       
   110             DrawWorld(Lag); // never place between ProcessKbd and DoGameTick - bugs due to /put cmd and isCursorVisible
       
   111 {$ENDIF}
    94             DoGameTick(Lag);
   112             DoGameTick(Lag);
    95             ProcessVisualGears(Lag);
   113             ProcessVisualGears(Lag);
    96             end;
   114             end;
    97         gsChat:
   115         gsChat:
    98             begin
   116             begin
   141 end;
   159 end;
   142 
   160 
   143 ///////////////////////////////////////////////////////////////////////////////
   161 ///////////////////////////////////////////////////////////////////////////////
   144 procedure MainLoop;
   162 procedure MainLoop;
   145 var event: TSDL_Event;
   163 var event: TSDL_Event;
   146     PrevTime, CurrTime: Longword;
   164 {$IFNDEF WEBGL}
       
   165     PrevTime, CurrTime: LongInt;
   147     isTerminated: boolean;
   166     isTerminated: boolean;
   148 {$IFDEF SDL13}
   167 {$IFDEF SDL13}
   149     previousGameState: TGameState;
   168     previousGameState: TGameState;
   150 {$ELSE}
   169 {$ELSE}
   151     prevFocusState: boolean;
   170     prevFocusState: boolean;
   152 {$ENDIF}
   171 {$ENDIF}
   153 begin
   172 
       
   173 {$ENDIF}
       
   174 
       
   175 begin
       
   176 
       
   177 {$IFNDEF WEBGL}
   154     isTerminated:= false;
   178     isTerminated:= false;
   155     PrevTime:= SDL_GetTicks;
   179     PrevTime:= SDL_GetTicks;
   156     while isTerminated = false do
   180     while isTerminated = false do
   157     begin
   181     begin
       
   182 {$ENDIF}
       
   183 
   158         SDL_PumpEvents();
   184         SDL_PumpEvents();
   159  
   185  
   160         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, {$IFDEF SDL13}SDL_FIRSTEVENT, SDL_LASTEVENT{$ELSE}SDL_ALLEVENTS{$ENDIF}) > 0 do
   186         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, {$IFDEF SDL13}SDL_FIRSTEVENT, SDL_LASTEVENT{$ELSE}SDL_ALLEVENTS{$ENDIF}) > 0 do
   161         begin
   187         begin
   162             case event.type_ of
   188             case event.type_ of
   269         end;
   295         end;
   270 
   296 
   271         CurrTime:= SDL_GetTicks();
   297         CurrTime:= SDL_GetTicks();
   272         if PrevTime + longword(cTimerInterval) <= CurrTime then
   298         if PrevTime + longword(cTimerInterval) <= CurrTime then
   273         begin
   299         begin
   274             isTerminated := isTerminated or DoTimer(CurrTime - PrevTime);
   300             isTerminated:= isTerminated or DoTimer(CurrTime - PrevTime);
   275             PrevTime:= CurrTime
   301             PrevTime:= CurrTime;
       
   302             {$IFDEF WEBGL}
       
   303             if not isTerminated then
       
   304                 mainloop_hook();
       
   305             else
       
   306             begin
       
   307                 freeEverything(true);
       
   308                 isRunning := false;
       
   309             end
       
   310             {$ENDIF}
   276         end
   311         end
   277         else SDL_Delay(1);
   312         else {$IFNDEF WEBGL}SDL_Delay(1){$ELSE}mainloop_hook(){$ENDIF};
   278         IPCCheckSock();
   313         IPCCheckSock();
       
   314 
       
   315 {$IFNDEF WEBGL}
   279     end;
   316     end;
       
   317 {$ENDIF}
   280 end;
   318 end;
   281 
   319 
   282 {$IFDEF USE_VIDEO_RECORDING}
   320 {$IFDEF USE_VIDEO_RECORDING}
   283 procedure RecorderMainLoop;
   321 procedure RecorderMainLoop;
   284 var oldGameTicks, oldRealTicks, newGameTicks, newRealTicks: LongInt;
   322 var oldGameTicks, oldRealTicks, newGameTicks, newRealTicks: LongInt;
   315 ///////////////////////////////////////////////////////////////////////////////
   353 ///////////////////////////////////////////////////////////////////////////////
   316 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   354 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   317 var p: TPathType;
   355 var p: TPathType;
   318     s: shortstring;
   356     s: shortstring;
   319     i: LongInt;
   357     i: LongInt;
       
   358 {$IFDEF WEBGL}
       
   359     l:TResourceList;
       
   360 {$ENDIF}
       
   361 
   320 begin
   362 begin
   321 {$IFDEF HWLIBRARY}
   363 {$IFDEF HWLIBRARY}
   322     preInitEverything();
   364     preInitEverything();
   323     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   365     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   324     ipcPort:= StrToInt(gameArgs[0]);
   366     ipcPort:= StrToInt(gameArgs[0]);
   407         LoadRecordFromFile(recordFileName);
   449         LoadRecordFromFile(recordFileName);
   408 
   450 
   409     ScriptOnGameInit;
   451     ScriptOnGameInit;
   410     s:= 'eproto ' + inttostr(cNetProtoVersion);
   452     s:= 'eproto ' + inttostr(cNetProtoVersion);
   411     SendIPCRaw(@s[0], Length(s) + 1); // send proto version
   453     SendIPCRaw(@s[0], Length(s) + 1); // send proto version
   412 
   454 	
   413     InitTeams();
   455     InitTeams();
   414     AssignStores();
   456     AssignStores();
   415 
   457 
   416     if GameType = gmtRecord then
   458     if GameType = gmtRecord then
   417         SetSound(false);
   459         SetSound(false);
   422     TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   464     TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   423     ParseCommand('rotmask', true);
   465     ParseCommand('rotmask', true);
   424 
   466 
   425 {$IFDEF USE_VIDEO_RECORDING}
   467 {$IFDEF USE_VIDEO_RECORDING}
   426     if GameType = gmtRecord then
   468     if GameType = gmtRecord then
   427         RecorderMainLoop()
   469     begin
   428     else
   470         RecorderMainLoop();
   429 {$ENDIF}
   471         freeEverything(true);
   430         MainLoop();
   472         exit;
   431 
   473     end;
       
   474 {$ENDIF}
       
   475 	
       
   476 {$IFDEF WEBGL}
       
   477     l := generateResourceList();
       
   478     clear_filelist_hook();
       
   479     for i:= 0 to l.count - 1 do
       
   480     add_file_hook(PChar(l.files[i] + '.png'));
       
   481     isTerminated := false;
       
   482     isRunning := true;
       
   483     PrevTime := SDL_GetTicks();
       
   484     idb_loader_hook();
       
   485 {$ELSE}
       
   486     MainLoop;
   432     // clean up all the memory allocated
   487     // clean up all the memory allocated
   433     freeEverything(true);
   488     freeEverything(true);
       
   489 {$ENDIF}
   434 end;
   490 end;
   435 
   491 
   436 ///////////////////////////////////////////////////////////////////////////////
   492 ///////////////////////////////////////////////////////////////////////////////
   437 // preInitEverything - init variables that are going to be ovewritten by arguments
   493 // preInitEverything - init variables that are going to be ovewritten by arguments
   438 // initEverything - init variables only. Should be coupled by below
   494 // initEverything - init variables only. Should be coupled by below
   602 end;
   658 end;
   603 
   659 
   604 ///////////////////////////////////////////////////////////////////////////////
   660 ///////////////////////////////////////////////////////////////////////////////
   605 /////////////////////////////////// m a i n ///////////////////////////////////
   661 /////////////////////////////////// m a i n ///////////////////////////////////
   606 ///////////////////////////////////////////////////////////////////////////////
   662 ///////////////////////////////////////////////////////////////////////////////
   607 begin
   663 {$IFDEF WEBGL}
       
   664 procedure hwmain(argc:Integer; argv:PPChar);
       
   665 {$ENDIF}
       
   666 begin
       
   667 {$IFDEF PAS2C}
       
   668     // workaround for pascal's ParamStr and ParamCount
       
   669     init(argc, argv);
       
   670 {$IFDEF WEBGL}
       
   671     // patch emscripten's SDL implementation
       
   672     SDL_InitPatch();
       
   673 {$ENDIF}
       
   674 {$ENDIF}
   608     preInitEverything();
   675     preInitEverything();
   609     GetParams();
   676     GetParams();
   610 
   677 
   611     if GameType = gmtLandPreview then
   678     if GameType = gmtLandPreview then
   612         GenLandPreview()
   679         GenLandPreview()
   613     else if GameType = gmtSyntax then
   680     else if GameType = gmtSyntax then
   614         DisplayUsage()
   681         DisplayUsage()
   615     else Game();
   682     else Game();
   616 
   683 
   617     // return 1 when engine is not called correctly
   684     // return 1 when engine is not called correctly
   618     halt(LongInt(GameType = gmtSyntax));
   685     {$IFDEF PAS2C}
       
   686        {$IFNDEF WEBGL}
       
   687        exit(LongInt(GameType = gmtSyntax));
       
   688        {$ENDIF}
       
   689     {$ELSE}
       
   690        halt(LongInt(GameType = gmtSyntax));
       
   691     {$ENDIF}
       
   692 
       
   693 
       
   694 {$IFDEF WEBGL}
       
   695 end;
       
   696 
       
   697 // hook
       
   698 procedure playFile(path: PChar);
       
   699 begin
       
   700     args[0] := PChar('');
       
   701     args[1] := PChar('');
       
   702     args[2] := PChar('Data');
       
   703     args[3] := path;
       
   704     hwmain(4, args);
       
   705 end;
       
   706 
       
   707 // hook
       
   708 function isEngineRunning:Integer;
       
   709 begin
       
   710     isEngineRunning := isRunning;
       
   711 end;
       
   712 
       
   713 // hook
       
   714 procedure shutdown;
       
   715 begin
       
   716     GameState := gsExit;
       
   717 end;
       
   718 
       
   719 // hook
       
   720 function getRealTicks():Integer;
       
   721 begin
       
   722     getRealTicks := RealTicks;
       
   723 end;
       
   724 
       
   725 // main
       
   726 begin
       
   727     isRunning := false;
       
   728 
       
   729     // avoid hooks to be eliminated by optimizer
       
   730     if argc = 1234 then
       
   731     begin
       
   732         mainhook();
       
   733         isRunning := isEngineRunning();
       
   734         playFile(argv);
       
   735         argc := getRealTicks();
       
   736         DrawWorld(argc);
       
   737         MainLoop;
       
   738         shutdown;
       
   739     end
       
   740 {$ENDIF}
       
   741 
   619 {$ENDIF}
   742 {$ENDIF}
   620 end.
   743 end.