DEADSOFTWARE

render: completely remove opengl calls form game code
[d2df-sdl.git] / src / engine / e_texture.pas
index 6c348baeef3d8a95551225db70058bc0b8c4c6db..58ae4cdb44369d4bc7a7af4f7c14501aa7e1d977 100644 (file)
@@ -1,9 +1,8 @@
-(* Copyright (C)  DooM 2D:Forever Developers
+(* Copyright (C)  Doom 2D: Forever Developers
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation, version 3 of the License ONLY.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -23,8 +22,8 @@ unit e_texture;
 interface
 
 uses
-  GL, GLExt, SysUtils, e_log,
-  ImagingTypes, Imaging, ImagingUtility;
+  {$INCLUDE ../nogl/noGLuses.inc}
+  SysUtils, e_log, ImagingTypes, Imaging, ImagingUtility;
 
 type
   GLTexture = record
@@ -37,24 +36,23 @@ type
 
 var
   e_DummyTextures: Boolean = False;
-  TEXTUREFILTER: Integer = GL_NEAREST;
 
-function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
+function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer; filter: Boolean = False): Boolean;
 
 // Standard set of images loading functions
-function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
-function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
-function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
-function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
+function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
+function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
+function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
+function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
 
 // `img` must be valid!
-function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
+function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
 
 
 implementation
 
 uses
-  Classes, BinEditor, g_options, utils;
+  Classes, g_options, utils;
 
 
 function AlignP2 (n: Word): Word;
@@ -71,12 +69,13 @@ end;
 
 
 // This is auxiliary function that creates OpenGL texture from raw image data
-function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
+function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer; filter: Boolean = False): Boolean;
 var
   Texture: GLuint;
   fmt: GLenum;
   //buf: PByte;
   //f, c: Integer;
+  TEXTUREFILTER: Integer;
 begin
   tex.width := Width;
   tex.height := Height;
@@ -103,15 +102,16 @@ begin
     Exit;
   end;
 
+  Texture := 0;
   glGenTextures(1, @Texture);
   tex.id := Texture;
   glBindTexture(GL_TEXTURE_2D, Texture);
 
   if (tex.glwidth <> tex.width) or (tex.glheight <> tex.height) then
-    e_WriteLog(Format('NPOT: %u is %ux%u; gl is %ux%u; u=%f; v=%f', [tex.id, Width, Height, tex.glwidth, tex.glheight, tex.u, tex.v]), MSG_NOTIFY);
+    e_WriteLog(Format('NPOT: %u is %ux%u; gl is %ux%u; u=%f; v=%f', [tex.id, Width, Height, tex.glwidth, tex.glheight, tex.u, tex.v]), TMsgType.Notify);
 
   // texture blends with object background
-  glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   // texture does NOT blend with object background
   //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
 
@@ -125,6 +125,8 @@ begin
     GL_LINEAR_MIPMAP_LINEAR  - BiLinear Mipmapped texture
   }
 
+  if filter then TEXTUREFILTER := GL_LINEAR else TEXTUREFILTER := GL_NEAREST;
+
   // for GL_TEXTURE_MAG_FILTER only first two can be used
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, TEXTUREFILTER);
   // for GL_TEXTURE_MIN_FILTER all of the above can be used
@@ -162,11 +164,15 @@ begin
 
   glBindTexture(GL_TEXTURE_2D, 0);
 
+  // so driver will really upload the texture (this is *sometimes* required for buggy videodrivers)
+  glFlush();
+  glFinish();
+
   Result := true;
 end;
 
 // `img` must be valid!
-function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
+function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
 var
   image, ii: PByte;
   width, height: Integer;
@@ -181,7 +187,7 @@ begin
 
   if (img.width < 1) or (img.width > 32768) or (img.height < 1) or (img.height > 32768) then
   begin
-    e_WriteLog('Error loading texture: invalid image dimensions', MSG_WARNING);
+    e_WriteLog('Error loading texture: invalid image dimensions', TMsgType.Warning);
     exit;
   end;
   //ConvertImage(img, ifA8R8G8B8);
@@ -192,7 +198,7 @@ begin
   imageSize := Width*Height*4;
   GetMem(image, imageSize);
   try
-    // it's slow, but i don't care for now
+    // it is slow, but i don't care for now
     ii := image;
     for y := height-1 downto 0 do
     begin
