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 GetMaxHeight (): Integer;
115 function GetSpace (): Integer;
118 procedure r_Textures_Initialize
;
119 procedure r_Textures_Finalize
;
121 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
122 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
123 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; backanim
: Boolean; log
: Boolean = True): TGLMultiTexture
;
124 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
126 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; skipch
: Integer; log
: Boolean = true): TGLFont
;
132 e_log
, e_res
, WADReader
, Config
,
133 Imaging
, ImagingTypes
, ImagingUtility
137 maxTileSize
: Integer;
138 atl
: array of TGLAtlas
;
140 (* --------- TGLAtlasNode --------- *)
142 constructor TGLAtlasNode
.Create (base
: TGLAtlas
);
149 destructor TGLAtlasNode
.Destroy
;
154 function TGLAtlasNode
.GetID (): GLuint
;
156 result
:= self
.base
.id
159 procedure r_Textures_UpdateNode (n
: TGLAtlasNode
; data
: Pointer; x
, y
, w
, h
: Integer);
163 ASSERT(n
.base
<> nil);
167 ASSERT(n
.l
+ x
+ w
- 1 <= n
.r
);
168 ASSERT(n
.t
+ y
+ h
- 1 <= n
.b
);
170 glBindTexture(GL_TEXTURE_2D
, n
.id
);
171 glTexSubImage2D(GL_TEXTURE_2D
, 0, n
.l
+ x
, n
.t
+ y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
172 glBindTexture(GL_TEXTURE_2D
, 0);
175 (* --------- TGLAtlas --------- *)
177 constructor TGLAtlas
.Create (ww
, hh
: Integer; id
: GLuint
);
181 inherited Create(ww
, hh
);
185 destructor TGLAtlas
.Destroy
;
190 function TGLAtlas
.CreateNode (): TGLAtlasNode
;
192 result
:= TGLAtlasNode
.Create(self
);
195 function TGLAtlas
.Alloc (ww
, hh
: Integer): TGLAtlasNode
;
197 result
:= TGLAtlasNode(inherited Alloc(ww
, hh
));
200 function r_Textures_AllocHWTexture (w
, h
: Integer): GLuint
;
203 glGenTextures(1, @id
);
206 glBindTexture(GL_TEXTURE_2D
, id
);
207 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
208 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
209 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nil);
210 glBindTexture(GL_TEXTURE_2D
, 0);
215 function r_Textures_AllocAtlas (): TGLAtlas
;
216 var i
: Integer; id
: GLuint
;
219 id
:= r_Textures_AllocHWTexture(maxTileSize
, maxTileSize
);
223 SetLength(atl
, i
+ 1);
224 atl
[i
] := TGLAtlas
.Create(maxTileSize
, maxTileSize
, id
);
229 function r_Textures_AllocNode (w
, h
: Integer): TGLAtlasNode
;
230 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
236 while (i
>= 0) and (n
= nil) do
238 n
:= atl
[i
].Alloc(w
, h
);
244 a
:= r_Textures_AllocAtlas();
251 (* --------- TGLTexture --------- *)
253 destructor TGLTexture
.Destroy
;
256 if self
.mTile
<> nil then
258 for i
:= 0 to High(self
.mTile
) do
260 if self
.mTile
[i
] <> nil then
262 self
.mTile
[i
].Dealloc
;
263 self
.mTile
[i
] := nil;
271 function TGLTexture
.GetLines (): Integer;
273 ASSERT(self
.mTile
<> nil);
274 result
:= Length(self
.mTile
) div self
.mCols
277 function TGLTexture
.GetTile (col
, line
: Integer): TGLAtlasNode
;
281 ASSERT(col
<= mCols
);
282 ASSERT(self
.mTile
<> nil);
283 i
:= line
* mCols
+ col
;
285 ASSERT(i
< Length(mTile
));
287 ASSERT(result
<> nil)
290 function r_Textures_Alloc (w
, h
: Integer): TGLTexture
;
291 var x
, y
, mw
, mh
, cols
, lines
: Integer; t
: TGLTexture
;
295 cols
:= (w
+ maxTileSize
- 1) div maxTileSize
;
296 lines
:= (h
+ maxTileSize
- 1) div maxTileSize
;
297 t
:= TGLTexture
.Create
;
301 // t.mLines := lines;
302 SetLength(t
.mTile
, cols
* lines
);
303 for y
:= 0 to lines
- 1 do
305 mh
:= Min(maxTileSize
, h
- y
* maxTileSize
);
307 for x
:= 0 to cols
- 1 do
309 mw
:= Min(maxTileSize
, w
- x
* maxTileSize
);
311 t
.mTile
[y
* cols
+ x
] := r_Textures_AllocNode(mw
, mh
);
317 (* --------- TGLMultiTexture --------- *)
319 destructor TGLMultiTexture
.Destroy
;
322 for i
:= 0 to self
.count
- 1 do
323 self
.mTexture
[i
].Free
;
324 self
.mTexture
:= nil;
328 function TGLMultiTexture
.GetWidth (): Integer;
330 result
:= self
.mTexture
[0].width
333 function TGLMultiTexture
.GetHeight (): Integer;
335 result
:= self
.mTexture
[0].height
338 function TGLMultiTexture
.GetCount (): Integer;
340 result
:= Length(self
.mTexture
)
343 function TGLMultiTexture
.GetTexture (i
: Integer): TGLTexture
;
346 ASSERT(i
< self
.count
);
347 result
:= self
.mTexture
[i
];
348 ASSERT(result
<> nil);
351 (* --------- Init / Fin --------- *)
353 function r_Textures_GetMaxHardwareSize (): Integer;
356 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, @size
);
357 if size
< 64 then size
:= 64;
358 //if size > 512 then size := 512;
363 procedure r_Textures_Initialize
;
365 maxTileSize
:= r_Textures_GetMaxHardwareSize();
368 procedure r_Textures_Finalize
;
373 for i
:= 0 to High(atl
) do
375 glDeleteTextures(1, @atl
[i
].id
);
383 function r_Textures_FixImageData (var img
: TImageData
): Boolean;
386 if ConvertImage(img
, TImageFormat
.ifA8R8G8B8
) then
387 if SwapChannels(img
, ChannelRed
, ChannelBlue
) then // wtf
391 function r_Textures_LoadFromImage (var img
: TImageData
): TGLTexture
;
392 var t
: TGLTexture
; n
: TGLAtlasNode
; c
: TDynImageDataArray
; cw
, ch
, i
, j
: LongInt;
395 if SplitImage(img
, c
, maxTileSize
, maxTileSize
, cw
, ch
, False) then
397 t
:= r_Textures_Alloc(img
.width
, img
.height
);
401 ASSERT(ch
= t
.lines
);
402 for j
:= 0 to ch
- 1 do
404 for i
:= 0 to cw
- 1 do
406 n
:= t
.GetTile(i
, j
);
408 r_Textures_UpdateNode(n
, c
[j
* cw
+ i
].bits
, 0, 0, n
.width
, n
.height
)
413 FreeImagesInArray(c
);
417 function r_Textures_LoadFromMemory (data
: Pointer; size
: LongInt): TGLTexture
;
421 if (data
<> nil) and (size
> 0) then
425 if LoadImageFromMemory(data
, size
, img
) then
426 if r_Textures_FixImageData(img
) then
427 result
:= r_Textures_LoadFromImage(img
)
434 function r_Textures_LoadFromFile (const filename
: AnsiString; log
: Boolean = True): TGLTexture
;
435 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
438 wadName
:= g_ExtractWadName(filename
);
439 wad
:= TWADFile
.Create();
440 if wad
.ReadFile(wadName
) then
442 resName
:= g_ExtractFilePathName(filename
);
443 if wad
.GetResource(resName
, data
, size
, log
) then
445 result
:= r_Textures_LoadFromMemory(data
, size
);
452 function r_Textures_LoadMultiFromImageAndInfo (var img
: TImageData
; w
, h
, c
: Integer; b
: Boolean): TGLMultiTexture
;
453 var t
: TImageData
; a
: array of TGLTexture
; i
: Integer; m
: TGLMultiTexture
;
460 for i
:= 0 to c
- 1 do
463 if NewImage(w
, h
, img
.Format
, t
) then
464 if CopyRect(img
, w
* i
, 0, w
, h
, t
, 0, 0) then
465 a
[i
] := r_Textures_LoadFromImage(t
);
469 m
:= TGLMultiTexture
.Create();
472 ASSERT(m
.mTexture
<> nil);
476 function r_Textures_LoadMultiFromDataAndInfo (data
: Pointer; size
: LongInt; w
, h
, c
: Integer; b
: Boolean): TGLMultiTexture
;
483 if (data
<> nil) and (size
> 0) then
487 if LoadImageFromMemory(data
, size
, img
) then
488 if r_Textures_FixImageData(img
) then
489 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
, b
)
496 function r_Textures_LoadMultiFromWad (wad
: TWADFile
): TGLMultiTexture
;
497 var data
: Pointer; size
: LongInt; TexRes
: AnsiString; w
, h
, c
: Integer; b
: Boolean; cfg
: TConfig
; img
: TImageData
;
501 if wad
.GetResource('TEXT/ANIM', data
, size
) then
503 cfg
:= TConfig
.CreateMem(data
, size
);
507 TexRes
:= cfg
.ReadStr('', 'resource', '');
508 w
:= cfg
.ReadInt('', 'framewidth', 0);
509 h
:= cfg
.ReadInt('', 'frameheight', 0);
510 c
:= cfg
.ReadInt('', 'framecount', 0);
511 b
:= cfg
.ReadBool('', 'backanim', false);
512 if (TexRes
<> '') and (w
> 0) and (h
> 0) and (c
> 0) then
514 if wad
.GetResource('TEXTURES/' + TexRes
, data
, size
) then
518 if LoadImageFromMemory(data
, size
, img
) then
519 if r_Textures_FixImageData(img
) then
520 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
, b
)
532 function r_Textures_LoadMultiFromMemory (data
: Pointer; size
: LongInt): TGLMultiTexture
;
533 var wad
: TWADFile
; t
: TGLTexture
; m
: TGLMultiTexture
;
536 if (data
<> nil) and (size
> 0) then
538 t
:= r_Textures_LoadFromMemory(data
, size
);
541 m
:= TGLMultiTexture
.Create();
542 SetLength(m
.mTexture
, 1);
544 m
.mBackanim
:= false;
547 else if IsWadData(data
, size
) then
549 wad
:= TWADFile
.Create();
550 if wad
.ReadMemory(data
, size
) then
552 result
:= r_Textures_LoadMultiFromWad(wad
);
559 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; log
: Boolean = True): TGLMultiTexture
;
560 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer; t
: TGLTexture
;
563 wadName
:= g_ExtractWadName(filename
);
564 wad
:= TWADFile
.Create();
565 if wad
.ReadFile(wadName
) then
567 resName
:= g_ExtractFilePathName(filename
);
568 if wad
.GetResource(resName
, data
, size
, log
) then
570 result
:= r_Textures_LoadMultiFromMemory(data
, size
);
577 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; backanim
: Boolean; log
: Boolean = True): TGLMultiTexture
;
578 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
584 wadName
:= g_ExtractWadName(filename
);
585 wad
:= TWADFile
.Create();
586 if wad
.ReadFile(wadName
) then
588 resName
:= g_ExtractFilePathName(filename
);
589 if wad
.GetResource(resName
, data
, size
, log
) then
591 result
:= r_Textures_LoadMultiFromDataAndInfo(data
, size
, w
, h
, count
, backanim
);
598 function r_Textures_GetRect (var img
: TImageData
): TRectWH
;
599 var i
, j
, w
, h
: Integer; done
: Boolean;
601 function IsVoid (i
, j
: Integer): Boolean; inline;
603 result
:= GetPixel32(img
, i
, j
).Channels
[3] = 0
610 (* trace x from right to left *)
611 done
:= false; i
:= 0;
612 while not done
and (i
< w
) do
615 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
616 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
621 (* trace y from up to down *)
622 done
:= false; j
:= 0;
623 while not done
and (j
< h
) do
626 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
627 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
632 (* trace x from right to left *)
633 done
:= false; i
:= w
- 1;
634 while not done
and (i
>= 0) do
637 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
638 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
639 result
.width
:= i
- result
.x
+ 1;
643 (* trace y from down to up *)
644 done
:= false; j
:= h
- 1;
645 while not done
and (j
>= 0) do
648 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
649 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
650 result
.height
:= j
- result
.y
+ 1;
655 function r_Textures_LoadStreamFromImage (var img
: TImageData
; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
656 var i
, x
, y
: Integer; t
: TImageData
;
662 ASSERT((st
<> nil) and (Length(st
) >= c
));
663 ASSERT((rs
= nil) or (Length(rs
) >= c
));
665 for i
:= 0 to c
- 1 do
671 if NewImage(w
, h
, img
.Format
, t
) then
673 if CopyRect(img
, x
* w
, y
* h
, w
, h
, t
, 0, 0) then
676 rs
[i
] := r_Textures_GetRect(t
);
677 st
[i
] := r_Textures_LoadFromImage(t
);
680 ASSERT(st
[i
] <> nil);
685 function r_Textures_LoadStreamFromMemory (data
: Pointer; size
: LongInt; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
): Boolean;
692 ASSERT((st
<> nil) and (Length(st
) >= c
));
693 ASSERT((rs
= nil) or (Length(rs
) >= c
));
695 if (data
<> nil) and (size
> 0) then
699 if LoadImageFromMemory(data
, size
, img
) then
701 if r_Textures_FixImageData(img
) then
703 result
:= r_Textures_LoadStreamFromImage(img
, w
, h
, c
, cw
, st
, rs
)
712 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; log
: Boolean = True): Boolean;
713 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
719 ASSERT((st
<> nil) and (Length(st
) >= count
));
720 ASSERT((rs
= nil) or (Length(rs
) >= count
));
722 wadName
:= g_ExtractWadName(filename
);
723 wad
:= TWADFile
.Create();
724 if wad
.ReadFile(wadName
) then
726 resName
:= g_ExtractFilePathName(filename
);
727 if wad
.GetResource(resName
, data
, size
, log
) then
729 result
:= r_Textures_LoadStreamFromMemory(data
, size
, w
, h
, count
, cw
, st
, rs
);
736 (* --------- TGLFont --------- *)
738 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; skipch
: Integer; log
: Boolean = true): TGLFont
;
739 var i
: Integer; st
: TGLTextureArray
; font
: TGLFont
; t
: TGLTexture
;
744 if r_Textures_LoadStreamFromFile(filename
, f
.w
, f
.h
, 256, 16, st
, nil, log
) then
751 st
[i
] := st
[(i
+ skipch
) mod 256];
752 st
[(i
+ skipch
) mod 256] := t
;
755 font
:= TGLFont
.Create();
762 destructor TGLFont
.Destroy
;
765 if self
.ch
<> nil then
766 for i
:= 0 to High(self
.ch
) do
771 function TGLFont
.GetChar (c
: AnsiChar): TGLTexture
;
773 result
:= self
.ch
[ORD(c
)];
776 function TGLFont
.GetWidth (c
: AnsiChar): Integer;
778 result
:= self
.info
.ch
[c
].w
;
780 result
:= self
.info
.w
;
783 function TGLFont
.GetMaxHeight (): Integer;
785 result
:= self
.info
.h
;
788 function TGLFont
.GetSpace (): Integer;
790 result
:= self
.info
.kern
;