hedgewars/hwengine.pas
branchwebgl
changeset 8330 aaefa587e277
parent 8096 453917e94e55
parent 8327 a6f3452f5f94
child 8340 46a9fde631f4
equal deleted inserted replaced
8116:d24257910f8d 8330:aaefa587e277
    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, uLandTexture, uCollisions,
    33      , uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uAILandMarks, uLandTexture, uCollisions
    34      uAILandMarks, SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers,
    34      , SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
    35      uLandPainted, uFloat, uPhysFSLayer
    35      , uPhysFSLayer, uCursor
    36      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    36      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    37      {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
    37      {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
    38      {$IFDEF ANDROID}, GLUnit{$ENDIF}
    38      {$IFDEF ANDROID}, GLUnit{$ENDIF}
    39      {$IFDEF WEBGL}, uWeb{$ENDIF}
    39      {$IFDEF WEBGL}, uWeb{$ENDIF}
    40      ;
    40      ;
    41 
    41 
       
    42 var isInternal: Boolean;
       
    43 
    42 {$IFDEF HWLIBRARY}
    44 {$IFDEF HWLIBRARY}
    43 procedure preInitEverything();
    45 procedure preInitEverything();
    44 procedure initEverything(complete:boolean);
    46 procedure initEverything(complete:boolean);
    45 procedure freeEverything(complete:boolean);
    47 procedure freeEverything(complete:boolean);
    46 procedure Game(gameArgs: PPChar); cdecl; export;
    48 procedure Game(argc: LongInt; argv: PPChar); cdecl; export;
    47 procedure GenLandPreview(port: Longint); cdecl; export;
    49 procedure GenLandPreview(port: Longint); cdecl; export;
    48 
    50 
    49 implementation
    51 implementation
    50 {$ELSE}
    52 {$ELSE}
    51 procedure preInitEverything(); forward;
    53 procedure preInitEverything(); forward;
    52 procedure initEverything(complete:boolean); forward;
    54 procedure initEverything(complete:boolean); forward;
    53 procedure freeEverything(complete:boolean); forward;
    55 procedure freeEverything(complete:boolean); forward;
    54 {$ENDIF}
    56 {$ENDIF}
       
    57 
       
    58 {$INCLUDE "ArgParsers.inc"}
    55 
    59 
    56 {$IFDEF WEBGL}
    60 {$IFDEF WEBGL}
    57 procedure playFile(path: PChar); forward;
    61 procedure playFile(path: PChar); forward;
    58 function isEngineRunning():Integer; forward;
    62 function isEngineRunning():Integer; forward;
    59 procedure shutdown();forward;
    63 procedure shutdown();forward;
   183     while isTerminated = false do
   187     while isTerminated = false do
   184     begin
   188     begin
   185 {$ENDIF}
   189 {$ENDIF}
   186 
   190 
   187         SDL_PumpEvents();
   191         SDL_PumpEvents();
   188  
   192 
   189         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, {$IFDEF SDL13}SDL_FIRSTEVENT, SDL_LASTEVENT{$ELSE}SDL_ALLEVENTS{$ENDIF}) > 0 do
   193         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, {$IFDEF SDL13}SDL_FIRSTEVENT, SDL_LASTEVENT{$ELSE}SDL_ALLEVENTS{$ENDIF}) > 0 do
   190         begin
   194         begin
   191             case event.type_ of
   195             case event.type_ of
   192 {$IFDEF SDL13}
   196 {$IFDEF SDL13}
   193                 SDL_KEYDOWN:
   197                 SDL_KEYDOWN:
   197                     else
   201                     else
   198                         ProcessKey(event.key);
   202                         ProcessKey(event.key);
   199                 SDL_KEYUP:
   203                 SDL_KEYUP:
   200                     if GameState <> gsChat then
   204                     if GameState <> gsChat then
   201                         ProcessKey(event.key);
   205                         ProcessKey(event.key);
   202                     
   206 
   203                 SDL_WINDOWEVENT:
   207                 SDL_WINDOWEVENT:
   204                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   208                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   205                     begin
   209                     begin
   206                         cHasFocus:= true;
   210                         cHasFocus:= true;
   207                         onFocusStateChanged()
   211                         onFocusStateChanged()
   223                     begin
   227                     begin
   224                         cNewScreenWidth:= max(2 * (event.window.data1 div 2), cMinScreenWidth);
   228                         cNewScreenWidth:= max(2 * (event.window.data1 div 2), cMinScreenWidth);
   225                         cNewScreenHeight:= max(2 * (event.window.data2 div 2), cMinScreenHeight);
   229                         cNewScreenHeight:= max(2 * (event.window.data2 div 2), cMinScreenHeight);
   226                         cScreenResizeDelay:= RealTicks + 500{$IFDEF IPHONEOS}div 2{$ENDIF};
   230                         cScreenResizeDelay:= RealTicks + 500{$IFDEF IPHONEOS}div 2{$ENDIF};
   227                     end;
   231                     end;
   228                         
   232 
   229                 SDL_FINGERMOTION:
   233                 SDL_FINGERMOTION:
   230                     onTouchMotion(event.tfinger.x, event.tfinger.y,event.tfinger.dx, event.tfinger.dy, event.tfinger.fingerId);
   234                     onTouchMotion(event.tfinger.x, event.tfinger.y,event.tfinger.dx, event.tfinger.dy, event.tfinger.fingerId);
   231                 
   235 
   232                 SDL_FINGERDOWN:
   236                 SDL_FINGERDOWN:
   233                     onTouchDown(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId);
   237                     onTouchDown(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId);
   234                 
   238 
   235                 SDL_FINGERUP:
   239                 SDL_FINGERUP:
   236                     onTouchUp(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId);
   240                     onTouchUp(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId);
   237 {$ELSE}
   241 {$ELSE}
   238                 SDL_KEYDOWN:
   242                 SDL_KEYDOWN:
   239                     if GameState = gsChat then
   243                     if GameState = gsChat then
   241                     else
   245                     else
   242                         ProcessKey(event.key);
   246                         ProcessKey(event.key);
   243                 SDL_KEYUP:
   247                 SDL_KEYUP:
   244                     if GameState <> gsChat then
   248                     if GameState <> gsChat then
   245                         ProcessKey(event.key);
   249                         ProcessKey(event.key);
   246                     
   250 
   247                 SDL_MOUSEBUTTONDOWN:
   251                 SDL_MOUSEBUTTONDOWN:
   248                     ProcessMouse(event.button, true);
   252                     if GameState = gsConfirm then
   249                     
   253                     begin
       
   254                         resetPosition();
       
   255                         ParseCommand('quit', true);
       
   256                     end
       
   257                     else
       
   258                         ProcessMouse(event.button, true);
       
   259 
   250                 SDL_MOUSEBUTTONUP:
   260                 SDL_MOUSEBUTTONUP:
   251                     ProcessMouse(event.button, false); 
   261                     ProcessMouse(event.button, false);
   252                     
   262 
   253                 SDL_ACTIVEEVENT:
   263                 SDL_ACTIVEEVENT:
   254                     if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   264                     if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   255                     begin
   265                     begin
   256                         prevFocusState:= cHasFocus;
   266                         prevFocusState:= cHasFocus;
   257                         cHasFocus:= event.active.gain = 1;
   267                         cHasFocus:= event.active.gain = 1;
   258                         if prevFocusState xor cHasFocus then
   268                         if prevFocusState xor cHasFocus then
   259                             onFocusStateChanged()
   269                             onFocusStateChanged()
   260                     end;
   270                     end;
   261                         
   271 
   262                 SDL_VIDEORESIZE:
   272                 SDL_VIDEORESIZE:
   263                 begin
   273                 begin
   264                     // using lower values than cMinScreenWidth or cMinScreenHeight causes widget overlap and off-screen widget parts
   274                     // using lower values than cMinScreenWidth or cMinScreenHeight causes widget overlap and off-screen widget parts
   265                     // Change by sheepluva:
   275                     // Change by sheepluva:
   266                     // Let's only use even numbers for custom width/height since I ran into scaling issues with odd width values.
   276                     // Let's only use even numbers for custom width/height since I ran into scaling issues with odd width values.
   352     StopVideoRecording();
   362     StopVideoRecording();
   353 end;
   363 end;
   354 {$ENDIF}
   364 {$ENDIF}
   355 
   365 
   356 ///////////////////////////////////////////////////////////////////////////////
   366 ///////////////////////////////////////////////////////////////////////////////
   357 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   367 procedure Game{$IFDEF HWLIBRARY}(argc: LongInt; argv: PPChar); cdecl; export{$ENDIF};
   358 var p: TPathType;
   368 var p: TPathType;
   359     s: shortstring;
   369     s: shortstring;
   360     i: LongInt;
   370     i: LongInt;
   361 {$IFDEF WEBGL}
   371 {$IFDEF WEBGL}
   362     l:TResourceList;
   372     l:TResourceList;
   363 {$ENDIF}
   373 {$ENDIF}
   364 
   374 
   365 begin
   375 begin
   366 {$IFDEF HWLIBRARY}
   376 {$IFDEF HWLIBRARY}
   367     preInitEverything();
   377     preInitEverything();
   368     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   378     parseCommandLine(argc, argv);
   369     ipcPort:= StrToInt(gameArgs[0]);
       
   370     cScreenWidth:= StrToInt(gameArgs[1]);
       
   371     cScreenHeight:= StrToInt(gameArgs[2]);
       
   372     cReducedQuality:= StrToInt(gameArgs[3]);
       
   373     cLocaleFName:= gameArgs[4];
       
   374     UserNick:= gameArgs[5];
       
   375     SetSound(gameArgs[6] = '1');
       
   376     SetMusic(gameArgs[7] = '1');
       
   377     cAltDamage:= gameArgs[8] = '1';
       
   378     PathPrefix:= gameArgs[9];
       
   379     UserPathPrefix:= '../Documents';
       
   380     recordFileName:= gameArgs[10];
       
   381 {$ENDIF}
   379 {$ENDIF}
   382     initEverything(true);
   380     initEverything(true);
   383 
       
   384     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   381     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   385     
   382 
   386     for i:= 0 to ParamCount do
   383     for i:= 0 to ParamCount do
   387         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   384         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   388 
   385 
   389     WriteToConsole('Init SDL... ');
   386     WriteToConsole('Init SDL... ');
   390     if not cOnlyStats then SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, true);
   387     if not cOnlyStats then SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, true);
   400 {$IFDEF USE_VIDEO_RECORDING}
   397 {$IFDEF USE_VIDEO_RECORDING}
   401     if GameType = gmtRecord then
   398     if GameType = gmtRecord then
   402         InitOffscreenOpenGL()
   399         InitOffscreenOpenGL()
   403     else
   400     else
   404 {$ENDIF}
   401 {$ENDIF}
   405         begin            
   402         begin
   406         // show main window
   403         // show main window
   407         if cFullScreen then
   404         if cFullScreen then
   408             ParseCommand('fullscr 1', true)
   405             ParseCommand('fullscr 1', true)
   409         else
   406         else
   410             ParseCommand('fullscr 0', true);
   407             ParseCommand('fullscr 0', true);
   458         RecorderMainLoop();
   455         RecorderMainLoop();
   459         freeEverything(true);
   456         freeEverything(true);
   460         exit;
   457         exit;
   461     end;
   458     end;
   462 {$ENDIF}
   459 {$ENDIF}
   463 	
   460 
   464 {$IFDEF WEBGL}
   461 {$IFDEF WEBGL}
   465     l := generateResourceList();
   462     l := generateResourceList();
   466     clear_filelist_hook();
   463     clear_filelist_hook();
   467     for i:= 0 to l.count - 1 do
   464     for i:= 0 to l.count - 1 do
   468     add_file_hook(PChar(l.files[i] + '.png'));
   465     add_file_hook(PChar(l.files[i] + '.png'));
   590     WriteLnToConsole('Preview sent, disconnect');
   587     WriteLnToConsole('Preview sent, disconnect');
   591     freeEverything(false);
   588     freeEverything(false);
   592 end;
   589 end;
   593 
   590 
   594 {$IFNDEF HWLIBRARY}
   591 {$IFNDEF HWLIBRARY}
   595 ///////////////////////////////////////////////////////////////////////////////
       
   596 procedure DisplayUsage;
       
   597 var i: LongInt;
       
   598 begin
       
   599     WriteLn(stdout, 'Wrong argument format: correct configurations is');
       
   600     WriteLn(stdout, '');
       
   601     WriteLn(stdout, '  hwengine <path to user hedgewars folder> <path to global data folder> <path to replay file> [options]');
       
   602     WriteLn(stdout, '');
       
   603     WriteLn(stdout, 'where [options] must be specified either as:');
       
   604     WriteLn(stdout, ' --set-video [screen width] [screen height] [color dept]');
       
   605     WriteLn(stdout, ' --set-audio [volume] [enable music] [enable sounds]');
       
   606     WriteLn(stdout, ' --set-other [language file] [full screen] [show FPS]');
       
   607     WriteLn(stdout, ' --set-multimedia [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
       
   608     WriteLn(stdout, ' --set-everything [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]');
       
   609     WriteLn(stdout, ' --stats-only');
       
   610     WriteLn(stdout, '');
       
   611     WriteLn(stdout, 'Read documentation online at http://code.google.com/p/hedgewars/wiki/CommandLineOptions for more information');
       
   612     WriteLn(stdout, '');
       
   613     Write(stdout, 'PARSED COMMAND: ');
       
   614     
       
   615     for i:=0 to ParamCount do
       
   616         Write(stdout, ParamStr(i) + ' ');
       
   617         
       
   618     WriteLn(stdout, '');
       
   619 end;
       
   620 
       
   621 ///////////////////////////////////////////////////////////////////////////////
       
   622 {$INCLUDE "ArgParsers.inc"}
       
   623 
       
   624 procedure GetParams;
       
   625 begin
       
   626     if (ParamCount < 3) then
       
   627         GameType:= gmtSyntax
       
   628     else
       
   629         if (ParamCount = 3) and (ParamStr(3) = 'landpreview') then
       
   630             begin
       
   631             ipcPort:= StrToInt(ParamStr(2));
       
   632             GameType:= gmtLandPreview;
       
   633             end
       
   634         else
       
   635             begin
       
   636             if (ParamCount = 3) and (ParamStr(3) = '--stats-only') then
       
   637                 playReplayFileWithParameters()
       
   638             else
       
   639                 if ParamCount = cDefaultParamNum then
       
   640                     internalStartGameWithParameters()
       
   641 {$IFDEF USE_VIDEO_RECORDING}
       
   642                 else if ParamCount = cVideorecParamNum then
       
   643                     internalStartVideoRecordingWithParameters()
       
   644 {$ENDIF}
       
   645                 else
       
   646                     playReplayFileWithParameters();
       
   647             end
       
   648 end;
       
   649 
   592 
   650 ///////////////////////////////////////////////////////////////////////////////
   593 ///////////////////////////////////////////////////////////////////////////////
   651 /////////////////////////////////// m a i n ///////////////////////////////////
   594 /////////////////////////////////// m a i n ///////////////////////////////////
   652 ///////////////////////////////////////////////////////////////////////////////
   595 ///////////////////////////////////////////////////////////////////////////////
   653 {$IFDEF WEBGL}
   596 {$IFDEF WEBGL}
   665     preInitEverything();
   608     preInitEverything();
   666     GetParams();
   609     GetParams();
   667 
   610 
   668     if GameType = gmtLandPreview then
   611     if GameType = gmtLandPreview then
   669         GenLandPreview()
   612         GenLandPreview()
   670     else if GameType = gmtSyntax then
   613     else if GameType <> gmtSyntax then
   671         DisplayUsage()
   614         Game();
   672     else Game();
       
   673 
   615 
   674     // return 1 when engine is not called correctly
   616     // return 1 when engine is not called correctly
   675     {$IFDEF PAS2C}
   617     {$IFDEF PAS2C}
   676        {$IFNDEF WEBGL}
   618        {$IFNDEF WEBGL}
   677        exit(LongInt(GameType = gmtSyntax));
   619        exit(LongInt(GameType = gmtSyntax));