1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../../../shared/a_modes.inc}
26 g_base
, g_animations
, // TRectHW, TAnimInfo
34 TGLAtlasNode
= class (TAtlasNode
)
39 constructor Create (base
: TGLAtlas
);
40 destructor Destroy
; override;
42 function GetID (): GLuint
;
44 property base
: TGLAtlas read mBase
;
45 property id
: GLuint read GetID
;
48 TGLAtlas
= class (TAtlas
)
53 constructor Create (ww
, hh
: Integer; id
: GLuint
);
54 destructor Destroy
; override;
56 function CreateNode (): TGLAtlasNode
; override;
57 function Alloc (ww
, hh
: Integer): TGLAtlasNode
; overload
;
59 property id
: GLuint read mID write mID default
0;
67 mTile
: array of TGLAtlasNode
;
70 destructor Destroy
; override;
72 function GetTile (col
, line
: Integer): TGLAtlasNode
;
74 function GetLines (): Integer; inline;
76 property width
: Integer read mWidth
;
77 property height
: Integer read mHeight
;
78 property cols
: Integer read mCols
;
79 property lines
: Integer read GetLines
;
82 TGLMultiTexture
= class
84 mTexture
: array of TGLTexture
;
87 destructor Destroy
; override;
89 function GetWidth (): Integer; inline;
90 function GetHeight (): Integer; inline;
91 function GetCount (): Integer; inline;
92 function GetTexture (i
: Integer): TGLTexture
; {inline;}
94 property width
: Integer read GetWidth
;
95 property height
: Integer read GetHeight
;
96 property count
: Integer read GetCount
;
99 TGLTextureArray
= array of TGLTexture
;
101 TRectArray
= array of TRectWH
;
103 TGLFont
= class sealed (TFont
)
109 destructor Destroy
; override;
110 function GetChar (c
: AnsiChar): TGLTexture
;
111 function GetWidth (c
: AnsiChar): Integer;
112 function GetMaxWidth (): Integer;
113 function GetMaxHeight (): Integer;
114 function GetSpace (): Integer;
117 TAnimTextInfo
= record
123 TConvProc
= function (x
: Integer): Integer;
125 procedure r_Textures_Initialize
;
126 procedure r_Textures_Finalize
;
128 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
129 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
130 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; log
: Boolean = True): TGLMultiTexture
;
131 function r_Textures_LoadMultiTextFromFile (const filename
: AnsiString; var txt
: TAnimTextInfo
; log
: Boolean = True): TGLMultiTexture
;
133 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
135 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; font2enc
: TConvProc
; log
: Boolean = true): TGLFont
;
141 e_log
, e_res
, WADReader
, Config
,
142 g_console
, // cvar declaration
143 Imaging
, ImagingTypes
, ImagingUtility
147 r_GL_MaxTexSize
: WORD;
148 maxTileSize
: Integer;
149 atl
: array of TGLAtlas
;
151 (* --------- TGLAtlasNode --------- *)
153 constructor TGLAtlasNode
.Create (base
: TGLAtlas
);
160 destructor TGLAtlasNode
.Destroy
;
165 function TGLAtlasNode
.GetID (): GLuint
;
167 result
:= self
.base
.id
170 procedure r_Textures_UpdateNode (n
: TGLAtlasNode
; data
: Pointer; x
, y
, w
, h
: Integer);
174 ASSERT(n
.base
<> nil);
178 ASSERT(n
.l
+ x
+ w
- 1 <= n
.r
);
179 ASSERT(n
.t
+ y
+ h
- 1 <= n
.b
);
181 glBindTexture(GL_TEXTURE_2D
, n
.id
);
182 glTexSubImage2D(GL_TEXTURE_2D
, 0, n
.l
+ x
, n
.t
+ y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
183 glBindTexture(GL_TEXTURE_2D
, 0);
186 (* --------- TGLAtlas --------- *)
188 constructor TGLAtlas
.Create (ww
, hh
: Integer; id
: GLuint
);
192 inherited Create(ww
, hh
);
196 destructor TGLAtlas
.Destroy
;
201 function TGLAtlas
.CreateNode (): TGLAtlasNode
;
203 result
:= TGLAtlasNode
.Create(self
);
206 function TGLAtlas
.Alloc (ww
, hh
: Integer): TGLAtlasNode
;
208 result
:= TGLAtlasNode(inherited Alloc(ww
, hh
));
211 function r_Textures_AllocHWTexture (w
, h
: Integer): GLuint
;
214 glGenTextures(1, @id
);
217 glBindTexture(GL_TEXTURE_2D
, id
);
218 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
219 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
220 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nil);
221 glBindTexture(GL_TEXTURE_2D
, 0);
226 function r_Textures_AllocAtlas (): TGLAtlas
;
227 var i
: Integer; id
: GLuint
;
230 id
:= r_Textures_AllocHWTexture(maxTileSize
, maxTileSize
);
234 SetLength(atl
, i
+ 1);
235 atl
[i
] := TGLAtlas
.Create(maxTileSize
, maxTileSize
, id
);
240 function r_Textures_AllocNode (w
, h
: Integer): TGLAtlasNode
;
241 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
247 while (i
>= 0) and (n
= nil) do
249 n
:= atl
[i
].Alloc(w
, h
);
255 a
:= r_Textures_AllocAtlas();
262 (* --------- TGLTexture --------- *)
264 destructor TGLTexture
.Destroy
;
267 if self
.mTile
<> nil then
269 for i
:= 0 to High(self
.mTile
) do
271 if self
.mTile
[i
] <> nil then
273 self
.mTile
[i
].Dealloc
;
274 self
.mTile
[i
] := nil;
282 function TGLTexture
.GetLines (): Integer;
284 ASSERT(self
.mTile
<> nil);
285 result
:= Length(self
.mTile
) div self
.mCols
288 function TGLTexture
.GetTile (col
, line
: Integer): TGLAtlasNode
;
292 ASSERT(col
<= mCols
);
293 ASSERT(self
.mTile
<> nil);
294 i
:= line
* mCols
+ col
;
296 ASSERT(i
< Length(mTile
));
298 ASSERT(result
<> nil)
301 function r_Textures_Alloc (w
, h
: Integer): TGLTexture
;
302 var x
, y
, mw
, mh
, cols
, lines
: Integer; t
: TGLTexture
;
306 cols
:= (w
+ maxTileSize
- 1) div maxTileSize
;
307 lines
:= (h
+ maxTileSize
- 1) div maxTileSize
;
308 t
:= TGLTexture
.Create
;
312 // t.mLines := lines;
313 SetLength(t
.mTile
, cols
* lines
);
314 for y
:= 0 to lines
- 1 do
316 mh
:= Min(maxTileSize
, h
- y
* maxTileSize
);
318 for x
:= 0 to cols
- 1 do
320 mw
:= Min(maxTileSize
, w
- x
* maxTileSize
);
322 t
.mTile
[y
* cols
+ x
] := r_Textures_AllocNode(mw
, mh
);
328 (* --------- TGLMultiTexture --------- *)
330 destructor TGLMultiTexture
.Destroy
;
333 for i
:= 0 to self
.count
- 1 do
334 self
.mTexture
[i
].Free
;
335 self
.mTexture
:= nil;
339 function TGLMultiTexture
.GetWidth (): Integer;
341 result
:= self
.mTexture
[0].width
344 function TGLMultiTexture
.GetHeight (): Integer;
346 result
:= self
.mTexture
[0].height
349 function TGLMultiTexture
.GetCount (): Integer;
351 result
:= Length(self
.mTexture
)
354 function TGLMultiTexture
.GetTexture (i
: Integer): TGLTexture
;
357 ASSERT(i
< self
.count
);
358 result
:= self
.mTexture
[i
];
359 ASSERT(result
<> nil);
362 (* --------- Init / Fin --------- *)
364 function IsPOT (v
: LongWord): Boolean;
366 result
:= (v
<> 0) and ((v
and (v
- 1)) = 0)
369 function NextPOT (v
: LongWord): LongWord;
381 function r_Textures_GetMaxHardwareSize (): Integer;
384 if r_GL_MaxTexSize
<= 0 then
386 // auto, max possible reccomended by driver
387 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, @size
);
388 if size
< 1 then size
:= 64;
393 if IsPOT(r_GL_MaxTexSize
) then
394 size
:= r_GL_MaxTexSize
396 size
:= NextPOT(r_GL_MaxTexSize
);
401 procedure r_Textures_Initialize
;
403 maxTileSize
:= r_Textures_GetMaxHardwareSize();
404 e_LogWritefln('TEXTURE SIZE: %s', [maxTileSize
]);
407 procedure r_Textures_Finalize
;
412 for i
:= 0 to High(atl
) do
414 glDeleteTextures(1, @atl
[i
].id
);
422 function r_Textures_FixImageData (var img
: TImageData
): Boolean;
425 if ConvertImage(img
, TImageFormat
.ifA8R8G8B8
) then
426 if SwapChannels(img
, ChannelRed
, ChannelBlue
) then // wtf
430 function r_Textures_LoadFromImage (var img
: TImageData
): TGLTexture
;
431 var t
: TGLTexture
; n
: TGLAtlasNode
; c
: TDynImageDataArray
; cw
, ch
, i
, j
: LongInt;
434 if SplitImage(img
, c
, maxTileSize
, maxTileSize
, cw
, ch
, False) then
436 t
:= r_Textures_Alloc(img
.width
, img
.height
);
440 ASSERT(ch
= t
.lines
);
441 for j
:= 0 to ch
- 1 do
443 for i
:= 0 to cw
- 1 do
445 n
:= t
.GetTile(i
, j
);
447 r_Textures_UpdateNode(n
, c
[j
* cw
+ i
].bits
, 0, 0, n
.width
, n
.height
)
452 FreeImagesInArray(c
);
456 function r_Textures_LoadFromMemory (data
: Pointer; size
: LongInt): TGLTexture
;
460 if (data
<> nil) and (size
> 0) then
464 if LoadImageFromMemory(data
, size
, img
) then
465 if r_Textures_FixImageData(img
) then
466 result
:= r_Textures_LoadFromImage(img
)
473 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
474 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
477 wadName
:= g_ExtractWadName(filename
);
478 wad
:= TWADFile
.Create();
479 if wad
.ReadFile(wadName
) then
481 resName
:= g_ExtractFilePathName(filename
);
482 if wad
.GetResource(resName
, data
, size
, log
) then
484 result
:= r_Textures_LoadFromMemory(data
, size
);
491 function r_Textures_LoadMultiFromImageAndInfo (var img
: TImageData
; w
, h
, c
: Integer): TGLMultiTexture
;
492 var t
: TImageData
; a
: array of TGLTexture
; i
: Integer; m
: TGLMultiTexture
;
499 for i
:= 0 to c
- 1 do
502 if NewImage(w
, h
, img
.Format
, t
) then
503 if CopyRect(img
, w
* i
, 0, w
, h
, t
, 0, 0) then
504 a
[i
] := r_Textures_LoadFromImage(t
);
508 m
:= TGLMultiTexture
.Create();
510 ASSERT(m
.mTexture
<> nil);
514 function r_Textures_LoadMultiFromDataAndInfo (data
: Pointer; size
: LongInt; w
, h
, c
: Integer): TGLMultiTexture
;
521 if (data
<> nil) and (size
> 0) then
525 if LoadImageFromMemory(data
, size
, img
) then
526 if r_Textures_FixImageData(img
) then
527 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
)
534 function r_Textures_LoadTextFromMemory (data
: Pointer; size
: LongInt; var txt
: TAnimTextInfo
): Boolean;
540 cfg
:= TConfig
.CreateMem(data
, size
);
543 txt
.name
:= cfg
.ReadStr('', 'resource', '');
544 txt
.w
:= MAX(0, cfg
.ReadInt('', 'framewidth', 0));
545 txt
.h
:= MAX(0, cfg
.ReadInt('', 'frameheight', 0));
546 txt
.anim
.loop
:= true;
547 txt
.anim
.delay
:= MAX(0, cfg
.ReadInt('', 'waitcount', 0));
548 txt
.anim
.frames
:= MAX(0, cfg
.ReadInt('', 'framecount', 0));
549 txt
.anim
.back
:= cfg
.ReadBool('', 'backanim', false);
551 result
:= (txt
.name
<> '') and (txt
.w
> 0) and (txt
.h
> 0) and (txt
.anim
.delay
> 0) and (txt
.anim
.frames
> 0);
556 function r_Textures_LoadMultiFromWad (wad
: TWADFile
; var txt
: TAnimTextInfo
): TGLMultiTexture
;
557 var data
: Pointer; size
: LongInt; img
: TImageData
;
561 if wad
.GetResource('TEXT/ANIM', data
, size
) then
563 if r_Textures_LoadTextFromMemory(data
, size
, txt
) then
566 if wad
.GetResource('TEXTURES/' + txt
.name
, data
, size
) then
570 if LoadImageFromMemory(data
, size
, img
) then
571 if r_Textures_FixImageData(img
) then
572 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, txt
.w
, txt
.h
, txt
.anim
.frames
);
584 function r_Textures_LoadMultiFromMemory (data
: Pointer; size
: LongInt; var txt
: TAnimTextInfo
): TGLMultiTexture
;
585 var wad
: TWADFile
; t
: TGLTexture
; m
: TGLMultiTexture
;
588 if (data
<> nil) and (size
> 0) then
590 t
:= r_Textures_LoadFromMemory(data
, size
);
593 m
:= TGLMultiTexture
.Create();
594 SetLength(m
.mTexture
, 1);
599 txt
.anim
.loop
:= true;
601 txt
.anim
.frames
:= 1;
602 txt
.anim
.back
:= false;
605 else if IsWadData(data
, size
) then
607 wad
:= TWADFile
.Create();
608 if wad
.ReadMemory(data
, size
) then
610 result
:= r_Textures_LoadMultiFromWad(wad
, txt
);
617 function r_Textures_LoadMultiTextFromFile (const filename
: AnsiString; var txt
: TAnimTextInfo
; log
: Boolean = True): TGLMultiTexture
;
618 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
621 wadName
:= g_ExtractWadName(filename
);
622 wad
:= TWADFile
.Create();
623 if wad
.ReadFile(wadName
) then
625 resName
:= g_ExtractFilePathName(filename
);
626 if wad
.GetResource(resName
, data
, size
, log
) then
628 result
:= r_Textures_LoadMultiFromMemory(data
, size
, txt
);
635 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
636 var txt
: TAnimTextInfo
;
638 result
:= r_Textures_LoadMultiTextFromFile(filename
, txt
, log
);
641 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; log
: Boolean = True): TGLMultiTexture
;
642 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
648 wadName
:= g_ExtractWadName(filename
);
649 wad
:= TWADFile
.Create();
650 if wad
.ReadFile(wadName
) then
652 resName
:= g_ExtractFilePathName(filename
);
653 if wad
.GetResource(resName
, data
, size
, log
) then
655 result
:= r_Textures_LoadMultiFromDataAndInfo(data
, size
, w
, h
, count
);
662 function r_Textures_GetRect (var img
: TImageData
): TRectWH
;
663 var i
, j
, w
, h
: Integer; done
: Boolean;
665 function IsVoid (i
, j
: Integer): Boolean; inline;
667 result
:= GetPixel32(img
, i
, j
).Channels
[3] = 0
674 (* trace x from right to left *)
675 done
:= false; i
:= 0;
676 while not done
and (i
< w
) do
679 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
680 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
685 (* trace y from up to down *)
686 done
:= false; j
:= 0;
687 while not done
and (j
< h
) do
690 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
691 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
696 (* trace x from right to left *)
697 done
:= false; i
:= w
- 1;
698 while not done
and (i
>= 0) do
701 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
702 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
703 result
.width
:= i
- result
.x
+ 1;
707 (* trace y from down to up *)
708 done
:= false; j
:= h
- 1;
709 while not done
and (j
>= 0) do
712 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
713 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
714 result
.height
:= j
- result
.y
+ 1;
719 function r_Textures_LoadStreamFromImage (var img
: TImageData
; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
720 var i
, x
, y
: Integer; t
: TImageData
;
726 ASSERT((st
<> nil) and (Length(st
) >= c
));
727 ASSERT((rs
= nil) or (Length(rs
) >= c
));
729 for i
:= 0 to c
- 1 do
735 if NewImage(w
, h
, img
.Format
, t
) then
737 if CopyRect(img
, x
* w
, y
* h
, w
, h
, t
, 0, 0) then
740 rs
[i
] := r_Textures_GetRect(t
);
741 st
[i
] := r_Textures_LoadFromImage(t
);
744 ASSERT(st
[i
] <> nil);
749 function r_Textures_LoadStreamFromMemory (data
: Pointer; size
: LongInt; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
756 ASSERT((st
<> nil) and (Length(st
) >= c
));
757 ASSERT((rs
= nil) or (Length(rs
) >= c
));
759 if (data
<> nil) and (size
> 0) then
763 if LoadImageFromMemory(data
, size
, img
) then
765 if r_Textures_FixImageData(img
) then
767 result
:= r_Textures_LoadStreamFromImage(img
, w
, h
, c
, cw
, st
, rs
)
776 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
777 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
783 ASSERT((st
<> nil) and (Length(st
) >= count
));
784 ASSERT((rs
= nil) or (Length(rs
) >= count
));
786 wadName
:= g_ExtractWadName(filename
);
787 wad
:= TWADFile
.Create();
788 if wad
.ReadFile(wadName
) then
790 resName
:= g_ExtractFilePathName(filename
);
791 if wad
.GetResource(resName
, data
, size
, log
) then
793 result
:= r_Textures_LoadStreamFromMemory(data
, size
, w
, h
, count
, cw
, st
, rs
);
800 (* --------- TGLFont --------- *)
802 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; font2enc
: TConvProc
; log
: Boolean = true): TGLFont
;
803 var i
, ch
: Integer; st
, stch
: TGLTextureArray
; font
: TGLFont
;
807 if r_Textures_LoadStreamFromFile(filename
, f
.w
, f
.h
, 256, 16, st
, nil, log
) then
809 font
:= TGLFont
.Create();
812 if Assigned(font2enc
) then
814 SetLength(stch
, 256);
818 ASSERT((ch
>= 0) and (ch
<= 255));
828 destructor TGLFont
.Destroy
;
831 if self
.ch
<> nil then
832 for i
:= 0 to High(self
.ch
) do
837 function TGLFont
.GetChar (c
: AnsiChar): TGLTexture
;
839 result
:= self
.ch
[ORD(c
)];
842 function TGLFont
.GetWidth (c
: AnsiChar): Integer;
844 result
:= self
.info
.ch
[c
].w
;
846 result
:= self
.info
.w
;
847 if self
.info
.kern
< 0 then
848 result
:= result
+ self
.info
.kern
;
851 function TGLFont
.GetMaxWidth (): Integer;
853 result
:= self
.info
.w
;
854 if self
.info
.kern
< 0 then
855 result
:= result
+ self
.info
.kern
;
858 function TGLFont
.GetMaxHeight (): Integer;
860 result
:= self
.info
.h
;
863 function TGLFont
.GetSpace (): Integer;
865 result
:= self
.info
.kern
;
869 conRegVar('r_gl_maxtexsize', @r_GL_MaxTexSize
, '', '');
870 r_GL_MaxTexSize
:= 0; // default is automatic value