hedgewars/uStore.pas
changeset 4874 f508adb769b8
parent 4850 434cd1284204
child 4889 f71e30eb1d37
--- 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}