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, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
27 SysUtils
, Classes
, Math
, e_log
, e_texture
, SDL2
, MAPDEF
, ImagingTypes
, Imaging
, ImagingUtility
;
30 TMirrorType
=(None
, Horizontal
, Vertical
);
31 TBlending
=(None
, Blend
, Filter
, Invert
);
42 Left
, Top
, Right
, Bottom
: Integer;
60 //------------------------------------------------------------------
62 //------------------------------------------------------------------
64 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
65 procedure e_ResizeWindow(Width
, Height
: Integer);
67 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
68 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
69 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
70 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
71 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
72 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
73 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
74 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
76 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
77 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
79 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
80 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
82 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
84 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
85 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
86 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
87 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
88 Blending
: TBlending
= TBlending
.None
);
89 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
90 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
92 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
93 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
94 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
95 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
96 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
97 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord
);
98 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
99 procedure e_DeleteTexture(ID
: DWORD
);
100 procedure e_RemoveAllTextures();
103 function e_CharFont_Create(sp
: ShortInt
=0): DWORD
;
104 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
105 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
106 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
107 Color
: TRGB
; Scale
: Single = 1.0);
108 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
109 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
110 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
111 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
112 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
113 procedure e_CharFont_Remove(FontID
: DWORD
);
114 procedure e_CharFont_RemoveAll();
117 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
119 procedure e_TextureFontKill(FontID
: DWORD
);
120 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
121 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
122 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
123 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
124 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
125 procedure e_RemoveAllTextureFont();
127 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
128 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
130 procedure e_ReleaseEngine();
131 procedure e_BeginRender();
132 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
133 procedure e_Clear(); overload
;
134 procedure e_EndRender();
136 function e_GetGamma(win
: PSDL_Window
): Byte;
137 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
139 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
141 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
142 function _Point(X
, Y
: Integer): TPoint2i
;
143 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
144 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
146 //function e_getTextGLId (ID: DWORD): GLuint;
150 e_NoGraphics
: Boolean = False;
151 e_FastScreenshots
: Boolean = true; // it's REALLY SLOW with `false`
152 g_dbg_scale
: Single = 1.0;
166 TTextureFont
= record
177 Chars
: array[0..255] of
187 TSavedTexture
= record
194 e_Textures
: array of TTexture
= nil;
195 e_TextureFonts
: array of TTextureFont
= nil;
196 e_CharFonts
: array of TCharFont
;
197 //e_SavedTextures: array of TSavedTexture;
199 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
201 //------------------------------------------------------------------
202 // Èíèöèàëèçèðóåò OpenGL
203 //------------------------------------------------------------------
204 procedure e_InitGL();
208 e_DummyTextures
:= True;
214 glDisable(GL_DEPTH_TEST
);
215 glEnable(GL_SCISSOR_TEST
);
216 glClearColor(0, 0, 0, 0);
219 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
221 mat
: Array [0..15] of GLDouble
;
224 if e_NoGraphics
then Exit
;
226 glScissor(X
, Y
, Width
, Height
);
227 glViewport(X
, Y
, Width
, Height
);
228 //gluOrtho2D(0, Width, Height, 0);
230 glMatrixMode(GL_PROJECTION
);
232 mat
[ 0] := 2.0 / Width
;
238 mat
[ 5] := -2.0 / Height
;
252 glLoadMatrixd(@mat
[0]);
254 glMatrixMode(GL_MODELVIEW
);
258 //------------------------------------------------------------------
259 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
260 //------------------------------------------------------------------
261 function FindTexture(): DWORD
;
265 if e_Textures
<> nil then
266 for i
:= 0 to High(e_Textures
) do
267 if e_Textures
[i
].tx
.Width
= 0 then
273 if e_Textures
= nil then
275 SetLength(e_Textures
, 32);
280 Result
:= High(e_Textures
) + 1;
281 SetLength(e_Textures
, Length(e_Textures
) + 32);
285 //------------------------------------------------------------------
287 //------------------------------------------------------------------
288 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
295 e_WriteLog('Loading texture from '+FileName
, TMsgType
.Notify
);
297 find_id
:= FindTexture();
299 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
300 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
307 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
314 find_id
:= FindTexture();
316 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
323 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
330 find_id
:= FindTexture
;
332 if not LoadTextureMem(pData
, dataSize
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
, e_Textures
[find_id
].tx
.Height
, @fmt
) then exit
;
339 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
346 find_id
:= FindTexture();
348 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
355 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
361 find_id
:= FindTexture();
362 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
367 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord
);
369 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
370 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
373 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
381 w
:= e_Textures
[ID
].tx
.Width
;
382 h
:= e_Textures
[ID
].tx
.Height
;
389 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support glGetTexImage
390 if e_NoGraphics
then Exit
;
392 data
:= GetMemory(w
*h
*4);
393 glEnable(GL_TEXTURE_2D
);
394 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
395 glGetTexImage(GL_TEXTURE_2D
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
397 for y
:= h
-1 downto 0 do
404 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
410 Result
.Y
:= h
-lastline
;
422 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
428 Result
.Height
:= h
-lastline
-Result
.Y
;
440 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
446 Result
.X
:= lastline
+1;
451 for x
:= w
-1 downto 0 do
458 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
464 Result
.Width
:= lastline
-Result
.X
+1;
473 procedure e_ResizeWindow(Width
, Height
: Integer);
477 e_SetViewPort(0, 0, Width
, Height
);
480 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
482 x1
, y1
, tmp
: Integer;
484 if (w
< 1) or (h
< 1) then exit
;
487 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
488 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
489 //HACK: make texture one pixel shorter, so it won't wrap
490 if (g_dbg_scale
<> 1.0) then
495 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
496 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
497 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
498 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
501 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
502 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
504 if e_NoGraphics
then Exit
;
505 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
507 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
512 if (AlphaChannel
) or (Alpha
> 0) then
513 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
516 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
519 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
521 glEnable(GL_TEXTURE_2D
);
522 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
525 drawTxQuad(X
, Y
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
527 //u := e_Textures[ID].tx.u;
528 //v := e_Textures[ID].tx.v;
531 if Mirror = M_NONE then
533 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
534 glTexCoord2f(0, 0); glVertex2i(X, Y);
535 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
536 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
539 if Mirror = M_HORIZONTAL then
541 glTexCoord2f(u, 0); glVertex2i(X, Y);
542 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
543 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
544 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
547 if Mirror = M_VERTICAL then
549 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
550 glTexCoord2f(0, -v); glVertex2i(X, Y);
551 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
552 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
561 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
562 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
566 if e_NoGraphics
then Exit
;
567 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
569 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
574 if (AlphaChannel
) or (Alpha
> 0) then
575 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
578 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
581 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
583 glEnable(GL_TEXTURE_2D
);
584 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
586 u
:= e_Textures
[ID
].tx
.u
;
587 v
:= e_Textures
[ID
].tx
.v
;
590 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
591 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
592 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
593 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
599 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
600 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
602 if e_NoGraphics
then Exit
;
603 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
605 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
610 if (AlphaChannel
) or (Alpha
> 0) then
611 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
614 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
617 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
619 glEnable(GL_TEXTURE_2D
);
620 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
622 drawTxQuad(X
, Y
, Width
, Height
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
628 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
629 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
631 X2
, Y2
, dx
, w
, h
: Integer;
634 if e_NoGraphics
then Exit
;
635 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
636 ambientBlendMode
:= false;
638 if (Alpha
> 0) or AlphaChannel
or Blending
then
644 if not ambientBlendMode
then glDisable(GL_BLEND
);
646 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
647 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
648 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
650 if (XCount
= 0) then XCount
:= 1;
651 if (YCount
= 0) then YCount
:= 1;
653 glEnable(GL_TEXTURE_2D
);
654 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
656 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
657 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
659 //k8: this SHOULD work... i hope
660 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
663 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
664 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
665 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
666 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
673 u
:= e_Textures
[ID
].tx
.u
;
674 v
:= e_Textures
[ID
].tx
.v
;
675 w
:= e_Textures
[ID
].tx
.width
;
676 h
:= e_Textures
[ID
].tx
.height
;
683 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
684 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
685 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
686 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
701 //TODO: overflow checks
702 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
707 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
708 // check for intersection
709 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
710 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
714 if (x0
< x1
) then x0
:= x1
;
715 if (y0
< y1
) then y0
:= y1
;
716 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
717 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
720 result
:= (w0
> 0) and (h0
> 0);
724 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
725 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
730 scxywh: array[0..3] of GLint;
731 vpxywh: array[0..3] of GLint;
733 w
, h
, dw
, cw
, ch
, yofs
: Integer;
734 u
, v
, cu
, cv
: Single;
738 procedure setScissorGLInternal (x, y, w, h: Integer);
740 //if not scallowed then exit;
745 y := vpxywh[3]-(y+h);
746 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
748 glScissor(0, 0, 0, 0);
752 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
753 glScissor(x, y, w, h);
759 if e_NoGraphics
then exit
;
760 ambientBlendMode
:= false;
762 if (wdt
< 1) or (hgt
< 1) then exit
;
764 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
766 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
770 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
772 if (Alpha
> 0) or AlphaChannel
or Blending
then
778 if not ambientBlendMode
then glDisable(GL_BLEND
);
780 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
781 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
782 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
784 glEnable(GL_TEXTURE_2D
);
785 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
790 //k8: this SHOULD work... i hope
791 if {false and} (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
794 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
795 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
796 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
797 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
802 // hard day's night; setup scissor
804 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
805 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
806 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
807 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
808 //glEnable(GL_SCISSOR_TEST);
809 setScissorGLInternal(x, y, wdt, hgt);
812 u
:= e_Textures
[ID
].tx
.u
;
813 v
:= e_Textures
[ID
].tx
.v
;
814 w
:= e_Textures
[ID
].tx
.width
;
815 h
:= e_Textures
[ID
].tx
.height
;
817 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
821 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
822 if onlyOneY
then yofs
:= 0;
828 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
830 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
831 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
832 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
833 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
839 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
846 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
848 if e_NoGraphics
then exit
;
849 if (w
< 1) or (h
< 1) then exit
;
850 if (a
<> 255) or ((r
or g
or b
) <> 0) then
853 glDisable(GL_TEXTURE_2D
);
854 glColor4ub(r
, g
, b
, a
);
855 if ((r
or g
or b
) <> 0) then
857 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
861 glVertex2i(x
+w
, y
+h
);
865 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
869 glVertex2i(x
+w
, y
+h
);
877 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
878 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
880 if e_NoGraphics
then Exit
;
882 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
884 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
889 if (AlphaChannel
) or (Alpha
> 0) then
890 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
893 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
896 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
898 if (Angle
<> 0) and (RC
<> nil) then
901 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
902 glRotatef(Angle
, 0, 0, 1);
903 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
906 glEnable(GL_TEXTURE_2D
);
907 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
908 glBegin(GL_QUADS
); //0-1 1-1
910 drawTxQuad(X
, Y
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
919 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
921 if e_NoGraphics
then Exit
;
922 glDisable(GL_TEXTURE_2D
);
923 glColor3ub(Red
, Green
, Blue
);
926 if (Size
= 2) or (Size
= 4) then
930 glVertex2f(X
+0.3, Y
+1.0);
933 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
936 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
938 // Make lines only top-left/bottom-right and top-right/bottom-left
950 // Pixel-perfect hack
958 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
960 nX1
, nY1
, nX2
, nY2
: Integer;
962 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support glBegin(GL_LINES)
963 if e_NoGraphics
then Exit
;
964 // Only top-left/bottom-right quad
981 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
985 glDisable(GL_TEXTURE_2D
);
986 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
990 nX1
:= X1
; nY1
:= Y1
;
991 nX2
:= X2
; nY2
:= Y1
;
992 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
993 glVertex2i(nX1
, nY1
);
994 glVertex2i(nX2
, nY2
);
996 nX1
:= X2
; nY1
:= Y1
;
997 nX2
:= X2
; nY2
:= Y2
;
998 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
999 glVertex2i(nX1
, nY1
);
1000 glVertex2i(nX2
, nY2
);
1002 nX1
:= X2
; nY1
:= Y2
;
1003 nX2
:= X1
; nY2
:= Y2
;
1004 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1005 glVertex2i(nX1
, nY1
);
1006 glVertex2i(nX2
, nY2
);
1008 nX1
:= X1
; nY1
:= Y2
;
1009 nX2
:= X1
; nY2
:= Y1
;
1010 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1011 glVertex2i(nX1
, nY1
);
1012 glVertex2i(nX2
, nY2
);
1015 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1017 glDisable(GL_BLEND
);
1021 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
1022 Blending
: TBlending
= TBlending
.None
);
1024 if e_NoGraphics
then Exit
;
1025 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
1028 glDisable(GL_BLEND
);
1030 if Blending
= TBlending
.Blend
then
1031 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
1033 if Blending
= TBlending
.Filter
then
1034 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
1036 if Blending
= TBlending
.Invert
then
1037 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
1040 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1042 glDisable(GL_TEXTURE_2D
);
1043 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1055 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1057 glDisable(GL_BLEND
);
1061 // ////////////////////////////////////////////////////////////////////////// //
1062 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1064 if (a
< 0) then a
:= 0;
1065 if (a
> 255) then a
:= 255;
1067 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1068 glDisable(GL_TEXTURE_2D
);
1069 glColor4ub(0, 0, 0, Byte(255-a
));
1076 //glRect(x, y, x+w, y+h);
1077 glColor4ub(1, 1, 1, 1);
1078 glDisable(GL_BLEND
);
1079 //glBlendEquation(GL_FUNC_ADD);
1082 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1084 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1088 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1091 v
: array [0..3] of GLfloat
;
1094 if e_NoGraphics
then Exit
;
1095 // Pixel-perfect lines
1097 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1102 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1104 glDisable(GL_BLEND
);
1106 glDisable(GL_TEXTURE_2D
);
1107 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1111 v
[0] := X1
; v
[1] := Y1
; v
[2] := X2
; v
[3] := Y2
;
1112 glVertexPointer(2, GL_FLOAT
, 0, @v
[0]);
1113 glEnableClientState(GL_VERTEX_ARRAY
);
1114 glDisableClientState(GL_COLOR_ARRAY
);
1115 glDisableClientState(GL_NORMAL_ARRAY
);
1116 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
1117 glDrawArrays(GL_LINES
, 0, 4);
1125 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1127 glDisable(GL_BLEND
);
1130 //------------------------------------------------------------------
1131 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1132 //------------------------------------------------------------------
1133 procedure e_DeleteTexture(ID
: DWORD
);
1135 if not e_NoGraphics
then
1136 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1137 e_Textures
[ID
].tx
.id
:= 0;
1138 e_Textures
[ID
].tx
.Width
:= 0;
1139 e_Textures
[ID
].tx
.Height
:= 0;
1142 //------------------------------------------------------------------
1143 // Óäàëÿåò âñå òåêñòóðû
1144 //------------------------------------------------------------------
1145 procedure e_RemoveAllTextures();
1149 if e_Textures
= nil then Exit
;
1151 for i
:= 0 to High(e_Textures
) do
1152 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1156 //------------------------------------------------------------------
1158 //------------------------------------------------------------------
1159 procedure e_ReleaseEngine();
1161 e_RemoveAllTextures
;
1162 e_RemoveAllTextureFont
;
1165 procedure e_BeginRender();
1167 if e_NoGraphics
then Exit
;
1168 glEnable(GL_ALPHA_TEST
);
1169 glAlphaFunc(GL_GREATER
, 0.0);
1172 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1174 if e_NoGraphics
then Exit
;
1175 glClearColor(Red
, Green
, Blue
, 0);
1179 procedure e_Clear(); overload
;
1181 if e_NoGraphics
then Exit
;
1182 glClearColor(0, 0, 0, 0);
1183 glClear(GL_COLOR_BUFFER_BIT
);
1186 procedure e_EndRender();
1188 if e_NoGraphics
then Exit
;
1192 function e_GetGamma(win
: PSDL_Window
): Byte;
1194 ramp
: array [0..256*3-1] of Word;
1195 rgb
: array [0..2] of Double;
1204 if e_NoGraphics
then Exit
;
1209 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1218 for j
:= min
to max
- 1 do
1221 B
:= (j
mod 256)/256;
1223 sum
:= sum
+ ln(A
)/ln(B
);
1226 rgb
[i
] := sum
/ count
;
1229 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1232 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1234 ramp
: array [0..256*3-1] of Word;
1239 if e_NoGraphics
then Exit
;
1240 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1242 for i
:= 0 to 255 do
1244 r
:= Exp(g
* ln(i
/256))*65536;
1245 if r
< 0 then r
:= 0
1246 else if r
> 65535 then r
:= 65535;
1247 ramp
[i
] := trunc(r
);
1248 ramp
[i
+ 256] := trunc(r
);
1249 ramp
[i
+ 512] := trunc(r
);
1252 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1255 function e_CharFont_Create(sp
: ShortInt
=0): DWORD
;
1259 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1263 if e_CharFonts
<> nil then
1264 for i
:= 0 to High(e_CharFonts
) do
1265 if not e_CharFonts
[i
].alive
then
1271 if id
= DWORD(-1) then
1273 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1274 id
:= High(e_CharFonts
);
1277 with e_CharFonts
[id
] do
1279 for i
:= 0 to High(Chars
) do
1293 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1295 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1297 TextureID
:= Texture
;
1302 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1306 if e_NoGraphics
then Exit
;
1307 if Text = '' then Exit
;
1308 if e_CharFonts
= nil then Exit
;
1309 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1311 with e_CharFonts
[FontID
] do
1313 for a
:= 1 to Length(Text) do
1314 with Chars
[Ord(Text[a
])] do
1315 if TextureID
<> -1 then
1317 e_Draw(TextureID
, X
, Y
, 0, True, False);
1318 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1323 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1324 Color
: TRGB
; Scale
: Single = 1.0);
1329 if e_NoGraphics
then Exit
;
1330 if Text = '' then Exit
;
1331 if e_CharFonts
= nil then Exit
;
1332 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1334 with e_CharFonts
[FontID
] do
1336 for a
:= 1 to Length(Text) do
1337 with Chars
[Ord(Text[a
])] do
1338 if TextureID
<> -1 then
1340 if Scale
<> 1.0 then
1343 glScalef(Scale
, Scale
, 0);
1348 e_Draw(TextureID
, X
, Y
, 0, True, False);
1351 if Scale
<> 1.0 then glPopMatrix
;
1353 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1358 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1360 a
, TX
, TY
, len
: Integer;
1364 if e_NoGraphics
then Exit
;
1365 if Text = '' then Exit
;
1366 if e_CharFonts
= nil then Exit
;
1367 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1375 len
:= Length(Text);
1377 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1379 with e_CharFonts
[FontID
] do
1381 for a
:= 1 to len
do
1392 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1397 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1402 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1407 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1412 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1417 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1422 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1427 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1432 with Chars
[Ord(Text[a
])] do
1433 if TextureID
<> -1 then
1437 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1440 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1446 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1454 if Text = '' then Exit
;
1455 if e_CharFonts
= nil then Exit
;
1456 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1458 with e_CharFonts
[FontID
] do
1460 for a
:= 1 to Length(Text) do
1461 with Chars
[Ord(Text[a
])] do
1462 if TextureID
<> -1 then
1464 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1465 e_GetTextureSize(TextureID
, nil, @h2
);
1466 if h2
> h
then h
:= h2
;
1471 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1473 a
, lines
, len
: Integer;
1474 h2
, w2
, tw
, th
: Word;
1481 if Text = '' then Exit
;
1482 if e_CharFonts
= nil then Exit
;
1483 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1486 len
:= Length(Text);
1488 with e_CharFonts
[FontID
] do
1490 for a
:= 1 to len
do
1492 if Text[a
] = #10 then
1495 if w2
> tw
then tw
:= w2
;
1500 with Chars
[Ord(Text[a
])] do
1501 if TextureID
<> -1 then
1503 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1504 e_GetTextureSize(TextureID
, nil, @h2
);
1505 if h2
> th
then th
:= h2
;
1517 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1523 if e_CharFonts
= nil then Exit
;
1524 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1526 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1527 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1530 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1537 if e_CharFonts
= nil then Exit
;
1538 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1540 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1542 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1543 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1545 if h2
> Result
then Result
:= h2
;
1549 procedure e_CharFont_Remove(FontID
: DWORD
);
1553 with e_CharFonts
[FontID
] do
1554 for a
:= 0 to High(Chars
) do
1555 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1557 e_CharFonts
[FontID
].alive
:= False;
1560 procedure e_CharFont_RemoveAll();
1564 if e_CharFonts
= nil then Exit
;
1566 for a
:= 0 to High(e_CharFonts
) do
1567 e_CharFont_Remove(a
);
1572 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1579 if e_NoGraphics
then Exit
;
1580 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1584 if e_TextureFonts
<> nil then
1585 for i
:= 0 to High(e_TextureFonts
) do
1586 if e_TextureFonts
[i
].Base
= 0 then
1592 if id
= DWORD(-1) then
1594 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1595 id
:= High(e_TextureFonts
);
1598 with e_TextureFonts
[id
] do
1600 {$IF not DEFINED(USE_NANOGL)}
1601 Base
:= glGenLists(XCount
*YCount
);
1603 TextureID
:= e_Textures
[Tex
].tx
.id
;
1604 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1605 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1612 {$IF not DEFINED(USE_NANOGL)}
1613 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1614 for loop1
:= 0 to XCount
*YCount
-1 do
1616 cx
:= (loop1
mod XCount
)/XCount
;
1617 cy
:= (loop1
div YCount
)/YCount
;
1619 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1621 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1622 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1624 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1625 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1627 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1628 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1630 glTexCoord2f(cx
, 1.0-cy
);
1633 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1641 procedure e_TextureFontKill(FontID
: DWORD
);
1643 if e_NoGraphics
then Exit
;
1644 {$IF not DEFINED(USE_NANOGL)}
1645 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1647 e_TextureFonts
[FontID
].Base
:= 0;
1651 procedure e_TextureFontDrawChar(ch
: Char; FontID
: DWORD
);
1656 Width
, Height
: Integer;
1657 XCount
, YCount
: Integer;
1659 index
:= Ord(ch
) - 32;
1660 Tex
:= e_TextureFonts
[FontID
].Texture
;
1661 Width
:= e_Textures
[Tex
].tx
.Width
;
1662 Height
:= e_Textures
[Tex
].tx
.Height
;
1663 XCount
:= e_TextureFonts
[FontID
].XC
;
1664 YCount
:= e_TextureFonts
[FontID
].YC
;
1665 cx
:= (index
mod XCount
)/XCount
;
1666 cy
:= (index
div YCount
)/YCount
;
1668 glTexCoord2f(cx
, 1 - cy
- 1/YCount
);
1669 glVertex2i(0, Height
div YCount
);
1670 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
- 1/YCount
);
1671 glVertex2i(Width
div XCount
, Height
div YCount
);
1672 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
);
1673 glVertex2i(Width
div XCount
, 0);
1674 glTexCoord2f(cx
, 1 - cy
);
1677 glTranslatef((e_Textures
[Tex
].tx
.Width
div XCount
) + e_TextureFonts
[FontID
].SPC
, 0, 0);
1680 procedure e_TextureFontDrawString(Text: String; FontID
: DWORD
);
1684 for i
:= 1 to High(Text) do
1685 e_TextureFontDrawChar(Text[i
], FontID
);
1689 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1691 if e_NoGraphics
then Exit
;
1692 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1693 if Text = '' then Exit
;
1695 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1698 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1701 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1702 glEnable(GL_TEXTURE_2D
);
1703 glTranslatef(x
, y
, 0);
1705 e_TextureFontDrawString(Text, FontID
);
1707 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1708 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1710 glDisable(GL_TEXTURE_2D
);
1713 glDisable(GL_BLEND
);
1716 // god forgive me for this, but i cannot figure out how to do it without lists
1717 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1719 if e_NoGraphics
then Exit
;
1724 glColor4ub(0, 0, 0, 128);
1725 glTranslatef(X
+1, Y
+1, 0);
1727 e_TextureFontDrawChar(Ch
, FontID
);
1729 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1735 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1736 glTranslatef(X
, Y
, 0);
1738 e_TextureFontDrawChar(Ch
, FontID
);
1740 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1746 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1748 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1749 glEnable(GL_TEXTURE_2D
);
1750 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1752 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1754 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1755 glDisable(GL_TEXTURE_2D
);
1756 glDisable(GL_BLEND
);
1759 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1761 result
:= e_TextureFonts
[FontID
].CharWidth
;
1764 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1766 a
, TX
, TY
, len
: Integer;
1770 if e_NoGraphics
then Exit
;
1771 if Text = '' then Exit
;
1772 if e_TextureFonts
= nil then Exit
;
1773 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1781 len
:= Length(Text);
1783 w
:= e_TextureFonts
[FontID
].CharWidth
;
1785 with e_TextureFonts
[FontID
] do
1787 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1788 glEnable(GL_TEXTURE_2D
);
1790 {$IF not DEFINED(USE_NANOGL)}
1791 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1794 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1797 for a
:= 1 to len
do
1808 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1813 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1818 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1823 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1828 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1833 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1838 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1843 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1850 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1855 glDisable(GL_TEXTURE_2D
);
1856 glDisable(GL_BLEND
);
1860 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1861 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1863 if e_NoGraphics
then Exit
;
1864 if Text = '' then Exit
;
1867 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1868 glEnable(GL_TEXTURE_2D
);
1870 {$IF not DEFINED(USE_NANOGL)}
1871 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1874 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1879 glColor4ub(0, 0, 0, 128);
1880 glTranslatef(x
+1, y
+1, 0);
1881 glScalef(Scale
, Scale
, 0);
1883 e_TextureFontDrawString(Text, FontID
);
1885 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1891 glColor4ub(Red
, Green
, Blue
, 255);
1892 glTranslatef(x
, y
, 0);
1893 glScalef(Scale
, Scale
, 0);
1895 e_TextureFontDrawString(Text, FontID
);
1897 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1900 glDisable(GL_TEXTURE_2D
);
1902 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1903 glDisable(GL_BLEND
);
1906 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1910 if e_NoGraphics
then Exit
;
1911 if Integer(ID
) > High(e_TextureFonts
) then
1913 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1914 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1917 procedure e_RemoveAllTextureFont();
1921 if e_NoGraphics
then Exit
;
1922 if e_TextureFonts
= nil then Exit
;
1924 for i
:= 0 to High(e_TextureFonts
) do
1925 if e_TextureFonts
[i
].Base
<> 0 then
1927 {$IFNDEF USE_NANOGL}
1928 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1930 e_TextureFonts
[i
].Base
:= 0;
1933 e_TextureFonts
:= nil;
1936 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1943 function _Point(X
, Y
: Integer): TPoint2i
;
1949 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1953 Result
.Width
:= Width
;
1954 Result
.Height
:= Height
;
1957 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1966 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1968 pixels
, obuf
, scln
, ps
, pd
: PByte
;
1971 i
, x
, y
, res
: Integer;
1972 sign
: array [0..7] of Byte;
1973 hbuf
: array [0..12] of Byte;
1978 if e_NoGraphics
then Exit
;
1981 // first, extract and pack graphics data
1982 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1984 GetMem(pixels
, Width
*Height
*3);
1986 FillChar(pixels
^, Width
*Height
*3, 0);
1987 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1988 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1990 if e_FastScreenshots
then
1993 GetMem(scln
, (Width
*3+1)*Height
);
1997 Inc(ps
, (Width
*3)*(Height
-1));
1998 for i
:= 0 to Height
-1 do
2002 Move(ps
^, pd
^, Width
*3);
2014 obufsize
:= (Width
*3+1)*Height
*2;
2015 GetMem(obuf
, obufsize
);
2020 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
2021 if res
= Z_OK
then break
;
2022 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
2023 obufsize
:= obufsize
*2;
2026 GetMem(obuf
, obufsize
);
2028 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
2041 st
.writeBuffer(sign
, 8);
2042 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2045 writeIntBE(st
, LongWord(13));
2050 st
.writeBuffer(sign
, 4);
2051 crc
:= crc32(0, @sign
[0], 4);
2054 hbuf
[2] := (Width
shr 8) and $ff;
2055 hbuf
[3] := Width
and $ff;
2058 hbuf
[6] := (Height
shr 8) and $ff;
2059 hbuf
[7] := Height
and $ff;
2060 hbuf
[8] := 8; // bit depth
2061 hbuf
[9] := 2; // RGB
2062 hbuf
[10] := 0; // compression method
2063 hbuf
[11] := 0; // filter method
2064 hbuf
[12] := 0; // no interlace
2065 crc
:= crc32(crc
, @hbuf
[0], 13);
2066 st
.writeBuffer(hbuf
, 13);
2067 writeIntBE(st
, crc
);
2068 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2071 writeIntBE(st
, LongWord(dlen
));
2076 st
.writeBuffer(sign
, 4);
2077 crc
:= crc32(0, @sign
[0], 4);
2078 crc
:= crc32(crc
, obuf
, dlen
);
2079 st
.writeBuffer(obuf
^, dlen
);
2080 writeIntBE(st
, crc
);
2081 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2084 writeIntBE(st
, LongWord(0));
2089 st
.writeBuffer(sign
, 4);
2090 crc
:= crc32(0, @sign
[0], 4);
2091 writeIntBE(st
, crc
);
2092 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2094 if obuf
<> nil then FreeMem(obuf
);
2099 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2100 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2103 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2105 //writeln(stderr, 'moving pixels...');
2106 for y
:= Height
-1 downto 0 do
2108 for x
:= 0 to Width
-1 do
2110 clr
.r
:= ps
^; Inc(ps
);
2111 clr
.g
:= ps
^; Inc(ps
);
2112 clr
.b
:= ps
^; Inc(ps
);
2114 SetPixel32(img
, x
, y
, clr
);
2117 GlobalMetadata
.ClearMetaItems();
2118 GlobalMetadata
.ClearMetaItemsForSaving();
2119 //writeln(stderr, 'compressing image...');
2120 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2121 //writeln(stderr, 'done!');