hedgewars/uStore.pas
branchwebgl
changeset 9950 2759212a27de
parent 9521 8054d9d775fd
parent 9938 6dbddbb7b229
child 9954 bf51bc7e2808
equal deleted inserted replaced
9521:8054d9d775fd 9950:2759212a27de
    27 procedure freeModule;
    27 procedure freeModule;
    28 
    28 
    29 procedure StoreLoad(reload: boolean);
    29 procedure StoreLoad(reload: boolean);
    30 procedure StoreRelease(reload: boolean);
    30 procedure StoreRelease(reload: boolean);
    31 procedure RenderHealth(var Hedgehog: THedgehog);
    31 procedure RenderHealth(var Hedgehog: THedgehog);
       
    32 function makeHealthBarTexture(w, h, Color: Longword): PTexture;
    32 procedure AddProgress;
    33 procedure AddProgress;
    33 procedure FinishProgress;
    34 procedure FinishProgress;
    34 function  LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    35 function  LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    35 
    36 
    36 // loads an image from the games data files
    37 // loads an image from the games data files
   130 finalRect.h:= h + cFontBorder * 2;
   131 finalRect.h:= h + cFontBorder * 2;
   131 WriteInRect:= finalRect
   132 WriteInRect:= finalRect
   132 end;
   133 end;
   133 
   134 
   134 procedure MakeCrossHairs;
   135 procedure MakeCrossHairs;
   135 var t: LongInt;
   136 var tmpsurf: PSDL_Surface;
   136     tmpsurf, texsurf: PSDL_Surface;
   137 begin
   137     Color, i: Longword;
   138     tmpsurf:= LoadDataImage(ptGraphics, cCHFileName, ifAlpha or ifCritical);
   138 begin
   139 
   139 tmpsurf:= LoadDataImage(ptGraphics, cCHFileName, ifAlpha or ifCritical);
   140     CrosshairTexture:= Surface2Tex(tmpsurf, false);
   140 
   141 
   141 for t:= 0 to Pred(TeamsCount) do
   142     SDL_FreeSurface(tmpsurf)
   142     with TeamsArray[t]^ do
   143 end;
   143     begin
   144 
   144     texsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, tmpsurf^.w, tmpsurf^.h, 32, RMask, GMask, BMask, AMask);
   145 function makeHealthBarTexture(w, h, Color: Longword): PTexture;
       
   146 var
       
   147     rr: TSDL_Rect;
       
   148     texsurf: PSDL_Surface;
       
   149 begin
       
   150     rr.x:= 0;
       
   151     rr.y:= 0;
       
   152     rr.w:= w;
       
   153     rr.h:= h;
       
   154 
       
   155     texsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, RMask, GMask, BMask, AMask);
   145     TryDo(texsurf <> nil, errmsgCreateSurface, true);
   156     TryDo(texsurf <> nil, errmsgCreateSurface, true);
   146 
   157     TryDo(SDL_SetColorKey(texsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true);
   147     Color:= Clan^.Color;
   158 
   148     Color:= SDL_MapRGB(texsurf^.format, Color shr 16, Color shr 8, Color and $FF);
   159     DrawRoundRect(@rr, cWhiteColor, cNearBlackColor, texsurf, true);
   149     SDL_FillRect(texsurf, nil, Color);
   160 
   150 
   161     rr.x:= 2;
   151     SDL_UpperBlit(tmpsurf, nil, texsurf, nil);
   162     rr.y:= 2;
   152 
   163     rr.w:= w - 4;
   153     TryDo(tmpsurf^.format^.BytesPerPixel = 4, 'Ooops', true);
   164     rr.h:= h - 4;
   154 
   165 
   155     if SDL_MustLock(texsurf) then
   166     DrawRoundRect(@rr, Color, Color, texsurf, false);
   156         SDLTry(SDL_LockSurface(texsurf) >= 0, true);
   167     makeHealthBarTexture:= Surface2Tex(texsurf, false);
   157 
   168     SDL_FreeSurface(texsurf);
   158     // make black pixel be alpha-transparent
   169 end;
   159     for i:= 0 to texsurf^.w * texsurf^.h - 1 do
       
   160         if PLongwordArray(texsurf^.pixels)^[i] = AMask then
       
   161             PLongwordArray(texsurf^.pixels)^[i]:= (RMask or GMask or BMask) and Color;
       
   162 
       
   163     if SDL_MustLock(texsurf) then
       
   164         SDL_UnlockSurface(texsurf);
       
   165 
       
   166     FreeTexture(CrosshairTex);
       
   167     CrosshairTex:= Surface2Tex(texsurf, false);
       
   168     SDL_FreeSurface(texsurf)
       
   169     end;
       
   170 
       
   171 SDL_FreeSurface(tmpsurf)
       
   172 end;
       
   173 
       
   174 
   170 
   175 procedure WriteNames(Font: THWFont);
   171 procedure WriteNames(Font: THWFont);
   176 var t: LongInt;
   172 var t: LongInt;
   177     i, maxLevel: LongInt;
   173     i, maxLevel: LongInt;
   178     r, rr: TSDL_Rect;
   174     r: TSDL_Rect;
   179     drY: LongInt;
   175     drY: LongInt;
   180     texsurf, flagsurf, iconsurf: PSDL_Surface;
   176     texsurf, flagsurf, iconsurf: PSDL_Surface;
   181     foundBot: boolean;
   177     foundBot: boolean;
       
   178     year, month, md : word;
   182 begin
   179 begin
   183     if cOnlyStats then exit;
   180     if cOnlyStats then exit;
   184 r.x:= 0;
   181 r.x:= 0;
   185 r.y:= 0;
   182 r.y:= 0;
   186 drY:= - 4;
   183 drY:= - 4;
       
   184 DecodeDate(Date, year, month, md);
   187 for t:= 0 to Pred(TeamsCount) do
   185 for t:= 0 to Pred(TeamsCount) do
   188     with TeamsArray[t]^ do
   186     with TeamsArray[t]^ do
   189         begin
   187         begin
   190         NameTagTex:= RenderStringTexLim(TeamName, Clan^.Color, Font, cTeamHealthWidth);
   188         NameTagTex:= RenderStringTexLim(TeamName, Clan^.Color, Font, cTeamHealthWidth);
   191 
   189         if length(Owner) > 0 then
   192         r.w:= cTeamHealthWidth + 5;
   190             OwnerTex:= RenderStringTexLim(Owner, Clan^.Color, Font, cTeamHealthWidth);
   193         r.h:= NameTagTex^.h;
       
   194 
       
   195         texsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 32, RMask, GMask, BMask, AMask);
       
   196         TryDo(texsurf <> nil, errmsgCreateSurface, true);
       
   197         TryDo(SDL_SetColorKey(texsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true);
       
   198 
       
   199         DrawRoundRect(@r, cWhiteColor, cNearBlackColor, texsurf, true);
       
   200         rr:= r;
       
   201         inc(rr.x, 2); dec(rr.w, 4); inc(rr.y, 2); dec(rr.h, 4);
       
   202         DrawRoundRect(@rr, Clan^.Color, Clan^.Color, texsurf, false);
       
   203         HealthTex:= Surface2Tex(texsurf, false);
       
   204         SDL_FreeSurface(texsurf);
       
   205 
   191 
   206         r.x:= 0;
   192         r.x:= 0;
   207         r.y:= 0;
   193         r.y:= 0;
   208         r.w:= 32;
   194         r.w:= 32;
   209         r.h:= 32;
   195         r.h:= 32;
   274         for i:= 0 to cMaxHHIndex do
   260         for i:= 0 to cMaxHHIndex do
   275             with Hedgehogs[i] do
   261             with Hedgehogs[i] do
   276                 if Gear <> nil then
   262                 if Gear <> nil then
   277                     begin
   263                     begin
   278                     NameTagTex:= RenderStringTexLim(Name, Clan^.Color, fnt16, cTeamHealthWidth);
   264                     NameTagTex:= RenderStringTexLim(Name, Clan^.Color, fnt16, cTeamHealthWidth);
       
   265                     if Hat = 'NoHat' then
       
   266                         begin
       
   267                         if (month = 4) and (md = 20) then
       
   268                             Hat := 'eastertop'   // Easter
       
   269                         else if (month = 12) and ((md = 24) or (md = 25) or (md = 26)) then
       
   270                             Hat := 'Santa'       // Christmas Eve/Christmas/Boxing Day
       
   271                         else if (month = 10) and (md = 31) then
       
   272                             Hat := 'fr_pumpkin'; // Halloween/Hedgewars' birthday
       
   273                         end;
       
   274                     
   279                     if Hat <> 'NoHat' then
   275                     if Hat <> 'NoHat' then
   280                         begin
   276                         begin
   281                         if (Length(Hat) > 39) and (Copy(Hat,1,8) = 'Reserved') and (Copy(Hat,9,32) = PlayerHash) then
   277                         if (Length(Hat) > 39) and (Copy(Hat,1,8) = 'Reserved') and (Copy(Hat,9,32) = PlayerHash) then
   282                             LoadHedgehogHat(Hedgehogs[i], 'Reserved/' + Copy(Hat,9,Length(Hat)-8))
   278                             LoadHedgehogHat(Hedgehogs[i], 'Reserved/' + Copy(Hat,9,Length(Hat)-8))
   283                         else
   279                         else
   296         DrawRoundRect(@r, cWhiteColor, cNearBlackColor, iconsurf, true);
   292         DrawRoundRect(@r, cWhiteColor, cNearBlackColor, iconsurf, true);
   297         ropeIconTex:= Surface2Tex(iconsurf, false);
   293         ropeIconTex:= Surface2Tex(iconsurf, false);
   298         SDL_FreeSurface(iconsurf);
   294         SDL_FreeSurface(iconsurf);
   299         iconsurf:= nil;
   295         iconsurf:= nil;
   300         end;
   296         end;
   301 end;
   297 
       
   298 
       
   299 for t:= 0 to Pred(ClansCount) do
       
   300     with ClansArray[t]^ do
       
   301         HealthTex:= makeHealthBarTexture(cTeamHealthWidth + 5, Teams[0]^.NameTagTex^.h, Color);
       
   302 
       
   303 GenericHealthTexture:= makeHealthBarTexture(cTeamHealthWidth + 5, TeamsArray[0]^.NameTagTex^.h, cWhiteColor)
       
   304 end;
       
   305 
   302 
   306 
   303 procedure InitHealth;
   307 procedure InitHealth;
   304 var i, t: LongInt;
   308 var i, t: LongInt;
   305 begin
   309 begin
   306 for t:= 0 to Pred(TeamsCount) do
   310 for t:= 0 to Pred(TeamsCount) do
   360     with SpritesData[ii] do
   364     with SpritesData[ii] do
   361         // FIXME - add a sprite attribute to match on rq flags?
   365         // FIXME - add a sprite attribute to match on rq flags?
   362         if (((cReducedQuality and (rqNoBackground or rqLowRes)) = 0) or   // why rqLowRes?
   366         if (((cReducedQuality and (rqNoBackground or rqLowRes)) = 0) or   // why rqLowRes?
   363                 (not (ii in [sprSky, sprSkyL, sprSkyR, sprHorizont, sprHorizontL, sprHorizontR]))) and
   367                 (not (ii in [sprSky, sprSkyL, sprSkyR, sprHorizont, sprHorizontL, sprHorizontR]))) and
   364            (((cReducedQuality and rqPlainSplash) = 0) or ((not (ii in [sprSplash, sprDroplet, sprSDSplash, sprSDDroplet])))) and
   368            (((cReducedQuality and rqPlainSplash) = 0) or ((not (ii in [sprSplash, sprDroplet, sprSDSplash, sprSDDroplet])))) and
   365            (((cReducedQuality and rqKillFlakes) = 0) or (Theme = 'Snow') or (Theme = 'Christmas') or ((not (ii in [sprFlake, sprSDFlake])))) and
   369            (((cReducedQuality and rqKillFlakes) = 0) or cSnow or ((not (ii in [sprFlake, sprSDFlake])))) and
   366            ((cCloudsNumber > 0) or (ii <> sprCloud)) and
   370            ((cCloudsNumber > 0) or (ii <> sprCloud)) and
   367            ((vobCount > 0) or (ii <> sprFlake)) then
   371            ((vobCount > 0) or (ii <> sprFlake)) then
   368             begin
   372             begin
   369             if reload then
   373             if reload then
   370                 tmpsurf:= Surface
   374                 tmpsurf:= Surface
   433 SDL_FreeSurface(tmpsurf);
   437 SDL_FreeSurface(tmpsurf);
   434 
   438 
   435 InitHealth;
   439 InitHealth;
   436 
   440 
   437 PauseTexture:= RenderStringTex(trmsg[sidPaused], cYellowColor, fntBig);
   441 PauseTexture:= RenderStringTex(trmsg[sidPaused], cYellowColor, fntBig);
       
   442 AFKTexture:= RenderStringTex(trmsg[sidAFK], cYellowColor, fntBig);
   438 ConfirmTexture:= RenderStringTex(trmsg[sidConfirm], cYellowColor, fntBig);
   443 ConfirmTexture:= RenderStringTex(trmsg[sidConfirm], cYellowColor, fntBig);
   439 SyncTexture:= RenderStringTex(trmsg[sidSync], cYellowColor, fntBig);
   444 SyncTexture:= RenderStringTex(trmsg[sidSync], cYellowColor, fntBig);
   440 
   445 
   441 if not reload then
   446 if not reload then
   442     AddProgress;
   447     AddProgress;
   503     ai: TAmmoType;
   508     ai: TAmmoType;
   504     i, t: LongInt;
   509     i, t: LongInt;
   505 begin
   510 begin
   506 for ii:= Low(TSprite) to High(TSprite) do
   511 for ii:= Low(TSprite) to High(TSprite) do
   507     begin
   512     begin
   508     FreeTexture(SpritesData[ii].Texture);
   513     FreeAndNilTexture(SpritesData[ii].Texture);
   509     SpritesData[ii].Texture:= nil;
   514 
   510     if (SpritesData[ii].Surface <> nil) and (not reload) then
   515     if (SpritesData[ii].Surface <> nil) and (not reload) then
   511         begin
   516         begin
   512         SDL_FreeSurface(SpritesData[ii].Surface);
   517         SDL_FreeSurface(SpritesData[ii].Surface);
   513         SpritesData[ii].Surface:= nil
   518         SpritesData[ii].Surface:= nil
   514         end
   519         end
   515     end;
   520     end;
   516 SDL_FreeSurface(MissionIcons);
   521 SDL_FreeSurface(MissionIcons);
   517 
   522 
   518 // free the textures declared in uVariables
   523 // free the textures declared in uVariables
   519 FreeTexture(WeaponTooltipTex);
   524 FreeAndNilTexture(CrosshairTexture);
   520 WeaponTooltipTex:= nil;
   525 FreeAndNilTexture(WeaponTooltipTex);
   521 FreeTexture(PauseTexture);
   526 FreeAndNilTexture(PauseTexture);
   522 PauseTexture:= nil;
   527 FreeAndNilTexture(AFKTexture);
   523 FreeTexture(SyncTexture);
   528 FreeAndNilTexture(SyncTexture);
   524 SyncTexture:= nil;
   529 FreeAndNilTexture(ConfirmTexture);
   525 FreeTexture(ConfirmTexture);
   530 FreeAndNilTexture(ropeIconTex);
   526 ConfirmTexture:= nil;
   531 FreeAndNilTexture(HHTexture);
   527 FreeTexture(ropeIconTex);
   532 FreeAndNilTexture(GenericHealthTexture);
   528 ropeIconTex:= nil;
       
   529 FreeTexture(HHTexture);
       
   530 HHTexture:= nil;
       
   531 
       
   532 // free all ammo name textures
   533 // free all ammo name textures
   533 for ai:= Low(TAmmoType) to High(TAmmoType) do
   534 for ai:= Low(TAmmoType) to High(TAmmoType) do
   534     begin
   535     FreeAndNilTexture(Ammoz[ai].NameTex);
   535     FreeTexture(Ammoz[ai].NameTex);
       
   536     Ammoz[ai].NameTex:= nil
       
   537     end;
       
   538 
   536 
   539 // free all count textures
   537 // free all count textures
   540 for i:= Low(CountTexz) to High(CountTexz) do
   538 for i:= Low(CountTexz) to High(CountTexz) do
   541     begin
   539     begin
   542     FreeTexture(CountTexz[i]);
   540     FreeAndNilTexture(CountTexz[i]);
   543     CountTexz[i]:= nil
   541     CountTexz[i]:= nil
   544     end;
   542     end;
       
   543 
       
   544     for t:= 0 to Pred(ClansCount) do
       
   545         begin
       
   546         if ClansArray[t] <> nil then
       
   547             FreeAndNilTexture(ClansArray[t]^.HealthTex);
       
   548         end;
   545 
   549 
   546     // free all team and hedgehog textures
   550     // free all team and hedgehog textures
   547     for t:= 0 to Pred(TeamsCount) do
   551     for t:= 0 to Pred(TeamsCount) do
   548         begin
   552         begin
   549         if TeamsArray[t] <> nil then
   553         if TeamsArray[t] <> nil then
   550             begin
   554             begin
   551             FreeTexture(TeamsArray[t]^.NameTagTex);
   555             FreeAndNilTexture(TeamsArray[t]^.NameTagTex);
   552             TeamsArray[t]^.NameTagTex:= nil;
   556             FreeAndNilTexture(TeamsArray[t]^.GraveTex);
   553             FreeTexture(TeamsArray[t]^.CrosshairTex);
   557             FreeAndNilTexture(TeamsArray[t]^.AIKillsTex);
   554             TeamsArray[t]^.CrosshairTex:= nil;
   558             FreeAndNilTexture(TeamsArray[t]^.FlagTex);
   555             FreeTexture(TeamsArray[t]^.GraveTex);
   559 
   556             TeamsArray[t]^.GraveTex:= nil;
       
   557             FreeTexture(TeamsArray[t]^.HealthTex);
       
   558             TeamsArray[t]^.HealthTex:= nil;
       
   559             FreeTexture(TeamsArray[t]^.AIKillsTex);
       
   560             TeamsArray[t]^.AIKillsTex:= nil;
       
   561             FreeTexture(TeamsArray[t]^.FlagTex);
       
   562             TeamsArray[t]^.FlagTex:= nil;
       
   563             for i:= 0 to cMaxHHIndex do
   560             for i:= 0 to cMaxHHIndex do
   564                 begin
   561                 begin
   565                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].NameTagTex);
   562                 FreeAndNilTexture(TeamsArray[t]^.Hedgehogs[i].NameTagTex);
   566                 TeamsArray[t]^.Hedgehogs[i].NameTagTex:= nil;
   563                 FreeAndNilTexture(TeamsArray[t]^.Hedgehogs[i].HealthTagTex);
   567                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].HealthTagTex);
   564                 FreeAndNilTexture(TeamsArray[t]^.Hedgehogs[i].HatTex);
   568                 TeamsArray[t]^.Hedgehogs[i].HealthTagTex:= nil;
       
   569                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].HatTex);
       
   570                 TeamsArray[t]^.Hedgehogs[i].HatTex:= nil;
       
   571                 end;
   565                 end;
   572             end;
   566             end;
   573         end;
   567         end;
   574 {$IFNDEF PAS2C}
   568 {$IFNDEF PAS2C}
   575 {$IFDEF USE_VIDEO_RECORDING}
   569 {$IFDEF USE_VIDEO_RECORDING}
   676 end;
   670 end;
   677 
   671 
   678 procedure LoadHedgehogHat(var HH: THedgehog; newHat: shortstring);
   672 procedure LoadHedgehogHat(var HH: THedgehog; newHat: shortstring);
   679 var texsurf: PSDL_Surface;
   673 var texsurf: PSDL_Surface;
   680 begin
   674 begin
       
   675     // free the mem of any previously assigned texture.  This was previously only if the new one could be loaded, but, NoHat is usually a better choice
       
   676     if HH.HatTex <> nil then
       
   677         begin
       
   678         FreeTexture(HH.HatTex);
       
   679         HH.HatTex:= nil
       
   680         end;
   681     texsurf:= LoadDataImage(ptHats, newHat, ifNone);
   681     texsurf:= LoadDataImage(ptHats, newHat, ifNone);
   682 AddFileLog('Hat => '+newHat);
   682 AddFileLog('Hat => '+newHat);
   683     // only do something if the hat could be loaded
   683     // only do something if the hat could be loaded
   684     if texsurf <> nil then
   684     if texsurf <> nil then
   685         begin
   685         begin
   686 AddFileLog('Got Hat');
   686 AddFileLog('Got Hat');
   687         // free the mem of any previously assigned texture
       
   688         FreeTexture(HH.HatTex);
       
   689 
   687 
   690         // assign new hat to hedgehog
   688         // assign new hat to hedgehog
   691         HH.HatTex:= Surface2Tex(texsurf, true);
   689         HH.HatTex:= Surface2Tex(texsurf, true);
   692 
   690 
   693         // cleanup: free temporary surface mem
   691         // cleanup: free temporary surface mem
   839 end;
   837 end;
   840 
   838 
   841 {$ENDIF}
   839 {$ENDIF}
   842 
   840 
   843 procedure SetupOpenGL;
   841 procedure SetupOpenGL;
   844 var name: array[byte] of char;
   842 var buf: array[byte] of char;
   845     AuxBufNum: LongInt = 0;
   843     AuxBufNum: LongInt = 0;
   846     tmpstr: AnsiString;
   844     tmpstr: AnsiString;
   847     tmpint: LongInt;
   845     tmpint: LongInt;
   848     tmpn: LongInt;
   846     tmpn: LongInt;
   849 begin
   847 begin
   850 
   848 
   851 {$IFDEF SDL2}
   849 {$IFDEF SDL2}
   852     name:= SDL_GetCurrentVideoDriver();
   850     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_GetCurrentVideoDriver()) + ')');
   853 {$ELSE}
   851 {$ELSE}
   854     name:= SDL_VideoDriverName(name, sizeof(name));
   852     buf[0]:= char(0); // avoid compiler hint
       
   853     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
   855 {$ENDIF}
   854 {$ENDIF}
   856 
   855 
   857     AuxBufNum:= AuxBufNum;
   856     AuxBufNum:= AuxBufNum;
   858     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(name) + ')');
       
   859 
   857 
   860 {$IFDEF MOBILE}
   858 {$IFDEF MOBILE}
   861     // TODO: this function creates an opengles1.1 context
   859     // TODO: this function creates an opengles1.1 context
   862     // un-comment below and add proper logic to support opengles2.0
   860     // un-comment below and add proper logic to support opengles2.0
   863     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
   861     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);