DEADSOFTWARE

gl: fix stdfont cyrillic chars
[d2df-sdl.git] / src / game / renders / opengl / r_textures.pas
index 5950e36f6f272bf67b39af7fd2cfac386291ae6a..aa3bbff038c02bb6c37246f754bf3deb1ee94891 100644 (file)
@@ -23,7 +23,7 @@ interface
     {$ELSE}
       GL, GLEXT,
     {$ENDIF}
-    g_base,  // TRectHW
+    g_base, g_animations,  // TRectHW, TAnimInfo
     utils,
     r_atlas, r_fonts
   ;
@@ -82,7 +82,6 @@ interface
     TGLMultiTexture = class
       private
         mTexture: array of TGLTexture;
-        mBackanim: Boolean;
 
       public
         destructor Destroy; override;
@@ -95,7 +94,6 @@ interface
         property width: Integer read GetWidth;
         property height: Integer read GetHeight;
         property count: Integer read GetCount;
-        property backAnim: Boolean read mBackanim; (* this property must be located at TAnimState? *)
     end;
 
     TGLTextureArray = array of TGLTexture;
@@ -111,28 +109,42 @@ interface
         destructor Destroy; override;
         function GetChar (c: AnsiChar): TGLTexture;
         function GetWidth (c: AnsiChar): Integer;
+        function GetMaxWidth (): Integer;
+        function GetMaxHeight (): Integer;
         function GetSpace (): Integer;
     end;
 
+    TAnimTextInfo = record
+      name: AnsiString;
+      w, h: Integer;
+      anim: TAnimInfo;
+    end;
+
+    TConvProc = function (x: Integer): Integer;
+
   procedure r_Textures_Initialize;
   procedure r_Textures_Finalize;
 
   function r_Textures_LoadFromFile (const filename: AnsiString; log: Boolean = True): TGLTexture;
   function r_Textures_LoadMultiFromFile (const filename: AnsiString; log: Boolean = True): TGLMultiTexture;
-  function r_Textures_LoadMultiFromFileAndInfo (const filename: AnsiString; w, h, count: Integer; backanim: Boolean; log: Boolean = True): TGLMultiTexture;
+  function r_Textures_LoadMultiFromFileAndInfo (const filename: AnsiString; w, h, count: Integer; log: Boolean = True): TGLMultiTexture;
+  function r_Textures_LoadMultiTextFromFile (const filename: AnsiString; var txt: TAnimTextInfo; log: Boolean = True): TGLMultiTexture;
+
   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,
     e_log, e_res, WADReader, Config,
+    g_console, // cvar declaration
     Imaging, ImagingTypes, ImagingUtility
   ;
 
   var
+    r_GL_MaxTexSize: WORD;
     maxTileSize: Integer;
     atl: array of TGLAtlas;
 
@@ -349,19 +361,47 @@ implementation
 
   (* --------- Init / Fin --------- *)
 
+  function IsPOT (v: LongWord): Boolean;
+  begin
+    result := (v <> 0) and ((v and (v - 1)) = 0)
+  end;
+
+  function NextPOT (v: LongWord): LongWord;
+  begin
+    DEC(v);
+    v := v or (v >> 1);
+    v := v or (v >> 2);
+    v := v or (v >> 4);
+    v := v or (v >> 8);
+    v := v or (v >> 16);
+    INC(v);
+    result := v;
+  end;
+
   function r_Textures_GetMaxHardwareSize (): Integer;
     var size: GLint = 0;
   begin
-    glGetIntegerv(GL_MAX_TEXTURE_SIZE, @size);
-    if size < 64 then size := 64;
-    //if size > 512 then size := 512;
-    //size := 64; // !!!
+    if r_GL_MaxTexSize <= 0 then
+    begin
+      // auto, max possible reccomended by driver
+      glGetIntegerv(GL_MAX_TEXTURE_SIZE, @size);
+      if size < 1 then size := 64;
+    end
+    else
+    begin
+      // selected by user
+      if IsPOT(r_GL_MaxTexSize) then
+        size := r_GL_MaxTexSize
+      else
+        size := NextPOT(r_GL_MaxTexSize);
+    end;
     result := size;
   end;
 
   procedure r_Textures_Initialize;
   begin
     maxTileSize := r_Textures_GetMaxHardwareSize();
+    e_LogWritefln('TEXTURE SIZE: %s', [maxTileSize]);
   end;
 
   procedure r_Textures_Finalize;
@@ -448,7 +488,7 @@ implementation
     end
   end;
 
