procedure for loading hats
authorsheepluva
Fri, 28 Jan 2011 02:47:11 +0100
changeset 4874 f508adb769b8
parent 4873 98dbb9b985e5
child 4875 2a37a0e0892d
procedure for loading hats
hedgewars/GSHandlers.inc
hedgewars/uStore.pas
--- a/hedgewars/GSHandlers.inc	Tue Jan 25 20:32:58 2011 +0100
+++ b/hedgewars/GSHandlers.inc	Fri Jan 28 02:47:11 2011 +0100
@@ -4507,13 +4507,9 @@
                 RenderHealth(resgear^.Hedgehog^);
                 RecountTeamHealth(resgear^.Hedgehog^.Team);
                 resgear^.Hedgehog^.Effects[heResurrected]:= true;
+                // only make hat-less hedgehogs look like zombies, preserve existing hats
                 if resgear^.Hedgehog^.Hat = 'NoHat' then
-                    begin
-                    FreeTexture(resgear^.Hedgehog^.HatTex);
-                    resgear^.Hedgehog^.HatTex := Surface2Tex(
-                        LoadImage(Pathz[ptHats] + '/Reserved/Zombie', ifNone),
-                        True)
-                    end
+                    LoadHedgehogHat(resgear, 'Reserved/Zombie');
                 end;
 
         hh^.Gear^.dY := _0;
--- a/hedgewars/uStore.pas	Tue Jan 25 20:32:58 2011 +0100
+++ b/hedgewars/uStore.pas	Fri Jan 28 02:47:11 2011 +0100
@@ -32,6 +32,7 @@
 procedure AddProgress;
 procedure FinishProgress;
 function  LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
+procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
 procedure SetupOpenGL;
 procedure SetScale(f: GLfloat);
 function  RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
@@ -141,6 +142,7 @@
 
         FlagTex:= Surface2Tex(texsurf, false);
         SDL_FreeSurface(texsurf);
+        texsurf:= nil;
 
         AIKillsTex := RenderStringTex(inttostr(stats.AIKills), Clan^.Color, fnt16);
 
@@ -154,15 +156,9 @@
                     if Hat <> 'NoHat' then
                         begin
                         if (Length(Hat) > 39) and (Copy(Hat,1,8) = 'Reserved') and (Copy(Hat,9,32) = PlayerHash) then
-                            texsurf:= LoadImage(Pathz[ptHats] + '/Reserved/' + Copy(Hat,9,Length(s)-8), ifNone)
+                            LoadHedgehogHat(Gear, 'Reserved/' + Copy(Hat,9,Length(s)-8))
                         else
-                            texsurf:= LoadImage(Pathz[ptHats] + '/' + Hat, ifNone);
-                        if texsurf <> nil then
-                            begin
-                            HatTex:= Surface2Tex(texsurf, true);
-                            SDL_FreeSurface(texsurf)
-                            end;
-                        texsurf:= nil;
+                            LoadHedgehogHat(Gear, Hat);
                         end
                     end;
         end;
@@ -435,6 +431,25 @@
     LoadImage:= tmpsurf //Result
 end;
 
+procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
+var texsurf: PSDL_Surface;
+begin
+    texsurf:= LoadImage(Pathz[ptHats] + '/' + newHat, ifNone);
+
+    // only do something if the hat could be loaded
+    if texsurf <> nil then
+        begin
+        // free the mem of any previously assigned texture
+        FreeTexture(HHGear^.Hedgehog^.HatTex);
+
+        // assign new hat to hedgehog
+        HHGear^.Hedgehog^.HatTex:= Surface2Tex(texsurf, true);
+
+        // cleanup: free temporary surface mem
+        SDL_FreeSurface(texsurf)
+        end;
+end;
+
 function glLoadExtension(extension : shortstring) : boolean;
 begin
 {$IF GLunit = gles11}