DEADSOFTWARE

gl: properly free memory
[d2df-sdl.git] / src / game / renders / opengl / r_common.pas
index 9c6b8b420ff0052207db872a45f3dae8b494dbbb..c12cbbbb44bfcdc446cd92f9ace3f041fe98fee1 100644 (file)
@@ -39,6 +39,9 @@ interface
   var
     r_Common_ProcessLoadingCallback: TProcedure;
 
+  procedure r_Common_FreeAndNil (var obj);
+  procedure r_Common_FreeMemAndNil (var p);
+
   function  r_Common_LoadThis (const name: AnsiString; var here: THereTexture): Boolean;
   procedure r_Common_FreeThis (var here: THereTexture);
 
@@ -70,7 +73,7 @@ interface
   function r_Common_LoadTextureMultiFromFileAndInfo (const filename: AnsiString; w, h, count: Integer; log: Boolean = True): TGLMultiTexture;
   function r_Common_LoadTextureMultiTextFromFile (const filename: AnsiString; var txt: TAnimTextInfo; log: Boolean = True): TGLMultiTexture;
   function r_Common_LoadTextureStreamFromFile (const filename: AnsiString; w, h, count, cw: Integer; st: TGLTextureArray; rs: TRectArray; log: Boolean = True): Boolean;
-  function r_Common_LoadTextureFontFromFile (const filename: AnsiString; constref f: TFontInfo; skipch: Integer; log: Boolean = true): TGLFont;
+  function r_Common_LoadTextureFontFromFile (const filename: AnsiString; constref f: TFontInfo; font2enc: TConvProc; log: Boolean = true): TGLFont;
 
   procedure r_Common_Load;
   procedure r_Common_Free;
@@ -90,6 +93,24 @@ implementation
   var
     BackgroundTexture: THereTexture;
 
+  procedure r_Common_FreeAndNil (var obj);
+    var temp: TObject;
+  begin
+    temp := TObject(obj);
+    Pointer(obj) := nil;
+    if temp <> nil then
+      temp.Free;
+  end;
+
+  procedure r_Common_FreeMemAndNil (var p);
+    var temp: Pointer;
+  begin
+    temp := Pointer(p);
+    Pointer(p) := nil;
+    if temp <> nil then
+      FreeMem(temp)
+  end;
+
   procedure r_Common_GetObjectPos (const obj: TObj; out x, y: Integer);
     var fx, fy: Integer;
   begin
@@ -299,9 +320,7 @@ implementation
   procedure r_Common_FreeThis (var here: THereTexture);
   begin
     here.name := '';
-    if here.id <> nil then
-      here.id.Free;
-    here.id := nil;
+    r_Common_FreeAndNil(here.id);
   end;
 
   function r_Common_LoadThis (const name: AnsiString; var here: THereTexture): Boolean;
@@ -349,13 +368,22 @@ implementation
       r_Common_DrawBackgroundImage(BackgroundTexture.id)
   end;
 
+  function r_Common_Std2Win (i: Integer): Integer;
+  begin
+    case i of
+      0..223:   result := i + 32;
+      224..255: result := i - 224;
+      otherwise result := -1;
+    end
+  end;
+
   function r_Common_LoadFont (const name: AnsiString): TGLFont;
-    var info: TFontInfo; skiphack: Integer;
+    var info: TFontInfo; p: TConvProc;
   begin
     result := nil;
-    if name = 'STD' then skiphack := 144 else skiphack := 0;
+    if name = 'STD' then p := @r_Common_Std2Win else p := nil;
     if r_Font_LoadInfoFromFile(GameWad + ':FONTS/' + name + 'TXT', info) then
-      result := r_Common_LoadTextureFontFromFile(GameWad + ':FONTS/' + name + 'FONT', info, skiphack, true);
+      result := r_Common_LoadTextureFontFromFile(GameWad + ':FONTS/' + name + 'FONT', info, p, true);
     if result = nil then
       e_logwritefln('failed to load font %s', [name]);
   end;
@@ -372,9 +400,9 @@ implementation
   procedure r_Common_Free;
   begin
     r_Common_FreeThis(BackgroundTexture);
-    menufont.Free;
-    smallfont.Free;
-    stdfont.Free;
+    FreeAndNil(menufont);
+    FreeAndNil(smallfont);
+    FreeAndNil(stdfont);
   end;
 
   (* --------- Loading screen helpers --------- *)
@@ -439,9 +467,9 @@ implementation
     r_Common_StepLoading(1);
   end;
 
-  function r_Common_LoadTextureFontFromFile (const filename: AnsiString; constref f: TFontInfo; skipch: Integer; log: Boolean = true): TGLFont;
+  function r_Common_LoadTextureFontFromFile (const filename: AnsiString; constref f: TFontInfo; font2enc: TConvProc; log: Boolean = true): TGLFont;
   begin
-    result := r_Textures_LoadFontFromFile (filename, f, skipch, log);
+    result := r_Textures_LoadFontFromFile (filename, f, font2enc, log);
     r_Common_StepLoading(1);
   end;