DEADSOFTWARE

gl: properly free memory
[d2df-sdl.git] / src / game / renders / opengl / r_textures.pas
index 0114880088cc352459cfd7911021c99978f2d85f..24ea1642e7aa37ec447a509ed4ce2c02627f7bbf 100644 (file)
@@ -120,6 +120,8 @@ interface
       anim: TAnimInfo;
     end;
 
+    TConvProc = function (x: Integer): Integer;
+
   procedure r_Textures_Initialize;
   procedure r_Textures_Finalize;
 
@@ -130,12 +132,13 @@ interface
 
   function r_Textures_LoadStreamFromFile (const filename: AnsiString; w, h, count, cw: Integer; st: TGLTextureArray; rs: TRectArray; log: Boolean = True): Boolean;
 
-  function r_Textures_LoadFontFromFile (const filename: AnsiString; constref f: TFontInfo; skipch: Integer; log: Boolean = true): TGLFont;
+  function r_Textures_LoadFontFromFile (const filename: AnsiString; constref f: TFontInfo; font2enc: TConvProc; log: Boolean = true): TGLFont;
 
 implementation
 
   uses
     SysUtils, Classes,
+    r_common,
     e_log, e_res, WADReader, Config,
     g_console, // cvar declaration
     Imaging, ImagingTypes, ImagingUtility
@@ -329,8 +332,8 @@ implementation
     var i: Integer;
   begin
     for i := 0 to self.count - 1 do
-      self.mTexture[i].Free;
-    self.mTexture := nil;
+      r_Common_FreeAndNil(self.mTexture[i]);
+    SetLength(self.mTexture, 0);
     inherited;
   end;
 
@@ -411,10 +414,10 @@ implementation
       begin
         glDeleteTextures(1, @atl[i].id);
         atl[i].id := 0;
-        atl[i].Free;
+        r_Common_FreeAndNil(atl[i]);
       end;
-      atl := nil;
     end;
+    SetLength(atl, 0);
   end;
 
   function r_Textures_FixImageData (var img: TImageData): Boolean;
@@ -797,26 +800,28 @@ implementation
 
   (* --------- TGLFont --------- *)
 
-  function r_Textures_LoadFontFromFile (const filename: AnsiString; constref f: TFontInfo; skipch: Integer; log: Boolean = true): TGLFont;
-    var i: Integer; st: TGLTextureArray; font: TGLFont; t: TGLTexture;
+  function r_Textures_LoadFontFromFile (const filename: AnsiString; constref f: TFontInfo; font2enc: TConvProc; log: Boolean = true): TGLFont;
+    var i, ch: Integer; st, stch: TGLTextureArray; font: TGLFont;
   begin
-    ASSERT(skipch >= 0);
     result := nil;
     SetLength(st, 256);
     if r_Textures_LoadStreamFromFile(filename, f.w, f.h, 256, 16, st, nil, log) then
     begin
-      if skipch > 0 then
+      font := TGLFont.Create();
+      font.info := f;
+      font.ch := st;
+      if Assigned(font2enc) then
       begin
+        SetLength(stch, 256);
         for i := 0 to 255 do
         begin
-          t := st[i];
-          st[i] := st[(i + skipch) mod 256];
-          st[(i + skipch) mod 256] := t;
+          ch := font2enc(i);
+          ASSERT((ch >= 0) and (ch <= 255));
+          stch[ch] := st[i];
         end;
+        font.ch := stch;
+        SetLength(st, 0);
       end;
-      font := TGLFont.Create();
-      font.info := f;
-      font.ch := st;
       result := font;
     end;
   end;