-  function r_Textures_LoadMultiFromImageAndInfo (var img: TImageData; w, h, c: Integer; b: Boolean): TGLMultiTexture;
+  function r_Textures_LoadMultiFromImageAndInfo (var img: TImageData; w, h, c: Integer): TGLMultiTexture;
     var t: TImageData; a: array of TGLTexture; i: Integer; m: TGLMultiTexture;
   begin
     ASSERT(w >= 0);
@@ -467,12 +507,11 @@ implementation
     end;
     m := TGLMultiTexture.Create();
     m.mTexture := a;
-    m.mBackanim := b;
     ASSERT(m.mTexture <> nil);
     result := m;
   end;
 
-  function r_Textures_LoadMultiFromDataAndInfo (data: Pointer; size: LongInt; w, h, c: Integer; b: Boolean): TGLMultiTexture;
+  function r_Textures_LoadMultiFromDataAndInfo (data: Pointer; size: LongInt; w, h, c: Integer): TGLMultiTexture;
     var img: TImageData;
   begin
     ASSERT(w > 0);
@@ -485,50 +524,64 @@ implementation
       try
         if LoadImageFromMemory(data, size, img) then
           if r_Textures_FixImageData(img) then
-            result := r_Textures_LoadMultiFromImageAndInfo(img, w, h, c, b)
+            result := r_Textures_LoadMultiFromImageAndInfo(img, w, h, c)
       except
       end;
       FreeImage(img);
     end;
   end;
 
-  function r_Textures_LoadMultiFromWad (wad: TWADFile): TGLMultiTexture;
-    var data: Pointer; size: LongInt; TexRes: AnsiString; w, h, c: Integer; b: Boolean; cfg: TConfig; img: TImageData;
+  function r_Textures_LoadTextFromMemory (data: Pointer; size: LongInt; var txt: TAnimTextInfo): Boolean;
+    var cfg: TConfig;
+  begin
+    result := false;
+    if data <> nil then
+    begin
+      cfg := TConfig.CreateMem(data, size);
+      if cfg <> nil then
+      begin
+        txt.name := cfg.ReadStr('', 'resource', '');
+        txt.w := MAX(0, cfg.ReadInt('', 'framewidth', 0));
+        txt.h := MAX(0, cfg.ReadInt('', 'frameheight', 0));
+        txt.anim.loop := true;
+        txt.anim.delay := MAX(0, cfg.ReadInt('', 'waitcount', 0));
+        txt.anim.frames := MAX(0, cfg.ReadInt('', 'framecount', 0));
+        txt.anim.back := cfg.ReadBool('', 'backanim', false);
+        cfg.Free;
+        result := (txt.name <> '') and (txt.w > 0) and (txt.h > 0) and (txt.anim.delay > 0) and (txt.anim.frames > 0);
+      end;
+    end;
+  end;
+
+  function r_Textures_LoadMultiFromWad (wad: TWADFile; var txt: TAnimTextInfo): TGLMultiTexture;
+    var data: Pointer; size: LongInt; img: TImageData;
   begin
     ASSERT(wad <> nil);
     result := nil;
     if wad.GetResource('TEXT/ANIM', data, size) then
     begin
-      cfg := TConfig.CreateMem(data, size);
-      FreeMem(data);
-      if cfg <> nil then
+      if r_Textures_LoadTextFromMemory(data, size, txt) then
       begin
-        TexRes := cfg.ReadStr('', 'resource', '');
-        w := cfg.ReadInt('', 'framewidth', 0);
-        h := cfg.ReadInt('', 'frameheight', 0);
-        c := cfg.ReadInt('', 'framecount', 0);
-        b := cfg.ReadBool('', 'backanim', false);
-        if (TexRes <> '') and (w > 0) and (h > 0) and (c > 0) then
+        FreeMem(data);
+        if wad.GetResource('TEXTURES/' + txt.name, data, size) then
         begin
-          if wad.GetResource('TEXTURES/' + TexRes, data, size) then
-          begin
-            InitImage(img);
-            try
-              if LoadImageFromMemory(data, size, img) then
-                if r_Textures_FixImageData(img) then
-                  result := r_Textures_LoadMultiFromImageAndInfo(img, w, h, c, b)
-            finally
-              FreeMem(data);
-            end;
-            FreeImage(img);
-          end
+          InitImage(img);
+          try
+            if LoadImageFromMemory(data, size, img) then
+              if r_Textures_FixImageData(img) then
+                result := r_Textures_LoadMultiFromImageAndInfo(img, txt.w, txt.h, txt.anim.frames);
+          finally
+            FreeMem(data);
+          end;
+          FreeImage(img);
         end;
