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 function r_Textures_GL_GetError (msg
: AnsiString): Boolean;
162 var code
: GLenum
; s
: AnsiString;
164 code
:= glGetError();
165 if code
<> GL_NO_ERROR
then
168 GL_INVALID_ENUM
: s
:= 'GL_INVALID_ENUM';
169 GL_INVALID_VALUE
: s
:= 'GL_INVALID_VALUE';
170 GL_INVALID_OPERATION
: s
:= 'GL_INVALID_OPERATION';
171 GL_STACK_OVERFLOW
: s
:= 'GL_STACK_OVERFLOW';
172 GL_STACK_UNDERFLOW
: s
:= 'GL_STACK_UNDERFLOW';
173 GL_OUT_OF_MEMORY
: s
:= 'GL_OUT_OF_MEMORY';
174 GL_TABLE_TOO_LARGE
: s
:= 'GL_TABLE_TOO_LARGE';
178 e_LogWritefln('%s: %s', [msg
, s
])
180 e_LogWritefln('%s: error code %s', [msg
, code
]);
182 result
:= code
<> GL_NO_ERROR
;
185 procedure r_Textures_GL_ClearError
;
189 code
:= glGetError();
190 until code
= GL_NO_ERROR
;
193 procedure r_Textures_GL_Bind (id
: GLuint
);
195 if id
<> currentTexture2D
then
197 glBindTexture(GL_TEXTURE_2D
, id
);
198 currentTexture2D
:= id
;
202 function r_Textures_GL_BindAndCheck (id
: GLuint
): Boolean;
205 if id
<> currentTexture2D
then
207 r_Textures_GL_ClearError
;
208 glBindTexture(GL_TEXTURE_2D
, id
);
209 result
:= not r_Textures_GL_GetError('failed to bind texture');
210 if result
= true then
211 currentTexture2D
:= id
;
215 (* --------- TGLAtlasNode --------- *)
217 constructor TGLAtlasNode
.Create (base
: TGLAtlas
);
224 destructor TGLAtlasNode
.Destroy
;
229 function TGLAtlasNode
.GetID (): GLuint
;
231 result
:= self
.base
.id
234 function r_Textures_UpdateNode (n
: TGLAtlasNode
; data
: Pointer; x
, y
, w
, h
: Integer): Boolean;
238 ASSERT(n
.base
<> nil);
242 ASSERT(n
.l
+ x
+ w
- 1 <= n
.r
);
243 ASSERT(n
.t
+ y
+ h
- 1 <= n
.b
);
246 if r_Textures_GL_BindAndCheck(n
.id
) then
248 glTexSubImage2D(GL_TEXTURE_2D
, 0, n
.l
+ x
, n
.t
+ y
, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
249 result
:= not r_Textures_GL_GetError('failed to update atlas node');
250 r_Textures_GL_Bind(0);
254 (* --------- TGLAtlas --------- *)
256 constructor TGLAtlas
.Create (ww
, hh
: Integer; id
: GLuint
);
260 inherited Create(ww
, hh
);
262 self
.mFilter
:= false;
265 destructor TGLAtlas
.Destroy
;
270 function TGLAtlas
.CreateNode (): TGLAtlasNode
;
272 result
:= TGLAtlasNode
.Create(self
);
275 function TGLAtlas
.Alloc (ww
, hh
: Integer): TGLAtlasNode
;
277 result
:= TGLAtlasNode(inherited Alloc(ww
, hh
));
280 procedure r_Textures_AllocHWTexture (w
, h
: Integer; out id
: GLuint
; out ok
: Boolean);
282 id
:= 0; ok
:= false;
283 r_Textures_GL_ClearError
;
284 glGenTextures(1, @id
);
285 if not r_Textures_GL_GetError('failed to allocate texture id') then
287 if r_Textures_GL_BindAndCheck(id
) then
289 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
290 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
291 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
292 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
293 r_Textures_GL_ClearError
;
294 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nil);
295 ok
:= not r_Textures_GL_GetError('failed to allocate hardware texture');
296 r_Textures_GL_Bind(0);
300 glDeleteTextures(1, @id
);
306 function r_Textures_AllocAtlas (): TGLAtlas
;
307 var i
: Integer; id
: GLuint
; ok
: Boolean;
310 r_Textures_AllocHWTexture(maxTileSize
, maxTileSize
, id
, ok
);
314 SetLength(atl
, i
+ 1);
315 atl
[i
] := TGLAtlas
.Create(maxTileSize
, maxTileSize
, id
);
320 function r_Textures_AllocRepeatAtlas (w
, h
: Integer): TGLAtlas
;
321 var i
: Integer; id
: GLuint
; ok
: Boolean;
324 r_Textures_AllocHWTexture(w
, h
, id
, ok
);
328 SetLength(ratl
, i
+ 1);
329 ratl
[i
] := TGLAtlas
.Create(w
, h
, id
);
334 function r_Textures_AllocNode (w
, h
: Integer): TGLAtlasNode
;
335 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
341 while (i
>= 0) and (n
= nil) do
343 n
:= atl
[i
].Alloc(w
, h
);
349 a
:= r_Textures_AllocAtlas();
356 function r_Textures_AllocRepeatNode (w
, h
: Integer): TGLAtlasNode
;
357 var i
: Integer; n
: TGLAtlasNode
; a
: TGLAtlas
;
363 while (i
>= 0) and (ratl
[i
] <> nil) do DEC(i
);
364 if i
>= 0 then a
:= ratl
[i
];
366 if a
= nil then a
:= r_Textures_AllocRepeatAtlas(w
, h
);
372 i
:= High(ratl
); while (i
>= 0) and (ratl
[i
] <> a
) do DEC(i
);
373 if i
>= 0 then ratl
[i
] := nil;
374 r_Common_FreeAndNil(a
);
380 (* --------- TGLTexture --------- *)
382 destructor TGLTexture
.Destroy
;
383 var i
: Integer; a
: TGLAtlas
;
385 if self
.mTile
<> nil then
387 if TGLHints
.txNoRepeat
in self
.hints
then (* non repeatable texture -> delete tiles only *)
389 for i
:= 0 to High(self
.mTile
) do
391 if self
.mTile
[i
] <> nil then
393 self
.mTile
[i
].Dealloc
;
398 else (* repeatable texture -> delete whole atlas *)
400 a
:= self
.mTile
[0].base
;
401 i
:= High(ratl
); while (i
>= 0) and (ratl
[i
] <> a
) do DEC(i
);
402 if i
>= 0 then ratl
[i
] := nil;
403 r_Common_FreeAndNil(a
);
405 SetLength(self
.mTile
, 0);
410 function TGLTexture
.GetLines (): Integer;
412 ASSERT(self
.mTile
<> nil);
413 result
:= Length(self
.mTile
) div self
.mCols
416 function TGLTexture
.GetTile (col
, line
: Integer): TGLAtlasNode
;
420 ASSERT(col
<= mCols
);
421 ASSERT(self
.mTile
<> nil);
422 i
:= line
* mCols
+ col
;
424 ASSERT(i
< Length(mTile
));
426 ASSERT(result
<> nil)
429 function r_Textures_Alloc (w
, h
: Integer; hints
: TGLHintsSet
): TGLTexture
;
430 var x
, y
, mw
, mh
, cols
, lines
: Integer; t
: TGLTexture
;
434 if TGLHints
.txNoRepeat
in hints
then
436 cols
:= (w
+ maxTileSize
- 1) div maxTileSize
;
437 lines
:= (h
+ maxTileSize
- 1) div maxTileSize
;
438 t
:= TGLTexture
.Create
;
442 // t.mLines := lines;
445 SetLength(t
.mTile
, cols
* lines
);
446 for y
:= 0 to lines
- 1 do
448 mh
:= Min(maxTileSize
, h
- y
* maxTileSize
);
450 for x
:= 0 to cols
- 1 do
452 mw
:= Min(maxTileSize
, w
- x
* maxTileSize
);
454 t
.mTile
[y
* cols
+ x
] := r_Textures_AllocNode(mw
, mh
);
460 t
:= TGLTexture
.Create
;
466 SetLength(t
.mTile
, 1);
467 t
.mTile
[0] := r_Textures_AllocRepeatNode(w
, h
);
472 (* --------- TGLMultiTexture --------- *)
474 destructor TGLMultiTexture
.Destroy
;
477 for i
:= 0 to self
.count
- 1 do
478 r_Common_FreeAndNil(self
.mTexture
[i
]);
479 SetLength(self
.mTexture
, 0);
483 function TGLMultiTexture
.GetWidth (): Integer;
485 result
:= self
.mTexture
[0].width
488 function TGLMultiTexture
.GetHeight (): Integer;
490 result
:= self
.mTexture
[0].height
493 function TGLMultiTexture
.GetCount (): Integer;
495 result
:= Length(self
.mTexture
)
498 function TGLMultiTexture
.GetTexture (i
: Integer): TGLTexture
;
501 ASSERT(i
< self
.count
);
502 result
:= self
.mTexture
[i
];
503 ASSERT(result
<> nil);
506 (* --------- Init / Fin --------- *)
508 function IsPOT (v
: LongWord): Boolean;
510 result
:= (v
<> 0) and ((v
and (v
- 1)) = 0)
513 function NextPOT (v
: LongWord): LongWord;
525 function r_Textures_GetMaxHardwareSize (): Integer;
528 if r_GL_MaxTexSize
<= 0 then
530 // auto, max possible reccomended by driver
531 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, @size
);
532 size
:= size
div 2; (* hack: on some devices max size may produce invalid texture *)
533 if size
< 64 then size
:= 64; (* at least 64x64 are guarantied by specification *)
538 if IsPOT(r_GL_MaxTexSize
) then
539 size
:= r_GL_MaxTexSize
541 size
:= NextPOT(r_GL_MaxTexSize
);
546 procedure r_Textures_Initialize
;
548 currentTexture2D
:= 0;
549 maxTileSize
:= r_Textures_GetMaxHardwareSize();
550 e_LogWritefln('Texture Tile Size: %s', [maxTileSize
]);
553 procedure r_Textures_Finalize
;
558 for i
:= 0 to High(atl
) do
560 if atl
[i
] <> nil then
562 glDeleteTextures(1, @atl
[i
].id
);
564 r_Common_FreeAndNil(atl
[i
]);
572 for i
:= 0 to High(ratl
) do
574 if ratl
[i
] <> nil then
576 glDeleteTextures(1, @ratl
[i
].id
);
578 r_Common_FreeAndNil(ratl
[i
]);
585 function r_Textures_FixImageData (var img
: TImageData
): Boolean;
588 if ConvertImage(img
, TImageFormat
.ifA8R8G8B8
) then
589 if SwapChannels(img
, ChannelRed
, ChannelBlue
) then // wtf
593 function r_Textures_ValidRepeatTexture (w
, h
: Integer; hints
: TGLHintsSet
): Boolean;
595 result
:= r_GL_RepeatOpt
and
596 not (TGLHints
.txNoRepeat
in hints
) and
597 (w
<= maxTileSize
) and
598 (h
<= maxTileSize
) and
603 function r_Textures_LoadFromImage (var img
: TImageData
; hints
: TGLHintsSet
): TGLTexture
; // !!!
604 var t
: TGLTexture
; n
: TGLAtlasNode
; c
: TDynImageDataArray
; cw
, ch
, i
, j
: LongInt; ok
: Boolean;
606 t
:= nil; ok
:= false;
607 if r_Textures_ValidRepeatTexture(img
.width
, img
.height
, hints
) then
609 t
:= r_Textures_Alloc(img
.width
, img
.height
, hints
- [TGLHints
.txNoRepeat
]);
612 n
:= t
.GetTile(0, 0);
614 ok
:= r_Textures_UpdateNode(n
, img
.bits
, 0, 0, n
.width
, n
.height
);
617 else if SplitImage(img
, c
, maxTileSize
, maxTileSize
, cw
, ch
, False) then
619 t
:= r_Textures_Alloc(img
.width
, img
.height
, hints
+ [TGLHints
.txNoRepeat
]);
624 ASSERT(ch
= t
.lines
);
625 for j
:= 0 to ch
- 1 do
627 for i
:= 0 to cw
- 1 do
629 n
:= t
.GetTile(i
, j
);
631 ok
:= ok
and r_Textures_UpdateNode(n
, c
[j
* cw
+ i
].bits
, 0, 0, n
.width
, n
.height
)
635 FreeImagesInArray(c
);
638 r_Common_FreeAndNil(t
);
642 function r_Textures_LoadFromMemory (data
: Pointer; size
: LongInt; hints
: TGLHintsSet
): TGLTexture
;
646 if (data
<> nil) and (size
> 0) then
650 if LoadImageFromMemory(data
, size
, img
) then
651 if r_Textures_FixImageData(img
) then
652 result
:= r_Textures_LoadFromImage(img
, hints
)
659 function r_Textures_LoadFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLTexture
;
660 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
663 wadName
:= g_ExtractWadName(filename
);
664 wad
:= TWADFile
.Create();
665 if wad
.ReadFile(wadName
) then
667 resName
:= g_ExtractFilePathName(filename
);
668 if wad
.GetResource(resName
, data
, size
, log
) then
670 result
:= r_Textures_LoadFromMemory(data
, size
, hints
);
677 function r_Textures_LoadMultiFromImageAndInfo (var img
: TImageData
; w
, h
, c
: Integer; hints
: TGLHintsSet
): TGLMultiTexture
;
678 var t
: TImageData
; a
: array of TGLTexture
; i
: Integer; m
: TGLMultiTexture
;
685 for i
:= 0 to c
- 1 do
688 if NewImage(w
, h
, img
.Format
, t
) then
689 if CopyRect(img
, w
* i
, 0, w
, h
, t
, 0, 0) then
690 a
[i
] := r_Textures_LoadFromImage(t
, hints
);
694 m
:= TGLMultiTexture
.Create();
696 ASSERT(m
.mTexture
<> nil);
700 function r_Textures_LoadMultiFromDataAndInfo (data
: Pointer; size
: LongInt; w
, h
, c
: Integer; hints
: TGLHintsSet
): TGLMultiTexture
;
707 if (data
<> nil) and (size
> 0) then
711 if LoadImageFromMemory(data
, size
, img
) then
712 if r_Textures_FixImageData(img
) then
713 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, w
, h
, c
, hints
)
720 function r_Textures_LoadTextFromMemory (data
: Pointer; size
: LongInt; var text: TAnimTextInfo
): Boolean;
726 cfg
:= TConfig
.CreateMem(data
, size
);
729 text.name
:= cfg
.ReadStr('', 'resource', '');
730 text.w
:= cfg
.ReadInt('', 'framewidth', 0);
731 text.h
:= cfg
.ReadInt('', 'frameheight', 0);
732 text.anim
.loop
:= true;
733 text.anim
.delay
:= cfg
.ReadInt('', 'waitcount', 0);
734 text.anim
.frames
:= cfg
.ReadInt('', 'framecount', 0);
735 text.anim
.back
:= cfg
.ReadBool('', 'backanim', false);
736 if text.w
<= 0 then e_LogWritefln('Warning: bad animation width %s for %s', [text.w
, text.name
]);
737 if text.h
<= 0 then e_LogWritefln('Warning: bad animation height %s for %s', [text.h
, text.name
]);
738 if text.anim
.delay
<= 0 then e_LogWritefln('Warning: bad animation delay %s for %s', [text.anim
.delay
, text.name
]);
739 if text.anim
.frames
<= 0 then e_LogWritefln('Warning: bad animation frame count %s for %s', [text.anim
.frames
, text.name
]);
740 text.w
:= MAX(0, text.w
);
741 text.h
:= MAX(0, text.h
);
742 text.anim
.delay
:= MAX(1, text.anim
.delay
);
743 text.anim
.frames
:= MAX(1, text.anim
.frames
);
745 result
:= (text.name
<> '') and (text.w
> 0) and (text.h
> 0) and (text.anim
.delay
> 0) and (text.anim
.frames
> 0);
750 function r_Textures_LoadMultiFromWad (wad
: TWADFile
; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
): TGLMultiTexture
;
751 var data
: Pointer; size
: LongInt; img
: TImageData
;
755 if wad
.GetResource('TEXT/ANIM', data
, size
) then
757 if r_Textures_LoadTextFromMemory(data
, size
, txt
) then
760 if wad
.GetResource('TEXTURES/' + txt
.name
, data
, size
) then
764 if LoadImageFromMemory(data
, size
, img
) then
765 if r_Textures_FixImageData(img
) then
766 result
:= r_Textures_LoadMultiFromImageAndInfo(img
, txt
.w
, txt
.h
, txt
.anim
.frames
, hints
);
778 function r_Textures_LoadMultiFromMemory (data
: Pointer; size
: LongInt; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
): TGLMultiTexture
;
779 var wad
: TWADFile
; t
: TGLTexture
; m
: TGLMultiTexture
;
782 if (data
<> nil) and (size
> 0) then
784 t
:= r_Textures_LoadFromMemory(data
, size
, hints
);
787 m
:= TGLMultiTexture
.Create();
788 SetLength(m
.mTexture
, 1);
793 txt
.anim
.loop
:= true;
795 txt
.anim
.frames
:= 1;
796 txt
.anim
.back
:= false;
799 else if IsWadData(data
, size
) then
801 wad
:= TWADFile
.Create();
802 if wad
.ReadMemory(data
, size
) then
804 result
:= r_Textures_LoadMultiFromWad(wad
, txt
, hints
);
811 function r_Textures_LoadMultiTextFromFile (const filename
: AnsiString; var txt
: TAnimTextInfo
; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
812 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
815 wadName
:= g_ExtractWadName(filename
);
816 wad
:= TWADFile
.Create();
817 if wad
.ReadFile(wadName
) then
819 resName
:= g_ExtractFilePathName(filename
);
820 if wad
.GetResource(resName
, data
, size
, log
) then
822 result
:= r_Textures_LoadMultiFromMemory(data
, size
, txt
, hints
);
829 function r_Textures_LoadMultiFromFile (const filename
: AnsiString; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
830 var txt
: TAnimTextInfo
;
832 result
:= r_Textures_LoadMultiTextFromFile(filename
, txt
, hints
, log
);
835 function r_Textures_LoadMultiFromFileAndInfo (const filename
: AnsiString; w
, h
, count
: Integer; hints
: TGLHintsSet
; log
: Boolean = True): TGLMultiTexture
;
836 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
842 wadName
:= g_ExtractWadName(filename
);
843 wad
:= TWADFile
.Create();
844 if wad
.ReadFile(wadName
) then
846 resName
:= g_ExtractFilePathName(filename
);
847 if wad
.GetResource(resName
, data
, size
, log
) then
849 result
:= r_Textures_LoadMultiFromDataAndInfo(data
, size
, w
, h
, count
, hints
);
856 function r_Textures_GetRect (var img
: TImageData
): TRectWH
;
857 var i
, j
, w
, h
: Integer; done
: Boolean;
859 function IsVoid (i
, j
: Integer): Boolean; inline;
861 result
:= GetPixel32(img
, i
, j
).Channels
[3] = 0
868 (* trace x from right to left *)
869 done
:= false; i
:= 0;
870 while not done
and (i
< w
) do
873 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
874 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
879 (* trace y from up to down *)
880 done
:= false; j
:= 0;
881 while not done
and (j
< h
) do
884 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
885 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
890 (* trace x from right to left *)
891 done
:= false; i
:= w
- 1;
892 while not done
and (i
>= 0) do
895 while (j
< h
) and IsVoid(i
, j
) do inc(j
);
896 done
:= (j
< h
) and (IsVoid(i
, j
) = false);
897 result
.width
:= i
- result
.x
+ 1;
901 (* trace y from down to up *)
902 done
:= false; j
:= h
- 1;
903 while not done
and (j
>= 0) do
906 while (i
< w
) and IsVoid(i
, j
) do inc(i
);
907 done
:= (i
< w
) and (IsVoid(i
, j
) = false);
908 result
.height
:= j
- result
.y
+ 1;
913 function r_Textures_LoadStreamFromImage (var img
: TImageData
; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
): Boolean;
914 var i
, x
, y
: Integer; t
: TImageData
;
920 ASSERT((st
<> nil) and (Length(st
) >= c
));
921 ASSERT((rs
= nil) or (Length(rs
) >= c
));
923 for i
:= 0 to c
- 1 do
929 if NewImage(w
, h
, img
.Format
, t
) then
931 if CopyRect(img
, x
* w
, y
* h
, w
, h
, t
, 0, 0) then
934 rs
[i
] := r_Textures_GetRect(t
);
935 st
[i
] := r_Textures_LoadFromImage(t
, hints
);
938 ASSERT(st
[i
] <> nil);
943 function r_Textures_LoadStreamFromMemory (data
: Pointer; size
: LongInt; w
, h
, c
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
): Boolean;
950 ASSERT((st
<> nil) and (Length(st
) >= c
));
951 ASSERT((rs
= nil) or (Length(rs
) >= c
));
953 if (data
<> nil) and (size
> 0) then
957 if LoadImageFromMemory(data
, size
, img
) then
959 if r_Textures_FixImageData(img
) then
961 result
:= r_Textures_LoadStreamFromImage(img
, w
, h
, c
, cw
, st
, rs
, hints
)
970 function r_Textures_LoadStreamFromFile (const filename
: AnsiString; w
, h
, count
, cw
: Integer; st
: TGLTextureArray
; rs
: TRectArray
; hints
: TGLHintsSet
; log
: Boolean = True): Boolean;
971 var wad
: TWADFile
; wadName
, resName
: AnsiString; data
: Pointer; size
: Integer;
977 ASSERT((st
<> nil) and (Length(st
) >= count
));
978 ASSERT((rs
= nil) or (Length(rs
) >= count
));
980 wadName
:= g_ExtractWadName(filename
);
981 wad
:= TWADFile
.Create();
982 if wad
.ReadFile(wadName
) then
984 resName
:= g_ExtractFilePathName(filename
);
985 if wad
.GetResource(resName
, data
, size
, log
) then
987 result
:= r_Textures_LoadStreamFromMemory(data
, size
, w
, h
, count
, cw
, st
, rs
, hints
);
994 (* --------- TGLFont --------- *)
996 function r_Textures_LoadFontFromFile (const filename
: AnsiString; constref f
: TFontInfo
; font2enc
: TConvProc
; log
: Boolean = true): TGLFont
;
997 var i
, ch
: Integer; st
, stch
: TGLTextureArray
; font
: TGLFont
;
1001 if r_Textures_LoadStreamFromFile(filename
, f
.w
, f
.h
, 256, 16, st
, nil, [TGLHints
.txNoRepeat
], log
) then
1003 font
:= TGLFont
.Create();
1006 if Assigned(font2enc
) then
1008 SetLength(stch
, 256);
1009 for i
:= 0 to 255 do
1012 ASSERT((ch
>= 0) and (ch
<= 255));
1022 destructor TGLFont
.Destroy
;
1025 if self
.ch
<> nil then
1026 for i
:= 0 to High(self
.ch
) do
1031 function TGLFont
.GetChar (c
: AnsiChar): TGLTexture
;
1033 result
:= self
.ch
[ORD(c
)];
1036 function TGLFont
.GetWidth (c
: AnsiChar): Integer;
1038 result
:= self
.info
.ch
[c
].w
;
1040 result
:= self
.info
.w
;
1041 if self
.info
.kern
< 0 then
1042 result
:= result
+ self
.info
.kern
;
1045 function TGLFont
.GetMaxWidth (): Integer;
1047 result
:= self
.info
.w
;
1048 if self
.info
.kern
< 0 then
1049 result
:= result
+ self
.info
.kern
;
1052 function TGLFont
.GetMaxHeight (): Integer;
1054 result
:= self
.info
.h
;
1057 function TGLFont
.GetSpace (): Integer;
1059 result
:= self
.info
.kern
;
1063 conRegVar('r_gl_maxtexsize', @r_GL_MaxTexSize
, '', '');
1064 conRegVar('r_gl_repeat', @r_GL_RepeatOpt
, '', '');
1065 r_GL_MaxTexSize
:= 0; // default is automatic value
1066 r_GL_RepeatOpt
:= true;