DEADSOFTWARE

gl: free old map textures before load new textures
[d2df-sdl.git] / src / game / renders / opengl / r_map.pas
index 5513d688256169d2c8dcfe0994e945d36912bc5d..c008f432def97874c3cce8089a30e1ce109847d5 100644 (file)
@@ -525,10 +525,22 @@ implementation
       r_Common_FreeAndNil(Items[i].tex);
   end;
 
+  procedure r_Map_FreeTextures;
+    var i: Integer;
+  begin
+    plist.Clear;
+    r_Common_FreeAndNil(SkyTexture);
+    if RenTextures <> nil then
+      for i := 0 to High(RenTextures) do
+        r_Common_FreeAndNil(RenTextures[i].tex);
+    SetLength(RenTextures, 0);
+  end;
+
   procedure r_Map_LoadTextures;
     const DefaultAnimInfo: TAnimInfo = (loop: true; delay: 1; frames: 1; back: false);
     var i, n: Integer; txt: TAnimTextInfo;
   begin
+    r_Map_FreeTextures;
     if Textures <> nil then
     begin
       n := Length(Textures);
@@ -536,8 +548,8 @@ implementation
       r_Common_SetLoading(_lc[I_LOAD_TEXTURES], n);
       for i := 0 to n - 1 do
       begin
-        txt.anim := DefaultAnimInfo;
         RenTextures[i].tex := nil;
+        RenTextures[i].anim := DefaultAnimInfo;
         case Textures[i].TextureName of
           TEXTURE_NAME_WATER: RenTextures[i].spec := TEXTURE_SPECIAL_WATER;
           TEXTURE_NAME_ACID1: RenTextures[i].spec := TEXTURE_SPECIAL_ACID1;
@@ -545,12 +557,18 @@ implementation
           else
             RenTextures[i].spec := 0;
             RenTextures[i].tex := r_Textures_LoadMultiTextFromFile(Textures[i].FullName, txt, []);
-            if RenTextures[i].tex = nil then
-              e_LogWritefln('r_Map_LoadTextures: failed to load texture: %s', [Textures[i].FullName])
-            else
+            if RenTextures[i].tex <> nil then
+            begin
+              RenTextures[i].anim := txt.anim;
               r_Common_StepLoading(1);
+            end
+            else
+            begin
+              e_LogWritefln('r_Map_LoadTextures: failed to load texture: %s', [Textures[i].FullName]);
+            end;
         end;
-        RenTextures[i].anim := txt.anim;
+        ASSERT(RenTextures[i].anim.frames > 0);
+        ASSERT(RenTextures[i].anim.delay > 0);
       end;
     end;
     if gMapInfo.SkyFullName <> '' then
@@ -561,41 +579,37 @@ implementation
     plist.Clear;
   end;
 
-  procedure r_Map_FreeTextures;
-    var i: Integer;
-  begin
-    plist.Clear;
-    r_Common_FreeAndNil(SkyTexture);
-    if RenTextures <> nil then
-      for i := 0 to High(RenTextures) do
-        r_Common_FreeAndNil(RenTextures[i].tex);
-    SetLength(RenTextures, 0);
-  end;
-
   procedure r_Map_DrawPanel (p: TPanel);
     var Texture: Integer; t: TGLMultiTexture; tex: TGLTexture; count, frame: LongInt; a: TAnimInfo;
   begin
     ASSERT(p <> nil);
     if p.FCurTexture >= 0 then
     begin
+      ASSERT(p.FCurTexture <= High(p.TextureIDs));
       Texture := p.TextureIDs[p.FCurTexture].Texture;
-      t := RenTextures[Texture].tex;
-      if (RenTextures[Texture].spec = 0) or (t <> nil) then
+      t := nil;
+      ASSERT(Texture >= -1);
+      ASSERT(Texture <= High(RenTextures));
+      if Texture >= 0 then
       begin
-        count := 0; frame := 0;
-        if p.AnimTime <= gTime then
+        t := RenTextures[Texture].tex;
+        if (RenTextures[Texture].spec = 0) or (t <> nil) then
         begin
-          a := RenTextures[Texture].anim;
-          a.loop := p.AnimLoop;
-          g_Anim_GetFrameByTime(a, (gTime - p.AnimTime) DIV GAME_TICK, count, frame);
+          count := 0; frame := 0;
+          if p.AnimTime <= gTime then
+          begin
+            a := RenTextures[Texture].anim;
+            a.loop := p.AnimLoop;
+            g_Anim_GetFrameByTime(a, (gTime - p.AnimTime) DIV GAME_TICK, count, frame);
+          end;
+          if t <> nil then
+          begin
+            tex := t.GetTexture(frame);
+            r_Draw_TextureRepeat(tex, p.x, p.y, p.width, p.height, false, 255, 255, 255, 255 - p.alpha, p.blending);
+          end
+          else
+            r_Draw_TextureRepeat(nil, p.x, p.y, p.width, p.height, false, 255, 255, 255, 255, false);
         end;
-        if t <> nil then
-        begin
-          tex := t.GetTexture(frame);
-          r_Draw_TextureRepeat(tex, p.x, p.y, p.width, p.height, false, 255, 255, 255, 255 - p.alpha, p.blending);
-        end
-        else
-          r_Draw_TextureRepeat(nil, p.x, p.y, p.width, p.height, false, 255, 255, 255, 255, false);
       end;
 
       if t = nil then