hedgewars/hwengine.pas
branchhedgeroid
changeset 5286 22c1f4833a86
parent 5242 e4f6aa3250eb
child 5339 bc8560452143
equal deleted inserted replaced
5284:34abd278344e 5286:22c1f4833a86
    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,
    33      uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions,
    34      sysutils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted;
    34      sysutils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted {$IFDEF ANDROID}, log in 'log.pas', GLUnit{$ENDIF};
    35 
    35 
    36 {$IFDEF HWLIBRARY}
    36 {$IFDEF HWLIBRARY}
    37 procedure initEverything(complete:boolean);
    37 procedure initEverything(complete:boolean);
    38 procedure freeEverything(complete:boolean);
    38 procedure freeEverything(complete:boolean);
    39 procedure Game(gameArgs: PPChar); cdecl; export;
    39 procedure Game(); cdecl; export;
    40 
    40 
    41 implementation
    41 implementation
    42 {$ELSE}
    42 {$ELSE}
    43 procedure OnDestroy; forward;
    43 procedure OnDestroy; forward;
    44 procedure initEverything(complete:boolean); forward;
    44 procedure initEverything(complete:boolean); forward;
   139     isTerminated:= false;
   139     isTerminated:= false;
   140 end;
   140 end;
   141 
   141 
   142 ///////////////////
   142 ///////////////////
   143 procedure MainLoop;
   143 procedure MainLoop;
   144 {$WARNINGS OFF}
       
   145 // disable "Some fields weren't initialized" warning
       
   146 const event: TSDL_Event = ();
       
   147 {$WARNINGS ON}
       
   148 var PrevTime, CurrTime: Longword;
   144 var PrevTime, CurrTime: Longword;
       
   145     event: TSDL_Event;
   149 begin
   146 begin
   150     PrevTime:= SDL_GetTicks;
   147     PrevTime:= SDL_GetTicks;
   151     while isTerminated = false do
   148     while isTerminated = false do
   152     begin
   149     begin
   153 
   150 {$IFDEF ANDROID}
   154         while SDL_PollEvent(@event) <> 0 do
   151 	SDL_PumpEvents();
       
   152         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do
       
   153 {$ELSE}
       
   154 	while SDL_PollEvent(@event) <> 0 do
       
   155 {$ENDIF}
   155         begin
   156         begin
   156             case event.type_ of
   157            case event.type_ of
   157                 SDL_KEYDOWN: if GameState = gsChat then
   158                 SDL_KEYDOWN: if GameState = gsChat then
   158 {$IFDEF SDL13}
   159 {$IFDEF SDL13}
   159                     // 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
   160                     KeyPressChat(event.key.keysym.sym);
   161                     KeyPressChat(event.key.keysym.sym);
   161                 SDL_WINDOWEVENT:
   162                 SDL_WINDOWEVENT:	
   162                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   163                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   163                         cHasFocus:= true;
   164                         cHasFocus:= true;
   164 {$ELSE}
   165 {$ELSE}
   165                     KeyPressChat(event.key.keysym.unicode);
   166                     KeyPressChat(event.key.keysym.unicode);
   166                 SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then wheelDown:= true;
   167                 SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then wheelDown:= true;
   199     SDL_ShowCursor(0)
   200     SDL_ShowCursor(0)
   200 end;
   201 end;
   201 
   202 
   202 ///////////////
   203 ///////////////
   203 {$IFDEF HWLIBRARY}
   204 {$IFDEF HWLIBRARY}
   204 procedure Game(gameArgs: PPChar); cdecl; export;
   205 procedure Game(); cdecl; export;
   205 {$ELSE}
   206 {$ELSE}
   206 procedure Game;
   207 procedure Game;
   207 {$ENDIF}
   208 {$ENDIF}
   208 var p: TPathType;
   209 var p: TPathType;
   209     s: shortstring;
   210     s: shortstring;
   211 begin
   212 begin
   212 {$IFDEF HWLIBRARY}
   213 {$IFDEF HWLIBRARY}
   213     cBits:= 32;
   214     cBits:= 32;
   214     cFullScreen:= false;
   215     cFullScreen:= false;
   215     cTimerInterval:= 8;
   216     cTimerInterval:= 8;
   216     PathPrefix:= 'Data';
   217     PathPrefix:= '/sdcard/Data';
   217     UserPathPrefix:= '.';
       
   218     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   218     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   219     val(gameArgs[0], ipcPort);
   219     val('0', ipcPort);
   220     val(gameArgs[1], cScreenWidth);
   220     val('800', cScreenWidth);
   221     val(gameArgs[2], cScreenHeight);
   221     val('480', cScreenHeight);
   222     val(gameArgs[3], cReducedQuality);
   222     //val('1', cReducedQuality);
   223     cLocaleFName:= gameArgs[4];
   223     cReducedQuality := $00000000;
   224     UserNick:= gameArgs[5];
   224     cLocaleFName:= 'null';
   225     isSoundEnabled:= gameArgs[6] = '1';
   225     UserNick:= 'xeli';
   226     isMusicEnabled:= gameArgs[7] = '1';
   226     isSoundEnabled:= '0' = '1';
   227     cAltDamage:= gameArgs[8] = '1';
   227     isMusicEnabled:= '0' = '1';
   228     val(gameArgs[9], rotationQt);
   228     cAltDamage:= '1' = '1';
   229     recordFileName:= gameArgs[10];
   229     val('0', rotationQt);
       
   230     recordFileName:= '/sdcard/bla.hwd';
   230     cStereoMode:= smNone;
   231     cStereoMode:= smNone;
   231 {$ENDIF}
   232 {$ENDIF}
   232 
   233 
   233     cLogfileBase:= 'game';
   234     cLogfileBase:= 'game';
   234     initEverything(true);
   235     initEverything(true);
   235 
   236 
   236     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   237     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   237     AddFileLog('Prefix: "' + PathPrefix +'"');
   238     AddFileLog('Prefix: "' + PathPrefix +'"');
   238     AddFileLog('UserPrefix: "' + UserPathPrefix +'"');
       
   239     for i:= 0 to ParamCount do
   239     for i:= 0 to ParamCount do
   240         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   240         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   241 
       
   242     for p:= Succ(Low(TPathType)) to High(TPathType) do
       
   243         if (p <> ptMapCurrent) and (p <> ptData) then UserPathz[p]:= UserPathPrefix + '/Data/' + Pathz[p];
       
   244 
       
   245     UserPathz[ptData]:= UserPathPrefix + '/Data';
       
   246 
   241 
   247     for p:= Succ(Low(TPathType)) to High(TPathType) do
   242     for p:= Succ(Low(TPathType)) to High(TPathType) do
   248         if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p];
   243         if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p];
   249 
   244 
   250     WriteToConsole('Init SDL... ');
   245     WriteToConsole('Init SDL... ');
   268 
   263 
   269     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
   264     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
   270     InitKbdKeyTable();
   265     InitKbdKeyTable();
   271     AddProgress();
   266     AddProgress();
   272 
   267 
   273     LoadLocale(UserPathz[ptLocale] + '/en.txt');  // Do an initial load with english
       
   274     LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
   268     LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
   275     if (Length(cLocaleFName) > 6) then cLocale := Copy(cLocaleFName,1,5)
   269     if (Length(cLocaleFName) > 6) then cLocale := Copy(cLocaleFName,1,5)
   276     else cLocale := Copy(cLocaleFName,1,2);
   270     else cLocale := Copy(cLocaleFName,1,2);
   277     if cLocaleFName <> 'en.txt' then
   271     if cLocaleFName <> 'en.txt' then
   278         begin
   272         begin
   279         // Try two letter locale first before trying specific locale overrides
   273         // Try two letter locale first before trying specific locale overrides
   280         if (Length(cLocale) > 2) and (Copy(cLocale,1,2) <> 'en') then
   274         if (Length(cLocale) > 2) and (Copy(cLocale,1,2) <> 'en') then
   281             begin
   275             LoadLocale(Pathz[ptLocale] + '/' + Copy(cLocale,1,2)+'.txt');
   282             LoadLocale(UserPathz[ptLocale] + '/' + Copy(cLocale,1,2)+'.txt');
   276         LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName);
   283             LoadLocale(Pathz[ptLocale] + '/' + Copy(cLocale,1,2)+'.txt')
       
   284             end;
       
   285         LoadLocale(UserPathz[ptLocale] + '/' + cLocaleFName);
       
   286         LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName)
       
   287         end
   277         end
   288     else cLocale := 'en';
   278     else cLocale := 'en';
   289 
   279 
   290     WriteLnToConsole(msgGettingConfig);
   280     WriteLnToConsole(msgGettingConfig);
   291 
   281 
   339 
   329 
   340     uIO.initModule;
   330     uIO.initModule;
   341 
   331 
   342     if complete then
   332     if complete then
   343     begin
   333     begin
   344         uAI.initModule;
   334         {$IFDEF ANDROID}
       
   335 	GLUnit.init;
       
   336 	{$ENDIF}
       
   337 	uAI.initModule;
   345         //uAIActions does not need initialization
   338         //uAIActions does not need initialization
   346         //uAIAmmoTests does not need initialization
   339         //uAIAmmoTests does not need initialization
   347         uAIMisc.initModule;
   340         uAIMisc.initModule;
   348         uAmmos.initModule;
   341         uAmmos.initModule;
   349         uChat.initModule;
   342         uChat.initModule;
   440 procedure DisplayUsage;
   433 procedure DisplayUsage;
   441 var i: LongInt;
   434 var i: LongInt;
   442 begin
   435 begin
   443     WriteLn('Wrong argument format: correct configurations is');
   436     WriteLn('Wrong argument format: correct configurations is');
   444     WriteLn();
   437     WriteLn();
   445     WriteLn('  hwengine <path to user hedgewars folder> <path to global data folder> <path to replay file> [options]');
   438     WriteLn('  hwengine <path to data folder> <path to replay file> [options]');
   446     WriteLn();
   439     WriteLn();
   447     WriteLn('where [options] must be specified either as:');
   440     WriteLn('where [options] must be specified either as:');
   448     WriteLn(' --set-video [screen width] [screen height] [color dept]');
   441     WriteLn(' --set-video [screen width] [screen height] [color dept]');
   449     WriteLn(' --set-audio [volume] [enable music] [enable sounds]');
   442     WriteLn(' --set-audio [volume] [enable music] [enable sounds]');
   450     WriteLn(' --set-other [language file] [full screen] [show FPS]');
   443     WriteLn(' --set-other [language file] [full screen] [show FPS]');
   463 ////////////////////
   456 ////////////////////
   464 {$INCLUDE "ArgParsers.inc"}
   457 {$INCLUDE "ArgParsers.inc"}
   465 
   458 
   466 procedure GetParams;
   459 procedure GetParams;
   467 begin
   460 begin
   468     if (ParamCount < 3) then
   461     if (ParamCount < 2) then
   469         GameType:= gmtSyntax
   462         GameType:= gmtSyntax
   470     else
   463     else
   471         if (ParamCount = 3) and ((ParamStr(3) = '--stats-only') or (ParamStr(3) = 'landpreview')) then
   464         if (ParamCount = 3) then
   472             internalSetGameTypeLandPreviewFromParameters()
   465             internalSetGameTypeLandPreviewFromParameters()
   473         else
   466         else
   474             if (ParamCount = cDefaultParamNum) then
   467             if (ParamCount = cDefaultParamNum) then
   475                 internalStartGameWithParameters()
   468                 internalStartGameWithParameters()
   476             else
   469             else