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}
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
;
88 destructor Destroy
; override;
90 function GetWidth (): Integer; inline;
91 function GetHeight (): Integer; inline;
92 function GetCount (): Integer; inline;
93 function GetTexture (i
: Integer): TGLTexture
; {inline;}
95 property width
: Integer read GetWidth
;
96 property height
: Integer read GetHeight
;
97 property count
: Integer read GetCount
;
98 property backAnim
: Boolean read mBackanim
; (* this property must be located at TAnimState? *)
101 TGLTextureArray
= array of TGLTexture
;
103 TRectArray
= array of TRectWH
;
105 TGLFont
= class sealed (TFont
)
111 destructor Destroy
; override;
112 function GetChar (c
: AnsiChar): TGLTexture
;
113 function GetWidth (c
: AnsiChar): Integer;
114 function GetMaxWidth (): Integer;
115 function GetMaxHeight (): Integer;
116 function GetSpace (): Integer;
119 procedure r_Textures_Initialize
;
120 procedure r_Textures_Finalize
;
122 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
123 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
124 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; backanim
: Boolean; log
: Boolean = True): TGLMultiTexture
;
125 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
127 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; skipch
: Integer; log
: Boolean = true): TGLFont
;
133 e_log
, e_res
, WADReader
, Config
,
134 Imaging
, ImagingTypes
, ImagingUtility
138 maxTileSize
: Integer;
139 atl
: array of TGLAtlas
;
141 (* --------- TGLAtlasNode --------- *)
143 constructor TGLAtlasNode
.Create (base
: TGLAtlas
);
150 destructor TGLAtlasNode
.Destroy
;
155 function TGLAtlasNode
.GetID (): GLuint
;
157 result
:= self
.base
.id
160 procedure r_Textures_UpdateNode (n
: TGLAtlasNode
; data
: Pointer; x
, y
, w
, h
: Integer);
164 ASSERT(n
.base
<> nil);
168 ASSERT(n
.l
+ x
+ w
- 1 <= n
.r
);
169 ASSERT(n
.t
+ y
+ h
- 1 <= n
.b
);
171 glBindTexture(GL_TEXTURE_2D
, n
.id
);
172 glTexSubImage2D(GL_TEXTURE_2D
, 0, n
.l
+ x
, n
.t
+ y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
173 glBindTexture(GL_TEXTURE_2D
, 0);
176 (* --------- TGLAtlas --------- *)
178 constructor TGLAtlas
.Create (ww
, hh
: Integer; id
: GLuint
);
182 inherited Create(ww
, hh
);
186 destructor TGLAtlas
.Destroy
;
191 function TGLAtlas
.CreateNode (): TGLAtlasNode
;
193 result
:= TGLAtlasNode
.Create(self
);
196 function TGLAtlas
.Alloc (ww
, hh
: Integer): TGLAtlasNode
;
198 result
:= TGLAtlasNode(inherited Alloc(ww
, hh
));
201 function r_Textures_AllocHWTexture (w
, h
: Integer): GLuint
;
204 glGenTextures(1, @id
);
207 glBindTexture(GL_TEXTURE_2D
, id
);
208 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
209 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
210 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nil);
211 glBindTexture(GL_TEXTURE_2D
, 0);
216 function r_Textures_AllocAtlas (): TGLAtlas
;
217 var i
: Integer; id
: GLuint
;
220 id
:= r_Textures_AllocHWTexture(maxTileSize
, maxTileSize
);
224 SetLength(atl
, i
+ 1);
225 atl
[i
] := TGLAtlas
.Create(maxTileSize
, maxTileSize
, id
);
230 function r_Textures_AllocNode (w
, h
: Integer): TGLAtlasNode
;
231 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
237 while (i
>= 0) and (n
= nil) do
239 n
:= atl
[i
].Alloc(w
, h
);
245 a
:= r_Textures_AllocAtlas();
252 (* --------- TGLTexture --------- *)
254 destructor TGLTexture
.Destroy
;
257 if self
.mTile
<> nil then
259 for i
:= 0 to High(self
.mTile
) do
261 if self
.mTile
[i
] <> nil then
263 self
.mTile
[i
].Dealloc
;
264 self
.mTile
[i
] := nil;
272 function TGLTexture
.GetLines (): Integer;
274 ASSERT(self
.mTile
<> nil);
275 result
:= Length(self
.mTile
) div self
.mCols
278 function TGLTexture
.GetTile (col
, line
: Integer): TGLAtlasNode
;
282 ASSERT(col
<= mCols
);
283 ASSERT(self
.mTile
<> nil);
284 i
:= line
* mCols
+ col
;
286 ASSERT(i
< Length(mTile
));
288 ASSERT(result
<> nil)
291 function r_Textures_Alloc (w
, h
: Integer): TGLTexture
;
292 var x
, y
, mw
, mh
, cols
, lines
: Integer; t
: TGLTexture
;
296 cols
:= (w
+ maxTileSize
- 1) div maxTileSize
;
297 lines
:= (h
+ maxTileSize
- 1) div maxTileSize
;
298 t
:= TGLTexture
.Create
;
302 // t.mLines := lines;
303 SetLength(t
.mTile
, cols
* lines
);
304 for y
:= 0 to lines
- 1 do
306 mh
:= Min(maxTileSize
, h
- y
* maxTileSize
);
308 for x
:= 0 to cols
- 1 do
310 mw
:= Min(maxTileSize
, w
- x
* maxTileSize
);
312 t
.mTile
[y
* cols
+ x
] := r_Textures_AllocNode(mw
, mh
);
318 (* --------- TGLMultiTexture --------- *)
320 destructor TGLMultiTexture
.Destroy
;
323 for i
:= 0 to self
.count
- 1 do
324 self
.mTexture
[i
].Free
;
325 self
.mTexture
:= nil;
329 function TGLMultiTexture
.GetWidth (): Integer;
331 result
:= self
.mTexture
[0].width
334 function TGLMultiTexture
.GetHeight (): Integer;
336 result
:= self
.mTexture
[0].height
339 function TGLMultiTexture
.GetCount (): Integer;
341 result
:= Length(self
.mTexture
)
344 function TGLMultiTexture
.GetTexture (i
: Integer): TGLTexture
;
347 ASSERT(i
< self
.count
);
348 result
:= self
.mTexture
[i
];
349 ASSERT(result
<> nil);
352 (* --------- Init / Fin --------- *)
354 function r_Textures_GetMaxHardwareSize (): Integer;
357 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, @size
);
358 if size
< 64 then size
:= 64;
359 //if size > 512 then size := 512;
364 procedure r_Textures_Initialize
;
366 maxTileSize
:= r_Textures_GetMaxHardwareSize();
369 procedure r_Textures_Finalize
;
374 for i
:= 0 to High(atl
) do
376 glDeleteTextures(1, @atl
[i
].id
);
384 function r_Textures_FixImageData (var img
: TImageData
): Boolean;
387 if ConvertImage(img
, TImageFormat
.ifA8R8G8B8
) then
388 if SwapChannels(img
, ChannelRed
, ChannelBlue
) then // wtf
392 function r_Textures_LoadFromImage (var img
: TImageData
): TGLTexture
;
393 var t
: TGLTexture
; n
: TGLAtlasNode
; c
: TDynImageDataArray
; cw
, ch
, i
, j
: LongInt;
396 if SplitImage(img
, c
, maxTileSize
, maxTileSize
, cw
, ch
, False) then
398 t
:= r_Textures_Alloc(img
.width
, img
.height
);
402 ASSERT(ch
= t
.lines
);
403 for j
:= 0 to ch
- 1 do
405 for i
:= 0 to cw
- 1 do
407 n
:= t
.GetTile(i
, j
);
409 r_Textures_UpdateNode(n
, c
[j
* cw
+ i
].bits
, 0, 0, n
.width
, n
.height
)
414 FreeImagesInArray(c
);
418 function r_Textures_LoadFromMemory (data
: Pointer; size
: LongInt): TGLTexture
;
422 if (data
<> nil) and (size
> 0) then
426 if LoadImageFromMemory(data
, size
, img
) then
427 if r_Textures_FixImageData(img
) then
428 result
:= r_Textures_LoadFromImage(img
)
435 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
436 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
439 wadName
:= g_ExtractWadName(filename
);
440 wad
:= TWADFile
.Create();
441 if wad
.ReadFile(wadName
) then
443 resName
:= g_ExtractFilePathName(filename
);
444 if wad
.GetResource(resName
, data
, size
, log
) then
446 result
:= r_Textures_LoadFromMemory(data
, size
);
453 function r_Textures_LoadMultiFromImageAndInfo (var img
: TImageData
; w
, h
, c
: Integer; b
: Boolean): TGLMultiTexture
;
454 var t
: TImageData
; a
: array of TGLTexture
; i
: Integer; m
: TGLMultiTexture
;
461 for i
:= 0 to c
- 1 do
464 if NewImage(w
, h
, img
.Format
, t
) then
465 if CopyRect(img
, w
* i
, 0, w
, h
, t
, 0, 0) then
466 a
[i
] := r_Textures_LoadFromImage(t
);
470 m
:= TGLMultiTexture
.Create();
473 ASSERT(m
.mTexture
<> nil);
477 function r_Textures_LoadMultiFromDataAndInfo (data
: Pointer; size
: LongInt; w
, h
, c
: Integer; b
: Boolean): TGLMultiTexture
;
484 if (data
<> nil) and (size
> 0) then
488 if LoadImageFromMemory(data
, size
, img
) then
489 if r_Textures_FixImageData(img
) then
490 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
, b
)
497 function r_Textures_LoadMultiFromWad (wad
: TWADFile
): TGLMultiTexture
;
498 var data
: Pointer; size
: LongInt; TexRes
: AnsiString; w
, h
, c
: Integer; b
: Boolean; cfg
: TConfig
; img
: TImageData
;
502 if wad
.GetResource('TEXT/ANIM', data
, size
) then
504 cfg
:= TConfig
.CreateMem(data
, size
);
508 TexRes
:= cfg
.ReadStr('', 'resource', '');
509 w
:= cfg
.ReadInt('', 'framewidth', 0);
510 h
:= cfg
.ReadInt('', 'frameheight', 0);
511 c
:= cfg
.ReadInt('', 'framecount', 0);
512 b
:= cfg
.ReadBool('', 'backanim', false);
513 if (TexRes
<> '') and (w
> 0) and (h
> 0) and (c
> 0) then
515 if wad
.GetResource('TEXTURES/' + TexRes
, data
, size
) then
519 if LoadImageFromMemory(data
, size
, img
) then
520 if r_Textures_FixImageData(img
) then
521 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
, b
)
533 function r_Textures_LoadMultiFromMemory (data
: Pointer; size
: LongInt): TGLMultiTexture
;
534 var wad
: TWADFile
; t
: TGLTexture
; m
: TGLMultiTexture
;
537 if (data
<> nil) and (size
> 0) then
539 t
:= r_Textures_LoadFromMemory(data
, size
);
542 m
:= TGLMultiTexture
.Create();
543 SetLength(m
.mTexture
, 1);
545 m
.mBackanim
:= false;
548 else if IsWadData(data
, size
) then
550 wad
:= TWADFile
.Create();
551 if wad
.ReadMemory(data
, size
) then
553 result
:= r_Textures_LoadMultiFromWad(wad
);
560 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
561 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer; t
: TGLTexture
;
564 wadName
:= g_ExtractWadName(filename
);
565 wad
:= TWADFile
.Create();
566 if wad
.ReadFile(wadName
) then
568 resName
:= g_ExtractFilePathName(filename
);
569 if wad
.GetResource(resName
, data
, size
, log
) then
571 result
:= r_Textures_LoadMultiFromMemory(data
, size
);
578 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; backanim
: Boolean; log
: Boolean = True): TGLMultiTexture
;
579 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
585 wadName
:= g_ExtractWadName(filename
);
586 wad
:= TWADFile
.Create();
587 if wad
.ReadFile(wadName
) then
589 resName
:= g_ExtractFilePathName(filename
);
590 if wad
.GetResource(resName
, data
, size
, log
) then
592 result
:= r_Textures_LoadMultiFromDataAndInfo(data
, size
, w
, h
, count
, backanim
);
599 function r_Textures_GetRect (var img
: TImageData
): TRectWH
;
600 var i
, j
, w
, h
: Integer; done
: Boolean;
602 function IsVoid (i
, j
: Integer): Boolean; inline;
604 result
:= GetPixel32(img
, i
, j
).Channels
[3] = 0
611 (* trace x from right to left *)
612 done
:= false; i
:= 0;
613 while not done
and (i
< w
) do
616 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
617 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
622 (* trace y from up to down *)
623 done
:= false; j
:= 0;
624 while not done
and (j
< h
) do
627 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
628 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
633 (* trace x from right to left *)
634 done
:= false; i
:= w
- 1;
635 while not done
and (i
>= 0) do
638 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
639 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
640 result
.width
:= i
- result
.x
+ 1;
644 (* trace y from down to up *)
645 done
:= false; j
:= h
- 1;
646 while not done
and (j
>= 0) do
649 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
650 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
651 result
.height
:= j
- result
.y
+ 1;
656 function r_Textures_LoadStreamFromImage (var img
: TImageData
; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
657 var i
, x
, y
: Integer; t
: TImageData
;
663 ASSERT((st
<> nil) and (Length(st
) >= c
));
664 ASSERT((rs
= nil) or (Length(rs
) >= c
));
666 for i
:= 0 to c
- 1 do
672 if NewImage(w
, h
, img
.Format
, t
) then
674 if CopyRect(img
, x
* w
, y
* h
, w
, h
, t
, 0, 0) then
677 rs
[i
] := r_Textures_GetRect(t
);
678 st
[i
] := r_Textures_LoadFromImage(t
);
681 ASSERT(st
[i
] <> nil);
686 function r_Textures_LoadStreamFromMemory (data
: Pointer; size
: LongInt; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
693 ASSERT((st
<> nil) and (Length(st
) >= c
));
694 ASSERT((rs
= nil) or (Length(rs
) >= c
));
696 if (data
<> nil) and (size
> 0) then
700 if LoadImageFromMemory(data
, size
, img
) then
702 if r_Textures_FixImageData(img
) then
704 result
:= r_Textures_LoadStreamFromImage(img
, w
, h
, c
, cw
, st
, rs
)
713 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
714 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
720 ASSERT((st
<> nil) and (Length(st
) >= count
));
721 ASSERT((rs
= nil) or (Length(rs
) >= count
));
723 wadName
:= g_ExtractWadName(filename
);
724 wad
:= TWADFile
.Create();
725 if wad
.ReadFile(wadName
) then
727 resName
:= g_ExtractFilePathName(filename
);
728 if wad
.GetResource(resName
, data
, size
, log
) then
730 result
:= r_Textures_LoadStreamFromMemory(data
, size
, w
, h
, count
, cw
, st
, rs
);
737 (* --------- TGLFont --------- *)
739 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; skipch
: Integer; log
: Boolean = true): TGLFont
;
740 var i
: Integer; st
: TGLTextureArray
; font
: TGLFont
; t
: TGLTexture
;
745 if r_Textures_LoadStreamFromFile(filename
, f
.w
, f
.h
, 256, 16, st
, nil, log
) then
752 st
[i
] := st
[(i
+ skipch
) mod 256];
753 st
[(i
+ skipch
) mod 256] := t
;
756 font
:= TGLFont
.Create();
763 destructor TGLFont
.Destroy
;
766 if self
.ch
<> nil then
767 for i
:= 0 to High(self
.ch
) do
772 function TGLFont
.GetChar (c
: AnsiChar): TGLTexture
;
774 result
:= self
.ch
[ORD(c
)];
777 function TGLFont
.GetWidth (c
: AnsiChar): Integer;
779 result
:= self
.info
.ch
[c
].w
;
781 result
:= self
.info
.w
;
782 if self
.info
.kern
< 0 then
783 result
:= result
+ self
.info
.kern
;
786 function TGLFont
.GetMaxWidth (): Integer;
788 result
:= self
.info
.w
;
789 if self
.info
.kern
< 0 then
790 result
:= result
+ self
.info
.kern
;
793 function TGLFont
.GetMaxHeight (): Integer;
795 result
:= self
.info
.h
;
798 function TGLFont
.GetSpace (): Integer;
800 result
:= self
.info
.kern
;