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 GetSpace (): Integer;
117 procedure r_Textures_Initialize
;
118 procedure r_Textures_Finalize
;
120 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
121 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
122 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; backanim
: Boolean; log
: Boolean = True): TGLMultiTexture
;
123 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
125 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; skipch
: Integer; log
: Boolean = true): TGLFont
;
131 e_log
, e_res
, WADReader
, Config
,
132 Imaging
, ImagingTypes
, ImagingUtility
136 maxTileSize
: Integer;
137 atl
: array of TGLAtlas
;
139 (* --------- TGLAtlasNode --------- *)
141 constructor TGLAtlasNode
.Create (base
: TGLAtlas
);
148 destructor TGLAtlasNode
.Destroy
;
153 function TGLAtlasNode
.GetID (): GLuint
;
155 result
:= self
.base
.id
158 procedure r_Textures_UpdateNode (n
: TGLAtlasNode
; data
: Pointer; x
, y
, w
, h
: Integer);
162 ASSERT(n
.base
<> nil);
166 ASSERT(n
.l
+ x
+ w
- 1 <= n
.r
);
167 ASSERT(n
.t
+ y
+ h
- 1 <= n
.b
);
169 glBindTexture(GL_TEXTURE_2D
, n
.id
);
170 glTexSubImage2D(GL_TEXTURE_2D
, 0, n
.l
+ x
, n
.t
+ y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
171 glBindTexture(GL_TEXTURE_2D
, 0);
174 (* --------- TGLAtlas --------- *)
176 constructor TGLAtlas
.Create (ww
, hh
: Integer; id
: GLuint
);
180 inherited Create(ww
, hh
);
184 destructor TGLAtlas
.Destroy
;
189 function TGLAtlas
.CreateNode (): TGLAtlasNode
;
191 result
:= TGLAtlasNode
.Create(self
);
194 function TGLAtlas
.Alloc (ww
, hh
: Integer): TGLAtlasNode
;
196 result
:= TGLAtlasNode(inherited Alloc(ww
, hh
));
199 function r_Textures_AllocHWTexture (w
, h
: Integer): GLuint
;
202 glGenTextures(1, @id
);
205 glBindTexture(GL_TEXTURE_2D
, id
);
206 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
207 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
208 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nil);
209 glBindTexture(GL_TEXTURE_2D
, 0);
214 function r_Textures_AllocAtlas (): TGLAtlas
;
215 var i
: Integer; id
: GLuint
;
218 id
:= r_Textures_AllocHWTexture(maxTileSize
, maxTileSize
);
222 SetLength(atl
, i
+ 1);
223 atl
[i
] := TGLAtlas
.Create(maxTileSize
, maxTileSize
, id
);
228 function r_Textures_AllocNode (w
, h
: Integer): TGLAtlasNode
;
229 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
235 while (i
>= 0) and (n
= nil) do
237 n
:= atl
[i
].Alloc(w
, h
);
243 a
:= r_Textures_AllocAtlas();
250 (* --------- TGLTexture --------- *)
252 destructor TGLTexture
.Destroy
;
255 if self
.mTile
<> nil then
257 for i
:= 0 to High(self
.mTile
) do
259 if self
.mTile
[i
] <> nil then
261 self
.mTile
[i
].Dealloc
;
262 self
.mTile
[i
] := nil;
270 function TGLTexture
.GetLines (): Integer;
272 ASSERT(self
.mTile
<> nil);
273 result
:= Length(self
.mTile
) div self
.mCols
276 function TGLTexture
.GetTile (col
, line
: Integer): TGLAtlasNode
;
280 ASSERT(col
<= mCols
);
281 ASSERT(self
.mTile
<> nil);
282 i
:= line
* mCols
+ col
;
284 ASSERT(i
< Length(mTile
));
286 ASSERT(result
<> nil)
289 function r_Textures_Alloc (w
, h
: Integer): TGLTexture
;
290 var x
, y
, mw
, mh
, cols
, lines
: Integer; t
: TGLTexture
;
294 cols
:= (w
+ maxTileSize
- 1) div maxTileSize
;
295 lines
:= (h
+ maxTileSize
- 1) div maxTileSize
;
296 t
:= TGLTexture
.Create
;
300 // t.mLines := lines;
301 SetLength(t
.mTile
, cols
* lines
);
302 for y
:= 0 to lines
- 1 do
304 mh
:= Min(maxTileSize
, h
- y
* maxTileSize
);
306 for x
:= 0 to cols
- 1 do
308 mw
:= Min(maxTileSize
, w
- x
* maxTileSize
);
310 t
.mTile
[y
* cols
+ x
] := r_Textures_AllocNode(mw
, mh
);
316 (* --------- TGLMultiTexture --------- *)
318 destructor TGLMultiTexture
.Destroy
;
321 for i
:= 0 to self
.count
- 1 do
322 self
.mTexture
[i
].Free
;
323 self
.mTexture
:= nil;
327 function TGLMultiTexture
.GetWidth (): Integer;
329 result
:= self
.mTexture
[0].width
332 function TGLMultiTexture
.GetHeight (): Integer;
334 result
:= self
.mTexture
[0].height
337 function TGLMultiTexture
.GetCount (): Integer;
339 result
:= Length(self
.mTexture
)
342 function TGLMultiTexture
.GetTexture (i
: Integer): TGLTexture
;
345 ASSERT(i
< self
.count
);
346 result
:= self
.mTexture
[i
];
347 ASSERT(result
<> nil);
350 (* --------- Init / Fin --------- *)
352 function r_Textures_GetMaxHardwareSize (): Integer;
355 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, @size
);
356 if size
< 64 then size
:= 64;
357 //if size > 512 then size := 512;
362 procedure r_Textures_Initialize
;
364 maxTileSize
:= r_Textures_GetMaxHardwareSize();
367 procedure r_Textures_Finalize
;
372 for i
:= 0 to High(atl
) do
374 glDeleteTextures(1, @atl
[i
].id
);
382 function r_Textures_FixImageData (var img
: TImageData
): Boolean;
385 if ConvertImage(img
, TImageFormat
.ifA8R8G8B8
) then
386 if SwapChannels(img
, ChannelRed
, ChannelBlue
) then // wtf
390 function r_Textures_LoadFromImage (var img
: TImageData
): TGLTexture
;
391 var t
: TGLTexture
; n
: TGLAtlasNode
; c
: TDynImageDataArray
; cw
, ch
, i
, j
: LongInt;
394 if SplitImage(img
, c
, maxTileSize
, maxTileSize
, cw
, ch
, False) then
396 t
:= r_Textures_Alloc(img
.width
, img
.height
);
400 ASSERT(ch
= t
.lines
);
401 for j
:= 0 to ch
- 1 do
403 for i
:= 0 to cw
- 1 do
405 n
:= t
.GetTile(i
, j
);
407 r_Textures_UpdateNode(n
, c
[j
* cw
+ i
].bits
, 0, 0, n
.width
, n
.height
)
412 FreeImagesInArray(c
);
416 function r_Textures_LoadFromMemory (data
: Pointer; size
: LongInt): TGLTexture
;
420 if (data
<> nil) and (size
> 0) then
424 if LoadImageFromMemory(data
, size
, img
) then
425 if r_Textures_FixImageData(img
) then
426 result
:= r_Textures_LoadFromImage(img
)
433 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
434 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
437 wadName
:= g_ExtractWadName(filename
);
438 wad
:= TWADFile
.Create();
439 if wad
.ReadFile(wadName
) then
441 resName
:= g_ExtractFilePathName(filename
);
442 if wad
.GetResource(resName
, data
, size
, log
) then
444 result
:= r_Textures_LoadFromMemory(data
, size
);
451 function r_Textures_LoadMultiFromImageAndInfo (var img
: TImageData
; w
, h
, c
: Integer; b
: Boolean): TGLMultiTexture
;
452 var t
: TImageData
; a
: array of TGLTexture
; i
: Integer; m
: TGLMultiTexture
;
459 for i
:= 0 to c
- 1 do
462 if NewImage(w
, h
, img
.Format
, t
) then
463 if CopyRect(img
, w
* i
, 0, w
, h
, t
, 0, 0) then
464 a
[i
] := r_Textures_LoadFromImage(t
);
468 m
:= TGLMultiTexture
.Create();
471 ASSERT(m
.mTexture
<> nil);
475 function r_Textures_LoadMultiFromDataAndInfo (data
: Pointer; size
: LongInt; w
, h
, c
: Integer; b
: Boolean): TGLMultiTexture
;
482 if (data
<> nil) and (size
> 0) then
486 if LoadImageFromMemory(data
, size
, img
) then
487 if r_Textures_FixImageData(img
) then
488 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
, b
)
495 function r_Textures_LoadMultiFromWad (wad
: TWADFile
): TGLMultiTexture
;
496 var data
: Pointer; size
: LongInt; TexRes
: AnsiString; w
, h
, c
: Integer; b
: Boolean; cfg
: TConfig
; img
: TImageData
;
500 if wad
.GetResource('TEXT/ANIM', data
, size
) then
502 cfg
:= TConfig
.CreateMem(data
, size
);
506 TexRes
:= cfg
.ReadStr('', 'resource', '');
507 w
:= cfg
.ReadInt('', 'framewidth', 0);
508 h
:= cfg
.ReadInt('', 'frameheight', 0);
509 c
:= cfg
.ReadInt('', 'framecount', 0);
510 b
:= cfg
.ReadBool('', 'backanim', false);
511 if (TexRes
<> '') and (w
> 0) and (h
> 0) and (c
> 0) then
513 if wad
.GetResource('TEXTURES/' + TexRes
, data
, size
) then
517 if LoadImageFromMemory(data
, size
, img
) then
518 if r_Textures_FixImageData(img
) then
519 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
, b
)
531 function r_Textures_LoadMultiFromMemory (data
: Pointer; size
: LongInt): TGLMultiTexture
;
532 var wad
: TWADFile
; t
: TGLTexture
; m
: TGLMultiTexture
;
535 if (data
<> nil) and (size
> 0) then
537 t
:= r_Textures_LoadFromMemory(data
, size
);
540 m
:= TGLMultiTexture
.Create();
541 SetLength(m
.mTexture
, 1);
543 m
.mBackanim
:= false;
546 else if IsWadData(data
, size
) then
548 wad
:= TWADFile
.Create();
549 if wad
.ReadMemory(data
, size
) then
551 result
:= r_Textures_LoadMultiFromWad(wad
);
558 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
559 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer; t
: TGLTexture
;
562 wadName
:= g_ExtractWadName(filename
);
563 wad
:= TWADFile
.Create();
564 if wad
.ReadFile(wadName
) then
566 resName
:= g_ExtractFilePathName(filename
);
567 if wad
.GetResource(resName
, data
, size
, log
) then
569 result
:= r_Textures_LoadMultiFromMemory(data
, size
);
576 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; backanim
: Boolean; log
: Boolean = True): TGLMultiTexture
;
577 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
583 wadName
:= g_ExtractWadName(filename
);
584 wad
:= TWADFile
.Create();
585 if wad
.ReadFile(wadName
) then
587 resName
:= g_ExtractFilePathName(filename
);
588 if wad
.GetResource(resName
, data
, size
, log
) then
590 result
:= r_Textures_LoadMultiFromDataAndInfo(data
, size
, w
, h
, count
, backanim
);
597 function r_Textures_GetRect (var img
: TImageData
): TRectWH
;
598 var i
, j
, w
, h
: Integer; done
: Boolean;
600 function IsVoid (i
, j
: Integer): Boolean; inline;
602 result
:= GetPixel32(img
, i
, j
).Channels
[3] = 0
609 (* trace x from right to left *)
610 done
:= false; i
:= 0;
611 while not done
and (i
< w
) do
614 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
615 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
620 (* trace y from up to down *)
621 done
:= false; j
:= 0;
622 while not done
and (j
< h
) do
625 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
626 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
631 (* trace x from right to left *)
632 done
:= false; i
:= w
- 1;
633 while not done
and (i
>= 0) do
636 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
637 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
638 result
.width
:= i
- result
.x
+ 1;
642 (* trace y from down to up *)
643 done
:= false; j
:= h
- 1;
644 while not done
and (j
>= 0) do
647 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
648 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
649 result
.height
:= j
- result
.y
+ 1;
654 function r_Textures_LoadStreamFromImage (var img
: TImageData
; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
655 var i
, x
, y
: Integer; t
: TImageData
;
661 ASSERT((st
<> nil) and (Length(st
) >= c
));
662 ASSERT((rs
= nil) or (Length(rs
) >= c
));
664 for i
:= 0 to c
- 1 do
670 if NewImage(w
, h
, img
.Format
, t
) then
672 if CopyRect(img
, x
* w
, y
* h
, w
, h
, t
, 0, 0) then
675 rs
[i
] := r_Textures_GetRect(t
);
676 st
[i
] := r_Textures_LoadFromImage(t
);
679 ASSERT(st
[i
] <> nil);
684 function r_Textures_LoadStreamFromMemory (data
: Pointer; size
: LongInt; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
691 ASSERT((st
<> nil) and (Length(st
) >= c
));
692 ASSERT((rs
= nil) or (Length(rs
) >= c
));
694 if (data
<> nil) and (size
> 0) then
698 if LoadImageFromMemory(data
, size
, img
) then
700 if r_Textures_FixImageData(img
) then
702 result
:= r_Textures_LoadStreamFromImage(img
, w
, h
, c
, cw
, st
, rs
)
711 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
712 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
718 ASSERT((st
<> nil) and (Length(st
) >= count
));
719 ASSERT((rs
= nil) or (Length(rs
) >= count
));
721 wadName
:= g_ExtractWadName(filename
);
722 wad
:= TWADFile
.Create();
723 if wad
.ReadFile(wadName
) then
725 resName
:= g_ExtractFilePathName(filename
);
726 if wad
.GetResource(resName
, data
, size
, log
) then
728 result
:= r_Textures_LoadStreamFromMemory(data
, size
, w
, h
, count
, cw
, st
, rs
);
735 (* --------- TGLFont --------- *)
737 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; skipch
: Integer; log
: Boolean = true): TGLFont
;
738 var i
: Integer; st
: TGLTextureArray
; font
: TGLFont
; t
: TGLTexture
;
743 if r_Textures_LoadStreamFromFile(filename
, f
.w
, f
.h
, 256, 16, st
, nil, log
) then
750 st
[i
] := st
[(i
+ skipch
) mod 256];
751 st
[(i
+ skipch
) mod 256] := t
;
754 font
:= TGLFont
.Create();
761 destructor TGLFont
.Destroy
;
764 if self
.ch
<> nil then
765 for i
:= 0 to High(self
.ch
) do
770 function TGLFont
.GetChar (c
: AnsiChar): TGLTexture
;
772 result
:= self
.ch
[ORD(c
)];
775 function TGLFont
.GetWidth (c
: AnsiChar): Integer;
777 result
:= self
.info
.ch
[c
].w
;
779 result
:= self
.info
.w
;
782 function TGLFont
.GetSpace (): Integer;
784 result
:= self
.info
.kern
;