hedgewars/hwengine.pas
branchhedgeroid
changeset 5339 bc8560452143
parent 5286 22c1f4833a86
child 5359 86b76c93a612
equal deleted inserted replaced
5337:16477d1c12b1 5339:bc8560452143
    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 {$IFDEF ANDROID}, log in 'log.pas', GLUnit{$ENDIF};
    34      sysutils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted;
    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(); cdecl; export;
    39 procedure Game(gameArgs: PPChar); 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}
   144 var PrevTime, CurrTime: Longword;
   148 var PrevTime, CurrTime: Longword;
   145     event: TSDL_Event;
       
   146 begin
   149 begin
   147     PrevTime:= SDL_GetTicks;
   150     PrevTime:= SDL_GetTicks;
   148     while isTerminated = false do
   151     while isTerminated = false do
   149     begin
   152     begin
   150 {$IFDEF ANDROID}
   153 {$IFDEF ANDROID}
   152         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do
   155         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do
   153 {$ELSE}
   156 {$ELSE}
   154 	while SDL_PollEvent(@event) <> 0 do
   157 	while SDL_PollEvent(@event) <> 0 do
   155 {$ENDIF}
   158 {$ENDIF}
   156         begin
   159         begin
   157            case event.type_ of
   160             case event.type_ of
   158                 SDL_KEYDOWN: if GameState = gsChat then
   161                 SDL_KEYDOWN: if GameState = gsChat then
   159 {$IFDEF SDL13}
   162 {$IFDEF SDL13}
   160                     // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
   163                     // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
   161                     KeyPressChat(event.key.keysym.sym);
   164                     KeyPressChat(event.key.keysym.sym);
   162                 SDL_WINDOWEVENT:	
   165                 SDL_WINDOWEVENT:
   163                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   166                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
   164                         cHasFocus:= true;
   167                         cHasFocus:= true;
   165 {$ELSE}
   168 {$ELSE}
   166                     KeyPressChat(event.key.keysym.unicode);
   169                     KeyPressChat(event.key.keysym.unicode);
   167                 SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then wheelDown:= true;
   170                 SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then wheelDown:= true;
   200     SDL_ShowCursor(0)
   203     SDL_ShowCursor(0)
   201 end;
   204 end;
   202 
   205 
   203 ///////////////
   206 ///////////////
   204 {$IFDEF HWLIBRARY}
   207 {$IFDEF HWLIBRARY}
   205 procedure Game(); cdecl; export;
   208 procedure Game(gameArgs: PPChar); cdecl; export;
   206 {$ELSE}
   209 {$ELSE}
   207 procedure Game;
   210 procedure Game;
   208 {$ENDIF}
   211 {$ENDIF}
   209 var p: TPathType;
   212 var p: TPathType;
   210     s: shortstring;
   213     s: shortstring;
   212 begin
   215 begin
   213 {$IFDEF HWLIBRARY}
   216 {$IFDEF HWLIBRARY}
   214     cBits:= 32;
   217     cBits:= 32;
   215     cFullScreen:= false;
   218     cFullScreen:= false;
   216     cTimerInterval:= 8;
   219     cTimerInterval:= 8;
   217     PathPrefix:= '/sdcard/Data';
   220     PathPrefix:= 'Data';
       
   221     UserPathPrefix:= '.';
   218     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   222     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   219     val('0', ipcPort);
   223     val(gameArgs[0], ipcPort);
   220     val('800', cScreenWidth);
   224     val(gameArgs[1], cScreenWidth);
   221     val('480', cScreenHeight);
   225     val(gameArgs[2], cScreenHeight);
   222     //val('1', cReducedQuality);
   226     val(gameArgs[3], cReducedQuality);
   223     cReducedQuality := $00000000;
   227     cLocaleFName:= gameArgs[4];
   224     cLocaleFName:= 'null';
   228     UserNick:= gameArgs[5];
   225     UserNick:= 'xeli';
   229     isSoundEnabled:= gameArgs[6] = '1';
   226     isSoundEnabled:= '0' = '1';
   230     isMusicEnabled:= gameArgs[7] = '1';
   227     isMusicEnabled:= '0' = '1';
   231     cAltDamage:= gameArgs[8] = '1';
   228     cAltDamage:= '1' = '1';
   232     val(gameArgs[9], rotationQt);
   229     val('0', rotationQt);
   233     recordFileName:= gameArgs[10];
   230     recordFileName:= '/sdcard/bla.hwd';
       
   231     cStereoMode:= smNone;
   234     cStereoMode:= smNone;
   232 {$ENDIF}
   235 {$ENDIF}
   233 
   236 
   234     cLogfileBase:= 'game';
   237     cLogfileBase:= 'game';
   235     initEverything(true);
   238     initEverything(true);
   236 
   239 
   237     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   240     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   238     AddFileLog('Prefix: "' + PathPrefix +'"');
   241     AddFileLog('Prefix: "' + PathPrefix +'"');
       
   242     AddFileLog('UserPrefix: "' + UserPathPrefix +'"');
   239     for i:= 0 to ParamCount do
   243     for i:= 0 to ParamCount do
   240         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   244         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
       
   245 
       
   246     for p:= Succ(Low(TPathType)) to High(TPathType) do
       
   247         if (p <> ptMapCurrent) and (p <> ptData) then UserPathz[p]:= UserPathPrefix + '/Data/' + Pathz[p];
       
   248 
       
   249     UserPathz[ptData]:= UserPathPrefix + '/Data';
   241 
   250 
   242     for p:= Succ(Low(TPathType)) to High(TPathType) do
   251     for p:= Succ(Low(TPathType)) to High(TPathType) do
   243         if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p];
   252         if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p];
   244 
   253 
   245     WriteToConsole('Init SDL... ');
   254     WriteToConsole('Init SDL... ');
   263 
   272 
   264     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
   273     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
   265     InitKbdKeyTable();
   274     InitKbdKeyTable();
   266     AddProgress();
   275     AddProgress();
   267 
   276 
       
   277     LoadLocale(UserPathz[ptLocale] + '/en.txt');  // Do an initial load with english
   268     LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
   278     LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
   269     if (Length(cLocaleFName) > 6) then cLocale := Copy(cLocaleFName,1,5)
   279     if (Length(cLocaleFName) > 6) then cLocale := Copy(cLocaleFName,1,5)
   270     else cLocale := Copy(cLocaleFName,1,2);
   280     else cLocale := Copy(cLocaleFName,1,2);
   271     if cLocaleFName <> 'en.txt' then
   281     if cLocaleFName <> 'en.txt' then
   272         begin
   282         begin
   273         // Try two letter locale first before trying specific locale overrides
   283         // Try two letter locale first before trying specific locale overrides
   274         if (Length(cLocale) > 2) and (Copy(cLocale,1,2) <> 'en') then
   284         if (Length(cLocale) > 2) and (Copy(cLocale,1,2) <> 'en') then
   275             LoadLocale(Pathz[ptLocale] + '/' + Copy(cLocale,1,2)+'.txt');
   285             begin
   276         LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName);
   286             LoadLocale(UserPathz[ptLocale] + '/' + Copy(cLocale,1,2)+'.txt');
       
   287             LoadLocale(Pathz[ptLocale] + '/' + Copy(cLocale,1,2)+'.txt')
       
   288             end;
       
   289         LoadLocale(UserPathz[ptLocale] + '/' + cLocaleFName);
       
   290         LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName)
   277         end
   291         end
   278     else cLocale := 'en';
   292     else cLocale := 'en';
   279 
   293 
   280     WriteLnToConsole(msgGettingConfig);
   294     WriteLnToConsole(msgGettingConfig);
   281 
   295 
   433 procedure DisplayUsage;
   447 procedure DisplayUsage;
   434 var i: LongInt;
   448 var i: LongInt;
   435 begin
   449 begin
   436     WriteLn('Wrong argument format: correct configurations is');
   450     WriteLn('Wrong argument format: correct configurations is');
   437     WriteLn();
   451     WriteLn();
   438     WriteLn('  hwengine <path to data folder> <path to replay file> [options]');
   452     WriteLn('  hwengine <path to user hedgewars folder> <path to global data folder> <path to replay file> [options]');
   439     WriteLn();
   453     WriteLn();
   440     WriteLn('where [options] must be specified either as:');
   454     WriteLn('where [options] must be specified either as:');
   441     WriteLn(' --set-video [screen width] [screen height] [color dept]');
   455     WriteLn(' --set-video [screen width] [screen height] [color dept]');
   442     WriteLn(' --set-audio [volume] [enable music] [enable sounds]');
   456     WriteLn(' --set-audio [volume] [enable music] [enable sounds]');
   443     WriteLn(' --set-other [language file] [full screen] [show FPS]');
   457     WriteLn(' --set-other [language file] [full screen] [show FPS]');
   456 ////////////////////
   470 ////////////////////
   457 {$INCLUDE "ArgParsers.inc"}
   471 {$INCLUDE "ArgParsers.inc"}
   458 
   472 
   459 procedure GetParams;
   473 procedure GetParams;
   460 begin
   474 begin
   461     if (ParamCount < 2) then
   475     if (ParamCount < 3) then
   462         GameType:= gmtSyntax
   476         GameType:= gmtSyntax
   463     else
   477     else
   464         if (ParamCount = 3) then
   478         if (ParamCount = 3) and ((ParamStr(3) = '--stats-only') or (ParamStr(3) = 'landpreview')) then
   465             internalSetGameTypeLandPreviewFromParameters()
   479             internalSetGameTypeLandPreviewFromParameters()
   466         else
   480         else
   467             if (ParamCount = cDefaultParamNum) then
   481             if (ParamCount = cDefaultParamNum) then
   468                 internalStartGameWithParameters()
   482                 internalStartGameWithParameters()
   469             else
   483             else