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}
21 {$I ../../../nogl/noGLuses.inc}
22 g_base
, g_animations
, // TRectHW, TAnimInfo
28 TGLHints
= (txNoRepeat
);
29 TGLHintsSet
= set of TGLHints
;
33 TGLAtlasNode
= class (TAtlasNode
)
38 constructor Create (base
: TGLAtlas
);
39 destructor Destroy
; override;
41 function GetID (): GLuint
;
43 property base
: TGLAtlas read mBase
;
44 property id
: GLuint read GetID
;
47 TGLAtlas
= class (TAtlas
)
52 constructor Create (ww
, hh
: Integer; id
: GLuint
);
53 destructor Destroy
; override;
55 function CreateNode (): TGLAtlasNode
; override;
56 function Alloc (ww
, hh
: Integer): TGLAtlasNode
; overload
;
58 property id
: GLuint read mID write mID default
0;
66 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
;
80 property hints
: TGLHintsSet read mHints
;
83 TGLMultiTexture
= class
85 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
;
100 TGLTextureArray
= array of TGLTexture
;
102 TRectArray
= array of TRectWH
;
104 TGLFont
= class sealed (TFont
)
110 destructor Destroy
; override;
111 function GetChar (c
: AnsiChar): TGLTexture
;
112 function GetWidth (c
: AnsiChar): Integer;
113 function GetMaxWidth (): Integer;
114 function GetMaxHeight (): Integer;
115 function GetSpace (): Integer;
118 TAnimTextInfo
= record
124 TConvProc
= function (x
: Integer): Integer;
126 procedure r_Textures_Initialize
;
127 procedure r_Textures_Finalize
;
129 function r_Textures_LoadFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLTexture
;
130 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
131 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
132 function r_Textures_LoadMultiTextFromFile (const filename
: AnsiString; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
134 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
; log
: Boolean = True): Boolean;
136 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; font2enc
: TConvProc
; log
: Boolean = true): TGLFont
;
138 procedure r_Textures_GL_Bind (id
: GLuint
);
145 e_log
, e_res
, WADReader
, Config
,
146 g_console
, // cvar declaration
147 Imaging
, ImagingTypes
, ImagingUtility
151 r_GL_MaxTexSize
: WORD;
152 r_GL_RepeatOpt
: Boolean;
153 maxTileSize
: Integer;
154 atl
, ratl
: array of TGLAtlas
;
155 currentTexture2D
: GLuint
;
157 procedure r_Textures_GL_Bind (id
: GLuint
);
159 if id
<> currentTexture2D
then
161 glBindTexture(GL_TEXTURE_2D
, id
);
162 currentTexture2D
:= id
;
166 (* --------- TGLAtlasNode --------- *)
168 constructor TGLAtlasNode
.Create (base
: TGLAtlas
);
175 destructor TGLAtlasNode
.Destroy
;
180 function TGLAtlasNode
.GetID (): GLuint
;
182 result
:= self
.base
.id
185 procedure r_Textures_UpdateNode (n
: TGLAtlasNode
; data
: Pointer; x
, y
, w
, h
: Integer);
189 ASSERT(n
.base
<> nil);
193 ASSERT(n
.l
+ x
+ w
- 1 <= n
.r
);
194 ASSERT(n
.t
+ y
+ h
- 1 <= n
.b
);
196 r_Textures_GL_Bind(n
.id
);
197 glTexSubImage2D(GL_TEXTURE_2D
, 0, n
.l
+ x
, n
.t
+ y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
198 r_Textures_GL_Bind(0);
201 (* --------- TGLAtlas --------- *)
203 constructor TGLAtlas
.Create (ww
, hh
: Integer; id
: GLuint
);
207 inherited Create(ww
, hh
);
211 destructor TGLAtlas
.Destroy
;
216 function TGLAtlas
.CreateNode (): TGLAtlasNode
;
218 result
:= TGLAtlasNode
.Create(self
);
221 function TGLAtlas
.Alloc (ww
, hh
: Integer): TGLAtlasNode
;
223 result
:= TGLAtlasNode(inherited Alloc(ww
, hh
));
226 function r_Textures_AllocHWTexture (w
, h
: Integer): GLuint
;
229 glGenTextures(1, @id
);
232 r_Textures_GL_Bind(id
);
233 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
234 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
235 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
236 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
237 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nil);
238 r_Textures_GL_Bind(0);
243 function r_Textures_AllocAtlas (): TGLAtlas
;
244 var i
: Integer; id
: GLuint
;
247 id
:= r_Textures_AllocHWTexture(maxTileSize
, maxTileSize
);
251 SetLength(atl
, i
+ 1);
252 atl
[i
] := TGLAtlas
.Create(maxTileSize
, maxTileSize
, id
);
257 function r_Textures_AllocRepeatAtlas (w
, h
: Integer): TGLAtlas
;
258 var i
: Integer; id
: GLuint
;
261 id
:= r_Textures_AllocHWTexture(w
, h
);
265 SetLength(ratl
, i
+ 1);
266 ratl
[i
] := TGLAtlas
.Create(w
, h
, id
);
271 function r_Textures_AllocNode (w
, h
: Integer): TGLAtlasNode
;
272 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
278 while (i
>= 0) and (n
= nil) do
280 n
:= atl
[i
].Alloc(w
, h
);
286 a
:= r_Textures_AllocAtlas();
293 function r_Textures_AllocRepeatNode (w
, h
: Integer): TGLAtlasNode
;
294 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
300 while (i
>= 0) and (ratl
[i
] <> nil) do DEC(i
);
301 if i
>= 0 then a
:= ratl
[i
];
303 if a
= nil then a
:= r_Textures_AllocRepeatAtlas(w
, h
);
309 i
:= High(ratl
); while (i
>= 0) and (ratl
[i
] <> a
) do DEC(i
);
310 if i
>= 0 then ratl
[i
] := nil;
311 r_Common_FreeAndNil(a
);
317 (* --------- TGLTexture --------- *)
319 destructor TGLTexture
.Destroy
;
320 var i
: Integer; a
: TGLAtlas
;
322 if self
.mTile
<> nil then
324 if TGLHints
.txNoRepeat
in self
.hints
then (* non repeatable texture -> delete tiles only *)
326 for i
:= 0 to High(self
.mTile
) do
328 if self
.mTile
[i
] <> nil then
330 self
.mTile
[i
].Dealloc
;
335 else (* repeatable texture -> delete whole atlas *)
337 a
:= self
.mTile
[0].base
;
338 i
:= High(ratl
); while (i
>= 0) and (ratl
[i
] <> a
) do DEC(i
);
339 if i
>= 0 then ratl
[i
] := nil;
340 r_Common_FreeAndNil(a
);
342 SetLength(self
.mTile
, 0);
347 function TGLTexture
.GetLines (): Integer;
349 ASSERT(self
.mTile
<> nil);
350 result
:= Length(self
.mTile
) div self
.mCols
353 function TGLTexture
.GetTile (col
, line
: Integer): TGLAtlasNode
;
357 ASSERT(col
<= mCols
);
358 ASSERT(self
.mTile
<> nil);
359 i
:= line
* mCols
+ col
;
361 ASSERT(i
< Length(mTile
));
363 ASSERT(result
<> nil)
366 function r_Textures_Alloc (w
, h
: Integer; hints
: TGLHintsSet
): TGLTexture
;
367 var x
, y
, mw
, mh
, cols
, lines
: Integer; t
: TGLTexture
;
371 if TGLHints
.txNoRepeat
in hints
then
373 cols
:= (w
+ maxTileSize
- 1) div maxTileSize
;
374 lines
:= (h
+ maxTileSize
- 1) div maxTileSize
;
375 t
:= TGLTexture
.Create
;
379 // t.mLines := lines;
381 SetLength(t
.mTile
, cols
* lines
);
382 for y
:= 0 to lines
- 1 do
384 mh
:= Min(maxTileSize
, h
- y
* maxTileSize
);
386 for x
:= 0 to cols
- 1 do
388 mw
:= Min(maxTileSize
, w
- x
* maxTileSize
);
390 t
.mTile
[y
* cols
+ x
] := r_Textures_AllocNode(mw
, mh
);
396 t
:= TGLTexture
.Create
;
402 SetLength(t
.mTile
, 1);
403 t
.mTile
[0] := r_Textures_AllocRepeatNode(w
, h
);
408 (* --------- TGLMultiTexture --------- *)
410 destructor TGLMultiTexture
.Destroy
;
413 for i
:= 0 to self
.count
- 1 do
414 r_Common_FreeAndNil(self
.mTexture
[i
]);
415 SetLength(self
.mTexture
, 0);
419 function TGLMultiTexture
.GetWidth (): Integer;
421 result
:= self
.mTexture
[0].width
424 function TGLMultiTexture
.GetHeight (): Integer;
426 result
:= self
.mTexture
[0].height
429 function TGLMultiTexture
.GetCount (): Integer;
431 result
:= Length(self
.mTexture
)
434 function TGLMultiTexture
.GetTexture (i
: Integer): TGLTexture
;
437 ASSERT(i
< self
.count
);
438 result
:= self
.mTexture
[i
];
439 ASSERT(result
<> nil);
442 (* --------- Init / Fin --------- *)
444 function IsPOT (v
: LongWord): Boolean;
446 result
:= (v
<> 0) and ((v
and (v
- 1)) = 0)
449 function NextPOT (v
: LongWord): LongWord;
461 function r_Textures_GetMaxHardwareSize (): Integer;
464 if r_GL_MaxTexSize
<= 0 then
466 // auto, max possible reccomended by driver
467 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, @size
);
468 size
:= size
div 2; (* hack: on some devices max size may produce invalid texture *)
469 if size
< 64 then size
:= 64; (* at least 64x64 are guarantied by specification *)
474 if IsPOT(r_GL_MaxTexSize
) then
475 size
:= r_GL_MaxTexSize
477 size
:= NextPOT(r_GL_MaxTexSize
);
482 procedure r_Textures_Initialize
;
484 currentTexture2D
:= 0;
485 maxTileSize
:= r_Textures_GetMaxHardwareSize();
486 e_LogWritefln('Texture Tile Size: %s', [maxTileSize
]);
489 procedure r_Textures_Finalize
;
494 for i
:= 0 to High(atl
) do
496 if atl
[i
] <> nil then
498 glDeleteTextures(1, @atl
[i
].id
);
500 r_Common_FreeAndNil(atl
[i
]);
508 for i
:= 0 to High(ratl
) do
510 if ratl
[i
] <> nil then
512 glDeleteTextures(1, @ratl
[i
].id
);
514 r_Common_FreeAndNil(ratl
[i
]);
521 function r_Textures_FixImageData (var img
: TImageData
): Boolean;
524 if ConvertImage(img
, TImageFormat
.ifA8R8G8B8
) then
525 if SwapChannels(img
, ChannelRed
, ChannelBlue
) then // wtf
529 function r_Textures_ValidRepeatTexture (w
, h
: Integer; hints
: TGLHintsSet
): Boolean;
531 result
:= r_GL_RepeatOpt
and
532 not (TGLHints
.txNoRepeat
in hints
) and
533 (w
<= maxTileSize
) and
534 (h
<= maxTileSize
) and
539 function r_Textures_LoadFromImage (var img
: TImageData
; hints
: TGLHintsSet
): TGLTexture
; // !!!
540 var t
: TGLTexture
; n
: TGLAtlasNode
; c
: TDynImageDataArray
; cw
, ch
, i
, j
: LongInt;
543 if r_Textures_ValidRepeatTexture(img
.width
, img
.height
, hints
) then
545 t
:= r_Textures_Alloc(img
.width
, img
.height
, hints
- [TGLHints
.txNoRepeat
]);
548 n
:= t
.GetTile(0, 0);
550 r_Textures_UpdateNode(n
, img
.bits
, 0, 0, n
.width
, n
.height
);
554 else if SplitImage(img
, c
, maxTileSize
, maxTileSize
, cw
, ch
, False) then
556 t
:= r_Textures_Alloc(img
.width
, img
.height
, hints
+ [TGLHints
.txNoRepeat
]);
560 ASSERT(ch
= t
.lines
);
561 for j
:= 0 to ch
- 1 do
563 for i
:= 0 to cw
- 1 do
565 n
:= t
.GetTile(i
, j
);
567 r_Textures_UpdateNode(n
, c
[j
* cw
+ i
].bits
, 0, 0, n
.width
, n
.height
)
572 FreeImagesInArray(c
);
576 function r_Textures_LoadFromMemory (data
: Pointer; size
: LongInt; hints
: TGLHintsSet
): TGLTexture
;
580 if (data
<> nil) and (size
> 0) then
584 if LoadImageFromMemory(data
, size
, img
) then
585 if r_Textures_FixImageData(img
) then
586 result
:= r_Textures_LoadFromImage(img
, hints
)
593 function r_Textures_LoadFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLTexture
;
594 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
597 wadName
:= g_ExtractWadName(filename
);
598 wad
:= TWADFile
.Create();
599 if wad
.ReadFile(wadName
) then
601 resName
:= g_ExtractFilePathName(filename
);
602 if wad
.GetResource(resName
, data
, size
, log
) then
604 result
:= r_Textures_LoadFromMemory(data
, size
, hints
);
611 function r_Textures_LoadMultiFromImageAndInfo (var img
: TImageData
; w
, h
, c
: Integer; hints
: TGLHintsSet
): TGLMultiTexture
;
612 var t
: TImageData
; a
: array of TGLTexture
; i
: Integer; m
: TGLMultiTexture
;
619 for i
:= 0 to c
- 1 do
622 if NewImage(w
, h
, img
.Format
, t
) then
623 if CopyRect(img
, w
* i
, 0, w
, h
, t
, 0, 0) then
624 a
[i
] := r_Textures_LoadFromImage(t
, hints
);
628 m
:= TGLMultiTexture
.Create();
630 ASSERT(m
.mTexture
<> nil);
634 function r_Textures_LoadMultiFromDataAndInfo (data
: Pointer; size
: LongInt; w
, h
, c
: Integer; hints
: TGLHintsSet
): TGLMultiTexture
;
641 if (data
<> nil) and (size
> 0) then
645 if LoadImageFromMemory(data
, size
, img
) then
646 if r_Textures_FixImageData(img
) then
647 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
, hints
)
654 function r_Textures_LoadTextFromMemory (data
: Pointer; size
: LongInt; var text: TAnimTextInfo
): Boolean;
660 cfg
:= TConfig
.CreateMem(data
, size
);
663 text.name
:= cfg
.ReadStr('', 'resource', '');
664 text.w
:= cfg
.ReadInt('', 'framewidth', 0);
665 text.h
:= cfg
.ReadInt('', 'frameheight', 0);
666 text.anim
.loop
:= true;
667 text.anim
.delay
:= cfg
.ReadInt('', 'waitcount', 0);
668 text.anim
.frames
:= cfg
.ReadInt('', 'framecount', 0);
669 text.anim
.back
:= cfg
.ReadBool('', 'backanim', false);
670 if text.w
<= 0 then e_LogWritefln('Warning: bad animation width %s for %s', [text.w
, text.name
]);
671 if text.h
<= 0 then e_LogWritefln('Warning: bad animation height %s for %s', [text.h
, text.name
]);
672 if text.anim
.delay
<= 0 then e_LogWritefln('Warning: bad animation delay %s for %s', [text.anim
.delay
, text.name
]);
673 if text.anim
.frames
<= 0 then e_LogWritefln('Warning: bad animation frame count %s for %s', [text.anim
.frames
, text.name
]);
674 text.w
:= MAX(0, text.w
);
675 text.h
:= MAX(0, text.h
);
676 text.anim
.delay
:= MAX(1, text.anim
.delay
);
677 text.anim
.frames
:= MAX(1, text.anim
.frames
);
679 result
:= (text.name
<> '') and (text.w
> 0) and (text.h
> 0) and (text.anim
.delay
> 0) and (text.anim
.frames
> 0);
684 function r_Textures_LoadMultiFromWad (wad
: TWADFile
; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
): TGLMultiTexture
;
685 var data
: Pointer; size
: LongInt; img
: TImageData
;
689 if wad
.GetResource('TEXT/ANIM', data
, size
) then
691 if r_Textures_LoadTextFromMemory(data
, size
, txt
) then
694 if wad
.GetResource('TEXTURES/' + txt
.name
, data
, size
) then
698 if LoadImageFromMemory(data
, size
, img
) then
699 if r_Textures_FixImageData(img
) then
700 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, txt
.w
, txt
.h
, txt
.anim
.frames
, hints
);
712 function r_Textures_LoadMultiFromMemory (data
: Pointer; size
: LongInt; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
): TGLMultiTexture
;
713 var wad
: TWADFile
; t
: TGLTexture
; m
: TGLMultiTexture
;
716 if (data
<> nil) and (size
> 0) then
718 t
:= r_Textures_LoadFromMemory(data
, size
, hints
);
721 m
:= TGLMultiTexture
.Create();
722 SetLength(m
.mTexture
, 1);
727 txt
.anim
.loop
:= true;
729 txt
.anim
.frames
:= 1;
730 txt
.anim
.back
:= false;
733 else if IsWadData(data
, size
) then
735 wad
:= TWADFile
.Create();
736 if wad
.ReadMemory(data
, size
) then
738 result
:= r_Textures_LoadMultiFromWad(wad
, txt
, hints
);
745 function r_Textures_LoadMultiTextFromFile (const filename
: AnsiString; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
746 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
749 wadName
:= g_ExtractWadName(filename
);
750 wad
:= TWADFile
.Create();
751 if wad
.ReadFile(wadName
) then
753 resName
:= g_ExtractFilePathName(filename
);
754 if wad
.GetResource(resName
, data
, size
, log
) then
756 result
:= r_Textures_LoadMultiFromMemory(data
, size
, txt
, hints
);
763 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
764 var txt
: TAnimTextInfo
;
766 result
:= r_Textures_LoadMultiTextFromFile(filename
, txt
, hints
, log
);
769 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
770 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
776 wadName
:= g_ExtractWadName(filename
);
777 wad
:= TWADFile
.Create();
778 if wad
.ReadFile(wadName
) then
780 resName
:= g_ExtractFilePathName(filename
);
781 if wad
.GetResource(resName
, data
, size
, log
) then
783 result
:= r_Textures_LoadMultiFromDataAndInfo(data
, size
, w
, h
, count
, hints
);
790 function r_Textures_GetRect (var img
: TImageData
): TRectWH
;
791 var i
, j
, w
, h
: Integer; done
: Boolean;
793 function IsVoid (i
, j
: Integer): Boolean; inline;
795 result
:= GetPixel32(img
, i
, j
).Channels
[3] = 0
802 (* trace x from right to left *)
803 done
:= false; i
:= 0;
804 while not done
and (i
< w
) do
807 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
808 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
813 (* trace y from up to down *)
814 done
:= false; j
:= 0;
815 while not done
and (j
< h
) do
818 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
819 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
824 (* trace x from right to left *)
825 done
:= false; i
:= w
- 1;
826 while not done
and (i
>= 0) do
829 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
830 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
831 result
.width
:= i
- result
.x
+ 1;
835 (* trace y from down to up *)
836 done
:= false; j
:= h
- 1;
837 while not done
and (j
>= 0) do
840 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
841 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
842 result
.height
:= j
- result
.y
+ 1;
847 function r_Textures_LoadStreamFromImage (var img
: TImageData
; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
): Boolean;
848 var i
, x
, y
: Integer; t
: TImageData
;
854 ASSERT((st
<> nil) and (Length(st
) >= c
));
855 ASSERT((rs
= nil) or (Length(rs
) >= c
));
857 for i
:= 0 to c
- 1 do
863 if NewImage(w
, h
, img
.Format
, t
) then
865 if CopyRect(img
, x
* w
, y
* h
, w
, h
, t
, 0, 0) then
868 rs
[i
] := r_Textures_GetRect(t
);
869 st
[i
] := r_Textures_LoadFromImage(t
, hints
);
872 ASSERT(st
[i
] <> nil);
877 function r_Textures_LoadStreamFromMemory (data
: Pointer; size
: LongInt; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
): Boolean;
884 ASSERT((st
<> nil) and (Length(st
) >= c
));
885 ASSERT((rs
= nil) or (Length(rs
) >= c
));
887 if (data
<> nil) and (size
> 0) then
891 if LoadImageFromMemory(data
, size
, img
) then
893 if r_Textures_FixImageData(img
) then
895 result
:= r_Textures_LoadStreamFromImage(img
, w
, h
, c
, cw
, st
, rs
, hints
)
904 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
; log
: Boolean = True): Boolean;
905 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
911 ASSERT((st
<> nil) and (Length(st
) >= count
));
912 ASSERT((rs
= nil) or (Length(rs
) >= count
));
914 wadName
:= g_ExtractWadName(filename
);
915 wad
:= TWADFile
.Create();
916 if wad
.ReadFile(wadName
) then
918 resName
:= g_ExtractFilePathName(filename
);
919 if wad
.GetResource(resName
, data
, size
, log
) then
921 result
:= r_Textures_LoadStreamFromMemory(data
, size
, w
, h
, count
, cw
, st
, rs
, hints
);
928 (* --------- TGLFont --------- *)
930 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; font2enc
: TConvProc
; log
: Boolean = true): TGLFont
;
931 var i
, ch
: Integer; st
, stch
: TGLTextureArray
; font
: TGLFont
;
935 if r_Textures_LoadStreamFromFile(filename
, f
.w
, f
.h
, 256, 16, st
, nil, [TGLHints
.txNoRepeat
], log
) then
937 font
:= TGLFont
.Create();
940 if Assigned(font2enc
) then
942 SetLength(stch
, 256);
946 ASSERT((ch
>= 0) and (ch
<= 255));
956 destructor TGLFont
.Destroy
;
959 if self
.ch
<> nil then
960 for i
:= 0 to High(self
.ch
) do
965 function TGLFont
.GetChar (c
: AnsiChar): TGLTexture
;
967 result
:= self
.ch
[ORD(c
)];
970 function TGLFont
.GetWidth (c
: AnsiChar): Integer;
972 result
:= self
.info
.ch
[c
].w
;
974 result
:= self
.info
.w
;
975 if self
.info
.kern
< 0 then
976 result
:= result
+ self
.info
.kern
;
979 function TGLFont
.GetMaxWidth (): Integer;
981 result
:= self
.info
.w
;
982 if self
.info
.kern
< 0 then
983 result
:= result
+ self
.info
.kern
;
986 function TGLFont
.GetMaxHeight (): Integer;
988 result
:= self
.info
.h
;
991 function TGLFont
.GetSpace (): Integer;
993 result
:= self
.info
.kern
;
997 conRegVar('r_gl_maxtexsize', @r_GL_MaxTexSize
, '', '');
998 conRegVar('r_gl_repeat', @r_GL_RepeatOpt
, '', '');
999 r_GL_MaxTexSize
:= 0; // default is automatic value
1000 r_GL_RepeatOpt
:= true;