@@ -205,7 +211,7 @@ begin
         ii^ := clr.a; Inc(ii);
       end;
     end;
-    CreateTexture(Texture, width, height, GL_RGBA, image);
+    CreateTexture(Texture, width, height, GL_RGBA, image, filter);
     result := true;
   finally
     FreeMem(image);
@@ -213,7 +219,7 @@ begin
 end;
 
 
-function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
+function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
 var
   //image, ii: PByte;
   //width, height: Integer;
@@ -230,18 +236,18 @@ begin
   InitImage(img);
   if not LoadImageFromMemory(pData, dataSize, img) then
   begin
-    e_WriteLog('Error loading texture: unknown image format', MSG_WARNING);
+    e_WriteLog('Error loading texture: unknown image format', TMsgType.Warning);
     exit;
   end;
   try
-    result := LoadTextureImg(img, Texture, pWidth, pHeight, Fmt);
+    result := LoadTextureImg(img, Texture, pWidth, pHeight, Fmt, filter);
   finally
     FreeImage(img);
   end;
 end;
 
 
-function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
+function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
 var
   image, ii: PByte;
   //width, height: Integer;
@@ -256,13 +262,13 @@ begin
   InitImage(img);
   if not LoadImageFromMemory(pData, dataSize, img) then
   begin
-    e_WriteLog('Error loading texture: unknown image format', MSG_WARNING);
+    e_WriteLog('Error loading texture: unknown image format', TMsgType.Warning);
     exit;
   end;
   try
     if (img.width < 1) or (img.width > 32768) or (img.height < 1) or (img.height > 32768) then
     begin
-      e_WriteLog('Error loading texture: invalid image dimensions', MSG_WARNING);
+      e_WriteLog('Error loading texture: invalid image dimensions', TMsgType.Warning);
       exit;
     end;
     //ConvertImage(img, ifA8R8G8B8);
@@ -274,7 +280,7 @@ begin
     imageSize := img.width*img.height*4;
     GetMem(image, imageSize);
     try
-      // it's slow, but i don't care for now
+      // it is slow, but i don't care for now
       ii := image;
       for y := fY+fHeight-1 downto fY do
       begin
@@ -287,7 +293,7 @@ begin
           ii^ := clr.a; Inc(ii);
         end;
       end;
-      CreateTexture(Texture, fWidth, fHeight, GL_RGBA, image);
+      CreateTexture(Texture, fWidth, fHeight, GL_RGBA, image, filter);
       result := true;
     finally
       FreeMem(image);
@@ -298,7 +304,7 @@ begin
 end;
 
 
-function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
+function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
 var
   fs: TStream;
   img: Pointer;
@@ -317,7 +323,7 @@ begin
   end;
   if fs = nil then
   begin
-    e_WriteLog('Texture "'+filename+'" not found', MSG_WARNING);
+    e_WriteLog('Texture "'+filename+'" not found', TMsgType.Warning);
     exit;
   end;
 
@@ -326,7 +332,7 @@ begin
     GetMem(img, imageSize);
     try
       fs.readBuffer(img^, imageSize);
-      result := LoadTextureMem(img, imageSize, Texture, pWidth, pHeight, Fmt);
+      result := LoadTextureMem(img, imageSize, Texture, pWidth, pHeight, Fmt, filter);
     finally
       FreeMem(img);
     end;
@@ -336,7 +342,7 @@ begin
 end;
 
 
-function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
+function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
 var
   fs: TStream;
   img: Pointer;
@@ -353,7 +359,7 @@ begin
   end;
   if fs = nil then
   begin
-    e_WriteLog('Texture "'+filename+'" not found', MSG_WARNING);
+    e_WriteLog('Texture "'+filename+'" not found', TMsgType.Warning);
     exit;
   end;
 
@@ -362,7 +368,7 @@ begin
     GetMem(img, imageSize);
     try
       fs.readBuffer(img^, imageSize);
-      result := LoadTextureMemEx(img, imageSize, Texture, fX, fY, fWidth, fHeight, Fmt);
+      result := LoadTextureMemEx(img, imageSize, Texture, fX, fY, fWidth, fHeight, Fmt, filter);
     finally
       FreeMem(img);
     end;