99d63e3a3fa22c9dff4a07fe26bd0c4e6eaa6146
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
176 Chars
: array[0..255] of
186 TSavedTexture
= record
193 e_Textures
: array of TTexture
= nil;
194 e_TextureFonts
: array of TTextureFont
= nil;
195 e_CharFonts
: array of TCharFont
;
196 //e_SavedTextures: array of TSavedTexture;
198 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
200 //------------------------------------------------------------------
201 // Èíèöèàëèçèðóåò OpenGL
202 //------------------------------------------------------------------
203 procedure e_InitGL();
207 e_DummyTextures
:= True;
213 glDisable(GL_DEPTH_TEST
);
214 glEnable(GL_SCISSOR_TEST
);
215 glClearColor(0, 0, 0, 0);
218 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
220 mat
: Array [0..15] of GLDouble
;
223 if e_NoGraphics
then Exit
;
225 glScissor(X
, Y
, Width
, Height
);
226 glViewport(X
, Y
, Width
, Height
);
227 //gluOrtho2D(0, Width, Height, 0);
229 glMatrixMode(GL_PROJECTION
);
231 mat
[ 0] := 2.0 / Width
;
237 mat
[ 5] := -2.0 / Height
;
251 glLoadMatrixd(@mat
[0]);
253 glMatrixMode(GL_MODELVIEW
);
257 //------------------------------------------------------------------
258 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
259 //------------------------------------------------------------------
260 function FindTexture(): DWORD
;
264 if e_Textures
<> nil then
265 for i
:= 0 to High(e_Textures
) do
266 if e_Textures
[i
].tx
.Width
= 0 then
272 if e_Textures
= nil then
274 SetLength(e_Textures
, 32);
279 Result
:= High(e_Textures
) + 1;
280 SetLength(e_Textures
, Length(e_Textures
) + 32);
284 //------------------------------------------------------------------
286 //------------------------------------------------------------------
287 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
294 e_WriteLog('Loading texture from '+FileName
, TMsgType
.Notify
);
296 find_id
:= FindTexture();
298 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
299 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
306 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
313 find_id
:= FindTexture();
315 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
322 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
329 find_id
:= FindTexture
;
331 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
;
338 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
345 find_id
:= FindTexture();
347 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
354 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
360 find_id
:= FindTexture();
361 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
366 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
368 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
369 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
372 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
380 w
:= e_Textures
[ID
].tx
.Width
;
381 h
:= e_Textures
[ID
].tx
.Height
;
389 if e_NoGraphics
then Exit
;
391 data
:= GetMemory(w
*h
*4);
392 glEnable(GL_TEXTURE_2D
);
393 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
394 glGetTexImage(GL_TEXTURE_2D
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
396 for y
:= h
-1 downto 0 do
403 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
409 Result
.Y
:= h
-lastline
;
421 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
427 Result
.Height
:= h
-lastline
-Result
.Y
;
439 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
445 Result
.X
:= lastline
+1;
450 for x
:= w
-1 downto 0 do
457 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
463 Result
.Width
:= lastline
-Result
.X
+1;
472 procedure e_ResizeWindow(Width
, Height
: Integer);
476 e_SetViewPort(0, 0, Width
, Height
);
479 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
481 x1
, y1
, tmp
: Integer;
483 if (w
< 1) or (h
< 1) then exit
;
486 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
487 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
488 //HACK: make texture one pixel shorter, so it won't wrap
489 if (g_dbg_scale
<> 1.0) then
494 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
495 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
496 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
497 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
500 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
501 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
503 if e_NoGraphics
then Exit
;
504 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
506 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
511 if (AlphaChannel
) or (Alpha
> 0) then
512 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
515 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
518 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
520 glEnable(GL_TEXTURE_2D
);
521 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
524 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
);
526 //u := e_Textures[ID].tx.u;
527 //v := e_Textures[ID].tx.v;
530 if Mirror = M_NONE then
532 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
533 glTexCoord2f(0, 0); glVertex2i(X, Y);
534 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
535 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
538 if Mirror = M_HORIZONTAL then
540 glTexCoord2f(u, 0); glVertex2i(X, Y);
541 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
542 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
543 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
546 if Mirror = M_VERTICAL then
548 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
549 glTexCoord2f(0, -v); glVertex2i(X, Y);
550 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
551 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
560 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
561 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
565 if e_NoGraphics
then Exit
;
566 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
568 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
573 if (AlphaChannel
) or (Alpha
> 0) then
574 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
577 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
580 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
582 glEnable(GL_TEXTURE_2D
);
583 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
585 u
:= e_Textures
[ID
].tx
.u
;
586 v
:= e_Textures
[ID
].tx
.v
;
589 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
590 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
591 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
592 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
598 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
599 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
601 if e_NoGraphics
then Exit
;
602 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
604 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
609 if (AlphaChannel
) or (Alpha
> 0) then
610 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
613 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
616 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
618 glEnable(GL_TEXTURE_2D
);
619 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
621 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
);
627 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
628 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
630 X2
, Y2
, dx
, w
, h
: Integer;
633 if e_NoGraphics
then Exit
;
634 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
635 ambientBlendMode
:= false;
637 if (Alpha
> 0) or AlphaChannel
or Blending
then
643 if not ambientBlendMode
then glDisable(GL_BLEND
);
645 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
646 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
647 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
649 if (XCount
= 0) then XCount
:= 1;
650 if (YCount
= 0) then YCount
:= 1;
652 glEnable(GL_TEXTURE_2D
);
653 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
655 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
656 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
658 //k8: this SHOULD work... i hope
659 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
662 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
663 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
664 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
665 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
672 u
:= e_Textures
[ID
].tx
.u
;
673 v
:= e_Textures
[ID
].tx
.v
;
674 w
:= e_Textures
[ID
].tx
.width
;
675 h
:= e_Textures
[ID
].tx
.height
;
682 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
683 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
684 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
685 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
700 //TODO: overflow checks
701 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
706 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
707 // check for intersection
708 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
709 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
713 if (x0
< x1
) then x0
:= x1
;
714 if (y0
< y1
) then y0
:= y1
;
715 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
716 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
719 result
:= (w0
> 0) and (h0
> 0);
723 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
724 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
729 scxywh: array[0..3] of GLint;
730 vpxywh: array[0..3] of GLint;
732 w
, h
, dw
, cw
, ch
, yofs
: Integer;
733 u
, v
, cu
, cv
: Single;
737 procedure setScissorGLInternal (x, y, w, h: Integer);
739 //if not scallowed then exit;
744 y := vpxywh[3]-(y+h);
745 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
747 glScissor(0, 0, 0, 0);
751 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
752 glScissor(x, y, w, h);
758 if e_NoGraphics
then exit
;
759 ambientBlendMode
:= false;
761 if (wdt
< 1) or (hgt
< 1) then exit
;
763 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
765 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
769 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
771 if (Alpha
> 0) or AlphaChannel
or Blending
then
777 if not ambientBlendMode
then glDisable(GL_BLEND
);
779 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
780 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
781 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
783 glEnable(GL_TEXTURE_2D
);
784 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
789 //k8: this SHOULD work... i hope
790 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
793 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
794 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
795 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
796 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
801 // hard day's night; setup scissor
803 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
804 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
805 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
806 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
807 //glEnable(GL_SCISSOR_TEST);
808 setScissorGLInternal(x, y, wdt, hgt);
811 u
:= e_Textures
[ID
].tx
.u
;
812 v
:= e_Textures
[ID
].tx
.v
;
813 w
:= e_Textures
[ID
].tx
.width
;
814 h
:= e_Textures
[ID
].tx
.height
;
816 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
820 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
821 if onlyOneY
then yofs
:= 0;
827 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
829 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
830 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
831 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
832 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
838 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
845 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
847 if e_NoGraphics
then exit
;
848 if (w
< 1) or (h
< 1) then exit
;
849 if (a
<> 255) or ((r
or g
or b
) <> 0) then
852 glDisable(GL_TEXTURE_2D
);
853 glColor4ub(r
, g
, b
, a
);
854 if ((r
or g
or b
) <> 0) then
856 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
860 glVertex2i(x
+w
, y
+h
);
864 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
868 glVertex2i(x
+w
, y
+h
);
876 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
877 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
879 if e_NoGraphics
then Exit
;
881 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
883 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
888 if (AlphaChannel
) or (Alpha
> 0) then
889 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
892 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
895 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
897 if (Angle
<> 0) and (RC
<> nil) then
900 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
901 glRotatef(Angle
, 0, 0, 1);
902 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
905 glEnable(GL_TEXTURE_2D
);
906 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
907 glBegin(GL_QUADS
); //0-1 1-1
909 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
);
918 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
920 if e_NoGraphics
then Exit
;
921 glDisable(GL_TEXTURE_2D
);
922 glColor3ub(Red
, Green
, Blue
);
925 if (Size
= 2) or (Size
= 4) then
929 glVertex2f(X
+0.3, Y
+1.0);
932 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
935 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
937 // Make lines only top-left/bottom-right and top-right/bottom-left
949 // Pixel-perfect hack
957 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
959 nX1
, nY1
, nX2
, nY2
: Integer;
961 if e_NoGraphics
then Exit
;
962 // Only top-left/bottom-right quad
979 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
983 glDisable(GL_TEXTURE_2D
);
984 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
988 nX1
:= X1
; nY1
:= Y1
;
989 nX2
:= X2
; nY2
:= Y1
;
990 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
991 glVertex2i(nX1
, nY1
);
992 glVertex2i(nX2
, nY2
);
994 nX1
:= X2
; nY1
:= Y1
;
995 nX2
:= X2
; nY2
:= Y2
;
996 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
997 glVertex2i(nX1
, nY1
);
998 glVertex2i(nX2
, nY2
);
1000 nX1
:= X2
; nY1
:= Y2
;
1001 nX2
:= X1
; nY2
:= Y2
;
1002 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1003 glVertex2i(nX1
, nY1
);
1004 glVertex2i(nX2
, nY2
);
1006 nX1
:= X1
; nY1
:= Y2
;
1007 nX2
:= X1
; nY2
:= Y1
;
1008 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1009 glVertex2i(nX1
, nY1
);
1010 glVertex2i(nX2
, nY2
);
1013 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1015 glDisable(GL_BLEND
);
1018 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
1019 Blending
: TBlending
= TBlending
.None
);
1021 if e_NoGraphics
then Exit
;
1022 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
1025 glDisable(GL_BLEND
);
1027 if Blending
= TBlending
.Blend
then
1028 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
1030 if Blending
= TBlending
.Filter
then
1031 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
1033 if Blending
= TBlending
.Invert
then
1034 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
1037 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1039 glDisable(GL_TEXTURE_2D
);
1040 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1052 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1054 glDisable(GL_BLEND
);
1058 // ////////////////////////////////////////////////////////////////////////// //
1059 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1061 if (a
< 0) then a
:= 0;
1062 if (a
> 255) then a
:= 255;
1064 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1065 glDisable(GL_TEXTURE_2D
);
1066 glColor4ub(0, 0, 0, Byte(255-a
));
1073 //glRect(x, y, x+w, y+h);
1074 glColor4ub(1, 1, 1, 1);
1075 glDisable(GL_BLEND
);
1076 //glBlendEquation(GL_FUNC_ADD);
1079 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1081 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1085 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1087 if e_NoGraphics
then Exit
;
1088 // Pixel-perfect lines
1090 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1095 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1097 glDisable(GL_BLEND
);
1099 glDisable(GL_TEXTURE_2D
);
1100 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1108 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1110 glDisable(GL_BLEND
);
1113 //------------------------------------------------------------------
1114 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1115 //------------------------------------------------------------------
1116 procedure e_DeleteTexture(ID
: DWORD
);
1118 if not e_NoGraphics
then
1119 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1120 e_Textures
[ID
].tx
.id
:= 0;
1121 e_Textures
[ID
].tx
.Width
:= 0;
1122 e_Textures
[ID
].tx
.Height
:= 0;
1125 //------------------------------------------------------------------
1126 // Óäàëÿåò âñå òåêñòóðû
1127 //------------------------------------------------------------------
1128 procedure e_RemoveAllTextures();
1132 if e_Textures
= nil then Exit
;
1134 for i
:= 0 to High(e_Textures
) do
1135 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1139 //------------------------------------------------------------------
1141 //------------------------------------------------------------------
1142 procedure e_ReleaseEngine();
1144 e_RemoveAllTextures
;
1145 e_RemoveAllTextureFont
;
1148 procedure e_BeginRender();
1150 if e_NoGraphics
then Exit
;
1151 glEnable(GL_ALPHA_TEST
);
1152 glAlphaFunc(GL_GREATER
, 0.0);
1155 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1157 if e_NoGraphics
then Exit
;
1158 glClearColor(Red
, Green
, Blue
, 0);
1162 procedure e_Clear(); overload
;
1164 if e_NoGraphics
then Exit
;
1165 glClearColor(0, 0, 0, 0);
1166 glClear(GL_COLOR_BUFFER_BIT
);
1169 procedure e_EndRender();
1171 if e_NoGraphics
then Exit
;
1175 function e_GetGamma(win
: PSDL_Window
): Byte;
1177 ramp
: array [0..256*3-1] of Word;
1178 rgb
: array [0..2] of Double;
1187 if e_NoGraphics
then Exit
;
1192 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1201 for j
:= min
to max
- 1 do
1204 B
:= (j
mod 256)/256;
1206 sum
:= sum
+ ln(A
)/ln(B
);
1209 rgb
[i
] := sum
/ count
;
1212 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1215 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1217 ramp
: array [0..256*3-1] of Word;
1222 if e_NoGraphics
then Exit
;
1223 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1225 for i
:= 0 to 255 do
1227 r
:= Exp(g
* ln(i
/256))*65536;
1228 if r
< 0 then r
:= 0
1229 else if r
> 65535 then r
:= 65535;
1230 ramp
[i
] := trunc(r
);
1231 ramp
[i
+ 256] := trunc(r
);
1232 ramp
[i
+ 512] := trunc(r
);
1235 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1238 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1242 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1246 if e_CharFonts
<> nil then
1247 for i
:= 0 to High(e_CharFonts
) do
1248 if not e_CharFonts
[i
].alive
then
1254 if id
= DWORD(-1) then
1256 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1257 id
:= High(e_CharFonts
);
1260 with e_CharFonts
[id
] do
1262 for i
:= 0 to High(Chars
) do
1276 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1278 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1280 TextureID
:= Texture
;
1285 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1289 if e_NoGraphics
then Exit
;
1290 if Text = '' then Exit
;
1291 if e_CharFonts
= nil then Exit
;
1292 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1294 with e_CharFonts
[FontID
] do
1296 for a
:= 1 to Length(Text) do
1297 with Chars
[Ord(Text[a
])] do
1298 if TextureID
<> -1 then
1300 e_Draw(TextureID
, X
, Y
, 0, True, False);
1301 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1306 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1307 Color
: TRGB
; Scale
: Single = 1.0);
1312 if e_NoGraphics
then Exit
;
1313 if Text = '' then Exit
;
1314 if e_CharFonts
= nil then Exit
;
1315 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1317 with e_CharFonts
[FontID
] do
1319 for a
:= 1 to Length(Text) do
1320 with Chars
[Ord(Text[a
])] do
1321 if TextureID
<> -1 then
1323 if Scale
<> 1.0 then
1326 glScalef(Scale
, Scale
, 0);
1331 e_Draw(TextureID
, X
, Y
, 0, True, False);
1334 if Scale
<> 1.0 then glPopMatrix
;
1336 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1341 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1343 a
, TX
, TY
, len
: Integer;
1347 if e_NoGraphics
then Exit
;
1348 if Text = '' then Exit
;
1349 if e_CharFonts
= nil then Exit
;
1350 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1358 len
:= Length(Text);
1360 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1362 with e_CharFonts
[FontID
] do
1364 for a
:= 1 to len
do
1375 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1380 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1385 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1390 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1395 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1400 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1405 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1410 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1415 with Chars
[Ord(Text[a
])] do
1416 if TextureID
<> -1 then
1420 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1423 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1429 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1437 if Text = '' then Exit
;
1438 if e_CharFonts
= nil then Exit
;
1439 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1441 with e_CharFonts
[FontID
] do
1443 for a
:= 1 to Length(Text) do
1444 with Chars
[Ord(Text[a
])] do
1445 if TextureID
<> -1 then
1447 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1448 e_GetTextureSize(TextureID
, nil, @h2
);
1449 if h2
> h
then h
:= h2
;
1454 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1456 a
, lines
, len
: Integer;
1457 h2
, w2
, tw
, th
: Word;
1464 if Text = '' then Exit
;
1465 if e_CharFonts
= nil then Exit
;
1466 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1469 len
:= Length(Text);
1471 with e_CharFonts
[FontID
] do
1473 for a
:= 1 to len
do
1475 if Text[a
] = #10 then
1478 if w2
> tw
then tw
:= w2
;
1483 with Chars
[Ord(Text[a
])] do
1484 if TextureID
<> -1 then
1486 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1487 e_GetTextureSize(TextureID
, nil, @h2
);
1488 if h2
> th
then th
:= h2
;
1500 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1506 if e_CharFonts
= nil then Exit
;
1507 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1509 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1510 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1513 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1520 if e_CharFonts
= nil then Exit
;
1521 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1523 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1525 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1526 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1528 if h2
> Result
then Result
:= h2
;
1532 procedure e_CharFont_Remove(FontID
: DWORD
);
1536 with e_CharFonts
[FontID
] do
1537 for a
:= 0 to High(Chars
) do
1538 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1540 e_CharFonts
[FontID
].alive
:= False;
1543 procedure e_CharFont_RemoveAll();
1547 if e_CharFonts
= nil then Exit
;
1549 for a
:= 0 to High(e_CharFonts
) do
1550 e_CharFont_Remove(a
);
1555 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1562 if e_NoGraphics
then Exit
;
1563 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1567 if e_TextureFonts
<> nil then
1568 for i
:= 0 to High(e_TextureFonts
) do
1569 if e_TextureFonts
[i
].Base
= 0 then
1575 if id
= DWORD(-1) then
1577 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1578 id
:= High(e_TextureFonts
);
1581 {$IFNDEF USE_NANOGL}
1582 with e_TextureFonts
[id
] do
1584 Base
:= glGenLists(XCount
*YCount
);
1585 TextureID
:= e_Textures
[Tex
].tx
.id
;
1586 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1587 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1594 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1595 for loop1
:= 0 to XCount
*YCount
-1 do
1597 cx
:= (loop1
mod XCount
)/XCount
;
1598 cy
:= (loop1
div YCount
)/YCount
;
1600 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1602 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1603 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1605 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1606 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1608 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1609 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1611 glTexCoord2f(cx
, 1.0-cy
);
1614 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1622 procedure e_TextureFontKill(FontID
: DWORD
);
1624 if e_NoGraphics
then Exit
;
1625 {$IFNDEF USE_NANOGL}
1626 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1628 e_TextureFonts
[FontID
].Base
:= 0;
1631 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1633 if e_NoGraphics
then Exit
;
1634 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1635 if Text = '' then Exit
;
1637 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1640 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1642 {$IFNDEF USE_NANOGL}
1644 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1645 glEnable(GL_TEXTURE_2D
);
1646 glTranslated(x
, y
, 0);
1647 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1648 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1649 glDisable(GL_TEXTURE_2D
);
1653 glDisable(GL_BLEND
);
1656 // god forgive me for this, but i cannot figure out how to do it without lists
1657 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1659 if e_NoGraphics
then Exit
;
1662 {$IFNDEF USE_NANOGL}
1665 glColor4ub(0, 0, 0, 128);
1666 glTranslated(X
+1, Y
+1, 0);
1667 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1672 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1673 glTranslated(X
, Y
, 0);
1674 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1680 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1682 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1683 glEnable(GL_TEXTURE_2D
);
1684 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1686 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1688 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1689 glDisable(GL_TEXTURE_2D
);
1690 glDisable(GL_BLEND
);
1693 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1695 result
:= e_TextureFonts
[FontID
].CharWidth
;
1698 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1700 a
, TX
, TY
, len
: Integer;
1704 if e_NoGraphics
then Exit
;
1705 if Text = '' then Exit
;
1706 if e_TextureFonts
= nil then Exit
;
1707 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1715 len
:= Length(Text);
1717 w
:= e_TextureFonts
[FontID
].CharWidth
;
1719 {$IFNDEF USE_NANOGL}
1720 with e_TextureFonts
[FontID
] do
1722 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1723 glEnable(GL_TEXTURE_2D
);
1724 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1726 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1729 for a
:= 1 to len
do
1740 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1745 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1750 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1755 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1760 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1765 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1770 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1775 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1782 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1787 glDisable(GL_TEXTURE_2D
);
1788 glDisable(GL_BLEND
);
1793 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1794 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1796 if e_NoGraphics
then Exit
;
1797 if Text = '' then Exit
;
1799 {$IFNDEF USE_NANOGL}
1801 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1802 glEnable(GL_TEXTURE_2D
);
1803 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1805 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1810 glColor4ub(0, 0, 0, 128);
1811 glTranslated(x
+1, y
+1, 0);
1812 glScalef(Scale
, Scale
, 0);
1813 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1818 glColor4ub(Red
, Green
, Blue
, 255);
1819 glTranslated(x
, y
, 0);
1820 glScalef(Scale
, Scale
, 0);
1821 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1823 glDisable(GL_TEXTURE_2D
);
1825 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1826 glDisable(GL_BLEND
);
1830 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1834 if e_NoGraphics
then Exit
;
1835 if Integer(ID
) > High(e_TextureFonts
) then
1837 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1838 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1841 procedure e_RemoveAllTextureFont();
1845 if e_NoGraphics
then Exit
;
1846 if e_TextureFonts
= nil then Exit
;
1848 for i
:= 0 to High(e_TextureFonts
) do
1849 if e_TextureFonts
[i
].Base
<> 0 then
1851 {$IFNDEF USE_NANOGL}
1852 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1854 e_TextureFonts
[i
].Base
:= 0;
1857 e_TextureFonts
:= nil;
1860 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1867 function _Point(X
, Y
: Integer): TPoint2i
;
1873 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1877 Result
.Width
:= Width
;
1878 Result
.Height
:= Height
;
1881 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1890 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1892 pixels
, obuf
, scln
, ps
, pd
: PByte;
1895 i
, x
, y
, res
: Integer;
1896 sign
: array [0..7] of Byte;
1897 hbuf
: array [0..12] of Byte;
1902 if e_NoGraphics
then Exit
;
1905 // first, extract and pack graphics data
1906 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1908 GetMem(pixels
, Width
*Height
*3);
1910 FillChar(pixels
^, Width
*Height
*3, 0);
1911 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1912 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1914 if e_FastScreenshots
then
1917 GetMem(scln
, (Width
*3+1)*Height
);
1921 Inc(ps
, (Width
*3)*(Height
-1));
1922 for i
:= 0 to Height
-1 do
1926 Move(ps
^, pd
^, Width
*3);
1938 obufsize
:= (Width
*3+1)*Height
*2;
1939 GetMem(obuf
, obufsize
);
1944 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1945 if res
= Z_OK
then break
;
1946 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1947 obufsize
:= obufsize
*2;
1950 GetMem(obuf
, obufsize
);
1952 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1965 st
.writeBuffer(sign
, 8);
1966 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1969 writeIntBE(st
, LongWord(13));
1974 st
.writeBuffer(sign
, 4);
1975 crc
:= crc32(0, @sign
[0], 4);
1978 hbuf
[2] := (Width
shr 8) and $ff;
1979 hbuf
[3] := Width
and $ff;
1982 hbuf
[6] := (Height
shr 8) and $ff;
1983 hbuf
[7] := Height
and $ff;
1984 hbuf
[8] := 8; // bit depth
1985 hbuf
[9] := 2; // RGB
1986 hbuf
[10] := 0; // compression method
1987 hbuf
[11] := 0; // filter method
1988 hbuf
[12] := 0; // no interlace
1989 crc
:= crc32(crc
, @hbuf
[0], 13);
1990 st
.writeBuffer(hbuf
, 13);
1991 writeIntBE(st
, crc
);
1992 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1995 writeIntBE(st
, LongWord(dlen
));
2000 st
.writeBuffer(sign
, 4);
2001 crc
:= crc32(0, @sign
[0], 4);
2002 crc
:= crc32(crc
, obuf
, dlen
);
2003 st
.writeBuffer(obuf
^, dlen
);
2004 writeIntBE(st
, crc
);
2005 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2008 writeIntBE(st
, LongWord(0));
2013 st
.writeBuffer(sign
, 4);
2014 crc
:= crc32(0, @sign
[0], 4);
2015 writeIntBE(st
, crc
);
2016 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2018 if obuf
<> nil then FreeMem(obuf
);
2023 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2024 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2027 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2029 //writeln(stderr, 'moving pixels...');
2030 for y
:= Height
-1 downto 0 do
2032 for x
:= 0 to Width
-1 do
2034 clr
.r
:= ps
^; Inc(ps
);
2035 clr
.g
:= ps
^; Inc(ps
);
2036 clr
.b
:= ps
^; Inc(ps
);
2038 SetPixel32(img
, x
, y
, clr
);
2041 GlobalMetadata
.ClearMetaItems();
2042 GlobalMetadata
.ClearMetaItemsForSaving();
2043 //writeln(stderr, 'compressing image...');
2044 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2045 //writeln(stderr, 'done!');