hedgewars/hwengine.pas
branchqmlfrontend
changeset 11403 b894922d58cc
parent 11071 3851ce4f2061
parent 11365 b8b208501475
child 11434 23912c93935a
equal deleted inserted replaced
11076:fcbdee9cdd74 11403:b894922d58cc
    23 {$ENDIF}
    23 {$ENDIF}
    24 
    24 
    25 unit hwengine;
    25 unit hwengine;
    26 interface
    26 interface
    27 
    27 
    28 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler
    28 uses {$IFDEF IPHONEOS}cmem, {$ENDIF} SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler
    29      , uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uAILandMarks, uLandTexture, uCollisions
    29      , uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uAILandMarks, uLandTexture, uCollisions
    30      , SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
    30      , SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
    31      , uPhysFSLayer, uCursor, uRandom, ArgParsers, uVisualGearsHandlers, uTextures, uRender
    31      , uPhysFSLayer, uCursor, uRandom, ArgParsers, uVisualGearsHandlers, uTextures, uRender
    32      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    32      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    33      {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
    33      {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
   141 ///////////////////////////////////////////////////////////////////////////////
   141 ///////////////////////////////////////////////////////////////////////////////
   142 procedure MainLoop;
   142 procedure MainLoop;
   143 var event: TSDL_Event;
   143 var event: TSDL_Event;
   144     PrevTime, CurrTime: LongWord;
   144     PrevTime, CurrTime: LongWord;
   145     isTerminated: boolean;
   145     isTerminated: boolean;
   146 {$IFDEF SDL2}
       
   147     previousGameState: TGameState;
   146     previousGameState: TGameState;
   148 {$ELSE}
       
   149     prevFocusState: boolean;
       
   150 {$ENDIF}
       
   151 begin
   147 begin
   152     isTerminated:= false;
   148     isTerminated:= false;
   153     PrevTime:= SDL_GetTicks;
   149     PrevTime:= SDL_GetTicks;
   154     while isTerminated = false do
   150     while isTerminated = false do
   155     begin
   151     begin
   156         SDL_PumpEvents();
   152         SDL_PumpEvents();
   157 
   153 
   158         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, {$IFDEF SDL2}SDL_FIRSTEVENT, SDL_LASTEVENT{$ELSE}SDL_ALLEVENTS{$ENDIF}) > 0 do
   154         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do
   159         begin
   155         begin
   160             case event.type_ of
   156             case event.type_ of
   161 {$IFDEF SDL2}
       
   162                 SDL_KEYDOWN:
   157                 SDL_KEYDOWN:
   163                     if GameState = gsChat then
   158                     if GameState = gsChat then
   164                         begin
   159                         begin
   165                     // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
   160                     // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
   166                         KeyPressChat(SDL_GetKeyFromScancode(event.key.keysym.sym), event.key.keysym.sym, event.key.keysym.modifier);
   161                         KeyPressChat(event.key.keysym);
   167                         end
   162                         end
   168                     else
   163                     else
   169                         if GameState >= gsGame then ProcessKey(event.key);
   164                         if GameState >= gsGame then ProcessKey(event.key);
   170                 SDL_KEYUP:
   165                 SDL_KEYUP:
   171                     if (GameState <> gsChat) and (GameState >= gsGame) then
   166                     if (GameState <> gsChat) and (GameState >= gsGame) then
   172                         ProcessKey(event.key);
   167                         ProcessKey(event.key);
       
   168 
       
   169                 SDL_MOUSEBUTTONDOWN:
       
   170                     if GameState = gsConfirm then
       
   171                         ParseCommand('quit', true)
       
   172                     else
       
   173                         if (GameState >= gsGame) then ProcessMouse(event.button, true);
       
   174 
       
   175                 SDL_MOUSEBUTTONUP:
       
   176                     if (GameState >= gsGame) then ProcessMouse(event.button, false);
       
   177 
       
   178                 SDL_MOUSEWHEEL:
       
   179                     ProcessMouseWheel(event.wheel.x, event.wheel.y);
       
   180 
       
   181                 SDL_TEXTINPUT: uChat.TextInput(event.text);
   173 
   182 
   174                 SDL_WINDOWEVENT:
   183                 SDL_WINDOWEVENT:
   175                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   184                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   176                     begin
   185                     begin
   177                         cHasFocus:= true;
   186                         cHasFocus:= true;
   204                     onTouchDown(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId);
   213                     onTouchDown(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId);
   205 
   214 
   206                 SDL_FINGERUP:
   215                 SDL_FINGERUP:
   207                     onTouchUp(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId);
   216                     onTouchUp(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId);
   208 {$ENDIF}
   217 {$ENDIF}
   209 {$ELSE}
       
   210                 SDL_KEYDOWN:
       
   211                     if GameState = gsChat then
       
   212                         KeyPressChat(event.key.keysym.unicode, event.key.keysym.sym, event.key.keysym.modifier)
       
   213                     else
       
   214                         if GameState >= gsGame then ProcessKey(event.key);
       
   215                 SDL_KEYUP:
       
   216                     if (GameState <> gsChat) and (GameState >= gsGame) then
       
   217                         ProcessKey(event.key);
       
   218 
       
   219                 SDL_MOUSEBUTTONDOWN:
       
   220                     if GameState = gsConfirm then
       
   221                         ParseCommand('quit', true)
       
   222                     else
       
   223                         if (GameState >= gsGame) then ProcessMouse(event.button, true);
       
   224 
       
   225                 SDL_MOUSEBUTTONUP:
       
   226                     if (GameState >= gsGame) then ProcessMouse(event.button, false);
       
   227 
       
   228                 SDL_ACTIVEEVENT:
       
   229                     if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
       
   230                     begin
       
   231                         prevFocusState:= cHasFocus;
       
   232                         cHasFocus:= event.active.gain = 1;
       
   233                         if prevFocusState xor cHasFocus then
       
   234                             onFocusStateChanged()
       
   235                     end;
       
   236 
       
   237                 SDL_VIDEORESIZE:
       
   238                 begin
       
   239                     // using lower values than cMinScreenWidth or cMinScreenHeight causes widget overlap and off-screen widget parts
       
   240                     // Change by sheepluva:
       
   241                     // Let's only use even numbers for custom width/height since I ran into scaling issues with odd width values.
       
   242                     // Maybe just fixes the symptom not the actual cause(?), I'm too tired to find out :P
       
   243                     cNewScreenWidth:= max(2 * (event.resize.w div 2), cMinScreenWidth);
       
   244                     cNewScreenHeight:= max(2 * (event.resize.h div 2), cMinScreenHeight);
       
   245                     cScreenResizeDelay:= RealTicks+500;
       
   246                 end;
       
   247 {$ENDIF}
       
   248                 SDL_JOYAXISMOTION:
   218                 SDL_JOYAXISMOTION:
   249                     ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
   219                     ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
   250                 SDL_JOYHATMOTION:
   220                 SDL_JOYHATMOTION:
   251                     ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
   221                     ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
   252                 SDL_JOYBUTTONDOWN:
   222                 SDL_JOYBUTTONDOWN:
   338 
   308 
   339     for i:= 0 to ParamCount do
   309     for i:= 0 to ParamCount do
   340         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   310         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   341 
   311 
   342     WriteToConsole('Init SDL... ');
   312     WriteToConsole('Init SDL... ');
   343     if not cOnlyStats then SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, true);
   313     if not cOnlyStats then SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, 'SDL_Init', true);
   344     WriteLnToConsole(msgOK);
   314     WriteLnToConsole(msgOK);
   345 
   315 
   346 {$IFDEF SDL2}
   316     //SDL_StartTextInput();
   347     SDL_StartTextInput();
       
   348 {$ELSE}
       
   349     SDL_EnableUNICODE(1);
       
   350 {$ENDIF}
       
   351     SDL_ShowCursor(0);
   317     SDL_ShowCursor(0);
   352 
   318 
   353     if not cOnlyStats then
   319     if not cOnlyStats then
   354         begin
   320         begin
   355         WriteToConsole('Init SDL_ttf... ');
   321         WriteToConsole('Init SDL_ttf... ');
   356         SDLTry(TTF_Init() <> -1, true);
   322         SDLTry(TTF_Init() <> -1, 'TTF_Init', true);
   357         WriteLnToConsole(msgOK);
   323         WriteLnToConsole(msgOK);
   358         end;
   324         end;
   359 
   325 
   360 {$IFDEF USE_VIDEO_RECORDING}
   326 {$IFDEF USE_VIDEO_RECORDING}
   361     if GameType = gmtRecord then
   327     if GameType = gmtRecord then
   525     uScript.freeModule;
   491     uScript.freeModule;
   526 end;
   492 end;
   527 
   493 
   528 ///////////////////////////////////////////////////////////////////////////////
   494 ///////////////////////////////////////////////////////////////////////////////
   529 procedure GenLandPreview;
   495 procedure GenLandPreview;
       
   496 {$IFDEF MOBILE}
       
   497 var Preview: TPreview;
       
   498 {$ELSE}
   530 var Preview: TPreviewAlpha;
   499 var Preview: TPreviewAlpha;
       
   500 {$ENDIF}
   531 begin
   501 begin
   532     initEverything(false);
   502     initEverything(false);
   533 
   503 
   534     IPCWaitPongEvent;
   504     IPCWaitPongEvent;
   535     TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   505     TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   536 
   506 
   537     ScriptOnPreviewInit;
   507     ScriptOnPreviewInit;
       
   508 {$IFDEF MOBILE}
       
   509     GenPreview(Preview);
       
   510 {$ELSE}
   538     GenPreviewAlpha(Preview);
   511     GenPreviewAlpha(Preview);
       
   512 {$ENDIF}
   539     WriteLnToConsole('Sending preview...');
   513     WriteLnToConsole('Sending preview...');
   540     SendIPCRaw(@Preview, sizeof(Preview));
   514     SendIPCRaw(@Preview, sizeof(Preview));
   541     SendIPCRaw(@MaxHedgehogs, sizeof(byte));
   515     SendIPCRaw(@MaxHedgehogs, sizeof(byte));
   542     WriteLnToConsole('Preview sent, disconnect');
   516     WriteLnToConsole('Preview sent, disconnect');
   543     freeEverything(false);
   517     freeEverything(false);
   568 
   542 
   569     if GameType = gmtSyntax then
   543     if GameType = gmtSyntax then
   570         RunEngine:= HaltUsageError
   544         RunEngine:= HaltUsageError
   571     else
   545     else
   572     begin
   546     begin
   573         SDL_CreateThread(@EngineThread{$IFDEF SDL2}, 'engine'{$ENDIF}, nil);
   547         SDL_CreateThread(@EngineThread, 'engine', nil);
   574         RunEngine:= 0
   548         RunEngine:= 0
   575     end
   549     end
   576 end;
   550 end;
   577 
   551 
   578 end.
   552 end.