hedgewars/uStore.pas
branchhedgeroid
changeset 5644 4ad07103cfae
parent 5577 272c82f82cee
parent 5608 1e5d925dde9d
child 5667 24f4d7a37038
equal deleted inserted replaced
5641:06558ee35f51 5644:4ad07103cfae
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 {$IF GLunit = GL}{$DEFINE GLunit:=GL,GLext}{$ENDIF}
    20 {$IF GLunit = GL}{$DEFINE GLunit:=GL,GLext}{$ENDIF}
    21 
    21 
    22 unit uStore;
    22 unit uStore;
    23 interface
    23 interface
    24 uses sysutils, uConsts, SDLh, GLunit, uTypes;
    24 uses sysutils, uConsts, SDLh, GLunit, uTypes, uLandTexture;
    25 
    25 
    26 procedure initModule;
    26 procedure initModule;
    27 procedure freeModule;
    27 procedure freeModule;
    28 
    28 
    29 procedure StoreLoad;
    29 procedure StoreLoad(reload: boolean = false);
    30 procedure StoreRelease;
    30 procedure StoreRelease(reload: boolean = false);
    31 procedure RenderHealth(var Hedgehog: THedgehog);
    31 procedure RenderHealth(var Hedgehog: THedgehog);
    32 procedure AddProgress;
    32 procedure AddProgress;
    33 procedure FinishProgress;
    33 procedure FinishProgress;
    34 function  LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    34 function  LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
    35 procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
    35 procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
   116     end;
   116     end;
   117 
   117 
   118 SDL_FreeSurface(tmpsurf)
   118 SDL_FreeSurface(tmpsurf)
   119 end;
   119 end;
   120 
   120 
   121 procedure StoreLoad;
   121 procedure StoreLoad(reload: boolean);
   122 var s: shortstring;
   122 var s: shortstring;
   123 
   123 
   124     procedure WriteNames(Font: THWFont);
   124     procedure WriteNames(Font: THWFont);
   125     var t: LongInt;
   125     var t: LongInt;
   126         i: LongInt;
   126         i: LongInt;
   288            ((vobCount > 0) or (ii <> sprFlake)) then
   288            ((vobCount > 0) or (ii <> sprFlake)) then
   289         begin
   289         begin
   290             if AltPath = ptNone then
   290             if AltPath = ptNone then
   291                 if ii in [sprHorizont, sprHorizontL, sprHorizontR, sprSky, sprSkyL, sprSkyR, sprChunk] then // FIXME: hack
   291                 if ii in [sprHorizont, sprHorizontL, sprHorizontR, sprSky, sprSkyL, sprSkyR, sprChunk] then // FIXME: hack
   292                     begin
   292                     begin
   293                     tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
   293                     if not reload then
   294                     if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent)
   294                         begin
       
   295                         tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
       
   296                         if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent)
       
   297                         end
       
   298                     else tmpsurf:= Surface
   295                     end
   299                     end
   296                 else
   300                 else
   297                     begin
   301                     begin
   298                     tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
   302                     if not reload then
   299                     if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent or ifCritical)
   303                         begin
       
   304                         tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
       
   305                         if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent or ifCritical)
       
   306                         end
       
   307                     else tmpsurf:= Surface
   300                     end
   308                     end
   301             else begin
   309             else begin
   302                 tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
   310                 if not reload then
   303                 if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
   311                     begin
   304                 if tmpsurf = nil then tmpsurf:= LoadImage(UserPathz[AltPath] + '/' + FileName, ifAlpha or ifTransparent);
   312                     tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
   305                 if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[AltPath] + '/' + FileName, ifAlpha or ifCritical or ifTransparent);
   313                     if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
       
   314                     if tmpsurf = nil then tmpsurf:= LoadImage(UserPathz[AltPath] + '/' + FileName, ifAlpha or ifTransparent);
       
   315                     if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[AltPath] + '/' + FileName, ifAlpha or ifCritical or ifTransparent)
       
   316                     end
       
   317                 else tmpsurf:= Surface
   306                 end;
   318                 end;
   307 
   319 
   308             if tmpsurf <> nil then
   320             if tmpsurf <> nil then
   309             begin
   321                 begin
   310                 if getImageDimensions then
   322                 if getImageDimensions then
   311 		begin
   323                     begin
   312                     imageWidth:= tmpsurf^.w;
   324                     imageWidth:= tmpsurf^.w;
   313                     imageHeight:= tmpsurf^.h
   325                     imageHeight:= tmpsurf^.h
   314                 end;
   326                     end;
   315                 if getDimensions then
   327                 if getDimensions then
   316                 begin
   328                     begin
   317                     Width:= tmpsurf^.w;
   329                     Width:= tmpsurf^.w;
   318                     Height:= tmpsurf^.h
   330                     Height:= tmpsurf^.h
   319                 end;
   331                     end;
   320                 if (ii in [sprSky, sprSkyL, sprSkyR, sprHorizont, sprHorizontL, sprHorizontR]) then
   332                 if (ii in [sprSky, sprSkyL, sprSkyR, sprHorizont, sprHorizontL, sprHorizontR]) then
   321                 begin
   333                     begin
   322                     Texture:= Surface2Tex(tmpsurf, true);
   334                     Texture:= Surface2Tex(tmpsurf, true);
   323                     Texture^.Scale:= 2
   335                     Texture^.Scale:= 2
   324                 end
   336                     end
   325                 else
   337                 else
   326                 begin
   338                     begin
   327                     Texture:= Surface2Tex(tmpsurf, false);
   339                     Texture:= Surface2Tex(tmpsurf, false);
   328                     // HACK: We should include some sprite attribute to define the texture wrap directions
   340                     // HACK: We should include some sprite attribute to define the texture wrap directions
   329                     if ((ii = sprWater) or (ii = sprSDWater)) and ((cReducedQuality and (rq2DWater or rqClampLess)) = 0) then
   341                     if ((ii = sprWater) or (ii = sprSDWater)) and ((cReducedQuality and (rq2DWater or rqClampLess)) = 0) then
   330                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   342                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   331                 end;
   343                     end;
   332                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, priority);
   344                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, priority);
   333                 if saveSurf then
   345 // This should maybe be flagged. It wastes quite a bit of memory.
   334                     Surface:= tmpsurf else SDL_FreeSurface(tmpsurf)
   346                 if not reload then
       
   347                     begin
       
   348 {$IFNDEF DARWIN & WIN32}
       
   349                     if saveSurf then Surface:= tmpsurf else SDL_FreeSurface(tmpsurf)
       
   350 {$ELSE}
       
   351                     Surface:= tmpsurf 
       
   352 {$ENDIF}
       
   353                     end
   335                 end
   354                 end
   336             else
   355             else
   337                 Surface:= nil
   356                 Surface:= nil
   338         end;
   357         end;
   339 
   358 
   382 {$IFDEF SDL_IMAGE_NEWER}
   401 {$IFDEF SDL_IMAGE_NEWER}
   383 IMG_Quit();
   402 IMG_Quit();
   384 {$ENDIF}
   403 {$ENDIF}
   385 end;
   404 end;
   386 
   405 
   387 procedure StoreRelease;
   406 procedure StoreRelease(reload: boolean);
   388 var ii: TSprite;
   407 var ii: TSprite;
   389     ai: TAmmoType;
   408     ai: TAmmoType;
   390     i, t: LongInt;
   409     i, t: LongInt;
   391 begin
   410 begin
   392     for ii:= Low(TSprite) to High(TSprite) do
   411     for ii:= Low(TSprite) to High(TSprite) do
   393         begin
   412         begin
   394         FreeTexture(SpritesData[ii].Texture);
   413         FreeTexture(SpritesData[ii].Texture);
   395         SpritesData[ii].Texture:= nil;
   414         SpritesData[ii].Texture:= nil;
   396         if SpritesData[ii].Surface <> nil then
   415         if (SpritesData[ii].Surface <> nil) and not reload then
       
   416             begin
   397             SDL_FreeSurface(SpritesData[ii].Surface);
   417             SDL_FreeSurface(SpritesData[ii].Surface);
   398         SpritesData[ii].Surface:= nil;
   418             SpritesData[ii].Surface:= nil
       
   419             end
   399         end;
   420         end;
   400     SDL_FreeSurface(MissionIcons);
   421     SDL_FreeSurface(MissionIcons);
   401     FreeTexture(ropeIconTex);
   422     FreeTexture(ropeIconTex);
       
   423     ropeIconTex:= nil;
   402     FreeTexture(HHTexture);
   424     FreeTexture(HHTexture);
       
   425     HHTexture:= nil;
   403     FreeTexture(PauseTexture);
   426     FreeTexture(PauseTexture);
       
   427     PauseTexture:= nil;
   404     FreeTexture(ConfirmTexture);
   428     FreeTexture(ConfirmTexture);
       
   429     ConfirmTexture:= nil;
   405     FreeTexture(SyncTexture);
   430     FreeTexture(SyncTexture);
       
   431     SyncTexture:= nil;
   406     // free all ammo name textures
   432     // free all ammo name textures
   407     for ai:= Low(TAmmoType) to High(TAmmoType) do
   433     for ai:= Low(TAmmoType) to High(TAmmoType) do
       
   434         begin
   408         FreeTexture(Ammoz[ai].NameTex);
   435         FreeTexture(Ammoz[ai].NameTex);
       
   436         Ammoz[ai].NameTex:= nil
       
   437         end;
   409 
   438 
   410     // free all count textures
   439     // free all count textures
   411     for i:= Low(CountTexz) to High(CountTexz) do
   440     for i:= Low(CountTexz) to High(CountTexz) do
       
   441         begin
   412         FreeTexture(CountTexz[i]);
   442         FreeTexture(CountTexz[i]);
       
   443         CountTexz[i]:= nil
       
   444         end;
   413 
   445 
   414     // free all team and hedgehog textures
   446     // free all team and hedgehog textures
   415     for t:= 0 to Pred(TeamsCount) do
   447     for t:= 0 to Pred(TeamsCount) do
   416     begin
   448     begin
   417         if TeamsArray[t] <> nil then
   449         if TeamsArray[t] <> nil then
   418         begin
   450         begin
   419             FreeTexture(TeamsArray[t]^.NameTagTex);
   451             FreeTexture(TeamsArray[t]^.NameTagTex);
       
   452             TeamsArray[t]^.NameTagTex:= nil;
   420             FreeTexture(TeamsArray[t]^.CrosshairTex);
   453             FreeTexture(TeamsArray[t]^.CrosshairTex);
       
   454             TeamsArray[t]^.CrosshairTex:= nil;
   421             FreeTexture(TeamsArray[t]^.GraveTex);
   455             FreeTexture(TeamsArray[t]^.GraveTex);
       
   456             TeamsArray[t]^.GraveTex:= nil;
   422             FreeTexture(TeamsArray[t]^.HealthTex);
   457             FreeTexture(TeamsArray[t]^.HealthTex);
       
   458             TeamsArray[t]^.HealthTex:= nil;
   423             FreeTexture(TeamsArray[t]^.AIKillsTex);
   459             FreeTexture(TeamsArray[t]^.AIKillsTex);
       
   460             TeamsArray[t]^.AIKillsTex:= nil;
   424             FreeTexture(TeamsArray[t]^.FlagTex);
   461             FreeTexture(TeamsArray[t]^.FlagTex);
       
   462             TeamsArray[t]^.FlagTex:= nil;
   425             for i:= 0 to cMaxHHIndex do
   463             for i:= 0 to cMaxHHIndex do
   426             begin
   464             begin
   427                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].NameTagTex);
   465                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].NameTagTex);
       
   466                 TeamsArray[t]^.Hedgehogs[i].NameTagTex:= nil;
   428                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].HealthTagTex);
   467                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].HealthTagTex);
       
   468                 TeamsArray[t]^.Hedgehogs[i].HealthTagTex:= nil;
   429                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].HatTex);
   469                 FreeTexture(TeamsArray[t]^.Hedgehogs[i].HatTex);
       
   470                 TeamsArray[t]^.Hedgehogs[i].HatTex:= nil;
   430             end;
   471             end;
   431         end;
   472         end;
   432     end;
   473     end;
   433 {$IFNDEF S3D_DISABLED}
   474 {$IFNDEF S3D_DISABLED}
   434     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   475     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   906 
   947 
   907 procedure chFullScr(var s: shortstring);
   948 procedure chFullScr(var s: shortstring);
   908 var flags: Longword = 0;
   949 var flags: Longword = 0;
   909     ico: PSDL_Surface;
   950     ico: PSDL_Surface;
   910     buf: array[byte] of char;
   951     buf: array[byte] of char;
       
   952     reinit: boolean;
   911     {$IFDEF SDL13}x, y: LongInt;{$ENDIF}
   953     {$IFDEF SDL13}x, y: LongInt;{$ENDIF}
   912 begin
   954 begin
   913     s:= s; // avoid compiler hint
   955     s:= s; // avoid compiler hint
   914     if Length(s) = 0 then cFullScreen:= not cFullScreen
   956     if Length(s) = 0 then cFullScreen:= not cFullScreen
   915     else cFullScreen:= s = '1';
   957     else cFullScreen:= s = '1';
   941         SDL_FreeSurface(ico)
   983         SDL_FreeSurface(ico)
   942     end;
   984     end;
   943 
   985 
   944     // set window title
   986     // set window title
   945     SDL_WM_SetCaption('Hedgewars', nil);
   987     SDL_WM_SetCaption('Hedgewars', nil);
   946 
   988     reinit:= false;
   947     if SDLPrimSurface <> nil then
   989     if SDLPrimSurface <> nil then
   948     begin
   990     begin
       
   991 {$IFDEF DARWIN | WIN32}
       
   992         reinit:= true;
       
   993 {$ENDIF}
   949         AddFileLog('Freeing old primary surface...');
   994         AddFileLog('Freeing old primary surface...');
   950         SDL_FreeSurface(SDLPrimSurface);
   995         SDL_FreeSurface(SDLPrimSurface);
   951         SDLPrimSurface:= nil;
   996         SDLPrimSurface:= nil;
   952     end;
   997     end;
   953 
   998 
   980         end;
  1025         end;
   981 {$ENDIF}
  1026 {$ENDIF}
   982 
  1027 
   983     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
  1028     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
   984     SetupOpenGL();
  1029     SetupOpenGL();
       
  1030     if reinit then
       
  1031         begin
       
  1032         StoreRelease(true);
       
  1033         StoreLoad(true);
       
  1034         UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT)
       
  1035         end;
   985 end;
  1036 end;
   986 
  1037 
   987 procedure initModule;
  1038 procedure initModule;
   988 var ai: TAmmoType;
  1039 var ai: TAmmoType;
   989     i: LongInt;
  1040     i: LongInt;