-        cfg.Free;
       end
+      else
+        FreeMem(data);
     end;
   end;
 
-  function r_Textures_LoadMultiFromMemory (data: Pointer; size: LongInt): TGLMultiTexture;
+  function r_Textures_LoadMultiFromMemory (data: Pointer; size: LongInt; var txt: TAnimTextInfo): TGLMultiTexture;
     var wad: TWADFile; t: TGLTexture; m: TGLMultiTexture;
   begin
     result := nil;
@@ -540,7 +593,13 @@ implementation
         m := TGLMultiTexture.Create();
         SetLength(m.mTexture, 1);
         m.mTexture[0] := t;
-        m.mBackanim := false;
+        txt.name := '';
+        txt.w := m.width;
+        txt.h := m.height;
+        txt.anim.loop := true;
+        txt.anim.delay := 1;
+        txt.anim.frames := 1;
+        txt.anim.back := false;
         result := m;
       end
       else if IsWadData(data, size) then
@@ -548,15 +607,15 @@ implementation
         wad := TWADFile.Create();
         if wad.ReadMemory(data, size) then
         begin
-          result := r_Textures_LoadMultiFromWad(wad);
+          result := r_Textures_LoadMultiFromWad(wad, txt);
           wad.Free;
         end
       end
     end
   end;
 
-  function r_Textures_LoadMultiFromFile (const filename: AnsiString; log: Boolean = True): TGLMultiTexture;
-    var wad: TWADFile; wadName, resName: AnsiString; data: Pointer; size: Integer; t: TGLTexture;
+  function r_Textures_LoadMultiTextFromFile (const filename: AnsiString; var txt: TAnimTextInfo; log: Boolean = True): TGLMultiTexture;
+    var wad: TWADFile; wadName, resName: AnsiString; data: Pointer; size: Integer;
   begin
     result := nil;
     wadName := g_ExtractWadName(filename);
@@ -566,14 +625,20 @@ implementation
       resName := g_ExtractFilePathName(filename);
       if wad.GetResource(resName, data, size, log) then
       begin
-        result := r_Textures_LoadMultiFromMemory(data, size);
+        result := r_Textures_LoadMultiFromMemory(data, size, txt);
         FreeMem(data);
       end;
       wad.Free
     end
   end;
 
-  function r_Textures_LoadMultiFromFileAndInfo (const filename: AnsiString; w, h, count: Integer; backanim: Boolean; log: Boolean = True): TGLMultiTexture;
+  function r_Textures_LoadMultiFromFile (const filename: AnsiString; log: Boolean = True): TGLMultiTexture;
+    var txt: TAnimTextInfo;
+  begin
+    result := r_Textures_LoadMultiTextFromFile(filename, txt, log);
+  end;
+
+  function r_Textures_LoadMultiFromFileAndInfo (const filename: AnsiString; w, h, count: Integer; log: Boolean = True): TGLMultiTexture;
     var wad: TWADFile; wadName, resName: AnsiString; data: Pointer; size: Integer;
   begin
     ASSERT(w > 0);
@@ -587,7 +652,7 @@ implementation
       resName := g_ExtractFilePathName(filename);
       if wad.GetResource(resName, data, size, log) then
       begin
-        result := r_Textures_LoadMultiFromDataAndInfo(data, size, w, h, count, backanim);
+        result := r_Textures_LoadMultiFromDataAndInfo(data, size, w, h, count);
         FreeMem(data);
       end;
       wad.Free
@@ -734,26 +799,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;
@@ -777,6 +844,20 @@ implementation
     result := self.info.ch[c].w;
     if result = 0 then
       result := self.info.w;
+    if self.info.kern < 0 then
+      result := result + self.info.kern;
+  end;
+
+  function TGLFont.GetMaxWidth (): Integer;
+  begin
+    result := self.info.w;
+    if self.info.kern < 0 then
+      result := result + self.info.kern;
+  end;
+
+  function TGLFont.GetMaxHeight (): Integer;
+  begin
+    result := self.info.h;
   end;
 
   function TGLFont.GetSpace (): Integer;
@@ -784,4 +865,7 @@ implementation
     result := self.info.kern;
   end;
 
+initialization
+  conRegVar('r_gl_maxtexsize', @r_GL_MaxTexSize, '', '');
+  r_GL_MaxTexSize := 0; // default is automatic value
 end.