DEADSOFTWARE

simple allocation counter for classes
[d2df-sdl.git] / src / game / g_textures.pas
index 613419e21e451e1f0793126bdbf9c67fe174eaf3..f18695861cd8007a217f07983c312d10c93af076 100644 (file)
@@ -1,10 +1,26 @@
-{$MODE DELPHI}
+(* 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *)
+{$INCLUDE ../shared/a_modes.inc}
 unit g_textures;
 
 interface
 
 uses
-  e_graphics, BinEditor, ImagingTypes, Imaging, ImagingUtility;
+  mempool,
+  e_graphics, MAPDEF, BinEditor, ImagingTypes, Imaging, ImagingUtility;
 
 Type
   TLevelTexture = record
@@ -20,7 +36,7 @@ Type
 
   TLevelTextureArray = Array of TLevelTexture;
 
-  TAnimation = class(TObject)
+  TAnimation = class(TPoolObject)
   private
     ID:            DWORD;
     FAlpha:        Byte;
@@ -40,7 +56,7 @@ Type
     constructor Create(FramesID: DWORD; Loop: Boolean; Speed: Byte);
     destructor  Destroy(); override;
     procedure   Draw(X, Y: Integer; Mirror: TMirrorType);
-    procedure   DrawEx(X, Y: Integer; Mirror: TMirrorType; RPoint: TPoint;
+    procedure   DrawEx(X, Y: Integer; Mirror: TMirrorType; RPoint: TDFPoint;
                        Angle: SmallInt);
     procedure   Reset();
     procedure   Update();
@@ -69,7 +85,7 @@ Type
 
 function g_Texture_CreateWAD(var ID: DWORD; Resource: String): Boolean;
 function g_Texture_CreateFile(var ID: DWORD; FileName: String): Boolean;
-function g_Texture_CreateWADEx(TextureName: ShortString; Resource: String): Boolean;
+function g_Texture_CreateWADEx(TextureName: ShortString; Resource: String; altrsrc: AnsiString=''): Boolean;
 function g_Texture_CreateFileEx(TextureName: ShortString; FileName: String): Boolean;
 function g_Texture_Get(TextureName: ShortString; var ID: DWORD): Boolean;
 procedure g_Texture_Delete(TextureName: ShortString);
@@ -83,9 +99,10 @@ function g_Frames_CreateFile(ID: PDWORD; Name: ShortString; FileName: String;
                              FWidth, FHeight, FCount: Word; BackAnimation: Boolean = False): Boolean;
 function g_Frames_CreateMemory(ID: PDWORD; Name: ShortString; pData: Pointer; dataSize: LongInt;
                                FWidth, FHeight, FCount: Word; BackAnimation: Boolean = False): Boolean;
+function g_Frames_Dup(NewName, OldName: ShortString): Boolean;
 //function g_Frames_CreateRevert(ID: PDWORD; Name: ShortString; Frames: string): Boolean;
-function g_Frames_Get(var ID: DWORD; FramesName: ShortString): Boolean;
-function g_Frames_GetTexture(var ID: DWORD; FramesName: ShortString; Frame: Word): Boolean;
+function g_Frames_Get(out ID: DWORD; FramesName: ShortString): Boolean;
+function g_Frames_GetTexture(out ID: DWORD; FramesName: ShortString; Frame: Word): Boolean;
 function g_Frames_Exists(FramesName: String): Boolean;
 procedure g_Frames_DeleteByName(FramesName: ShortString);
 procedure g_Frames_DeleteByID(ID: DWORD);
@@ -93,11 +110,13 @@ procedure g_Frames_DeleteAll();
 
 procedure DumpTextureNames();
 
+function g_Texture_Light(): Integer;
+
 implementation
 
 uses
   g_game, e_log, g_basic, SysUtils, g_console, wadreader,
-  g_language;
+  g_language, GL;
 
 type
   _TTexture = record
@@ -182,7 +201,7 @@ begin
   end;
 end;
 
-function g_Texture_CreateWADEx(TextureName: ShortString; Resource: String): Boolean;
+function texture_CreateWADExInternal (TextureName: ShortString; Resource: String; showmsg: Boolean): Boolean;
 var
   WAD: TWADFile;
   FileName: String;
@@ -199,25 +218,39 @@ begin
 
   if WAD.GetResource(g_ExtractFilePathName(Resource), TextureData, ResourceLength) then
   begin
-    Result := e_CreateTextureMem(TextureData, ResourceLength, TexturesArray[find_id].ID);
-    if Result then
+    result := e_CreateTextureMem(TextureData, ResourceLength, TexturesArray[find_id].ID);
+    if result then
     begin
-      e_GetTextureSize(TexturesArray[find_id].ID, @TexturesArray[find_id].Width,
-                       @TexturesArray[find_id].Height);
+      e_GetTextureSize(TexturesArray[find_id].ID, @TexturesArray[find_id].Width, @TexturesArray[find_id].Height);
       TexturesArray[find_id].Name := LowerCase(TextureName);
     end
     else
+    begin
       FreeMem(TextureData);
+    end;
   end
   else
   begin
-    e_WriteLog(Format('Error loading texture %s', [Resource]), MSG_WARNING);
+    if showmsg then
+    begin
+      e_WriteLog(Format('Error loading texture %s', [Resource]), MSG_WARNING);
+    end;
     //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
-    Result := False;
+    result := false;
   end;
   WAD.Free();
 end;
 
+function g_Texture_CreateWADEx(TextureName: ShortString; Resource: String; altrsrc: AnsiString=''): Boolean;
+begin
+  if (Length(altrsrc) > 0) then
+  begin
+    result := texture_CreateWADExInternal(TextureName, altrsrc, false);
+    if result then exit;
+  end;
+  result := texture_CreateWADExInternal(TextureName, Resource, true);
+end;
+
 function g_Texture_CreateFileEx(TextureName: ShortString; FileName: String): Boolean;
 var
   find_id: DWORD;
@@ -367,7 +400,7 @@ begin
     if not e_CreateTextureMemEx(pData, dataSize, FramesArray[find_id].TexturesID[a],
                                 a*FWidth, 0, FWidth, FHeight) then
     begin
-      FreeMem(pData);
+      //!!!FreeMem(pData);
       Exit;
     end;
 
@@ -444,6 +477,9 @@ var
 begin
   Result := False;
 
+  // models without "advanced" animations asks for "nothing" like this; don't spam log
+  if (Length(Resource) > 0) and ((Resource[Length(Resource)] = '/') or (Resource[Length(Resource)] = '\')) then exit;
+
   FileName := g_ExtractWadName(Resource);
 
   WAD := TWADFile.Create();
@@ -497,6 +533,30 @@ begin
  Result := True;
 end;}
 
+function g_Frames_Dup(NewName, OldName: ShortString): Boolean;
+var
+  find_id, b: DWORD;
+  a, c: Integer;
+begin
+  Result := False;
+
+  if not g_Frames_Get(b, OldName) then Exit;
+
+  find_id := FindFrame();
+
+  FramesArray[find_id].Name := LowerCase(NewName);
+  FramesArray[find_id].FrameWidth := FramesArray[b].FrameWidth;
+  FramesArray[find_id].FrameHeight := FramesArray[b].FrameHeight;
+
+  c := High(FramesArray[b].TexturesID);
+  SetLength(FramesArray[find_id].TexturesID, c+1);
+
+  for a := 0 to c do
+    FramesArray[find_id].TexturesID[a] := FramesArray[b].TexturesID[a];
+
+  Result := True;
+end;
+
 procedure g_Frames_DeleteByName(FramesName: ShortString);
 var
   a: DWORD;
@@ -555,7 +615,7 @@ begin
   FramesArray := nil;
 end;
 
-function g_Frames_Get(var ID: DWORD; FramesName: ShortString): Boolean;
+function g_Frames_Get(out ID: DWORD; FramesName: ShortString): Boolean;
 var
   a: DWORD;
 begin
@@ -578,7 +638,7 @@ begin
     g_FatalError(Format(_lc[I_GAME_ERROR_FRAMES], [FramesName]));
 end;
 
-function g_Frames_GetTexture(var ID: DWORD; FramesName: ShortString; Frame: Word): Boolean;
+function g_Frames_GetTexture(out ID: DWORD; FramesName: ShortString; Frame: Word): Boolean;
 var
   a: DWORD;
 begin
@@ -740,7 +800,7 @@ begin
   FEnabled := True;
 end;
 
-procedure TAnimation.DrawEx(X, Y: Integer; Mirror: TMirrorType; RPoint: TPoint;
+procedure TAnimation.DrawEx(X, Y: Integer; Mirror: TMirrorType; RPoint: TDFPoint;
                             Angle: SmallInt);
 begin
   if not FEnabled then
@@ -828,4 +888,64 @@ begin
   Mem.ReadBoolean(FRevert);
 end;
 
+
+var
+  ltexid: GLuint = 0;
+
+function g_Texture_Light(): Integer;
+const
+  Radius: Integer = 128;
+var
+  tex, tpp: PByte;
+  x, y, a: Integer;
+  dist: Double;
+begin
+  if ltexid = 0 then
+  begin
+    GetMem(tex, (Radius*2)*(Radius*2)*4);
+    tpp := tex;
+    for y := 0 to Radius*2-1 do
+    begin
+      for x := 0 to Radius*2-1 do
+      begin
+        dist := 1.0-sqrt((x-Radius)*(x-Radius)+(y-Radius)*(y-Radius))/Radius;
+        if (dist < 0) then
+        begin
+          tpp^ := 0; Inc(tpp);
+          tpp^ := 0; Inc(tpp);
+          tpp^ := 0; Inc(tpp);
+          tpp^ := 0; Inc(tpp);
+        end
+        else
+        begin
+          //tc.setPixel(x, y, Color(cast(int)(dist*255), cast(int)(dist*255), cast(int)(dist*255)));
+          if (dist > 0.5) then dist := 0.5;
+          a := round(dist*255);
+          if (a < 0) then a := 0 else if (a > 255) then a := 255;
+          tpp^ := 255; Inc(tpp);
+          tpp^ := 255; Inc(tpp);
+          tpp^ := 255; Inc(tpp);
+          tpp^ := Byte(a); Inc(tpp);
+        end;
+      end;
+    end;
+
+    glGenTextures(1, @ltexid);
+    //if (tid == 0) assert(0, "VGL: can't create screen texture");
+
+    glBindTexture(GL_TEXTURE_2D, ltexid);
+    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+    //GLfloat[4] bclr = 0.0;
+    //glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bclr.ptr);
+
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Radius*2, Radius*2, 0, GL_RGBA{gltt}, GL_UNSIGNED_BYTE, tex);
+  end;
+
+  result := ltexid;
+end;
+
 end.