hedgewars/hwengine.pas
changeset 7025 831a25c74c7b
parent 7021 0c75512a8920
child 7026 8d1724e1337e
equal deleted inserted replaced
7024:3e1cca870090 7025:831a25c74c7b
    46 procedure initEverything(complete:boolean); forward;
    46 procedure initEverything(complete:boolean); forward;
    47 procedure freeEverything(complete:boolean); forward;
    47 procedure freeEverything(complete:boolean); forward;
    48 {$ENDIF}
    48 {$ENDIF}
    49 
    49 
    50 ////////////////////////////////
    50 ////////////////////////////////
    51 procedure DoTimer(Lag: LongInt);
    51 function DoTimer(Lag: LongInt): boolean;
    52 var s: shortstring;
    52 var s: shortstring;
    53 begin
    53 begin
       
    54     DoTimer:= false;
    54     inc(RealTicks, Lag);
    55     inc(RealTicks, Lag);
    55 
    56 
    56     case GameState of
    57     case GameState of
    57         gsLandGen:
    58         gsLandGen:
    58             begin
    59             begin
    92             DoGameTick(Lag);
    93             DoGameTick(Lag);
    93             ProcessVisualGears(Lag);
    94             ProcessVisualGears(Lag);
    94             end;
    95             end;
    95         gsExit:
    96         gsExit:
    96             begin
    97             begin
    97             isTerminated:= true;
    98             DoTimer:= true;
    98             end;
    99             end;
    99         gsSuspend:
   100         gsSuspend:
   100             exit;
   101             exit;
   101             end;
   102             end;
   102 
   103 
   137     SDL_DestroyWindow(SDLwindow);
   138     SDL_DestroyWindow(SDLwindow);
   138     SDLGLcontext:= nil;
   139     SDLGLcontext:= nil;
   139     SDLwindow:= nil;
   140     SDLwindow:= nil;
   140 {$ENDIF}
   141 {$ENDIF}
   141     SDL_Quit();
   142     SDL_Quit();
   142     isTerminated:= false;
       
   143 end;
   143 end;
   144 
   144 
   145 ///////////////////
   145 ///////////////////
   146 procedure MainLoop;
   146 procedure MainLoop;
   147 var event: TSDL_Event;
   147 var event: TSDL_Event;
   148     PrevTime, CurrTime: Longword;
   148     PrevTime, CurrTime: Longword;
       
   149     isTerminated: boolean;
   149 {$IFDEF SDL13}
   150 {$IFDEF SDL13}
   150     previousGameState: TGameState;
   151     previousGameState: TGameState;
   151 {$ELSE}
   152 {$ELSE}
   152     prevFocusState: boolean;
   153     prevFocusState: boolean;
   153 {$ENDIF}
   154 {$ENDIF}
   154 begin
   155 begin
       
   156     isTerminated:= false;
   155     PrevTime:= SDL_GetTicks;
   157     PrevTime:= SDL_GetTicks;
   156     while isTerminated = false do
   158     while isTerminated = false do
   157     begin
   159     begin
   158         SDL_PumpEvents();
   160         SDL_PumpEvents();
   159  
   161  
   160         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, {$IFDEF SDL13}SDL_FIRSTEVENT, SDL_LASTEVENT{$ELSE}SDL_ALLEVENTS{$ENDIF}) > 0 do
   162         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, {$IFDEF SDL13}SDL_FIRSTEVENT, SDL_LASTEVENT{$ELSE}SDL_ALLEVENTS{$ENDIF}) > 0 do
   161         begin
   163         begin
   162             case event.type_ of
   164             case event.type_ of
   163 {$IFDEF SDL13}
   165 {$IFDEF SDL13}
   164                 SDL_KEYDOWN:
   166                 SDL_KEYDOWN:
   165                 if GameState = gsChat then
   167                     if GameState = gsChat then
   166                     // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
   168                     // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
   167                     KeyPressChat(SDL_GetKeyFromScancode(event.key.keysym.sym))//TODO correct for keymodifiers
   169                         KeyPressChat(SDL_GetKeyFromScancode(event.key.keysym.sym))//TODO correct for keymodifiers
   168                 else
   170                     else
   169                     ProcessKey(event.key);
   171                         ProcessKey(event.key);
   170                 SDL_KEYUP:
   172                 SDL_KEYUP:
   171                 if GameState <> gsChat then
   173                     if GameState <> gsChat then
   172                     ProcessKey(event.key);
   174                         ProcessKey(event.key);
   173                     
   175                     
   174                 SDL_WINDOWEVENT:
   176                 SDL_WINDOWEVENT:
   175                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   177                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   176                         begin
   178                     begin
   177                         cHasFocus:= true;
   179                         cHasFocus:= true;
   178                         onFocusStateChanged()
   180                         onFocusStateChanged()
   179                         end
   181                     end
   180                     else if event.window.event = SDL_WINDOWEVENT_MINIMIZED then
   182                     else if event.window.event = SDL_WINDOWEVENT_MINIMIZED then
   181                         begin
   183                     begin
   182                         previousGameState:= GameState;
   184                         previousGameState:= GameState;
   183                         GameState:= gsSuspend;
   185                         GameState:= gsSuspend;
   184                         end
   186                     end
   185                     else if event.window.event = SDL_WINDOWEVENT_RESTORED then
   187                     else if event.window.event = SDL_WINDOWEVENT_RESTORED then
   186                         begin
   188                     begin
   187                         GameState:= previousGameState;
   189                         GameState:= previousGameState;
   188 {$IFDEF ANDROID}
   190 {$IFDEF ANDROID}
   189                         //This call is used to reinitialize the glcontext and reload the textures
   191                         //This call is used to reinitialize the glcontext and reload the textures
   190                         ParseCommand('fullscr '+intToStr(LongInt(cFullScreen)), true);
   192                         ParseCommand('fullscr '+intToStr(LongInt(cFullScreen)), true);
   191 {$ENDIF}
   193 {$ENDIF}
   192                         end
   194                     end
   193                     else if event.window.event = SDL_WINDOWEVENT_RESIZED then
   195                     else if event.window.event = SDL_WINDOWEVENT_RESIZED then
   194                         begin
   196                     begin
   195                         cNewScreenWidth:= max(2 * (event.window.data1 div 2), cMinScreenWidth);
   197                         cNewScreenWidth:= max(2 * (event.window.data1 div 2), cMinScreenWidth);
   196                         cNewScreenHeight:= max(2 * (event.window.data2 div 2), cMinScreenHeight);
   198                         cNewScreenHeight:= max(2 * (event.window.data2 div 2), cMinScreenHeight);
   197                         cScreenResizeDelay:= RealTicks + 500{$IFDEF IPHONEOS}div 2{$ENDIF};
   199                         cScreenResizeDelay:= RealTicks + 500{$IFDEF IPHONEOS}div 2{$ENDIF};
   198                         end;
   200                     end;
   199                         
   201                         
   200                 SDL_FINGERMOTION:
   202                 SDL_FINGERMOTION:
   201                     onTouchMotion(event.tfinger.x, event.tfinger.y,event.tfinger.dx, event.tfinger.dy, event.tfinger.fingerId);
   203                     onTouchMotion(event.tfinger.x, event.tfinger.y,event.tfinger.dx, event.tfinger.dy, event.tfinger.fingerId);
   202                 
   204                 
   203                 SDL_FINGERDOWN:
   205                 SDL_FINGERDOWN:
   210                     if GameState = gsChat then
   212                     if GameState = gsChat then
   211                         KeyPressChat(event.key.keysym.unicode)
   213                         KeyPressChat(event.key.keysym.unicode)
   212                     else
   214                     else
   213                         ProcessKey(event.key);
   215                         ProcessKey(event.key);
   214                 SDL_KEYUP:
   216                 SDL_KEYUP:
   215                 if GameState <> gsChat then
   217                     if GameState <> gsChat then
   216                     ProcessKey(event.key);
   218                         ProcessKey(event.key);
   217                     
   219                     
   218                 SDL_MOUSEBUTTONDOWN:
   220                 SDL_MOUSEBUTTONDOWN:
   219                     ProcessMouse(event.button, true); 
   221                     ProcessMouse(event.button, true);
       
   222                     
   220                 SDL_MOUSEBUTTONUP:
   223                 SDL_MOUSEBUTTONUP:
   221                     ProcessMouse(event.button, false); 
   224                     ProcessMouse(event.button, false); 
   222                     
   225                     
   223                 SDL_ACTIVEEVENT:
   226                 SDL_ACTIVEEVENT:
   224                     if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   227                     if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   225                         begin
   228                     begin
   226                         prevFocusState:= cHasFocus;
   229                         prevFocusState:= cHasFocus;
   227                         cHasFocus:= event.active.gain = 1;
   230                         cHasFocus:= event.active.gain = 1;
   228                         if prevFocusState xor cHasFocus then
   231                         if prevFocusState xor cHasFocus then
   229                             onFocusStateChanged()
   232                             onFocusStateChanged()
   230                         end;
   233                     end;
   231                         
   234                         
   232                 SDL_VIDEORESIZE:
   235                 SDL_VIDEORESIZE:
   233                     begin
   236                 begin
   234                     // using lower values than cMinScreenWidth or cMinScreenHeight causes widget overlap and off-screen widget parts
   237                     // using lower values than cMinScreenWidth or cMinScreenHeight causes widget overlap and off-screen widget parts
   235                     // Change by sheepluva:
   238                     // Change by sheepluva:
   236                     // Let's only use even numbers for custom width/height since I ran into scaling issues with odd width values.
   239                     // Let's only use even numbers for custom width/height since I ran into scaling issues with odd width values.
   237                     // Maybe just fixes the symptom not the actual cause(?), I'm too tired to find out :P
   240                     // Maybe just fixes the symptom not the actual cause(?), I'm too tired to find out :P
   238                     cNewScreenWidth:= max(2 * (event.resize.w div 2), cMinScreenWidth);
   241                     cNewScreenWidth:= max(2 * (event.resize.w div 2), cMinScreenWidth);
   239                     cNewScreenHeight:= max(2 * (event.resize.h div 2), cMinScreenHeight);
   242                     cNewScreenHeight:= max(2 * (event.resize.h div 2), cMinScreenHeight);
   240                     cScreenResizeDelay:= RealTicks+500;
   243                     cScreenResizeDelay:= RealTicks+500;
   241                     end;
   244                 end;
   242 {$ENDIF}
   245 {$ENDIF}
   243                 SDL_JOYAXISMOTION:
   246                 SDL_JOYAXISMOTION:
   244                     ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
   247                     ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
   245                 SDL_JOYHATMOTION:
   248                 SDL_JOYHATMOTION:
   246                     ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
   249                     ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
   248                     ControllerButtonEvent(event.jbutton.which, event.jbutton.button, true);
   251                     ControllerButtonEvent(event.jbutton.which, event.jbutton.button, true);
   249                 SDL_JOYBUTTONUP:
   252                 SDL_JOYBUTTONUP:
   250                     ControllerButtonEvent(event.jbutton.which, event.jbutton.button, false);
   253                     ControllerButtonEvent(event.jbutton.which, event.jbutton.button, false);
   251                 SDL_QUITEV:
   254                 SDL_QUITEV:
   252                     isTerminated:= true
   255                     isTerminated:= true
   253         end; //end case event.type_ of
   256             end; //end case event.type_ of
   254     end; //end while SDL_PollEvent(@event) <> 0 do
   257         end; //end while SDL_PollEvent(@event) <> 0 do
   255 
   258 
   256     if (cScreenResizeDelay <> 0) and (cScreenResizeDelay < RealTicks) and
   259         if (cScreenResizeDelay <> 0) and (cScreenResizeDelay < RealTicks) and
   257        ((cNewScreenWidth <> cScreenWidth) or (cNewScreenHeight <> cScreenHeight)) then
   260            ((cNewScreenWidth <> cScreenWidth) or (cNewScreenHeight <> cScreenHeight)) then
   258     begin
   261         begin
   259         cScreenResizeDelay:= 0;
   262             cScreenResizeDelay:= 0;
   260         cScreenWidth:= cNewScreenWidth;
   263             cScreenWidth:= cNewScreenWidth;
   261         cScreenHeight:= cNewScreenHeight;
   264             cScreenHeight:= cNewScreenHeight;
   262 
   265 
   263         ParseCommand('fullscr '+intToStr(LongInt(cFullScreen)), true);
   266             ParseCommand('fullscr '+intToStr(LongInt(cFullScreen)), true);
   264         WriteLnToConsole('window resize: ' + IntToStr(cScreenWidth) + ' x ' + IntToStr(cScreenHeight));
   267             WriteLnToConsole('window resize: ' + IntToStr(cScreenWidth) + ' x ' + IntToStr(cScreenHeight));
   265         ScriptOnScreenResize();
   268             ScriptOnScreenResize();
   266         InitCameraBorders();
   269             InitCameraBorders();
   267         InitTouchInterface();
   270             InitTouchInterface();
   268     end;
   271         end;
   269 
   272 
   270     if isTerminated = false then
   273         CurrTime:= SDL_GetTicks();
   271     begin
       
   272         CurrTime:= SDL_GetTicks;
       
   273         if PrevTime + longword(cTimerInterval) <= CurrTime then
   274         if PrevTime + longword(cTimerInterval) <= CurrTime then
   274         begin
   275         begin
   275             DoTimer(CurrTime - PrevTime);
   276             isTerminated:= DoTimer(CurrTime - PrevTime);
   276             PrevTime:= CurrTime
   277             PrevTime:= CurrTime
   277         end
   278         end
   278         else SDL_Delay(1);
   279         else SDL_Delay(1);
   279         IPCCheckSock();
   280         IPCCheckSock();
   280         end;
       
   281     end;
   281     end;
   282 end;
   282 end;
   283 
   283 
   284 ///////////////
   284 ///////////////
   285 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   285 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   286 var
   286 var p: TPathType;
   287     p: TPathType;
       
   288     s: shortstring;
   287     s: shortstring;
   289     i: LongInt;
   288     i: LongInt;
   290 begin
   289 begin
   291 {$IFDEF HWLIBRARY}
   290 {$IFDEF HWLIBRARY}
   292     cBits:= 32;
   291     cBits:= 32;