1f3f23885eaef05e98ac9303c6f89459a6e1b4b9
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 procedure r_Textures_Initialize
;
124 procedure r_Textures_Finalize
;
126 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
127 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
128 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; log
: Boolean = True): TGLMultiTexture
;
129 function r_Textures_LoadMultiTextFromFile (const filename
: AnsiString; var txt
: TAnimTextInfo
; log
: Boolean = True): TGLMultiTexture
;
131 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
133 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; skipch
: Integer; log
: Boolean = true): TGLFont
;
139 e_log
, e_res
, WADReader
, Config
,
140 Imaging
, ImagingTypes
, ImagingUtility
144 maxTileSize
: Integer;
145 atl
: array of TGLAtlas
;
147 (* --------- TGLAtlasNode --------- *)
149 constructor TGLAtlasNode
.Create (base
: TGLAtlas
);
156 destructor TGLAtlasNode
.Destroy
;
161 function TGLAtlasNode
.GetID (): GLuint
;
163 result
:= self
.base
.id
166 procedure r_Textures_UpdateNode (n
: TGLAtlasNode
; data
: Pointer; x
, y
, w
, h
: Integer);
170 ASSERT(n
.base
<> nil);
174 ASSERT(n
.l
+ x
+ w
- 1 <= n
.r
);
175 ASSERT(n
.t
+ y
+ h
- 1 <= n
.b
);
177 glBindTexture(GL_TEXTURE_2D
, n
.id
);
178 glTexSubImage2D(GL_TEXTURE_2D
, 0, n
.l
+ x
, n
.t
+ y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
179 glBindTexture(GL_TEXTURE_2D
, 0);
182 (* --------- TGLAtlas --------- *)
184 constructor TGLAtlas
.Create (ww
, hh
: Integer; id
: GLuint
);
188 inherited Create(ww
, hh
);
192 destructor TGLAtlas
.Destroy
;
197 function TGLAtlas
.CreateNode (): TGLAtlasNode
;
199 result
:= TGLAtlasNode
.Create(self
);
202 function TGLAtlas
.Alloc (ww
, hh
: Integer): TGLAtlasNode
;
204 result
:= TGLAtlasNode(inherited Alloc(ww
, hh
));
207 function r_Textures_AllocHWTexture (w
, h
: Integer): GLuint
;
210 glGenTextures(1, @id
);
213 glBindTexture(GL_TEXTURE_2D
, id
);
214 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
215 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
216 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nil);
217 glBindTexture(GL_TEXTURE_2D
, 0);
222 function r_Textures_AllocAtlas (): TGLAtlas
;
223 var i
: Integer; id
: GLuint
;
226 id
:= r_Textures_AllocHWTexture(maxTileSize
, maxTileSize
);
230 SetLength(atl
, i
+ 1);
231 atl
[i
] := TGLAtlas
.Create(maxTileSize
, maxTileSize
, id
);
236 function r_Textures_AllocNode (w
, h
: Integer): TGLAtlasNode
;
237 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
243 while (i
>= 0) and (n
= nil) do
245 n
:= atl
[i
].Alloc(w
, h
);
251 a
:= r_Textures_AllocAtlas();
258 (* --------- TGLTexture --------- *)
260 destructor TGLTexture
.Destroy
;
263 if self
.mTile
<> nil then
265 for i
:= 0 to High(self
.mTile
) do
267 if self
.mTile
[i
] <> nil then
269 self
.mTile
[i
].Dealloc
;
270 self
.mTile
[i
] := nil;
278 function TGLTexture
.GetLines (): Integer;
280 ASSERT(self
.mTile
<> nil);
281 result
:= Length(self
.mTile
) div self
.mCols
284 function TGLTexture
.GetTile (col
, line
: Integer): TGLAtlasNode
;
288 ASSERT(col
<= mCols
);
289 ASSERT(self
.mTile
<> nil);
290 i
:= line
* mCols
+ col
;
292 ASSERT(i
< Length(mTile
));
294 ASSERT(result
<> nil)
297 function r_Textures_Alloc (w
, h
: Integer): TGLTexture
;
298 var x
, y
, mw
, mh
, cols
, lines
: Integer; t
: TGLTexture
;
302 cols
:= (w
+ maxTileSize
- 1) div maxTileSize
;
303 lines
:= (h
+ maxTileSize
- 1) div maxTileSize
;
304 t
:= TGLTexture
.Create
;
308 // t.mLines := lines;
309 SetLength(t
.mTile
, cols
* lines
);
310 for y
:= 0 to lines
- 1 do
312 mh
:= Min(maxTileSize
, h
- y
* maxTileSize
);
314 for x
:= 0 to cols
- 1 do
316 mw
:= Min(maxTileSize
, w
- x
* maxTileSize
);
318 t
.mTile
[y
* cols
+ x
] := r_Textures_AllocNode(mw
, mh
);
324 (* --------- TGLMultiTexture --------- *)
326 destructor TGLMultiTexture
.Destroy
;
329 for i
:= 0 to self
.count
- 1 do
330 self
.mTexture
[i
].Free
;
331 self
.mTexture
:= nil;
335 function TGLMultiTexture
.GetWidth (): Integer;
337 result
:= self
.mTexture
[0].width
340 function TGLMultiTexture
.GetHeight (): Integer;
342 result
:= self
.mTexture
[0].height
345 function TGLMultiTexture
.GetCount (): Integer;
347 result
:= Length(self
.mTexture
)
350 function TGLMultiTexture
.GetTexture (i
: Integer): TGLTexture
;
353 ASSERT(i
< self
.count
);
354 result
:= self
.mTexture
[i
];
355 ASSERT(result
<> nil);
358 (* --------- Init / Fin --------- *)
360 function r_Textures_GetMaxHardwareSize (): Integer;
363 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, @size
);
364 if size
< 64 then size
:= 64;
365 //if size > 512 then size := 512;
370 procedure r_Textures_Initialize
;
372 maxTileSize
:= r_Textures_GetMaxHardwareSize();
375 procedure r_Textures_Finalize
;
380 for i
:= 0 to High(atl
) do
382 glDeleteTextures(1, @atl
[i
].id
);
390 function r_Textures_FixImageData (var img
: TImageData
): Boolean;
393 if ConvertImage(img
, TImageFormat
.ifA8R8G8B8
) then
394 if SwapChannels(img
, ChannelRed
, ChannelBlue
) then // wtf
398 function r_Textures_LoadFromImage (var img
: TImageData
): TGLTexture
;
399 var t
: TGLTexture
; n
: TGLAtlasNode
; c
: TDynImageDataArray
; cw
, ch
, i
, j
: LongInt;
402 if SplitImage(img
, c
, maxTileSize
, maxTileSize
, cw
, ch
, False) then
404 t
:= r_Textures_Alloc(img
.width
, img
.height
);
408 ASSERT(ch
= t
.lines
);
409 for j
:= 0 to ch
- 1 do
411 for i
:= 0 to cw
- 1 do
413 n
:= t
.GetTile(i
, j
);
415 r_Textures_UpdateNode(n
, c
[j
* cw
+ i
].bits
, 0, 0, n
.width
, n
.height
)
420 FreeImagesInArray(c
);
424 function r_Textures_LoadFromMemory (data
: Pointer; size
: LongInt): TGLTexture
;
428 if (data
<> nil) and (size
> 0) then
432 if LoadImageFromMemory(data
, size
, img
) then
433 if r_Textures_FixImageData(img
) then
434 result
:= r_Textures_LoadFromImage(img
)
441 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
442 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
445 wadName
:= g_ExtractWadName(filename
);
446 wad
:= TWADFile
.Create();
447 if wad
.ReadFile(wadName
) then
449 resName
:= g_ExtractFilePathName(filename
);
450 if wad
.GetResource(resName
, data
, size
, log
) then
452 result
:= r_Textures_LoadFromMemory(data
, size
);
459 function r_Textures_LoadMultiFromImageAndInfo (var img
: TImageData
; w
, h
, c
: Integer): TGLMultiTexture
;
460 var t
: TImageData
; a
: array of TGLTexture
; i
: Integer; m
: TGLMultiTexture
;
467 for i
:= 0 to c
- 1 do
470 if NewImage(w
, h
, img
.Format
, t
) then
471 if CopyRect(img
, w
* i
, 0, w
, h
, t
, 0, 0) then
472 a
[i
] := r_Textures_LoadFromImage(t
);
476 m
:= TGLMultiTexture
.Create();
478 ASSERT(m
.mTexture
<> nil);
482 function r_Textures_LoadMultiFromDataAndInfo (data
: Pointer; size
: LongInt; w
, h
, c
: Integer): TGLMultiTexture
;
489 if (data
<> nil) and (size
> 0) then
493 if LoadImageFromMemory(data
, size
, img
) then
494 if r_Textures_FixImageData(img
) then
495 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
)
502 function r_Textures_LoadTextFromMemory (data
: Pointer; size
: LongInt; var txt
: TAnimTextInfo
): Boolean;
508 cfg
:= TConfig
.CreateMem(data
, size
);
511 txt
.name
:= cfg
.ReadStr('', 'resource', '');
512 txt
.w
:= MAX(0, cfg
.ReadInt('', 'framewidth', 0));
513 txt
.h
:= MAX(0, cfg
.ReadInt('', 'frameheight', 0));
514 txt
.anim
.loop
:= true;
515 txt
.anim
.delay
:= MAX(0, cfg
.ReadInt('', 'waitcount', 0));
516 txt
.anim
.frames
:= MAX(0, cfg
.ReadInt('', 'framecount', 0));
517 txt
.anim
.back
:= cfg
.ReadBool('', 'backanim', false);
519 result
:= (txt
.name
<> '') and (txt
.w
> 0) and (txt
.h
> 0) and (txt
.anim
.delay
> 0) and (txt
.anim
.frames
> 0);
524 function r_Textures_LoadMultiFromWad (wad
: TWADFile
; var txt
: TAnimTextInfo
): TGLMultiTexture
;
525 var data
: Pointer; size
: LongInt; img
: TImageData
;
529 if wad
.GetResource('TEXT/ANIM', data
, size
) then
531 if r_Textures_LoadTextFromMemory(data
, size
, txt
) then
534 if wad
.GetResource('TEXTURES/' + txt
.name
, data
, size
) then
538 if LoadImageFromMemory(data
, size
, img
) then
539 if r_Textures_FixImageData(img
) then
540 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, txt
.w
, txt
.h
, txt
.anim
.frames
);
552 function r_Textures_LoadMultiFromMemory (data
: Pointer; size
: LongInt; var txt
: TAnimTextInfo
): TGLMultiTexture
;
553 var wad
: TWADFile
; t
: TGLTexture
; m
: TGLMultiTexture
;
556 if (data
<> nil) and (size
> 0) then
558 t
:= r_Textures_LoadFromMemory(data
, size
);
561 m
:= TGLMultiTexture
.Create();
562 SetLength(m
.mTexture
, 1);
567 txt
.anim
.loop
:= true;
569 txt
.anim
.frames
:= 1;
570 txt
.anim
.back
:= false;
573 else if IsWadData(data
, size
) then
575 wad
:= TWADFile
.Create();
576 if wad
.ReadMemory(data
, size
) then
578 result
:= r_Textures_LoadMultiFromWad(wad
, txt
);
585 function r_Textures_LoadMultiTextFromFile (const filename
: AnsiString; var txt
: TAnimTextInfo
; log
: Boolean = True): TGLMultiTexture
;
586 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
589 wadName
:= g_ExtractWadName(filename
);
590 wad
:= TWADFile
.Create();
591 if wad
.ReadFile(wadName
) then
593 resName
:= g_ExtractFilePathName(filename
);
594 if wad
.GetResource(resName
, data
, size
, log
) then
596 result
:= r_Textures_LoadMultiFromMemory(data
, size
, txt
);
603 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
604 var txt
: TAnimTextInfo
;
606 result
:= r_Textures_LoadMultiTextFromFile(filename
, txt
, log
);
609 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; log
: Boolean = True): TGLMultiTexture
;
610 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
616 wadName
:= g_ExtractWadName(filename
);
617 wad
:= TWADFile
.Create();
618 if wad
.ReadFile(wadName
) then
620 resName
:= g_ExtractFilePathName(filename
);
621 if wad
.GetResource(resName
, data
, size
, log
) then
623 result
:= r_Textures_LoadMultiFromDataAndInfo(data
, size
, w
, h
, count
);
630 function r_Textures_GetRect (var img
: TImageData
): TRectWH
;
631 var i
, j
, w
, h
: Integer; done
: Boolean;
633 function IsVoid (i
, j
: Integer): Boolean; inline;
635 result
:= GetPixel32(img
, i
, j
).Channels
[3] = 0
642 (* trace x from right to left *)
643 done
:= false; i
:= 0;
644 while not done
and (i
< w
) do
647 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
648 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
653 (* trace y from up to down *)
654 done
:= false; j
:= 0;
655 while not done
and (j
< h
) do
658 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
659 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
664 (* trace x from right to left *)
665 done
:= false; i
:= w
- 1;
666 while not done
and (i
>= 0) do
669 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
670 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
671 result
.width
:= i
- result
.x
+ 1;
675 (* trace y from down to up *)
676 done
:= false; j
:= h
- 1;
677 while not done
and (j
>= 0) do
680 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
681 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
682 result
.height
:= j
- result
.y
+ 1;
687 function r_Textures_LoadStreamFromImage (var img
: TImageData
; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
688 var i
, x
, y
: Integer; t
: TImageData
;
694 ASSERT((st
<> nil) and (Length(st
) >= c
));
695 ASSERT((rs
= nil) or (Length(rs
) >= c
));
697 for i
:= 0 to c
- 1 do
703 if NewImage(w
, h
, img
.Format
, t
) then
705 if CopyRect(img
, x
* w
, y
* h
, w
, h
, t
, 0, 0) then
708 rs
[i
] := r_Textures_GetRect(t
);
709 st
[i
] := r_Textures_LoadFromImage(t
);
712 ASSERT(st
[i
] <> nil);
717 function r_Textures_LoadStreamFromMemory (data
: Pointer; size
: LongInt; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
724 ASSERT((st
<> nil) and (Length(st
) >= c
));
725 ASSERT((rs
= nil) or (Length(rs
) >= c
));
727 if (data
<> nil) and (size
> 0) then
731 if LoadImageFromMemory(data
, size
, img
) then
733 if r_Textures_FixImageData(img
) then
735 result
:= r_Textures_LoadStreamFromImage(img
, w
, h
, c
, cw
, st
, rs
)
744 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
745 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
751 ASSERT((st
<> nil) and (Length(st
) >= count
));
752 ASSERT((rs
= nil) or (Length(rs
) >= count
));
754 wadName
:= g_ExtractWadName(filename
);
755 wad
:= TWADFile
.Create();
756 if wad
.ReadFile(wadName
) then
758 resName
:= g_ExtractFilePathName(filename
);
759 if wad
.GetResource(resName
, data
, size
, log
) then
761 result
:= r_Textures_LoadStreamFromMemory(data
, size
, w
, h
, count
, cw
, st
, rs
);
768 (* --------- TGLFont --------- *)
770 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; skipch
: Integer; log
: Boolean = true): TGLFont
;
771 var i
: Integer; st
: TGLTextureArray
; font
: TGLFont
; t
: TGLTexture
;
776 if r_Textures_LoadStreamFromFile(filename
, f
.w
, f
.h
, 256, 16, st
, nil, log
) then
783 st
[i
] := st
[(i
+ skipch
) mod 256];
784 st
[(i
+ skipch
) mod 256] := t
;
787 font
:= TGLFont
.Create();
794 destructor TGLFont
.Destroy
;
797 if self
.ch
<> nil then
798 for i
:= 0 to High(self
.ch
) do
803 function TGLFont
.GetChar (c
: AnsiChar): TGLTexture
;
805 result
:= self
.ch
[ORD(c
)];
808 function TGLFont
.GetWidth (c
: AnsiChar): Integer;
810 result
:= self
.info
.ch
[c
].w
;
812 result
:= self
.info
.w
;
813 if self
.info
.kern
< 0 then
814 result
:= result
+ self
.info
.kern
;
817 function TGLFont
.GetMaxWidth (): Integer;
819 result
:= self
.info
.w
;
820 if self
.info
.kern
< 0 then
821 result
:= result
+ self
.info
.kern
;
824 function TGLFont
.GetMaxHeight (): Integer;
826 result
:= self
.info
.h
;
829 function TGLFont
.GetSpace (): Integer;
831 result
:= self
.info
.kern
;