hedgewars/hwengine.pas
branchexperimental3D
changeset 4812 f924be23ffb4
parent 4343 19cbea33e4d2
parent 4806 48c1a395f0a7
child 4814 e19791f08443
equal deleted inserted replaced
4347:0ddb100fea61 4812:f924be23ffb4
    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, uKeys, uSound,
    32 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uKeys, uSound,
    33      uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions, uMobile, sysutils;
    33      uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions, uMobile,
    34 
    34      sysutils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted;
    35 var isTerminated: boolean = false;
       
    36     alsoShutdownFrontend: boolean = false;
       
    37 
    35 
    38 {$IFDEF HWLIBRARY}
    36 {$IFDEF HWLIBRARY}
    39 procedure initEverything(complete:boolean);
    37 procedure initEverything(complete:boolean);
    40 procedure freeEverything(complete:boolean);
    38 procedure freeEverything(complete:boolean);
    41 
    39 
    48 
    46 
    49 ////////////////////////////////
    47 ////////////////////////////////
    50 procedure DoTimer(Lag: LongInt);
    48 procedure DoTimer(Lag: LongInt);
    51 var s: shortstring;
    49 var s: shortstring;
    52 begin
    50 begin
    53     if not isPaused then inc(RealTicks, Lag);
    51     if isPaused = false then
       
    52         inc(RealTicks, Lag);
    54 
    53 
    55     case GameState of
    54     case GameState of
    56         gsLandGen: begin
    55         gsLandGen: begin
    57                 GenMap;
    56                 GenMap;
       
    57                 ParseCommand('sendlanddigest', true);
    58                 GameState:= gsStart;
    58                 GameState:= gsStart;
    59                 end;
    59                 end;
    60         gsStart: begin
    60         gsStart: begin
    61                 if HasBorder then DisableSomeWeapons;
    61                 if HasBorder then DisableSomeWeapons;
    62                 AddClouds;
    62                 AddClouds;
       
    63                 AddFlakes;
    63                 AssignHHCoords;
    64                 AssignHHCoords;
    64                 AddMiscGears;
    65                 AddMiscGears;
    65                 StoreLoad;
    66                 StoreLoad;
    66                 InitWorld;
    67                 InitWorld;
    67                 ResetKbd;
    68                 ResetKbd;
    96                     end;
    97                     end;
    97                 end;
    98                 end;
    98         gsExit: begin
    99         gsExit: begin
    99                 isTerminated:= true;
   100                 isTerminated:= true;
   100                 end;
   101                 end;
       
   102         gsSuspend: exit;
   101         end;
   103         end;
   102 
   104 
   103 {$IFDEF SDL13}
   105 {$IFDEF SDL13}
   104     SDL_RenderPresent();
   106     SDL_RenderPresent();
   105 {$ELSE}
   107 {$ELSE}
   106     SDL_GL_SwapBuffers();
   108     SDL_GL_SwapBuffers();
   107 {$ENDIF}
   109 {$ENDIF}
   108     // not going to make captures on the iPhone
   110 
   109     if flagMakeCapture then
   111     if flagMakeCapture then
   110     begin
   112     begin
   111         flagMakeCapture:= false;
   113         flagMakeCapture:= false;
   112         s:= 'hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
   114         s:= 'hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
   113         WriteLnToConsole('Saving ' + s + '...');
   115         WriteLnToConsole('Saving ' + s + '...');
       
   116         playSound(sndShutter);
   114         MakeScreenshot(s);
   117         MakeScreenshot(s);
   115         //SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1)
   118         //SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1)
   116     end;
   119     end;
   117 end;
   120 end;
   118 
   121 
   121 begin
   124 begin
   122     WriteLnToConsole('Freeing resources...');
   125     WriteLnToConsole('Freeing resources...');
   123     FreeActionsList();
   126     FreeActionsList();
   124     StoreRelease();
   127     StoreRelease();
   125     ControllerClose();
   128     ControllerClose();
   126     SendKB();
       
   127     CloseIPC();
   129     CloseIPC();
   128     TTF_Quit();
   130     TTF_Quit();
   129 {$IFDEF SDL13}
   131 {$IFDEF SDL13}
   130     SDL_DestroyRenderer(SDLwindow);
   132     SDL_DestroyRenderer(SDLwindow);
   131     SDL_DestroyWindow(SDLwindow);
   133     SDL_DestroyWindow(SDLwindow);
   145 
   147 
   146         while SDL_PollEvent(@event) <> 0 do
   148         while SDL_PollEvent(@event) <> 0 do
   147         begin
   149         begin
   148             case event.type_ of
   150             case event.type_ of
   149                 SDL_KEYDOWN: if GameState = gsChat then
   151                 SDL_KEYDOWN: if GameState = gsChat then
   150 {$IFDEF IPHONEOS}
   152 {$IFDEF SDL13}
   151                     // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
   153                     // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
   152                     KeyPressChat(event.key.keysym.sym);
   154                     KeyPressChat(event.key.keysym.sym);
       
   155                 SDL_WINDOWEVENT:
       
   156                     if event.wevent.event = SDL_WINDOWEVENT_SHOWN then
       
   157                         cHasFocus:= true;
   153 {$ELSE}
   158 {$ELSE}
   154                     KeyPressChat(event.key.keysym.unicode);
   159                     KeyPressChat(event.key.keysym.unicode);
   155                 SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then uKeys.wheelDown:= true;
   160                 SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then uKeys.wheelDown:= true;
   156                 SDL_MOUSEBUTTONUP: if event.button.button = SDL_BUTTON_WHEELUP then uKeys.wheelUp:= true;
   161                 SDL_MOUSEBUTTONUP: if event.button.button = SDL_BUTTON_WHEELUP then uKeys.wheelUp:= true;
   157 {$ENDIF}
       
   158 {$IFDEF SDL13}
       
   159                 SDL_WINDOWEVENT:
       
   160                     if event.wevent.event = SDL_WINDOWEVENT_SHOWN then
       
   161                         cHasFocus:= true;
       
   162 {$ELSE}
       
   163                 SDL_ACTIVEEVENT:
   162                 SDL_ACTIVEEVENT:
   164                     if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   163                     if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   165                         cHasFocus:= event.active.gain = 1;
   164                         cHasFocus:= event.active.gain = 1;
   166 {$ENDIF}
   165 {$ENDIF}
   167                 SDL_JOYAXISMOTION: ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
   166                 SDL_JOYAXISMOTION: ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
   168                 SDL_JOYHATMOTION: ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
   167                 SDL_JOYHATMOTION: ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
   169                 SDL_JOYBUTTONDOWN: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, true);
   168                 SDL_JOYBUTTONDOWN: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, true);
   170                 SDL_JOYBUTTONUP: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, false);
   169                 SDL_JOYBUTTONUP: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, false);
   171                 SDL_QUITEV: isTerminated:= true
   170                 SDL_QUITEV: isTerminated:= true
   172             end; // end case event.type_
   171             end; //end case event.type_ of
   173         end; // end while SDL_PollEvent(@event) <> 0
   172         end; //end while SDL_PollEvent(@event) <> 0 do
   174 
   173 
   175         if isTerminated = false then
   174         if isTerminated = false then
   176         begin
   175         begin
   177             CurrTime:= SDL_GetTicks;
   176             CurrTime:= SDL_GetTicks;
   178             if PrevTime + longword(cTimerInterval) <= CurrTime then
   177             if PrevTime + longword(cTimerInterval) <= CurrTime then
   317 procedure initEverything (complete:boolean);
   316 procedure initEverything (complete:boolean);
   318 begin
   317 begin
   319     Randomize();
   318     Randomize();
   320 
   319 
   321     // uConsts does not need initialization as they are all consts
   320     // uConsts does not need initialization as they are all consts
       
   321     uUtils.initModule;
   322     uMisc.initModule;
   322     uMisc.initModule;
       
   323     uVariables.initModule;
   323     uConsole.initModule;    // MUST happen after uMisc
   324     uConsole.initModule;    // MUST happen after uMisc
       
   325     uCommands.initModule;
       
   326     uCommandHandlers.initModule;
   324 
   327 
   325     uLand.initModule;
   328     uLand.initModule;
       
   329     uLandPainted.initModule;
       
   330 
   326     uIO.initModule;
   331     uIO.initModule;
   327 
   332 
   328     if complete then
   333     if complete then
   329     begin
   334     begin
   330         uAI.initModule;
   335         uAI.initModule;
   349         uStats.initModule;
   354         uStats.initModule;
   350         uStore.initModule;
   355         uStore.initModule;
   351         uTeams.initModule;
   356         uTeams.initModule;
   352         uVisualGears.initModule;
   357         uVisualGears.initModule;
   353         uWorld.initModule;
   358         uWorld.initModule;
       
   359         uCaptions.initModule;
   354     end;
   360     end;
   355 end;
   361 end;
   356 
   362 
   357 procedure freeEverything (complete:boolean);
   363 procedure freeEverything (complete:boolean);
   358 begin
   364 begin
   359     if complete then
   365     if complete then
   360     begin
   366     begin
       
   367         uCaptions.freeModule;
   361         uWorld.freeModule;
   368         uWorld.freeModule;
   362         uVisualGears.freeModule;
   369         uVisualGears.freeModule;
   363         uTeams.freeModule;
   370         uTeams.freeModule;
   364         uStore.freeModule;          //stub
   371         uStore.freeModule;          //stub
   365         uStats.freeModule;          //stub
   372         uStats.freeModule;          //stub
   385     end;
   392     end;
   386 
   393 
   387     uIO.freeModule;             //stub
   394     uIO.freeModule;             //stub
   388     uLand.freeModule;
   395     uLand.freeModule;
   389 
   396 
       
   397     uCommandHandlers.freeModule;
       
   398     uCommands.freeModule;
   390     uConsole.freeModule;
   399     uConsole.freeModule;
       
   400     uVariables.freeModule;
       
   401     uUtils.freeModule;
   391     uMisc.freeModule;           // uMisc closes the debug log.
   402     uMisc.freeModule;           // uMisc closes the debug log.
   392 end;
   403 end;
   393 
   404 
   394 /////////////////////////
   405 /////////////////////////
   395 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
   406 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};