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
)
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;
60 property filter
: Boolean read mFilter write mFilter
;
68 mTile
: array of TGLAtlasNode
;
73 destructor Destroy
; override;
75 function GetTile (col
, line
: Integer): TGLAtlasNode
;
77 function GetLines (): Integer; inline;
79 property width
: Integer read mWidth
;
80 property height
: Integer read mHeight
;
81 property cols
: Integer read mCols
;
82 property lines
: Integer read GetLines
;
83 property hints
: TGLHintsSet read mHints
;
84 property filter
: Boolean read mFilter write mFilter
;
87 TGLMultiTexture
= class
89 mTexture
: array of TGLTexture
;
92 destructor Destroy
; override;
94 function GetWidth (): Integer; inline;
95 function GetHeight (): Integer; inline;
96 function GetCount (): Integer; inline;
97 function GetTexture (i
: Integer): TGLTexture
; {inline;}
99 property width
: Integer read GetWidth
;
100 property height
: Integer read GetHeight
;
101 property count
: Integer read GetCount
;
104 TGLTextureArray
= array of TGLTexture
;
106 TRectArray
= array of TRectWH
;
108 TGLFont
= class sealed (TFont
)
114 destructor Destroy
; override;
115 function GetChar (c
: AnsiChar): TGLTexture
;
116 function GetWidth (c
: AnsiChar): Integer;
117 function GetMaxWidth (): Integer;
118 function GetMaxHeight (): Integer;
119 function GetSpace (): Integer;
122 TAnimTextInfo
= record
128 TConvProc
= function (x
: Integer): Integer;
130 procedure r_Textures_Initialize
;
131 procedure r_Textures_Finalize
;
133 function r_Textures_LoadFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLTexture
;
134 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
135 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
136 function r_Textures_LoadMultiTextFromFile (const filename
: AnsiString; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
138 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
; log
: Boolean = True): Boolean;
140 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; font2enc
: TConvProc
; log
: Boolean = true): TGLFont
;
142 procedure r_Textures_GL_Bind (id
: GLuint
);
149 e_log
, e_res
, WADReader
, Config
,
150 g_console
, // cvar declaration
151 Imaging
, ImagingTypes
, ImagingUtility
155 r_GL_MaxTexSize
: WORD;
156 r_GL_RepeatOpt
: Boolean;
157 maxTileSize
: Integer;
158 atl
, ratl
: array of TGLAtlas
;
159 currentTexture2D
: GLuint
;
161 procedure r_Textures_GL_Bind (id
: GLuint
);
163 if id
<> currentTexture2D
then
165 glBindTexture(GL_TEXTURE_2D
, id
);
166 currentTexture2D
:= id
;
170 (* --------- TGLAtlasNode --------- *)
172 constructor TGLAtlasNode
.Create (base
: TGLAtlas
);
179 destructor TGLAtlasNode
.Destroy
;
184 function TGLAtlasNode
.GetID (): GLuint
;
186 result
:= self
.base
.id
189 procedure r_Textures_UpdateNode (n
: TGLAtlasNode
; data
: Pointer; x
, y
, w
, h
: Integer);
193 ASSERT(n
.base
<> nil);
197 ASSERT(n
.l
+ x
+ w
- 1 <= n
.r
);
198 ASSERT(n
.t
+ y
+ h
- 1 <= n
.b
);
200 r_Textures_GL_Bind(n
.id
);
201 glTexSubImage2D(GL_TEXTURE_2D
, 0, n
.l
+ x
, n
.t
+ y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
202 r_Textures_GL_Bind(0);
205 (* --------- TGLAtlas --------- *)
207 constructor TGLAtlas
.Create (ww
, hh
: Integer; id
: GLuint
);
211 inherited Create(ww
, hh
);
213 self
.mFilter
:= false;
216 destructor TGLAtlas
.Destroy
;
221 function TGLAtlas
.CreateNode (): TGLAtlasNode
;
223 result
:= TGLAtlasNode
.Create(self
);
226 function TGLAtlas
.Alloc (ww
, hh
: Integer): TGLAtlasNode
;
228 result
:= TGLAtlasNode(inherited Alloc(ww
, hh
));
231 function r_Textures_AllocHWTexture (w
, h
: Integer): GLuint
;
234 glGenTextures(1, @id
);
237 r_Textures_GL_Bind(id
);
238 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
239 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
240 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
241 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
242 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nil);
243 r_Textures_GL_Bind(0);
248 function r_Textures_AllocAtlas (): TGLAtlas
;
249 var i
: Integer; id
: GLuint
;
252 id
:= r_Textures_AllocHWTexture(maxTileSize
, maxTileSize
);
256 SetLength(atl
, i
+ 1);
257 atl
[i
] := TGLAtlas
.Create(maxTileSize
, maxTileSize
, id
);
262 function r_Textures_AllocRepeatAtlas (w
, h
: Integer): TGLAtlas
;
263 var i
: Integer; id
: GLuint
;
266 id
:= r_Textures_AllocHWTexture(w
, h
);
270 SetLength(ratl
, i
+ 1);
271 ratl
[i
] := TGLAtlas
.Create(w
, h
, id
);
276 function r_Textures_AllocNode (w
, h
: Integer): TGLAtlasNode
;
277 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
283 while (i
>= 0) and (n
= nil) do
285 n
:= atl
[i
].Alloc(w
, h
);
291 a
:= r_Textures_AllocAtlas();
298 function r_Textures_AllocRepeatNode (w
, h
: Integer): TGLAtlasNode
;
299 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
305 while (i
>= 0) and (ratl
[i
] <> nil) do DEC(i
);
306 if i
>= 0 then a
:= ratl
[i
];
308 if a
= nil then a
:= r_Textures_AllocRepeatAtlas(w
, h
);
314 i
:= High(ratl
); while (i
>= 0) and (ratl
[i
] <> a
) do DEC(i
);
315 if i
>= 0 then ratl
[i
] := nil;
316 r_Common_FreeAndNil(a
);
322 (* --------- TGLTexture --------- *)
324 destructor TGLTexture
.Destroy
;
325 var i
: Integer; a
: TGLAtlas
;
327 if self
.mTile
<> nil then
329 if TGLHints
.txNoRepeat
in self
.hints
then (* non repeatable texture -> delete tiles only *)
331 for i
:= 0 to High(self
.mTile
) do
333 if self
.mTile
[i
] <> nil then
335 self
.mTile
[i
].Dealloc
;
340 else (* repeatable texture -> delete whole atlas *)
342 a
:= self
.mTile
[0].base
;
343 i
:= High(ratl
); while (i
>= 0) and (ratl
[i
] <> a
) do DEC(i
);
344 if i
>= 0 then ratl
[i
] := nil;
345 r_Common_FreeAndNil(a
);
347 SetLength(self
.mTile
, 0);
352 function TGLTexture
.GetLines (): Integer;
354 ASSERT(self
.mTile
<> nil);
355 result
:= Length(self
.mTile
) div self
.mCols
358 function TGLTexture
.GetTile (col
, line
: Integer): TGLAtlasNode
;
362 ASSERT(col
<= mCols
);
363 ASSERT(self
.mTile
<> nil);
364 i
:= line
* mCols
+ col
;
366 ASSERT(i
< Length(mTile
));
368 ASSERT(result
<> nil)
371 function r_Textures_Alloc (w
, h
: Integer; hints
: TGLHintsSet
): TGLTexture
;
372 var x
, y
, mw
, mh
, cols
, lines
: Integer; t
: TGLTexture
;
376 if TGLHints
.txNoRepeat
in hints
then
378 cols
:= (w
+ maxTileSize
- 1) div maxTileSize
;
379 lines
:= (h
+ maxTileSize
- 1) div maxTileSize
;
380 t
:= TGLTexture
.Create
;
384 // t.mLines := lines;
387 SetLength(t
.mTile
, cols
* lines
);
388 for y
:= 0 to lines
- 1 do
390 mh
:= Min(maxTileSize
, h
- y
* maxTileSize
);
392 for x
:= 0 to cols
- 1 do
394 mw
:= Min(maxTileSize
, w
- x
* maxTileSize
);
396 t
.mTile
[y
* cols
+ x
] := r_Textures_AllocNode(mw
, mh
);
402 t
:= TGLTexture
.Create
;
408 SetLength(t
.mTile
, 1);
409 t
.mTile
[0] := r_Textures_AllocRepeatNode(w
, h
);
414 (* --------- TGLMultiTexture --------- *)
416 destructor TGLMultiTexture
.Destroy
;
419 for i
:= 0 to self
.count
- 1 do
420 r_Common_FreeAndNil(self
.mTexture
[i
]);
421 SetLength(self
.mTexture
, 0);
425 function TGLMultiTexture
.GetWidth (): Integer;
427 result
:= self
.mTexture
[0].width
430 function TGLMultiTexture
.GetHeight (): Integer;
432 result
:= self
.mTexture
[0].height
435 function TGLMultiTexture
.GetCount (): Integer;
437 result
:= Length(self
.mTexture
)
440 function TGLMultiTexture
.GetTexture (i
: Integer): TGLTexture
;
443 ASSERT(i
< self
.count
);
444 result
:= self
.mTexture
[i
];
445 ASSERT(result
<> nil);
448 (* --------- Init / Fin --------- *)
450 function IsPOT (v
: LongWord): Boolean;
452 result
:= (v
<> 0) and ((v
and (v
- 1)) = 0)
455 function NextPOT (v
: LongWord): LongWord;
467 function r_Textures_GetMaxHardwareSize (): Integer;
470 if r_GL_MaxTexSize
<= 0 then
472 // auto, max possible reccomended by driver
473 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, @size
);
474 size
:= size
div 2; (* hack: on some devices max size may produce invalid texture *)
475 if size
< 64 then size
:= 64; (* at least 64x64 are guarantied by specification *)
480 if IsPOT(r_GL_MaxTexSize
) then
481 size
:= r_GL_MaxTexSize
483 size
:= NextPOT(r_GL_MaxTexSize
);
488 procedure r_Textures_Initialize
;
490 currentTexture2D
:= 0;
491 maxTileSize
:= r_Textures_GetMaxHardwareSize();
492 e_LogWritefln('Texture Tile Size: %s', [maxTileSize
]);
495 procedure r_Textures_Finalize
;
500 for i
:= 0 to High(atl
) do
502 if atl
[i
] <> nil then
504 glDeleteTextures(1, @atl
[i
].id
);
506 r_Common_FreeAndNil(atl
[i
]);
514 for i
:= 0 to High(ratl
) do
516 if ratl
[i
] <> nil then
518 glDeleteTextures(1, @ratl
[i
].id
);
520 r_Common_FreeAndNil(ratl
[i
]);
527 function r_Textures_FixImageData (var img
: TImageData
): Boolean;
530 if ConvertImage(img
, TImageFormat
.ifA8R8G8B8
) then
531 if SwapChannels(img
, ChannelRed
, ChannelBlue
) then // wtf
535 function r_Textures_ValidRepeatTexture (w
, h
: Integer; hints
: TGLHintsSet
): Boolean;
537 result
:= r_GL_RepeatOpt
and
538 not (TGLHints
.txNoRepeat
in hints
) and
539 (w
<= maxTileSize
) and
540 (h
<= maxTileSize
) and
545 function r_Textures_LoadFromImage (var img
: TImageData
; hints
: TGLHintsSet
): TGLTexture
; // !!!
546 var t
: TGLTexture
; n
: TGLAtlasNode
; c
: TDynImageDataArray
; cw
, ch
, i
, j
: LongInt;
549 if r_Textures_ValidRepeatTexture(img
.width
, img
.height
, hints
) then
551 t
:= r_Textures_Alloc(img
.width
, img
.height
, hints
- [TGLHints
.txNoRepeat
]);
554 n
:= t
.GetTile(0, 0);
556 r_Textures_UpdateNode(n
, img
.bits
, 0, 0, n
.width
, n
.height
);
560 else if SplitImage(img
, c
, maxTileSize
, maxTileSize
, cw
, ch
, False) then
562 t
:= r_Textures_Alloc(img
.width
, img
.height
, hints
+ [TGLHints
.txNoRepeat
]);
566 ASSERT(ch
= t
.lines
);
567 for j
:= 0 to ch
- 1 do
569 for i
:= 0 to cw
- 1 do
571 n
:= t
.GetTile(i
, j
);
573 r_Textures_UpdateNode(n
, c
[j
* cw
+ i
].bits
, 0, 0, n
.width
, n
.height
)
578 FreeImagesInArray(c
);
582 function r_Textures_LoadFromMemory (data
: Pointer; size
: LongInt; hints
: TGLHintsSet
): TGLTexture
;
586 if (data
<> nil) and (size
> 0) then
590 if LoadImageFromMemory(data
, size
, img
) then
591 if r_Textures_FixImageData(img
) then
592 result
:= r_Textures_LoadFromImage(img
, hints
)
599 function r_Textures_LoadFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLTexture
;
600 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
603 wadName
:= g_ExtractWadName(filename
);
604 wad
:= TWADFile
.Create();
605 if wad
.ReadFile(wadName
) then
607 resName
:= g_ExtractFilePathName(filename
);
608 if wad
.GetResource(resName
, data
, size
, log
) then
610 result
:= r_Textures_LoadFromMemory(data
, size
, hints
);
617 function r_Textures_LoadMultiFromImageAndInfo (var img
: TImageData
; w
, h
, c
: Integer; hints
: TGLHintsSet
): TGLMultiTexture
;
618 var t
: TImageData
; a
: array of TGLTexture
; i
: Integer; m
: TGLMultiTexture
;
625 for i
:= 0 to c
- 1 do
628 if NewImage(w
, h
, img
.Format
, t
) then
629 if CopyRect(img
, w
* i
, 0, w
, h
, t
, 0, 0) then
630 a
[i
] := r_Textures_LoadFromImage(t
, hints
);
634 m
:= TGLMultiTexture
.Create();
636 ASSERT(m
.mTexture
<> nil);
640 function r_Textures_LoadMultiFromDataAndInfo (data
: Pointer; size
: LongInt; w
, h
, c
: Integer; hints
: TGLHintsSet
): TGLMultiTexture
;
647 if (data
<> nil) and (size
> 0) then
651 if LoadImageFromMemory(data
, size
, img
) then
652 if r_Textures_FixImageData(img
) then
653 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
, hints
)
660 function r_Textures_LoadTextFromMemory (data
: Pointer; size
: LongInt; var text: TAnimTextInfo
): Boolean;
666 cfg
:= TConfig
.CreateMem(data
, size
);
669 text.name
:= cfg
.ReadStr('', 'resource', '');
670 text.w
:= cfg
.ReadInt('', 'framewidth', 0);
671 text.h
:= cfg
.ReadInt('', 'frameheight', 0);
672 text.anim
.loop
:= true;
673 text.anim
.delay
:= cfg
.ReadInt('', 'waitcount', 0);
674 text.anim
.frames
:= cfg
.ReadInt('', 'framecount', 0);
675 text.anim
.back
:= cfg
.ReadBool('', 'backanim', false);
676 if text.w
<= 0 then e_LogWritefln('Warning: bad animation width %s for %s', [text.w
, text.name
]);
677 if text.h
<= 0 then e_LogWritefln('Warning: bad animation height %s for %s', [text.h
, text.name
]);
678 if text.anim
.delay
<= 0 then e_LogWritefln('Warning: bad animation delay %s for %s', [text.anim
.delay
, text.name
]);
679 if text.anim
.frames
<= 0 then e_LogWritefln('Warning: bad animation frame count %s for %s', [text.anim
.frames
, text.name
]);
680 text.w
:= MAX(0, text.w
);
681 text.h
:= MAX(0, text.h
);
682 text.anim
.delay
:= MAX(1, text.anim
.delay
);
683 text.anim
.frames
:= MAX(1, text.anim
.frames
);
685 result
:= (text.name
<> '') and (text.w
> 0) and (text.h
> 0) and (text.anim
.delay
> 0) and (text.anim
.frames
> 0);
690 function r_Textures_LoadMultiFromWad (wad
: TWADFile
; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
): TGLMultiTexture
;
691 var data
: Pointer; size
: LongInt; img
: TImageData
;
695 if wad
.GetResource('TEXT/ANIM', data
, size
) then
697 if r_Textures_LoadTextFromMemory(data
, size
, txt
) then
700 if wad
.GetResource('TEXTURES/' + txt
.name
, data
, size
) then
704 if LoadImageFromMemory(data
, size
, img
) then
705 if r_Textures_FixImageData(img
) then
706 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, txt
.w
, txt
.h
, txt
.anim
.frames
, hints
);
718 function r_Textures_LoadMultiFromMemory (data
: Pointer; size
: LongInt; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
): TGLMultiTexture
;
719 var wad
: TWADFile
; t
: TGLTexture
; m
: TGLMultiTexture
;
722 if (data
<> nil) and (size
> 0) then
724 t
:= r_Textures_LoadFromMemory(data
, size
, hints
);
727 m
:= TGLMultiTexture
.Create();
728 SetLength(m
.mTexture
, 1);
733 txt
.anim
.loop
:= true;
735 txt
.anim
.frames
:= 1;
736 txt
.anim
.back
:= false;
739 else if IsWadData(data
, size
) then
741 wad
:= TWADFile
.Create();
742 if wad
.ReadMemory(data
, size
) then
744 result
:= r_Textures_LoadMultiFromWad(wad
, txt
, hints
);
751 function r_Textures_LoadMultiTextFromFile (const filename
: AnsiString; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
752 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
755 wadName
:= g_ExtractWadName(filename
);
756 wad
:= TWADFile
.Create();
757 if wad
.ReadFile(wadName
) then
759 resName
:= g_ExtractFilePathName(filename
);
760 if wad
.GetResource(resName
, data
, size
, log
) then
762 result
:= r_Textures_LoadMultiFromMemory(data
, size
, txt
, hints
);
769 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
770 var txt
: TAnimTextInfo
;
772 result
:= r_Textures_LoadMultiTextFromFile(filename
, txt
, hints
, log
);
775 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
776 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
782 wadName
:= g_ExtractWadName(filename
);
783 wad
:= TWADFile
.Create();
784 if wad
.ReadFile(wadName
) then
786 resName
:= g_ExtractFilePathName(filename
);
787 if wad
.GetResource(resName
, data
, size
, log
) then
789 result
:= r_Textures_LoadMultiFromDataAndInfo(data
, size
, w
, h
, count
, hints
);
796 function r_Textures_GetRect (var img
: TImageData
): TRectWH
;
797 var i
, j
, w
, h
: Integer; done
: Boolean;
799 function IsVoid (i
, j
: Integer): Boolean; inline;
801 result
:= GetPixel32(img
, i
, j
).Channels
[3] = 0
808 (* trace x from right to left *)
809 done
:= false; i
:= 0;
810 while not done
and (i
< w
) do
813 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
814 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
819 (* trace y from up to down *)
820 done
:= false; j
:= 0;
821 while not done
and (j
< h
) do
824 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
825 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
830 (* trace x from right to left *)
831 done
:= false; i
:= w
- 1;
832 while not done
and (i
>= 0) do
835 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
836 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
837 result
.width
:= i
- result
.x
+ 1;
841 (* trace y from down to up *)
842 done
:= false; j
:= h
- 1;
843 while not done
and (j
>= 0) do
846 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
847 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
848 result
.height
:= j
- result
.y
+ 1;
853 function r_Textures_LoadStreamFromImage (var img
: TImageData
; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
): Boolean;
854 var i
, x
, y
: Integer; t
: TImageData
;
860 ASSERT((st
<> nil) and (Length(st
) >= c
));
861 ASSERT((rs
= nil) or (Length(rs
) >= c
));
863 for i
:= 0 to c
- 1 do
869 if NewImage(w
, h
, img
.Format
, t
) then
871 if CopyRect(img
, x
* w
, y
* h
, w
, h
, t
, 0, 0) then
874 rs
[i
] := r_Textures_GetRect(t
);
875 st
[i
] := r_Textures_LoadFromImage(t
, hints
);
878 ASSERT(st
[i
] <> nil);
883 function r_Textures_LoadStreamFromMemory (data
: Pointer; size
: LongInt; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
): Boolean;
890 ASSERT((st
<> nil) and (Length(st
) >= c
));
891 ASSERT((rs
= nil) or (Length(rs
) >= c
));
893 if (data
<> nil) and (size
> 0) then
897 if LoadImageFromMemory(data
, size
, img
) then
899 if r_Textures_FixImageData(img
) then
901 result
:= r_Textures_LoadStreamFromImage(img
, w
, h
, c
, cw
, st
, rs
, hints
)
910 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
; log
: Boolean = True): Boolean;
911 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
917 ASSERT((st
<> nil) and (Length(st
) >= count
));
918 ASSERT((rs
= nil) or (Length(rs
) >= count
));
920 wadName
:= g_ExtractWadName(filename
);
921 wad
:= TWADFile
.Create();
922 if wad
.ReadFile(wadName
) then
924 resName
:= g_ExtractFilePathName(filename
);
925 if wad
.GetResource(resName
, data
, size
, log
) then
927 result
:= r_Textures_LoadStreamFromMemory(data
, size
, w
, h
, count
, cw
, st
, rs
, hints
);
934 (* --------- TGLFont --------- *)
936 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; font2enc
: TConvProc
; log
: Boolean = true): TGLFont
;
937 var i
, ch
: Integer; st
, stch
: TGLTextureArray
; font
: TGLFont
;
941 if r_Textures_LoadStreamFromFile(filename
, f
.w
, f
.h
, 256, 16, st
, nil, [TGLHints
.txNoRepeat
], log
) then
943 font
:= TGLFont
.Create();
946 if Assigned(font2enc
) then
948 SetLength(stch
, 256);
952 ASSERT((ch
>= 0) and (ch
<= 255));
962 destructor TGLFont
.Destroy
;
965 if self
.ch
<> nil then
966 for i
:= 0 to High(self
.ch
) do
971 function TGLFont
.GetChar (c
: AnsiChar): TGLTexture
;
973 result
:= self
.ch
[ORD(c
)];
976 function TGLFont
.GetWidth (c
: AnsiChar): Integer;
978 result
:= self
.info
.ch
[c
].w
;
980 result
:= self
.info
.w
;
981 if self
.info
.kern
< 0 then
982 result
:= result
+ self
.info
.kern
;
985 function TGLFont
.GetMaxWidth (): Integer;
987 result
:= self
.info
.w
;
988 if self
.info
.kern
< 0 then
989 result
:= result
+ self
.info
.kern
;
992 function TGLFont
.GetMaxHeight (): Integer;
994 result
:= self
.info
.h
;
997 function TGLFont
.GetSpace (): Integer;
999 result
:= self
.info
.kern
;
1003 conRegVar('r_gl_maxtexsize', @r_GL_MaxTexSize
, '', '');
1004 conRegVar('r_gl_repeat', @r_GL_RepeatOpt
, '', '');
1005 r_GL_MaxTexSize
:= 0; // default is automatic value
1006 r_GL_RepeatOpt
:= true;