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
;
388 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support glGetTexImage
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 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support glBegin(GL_LINES)
962 if e_NoGraphics
then Exit
;
963 // Only top-left/bottom-right quad
980 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
984 glDisable(GL_TEXTURE_2D
);
985 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
989 nX1
:= X1
; nY1
:= Y1
;
990 nX2
:= X2
; nY2
:= Y1
;
991 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
992 glVertex2i(nX1
, nY1
);
993 glVertex2i(nX2
, nY2
);
995 nX1
:= X2
; nY1
:= Y1
;
996 nX2
:= X2
; nY2
:= Y2
;
997 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
998 glVertex2i(nX1
, nY1
);
999 glVertex2i(nX2
, nY2
);
1001 nX1
:= X2
; nY1
:= Y2
;
1002 nX2
:= X1
; nY2
:= Y2
;
1003 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1004 glVertex2i(nX1
, nY1
);
1005 glVertex2i(nX2
, nY2
);
1007 nX1
:= X1
; nY1
:= Y2
;
1008 nX2
:= X1
; nY2
:= Y1
;
1009 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1010 glVertex2i(nX1
, nY1
);
1011 glVertex2i(nX2
, nY2
);
1014 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1016 glDisable(GL_BLEND
);
1020 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
1021 Blending
: TBlending
= TBlending
.None
);
1023 if e_NoGraphics
then Exit
;
1024 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
1027 glDisable(GL_BLEND
);
1029 if Blending
= TBlending
.Blend
then
1030 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
1032 if Blending
= TBlending
.Filter
then
1033 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
1035 if Blending
= TBlending
.Invert
then
1036 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
1039 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1041 glDisable(GL_TEXTURE_2D
);
1042 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1054 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1056 glDisable(GL_BLEND
);
1060 // ////////////////////////////////////////////////////////////////////////// //
1061 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1063 if (a
< 0) then a
:= 0;
1064 if (a
> 255) then a
:= 255;
1066 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1067 glDisable(GL_TEXTURE_2D
);
1068 glColor4ub(0, 0, 0, Byte(255-a
));
1075 //glRect(x, y, x+w, y+h);
1076 glColor4ub(1, 1, 1, 1);
1077 glDisable(GL_BLEND
);
1078 //glBlendEquation(GL_FUNC_ADD);
1081 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1083 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1087 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1089 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support glBegin(GL_LINES)
1090 if e_NoGraphics
then Exit
;
1091 // Pixel-perfect lines
1093 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1098 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1100 glDisable(GL_BLEND
);
1102 glDisable(GL_TEXTURE_2D
);
1103 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1111 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1113 glDisable(GL_BLEND
);
1117 //------------------------------------------------------------------
1118 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1119 //------------------------------------------------------------------
1120 procedure e_DeleteTexture(ID
: DWORD
);
1122 if not e_NoGraphics
then
1123 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1124 e_Textures
[ID
].tx
.id
:= 0;
1125 e_Textures
[ID
].tx
.Width
:= 0;
1126 e_Textures
[ID
].tx
.Height
:= 0;
1129 //------------------------------------------------------------------
1130 // Óäàëÿåò âñå òåêñòóðû
1131 //------------------------------------------------------------------
1132 procedure e_RemoveAllTextures();
1136 if e_Textures
= nil then Exit
;
1138 for i
:= 0 to High(e_Textures
) do
1139 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1143 //------------------------------------------------------------------
1145 //------------------------------------------------------------------
1146 procedure e_ReleaseEngine();
1148 e_RemoveAllTextures
;
1149 e_RemoveAllTextureFont
;
1152 procedure e_BeginRender();
1154 if e_NoGraphics
then Exit
;
1155 glEnable(GL_ALPHA_TEST
);
1156 glAlphaFunc(GL_GREATER
, 0.0);
1159 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1161 if e_NoGraphics
then Exit
;
1162 glClearColor(Red
, Green
, Blue
, 0);
1166 procedure e_Clear(); overload
;
1168 if e_NoGraphics
then Exit
;
1169 glClearColor(0, 0, 0, 0);
1170 glClear(GL_COLOR_BUFFER_BIT
);
1173 procedure e_EndRender();
1175 if e_NoGraphics
then Exit
;
1179 function e_GetGamma(win
: PSDL_Window
): Byte;
1181 ramp
: array [0..256*3-1] of Word;
1182 rgb
: array [0..2] of Double;
1191 if e_NoGraphics
then Exit
;
1196 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1205 for j
:= min
to max
- 1 do
1208 B
:= (j
mod 256)/256;
1210 sum
:= sum
+ ln(A
)/ln(B
);
1213 rgb
[i
] := sum
/ count
;
1216 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1219 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1221 ramp
: array [0..256*3-1] of Word;
1226 if e_NoGraphics
then Exit
;
1227 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1229 for i
:= 0 to 255 do
1231 r
:= Exp(g
* ln(i
/256))*65536;
1232 if r
< 0 then r
:= 0
1233 else if r
> 65535 then r
:= 65535;
1234 ramp
[i
] := trunc(r
);
1235 ramp
[i
+ 256] := trunc(r
);
1236 ramp
[i
+ 512] := trunc(r
);
1239 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1242 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1246 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1250 if e_CharFonts
<> nil then
1251 for i
:= 0 to High(e_CharFonts
) do
1252 if not e_CharFonts
[i
].alive
then
1258 if id
= DWORD(-1) then
1260 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1261 id
:= High(e_CharFonts
);
1264 with e_CharFonts
[id
] do
1266 for i
:= 0 to High(Chars
) do
1280 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1282 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1284 TextureID
:= Texture
;
1289 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1293 if e_NoGraphics
then Exit
;
1294 if Text = '' then Exit
;
1295 if e_CharFonts
= nil then Exit
;
1296 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1298 with e_CharFonts
[FontID
] do
1300 for a
:= 1 to Length(Text) do
1301 with Chars
[Ord(Text[a
])] do
1302 if TextureID
<> -1 then
1304 e_Draw(TextureID
, X
, Y
, 0, True, False);
1305 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1310 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1311 Color
: TRGB
; Scale
: Single = 1.0);
1316 if e_NoGraphics
then Exit
;
1317 if Text = '' then Exit
;
1318 if e_CharFonts
= nil then Exit
;
1319 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1321 with e_CharFonts
[FontID
] do
1323 for a
:= 1 to Length(Text) do
1324 with Chars
[Ord(Text[a
])] do
1325 if TextureID
<> -1 then
1327 if Scale
<> 1.0 then
1330 glScalef(Scale
, Scale
, 0);
1335 e_Draw(TextureID
, X
, Y
, 0, True, False);
1338 if Scale
<> 1.0 then glPopMatrix
;
1340 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1345 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1347 a
, TX
, TY
, len
: Integer;
1351 if e_NoGraphics
then Exit
;
1352 if Text = '' then Exit
;
1353 if e_CharFonts
= nil then Exit
;
1354 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1362 len
:= Length(Text);
1364 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1366 with e_CharFonts
[FontID
] do
1368 for a
:= 1 to len
do
1379 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1384 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1389 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1394 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1399 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1404 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1409 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1414 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1419 with Chars
[Ord(Text[a
])] do
1420 if TextureID
<> -1 then
1424 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1427 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1433 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1441 if Text = '' then Exit
;
1442 if e_CharFonts
= nil then Exit
;
1443 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1445 with e_CharFonts
[FontID
] do
1447 for a
:= 1 to Length(Text) do
1448 with Chars
[Ord(Text[a
])] do
1449 if TextureID
<> -1 then
1451 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1452 e_GetTextureSize(TextureID
, nil, @h2
);
1453 if h2
> h
then h
:= h2
;
1458 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1460 a
, lines
, len
: Integer;
1461 h2
, w2
, tw
, th
: Word;
1468 if Text = '' then Exit
;
1469 if e_CharFonts
= nil then Exit
;
1470 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1473 len
:= Length(Text);
1475 with e_CharFonts
[FontID
] do
1477 for a
:= 1 to len
do
1479 if Text[a
] = #10 then
1482 if w2
> tw
then tw
:= w2
;
1487 with Chars
[Ord(Text[a
])] do
1488 if TextureID
<> -1 then
1490 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1491 e_GetTextureSize(TextureID
, nil, @h2
);
1492 if h2
> th
then th
:= h2
;
1504 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1510 if e_CharFonts
= nil then Exit
;
1511 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1513 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1514 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1517 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1524 if e_CharFonts
= nil then Exit
;
1525 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1527 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1529 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1530 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1532 if h2
> Result
then Result
:= h2
;
1536 procedure e_CharFont_Remove(FontID
: DWORD
);
1540 with e_CharFonts
[FontID
] do
1541 for a
:= 0 to High(Chars
) do
1542 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1544 e_CharFonts
[FontID
].alive
:= False;
1547 procedure e_CharFont_RemoveAll();
1551 if e_CharFonts
= nil then Exit
;
1553 for a
:= 0 to High(e_CharFonts
) do
1554 e_CharFont_Remove(a
);
1559 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1566 if e_NoGraphics
then Exit
;
1567 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1571 if e_TextureFonts
<> nil then
1572 for i
:= 0 to High(e_TextureFonts
) do
1573 if e_TextureFonts
[i
].Base
= 0 then
1579 if id
= DWORD(-1) then
1581 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1582 id
:= High(e_TextureFonts
);
1585 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1586 with e_TextureFonts
[id
] do
1588 Base
:= glGenLists(XCount
*YCount
);
1589 TextureID
:= e_Textures
[Tex
].tx
.id
;
1590 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1591 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1598 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1599 for loop1
:= 0 to XCount
*YCount
-1 do
1601 cx
:= (loop1
mod XCount
)/XCount
;
1602 cy
:= (loop1
div YCount
)/YCount
;
1604 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1606 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1607 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1609 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1610 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1612 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1613 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1615 glTexCoord2f(cx
, 1.0-cy
);
1618 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1626 procedure e_TextureFontKill(FontID
: DWORD
);
1628 if e_NoGraphics
then Exit
;
1629 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1630 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1632 e_TextureFonts
[FontID
].Base
:= 0;
1635 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1637 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1638 if e_NoGraphics
then Exit
;
1639 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1640 if Text = '' then Exit
;
1642 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1645 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1648 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1649 glEnable(GL_TEXTURE_2D
);
1650 glTranslated(x
, y
, 0);
1651 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1652 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1653 glDisable(GL_TEXTURE_2D
);
1656 glDisable(GL_BLEND
);
1660 // god forgive me for this, but i cannot figure out how to do it without lists
1661 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1663 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1664 if e_NoGraphics
then Exit
;
1669 glColor4ub(0, 0, 0, 128);
1670 glTranslated(X
+1, Y
+1, 0);
1671 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1676 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1677 glTranslated(X
, Y
, 0);
1678 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1684 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1686 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1687 glEnable(GL_TEXTURE_2D
);
1688 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1690 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1692 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1693 glDisable(GL_TEXTURE_2D
);
1694 glDisable(GL_BLEND
);
1697 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1699 result
:= e_TextureFonts
[FontID
].CharWidth
;
1702 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1704 a
, TX
, TY
, len
: Integer;
1708 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1709 if e_NoGraphics
then Exit
;
1710 if Text = '' then Exit
;
1711 if e_TextureFonts
= nil then Exit
;
1712 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1720 len
:= Length(Text);
1722 w
:= e_TextureFonts
[FontID
].CharWidth
;
1724 with e_TextureFonts
[FontID
] do
1726 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1727 glEnable(GL_TEXTURE_2D
);
1728 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1730 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1733 for a
:= 1 to len
do
1744 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1749 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1754 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1759 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1764 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1769 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1774 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1779 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1786 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1791 glDisable(GL_TEXTURE_2D
);
1792 glDisable(GL_BLEND
);
1797 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1798 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1800 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1801 if e_NoGraphics
then Exit
;
1802 if Text = '' then Exit
;
1805 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1806 glEnable(GL_TEXTURE_2D
);
1807 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1809 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1814 glColor4ub(0, 0, 0, 128);
1815 glTranslated(x
+1, y
+1, 0);
1816 glScalef(Scale
, Scale
, 0);
1817 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1822 glColor4ub(Red
, Green
, Blue
, 255);
1823 glTranslated(x
, y
, 0);
1824 glScalef(Scale
, Scale
, 0);
1825 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1827 glDisable(GL_TEXTURE_2D
);
1829 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1830 glDisable(GL_BLEND
);
1834 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1838 if e_NoGraphics
then Exit
;
1839 if Integer(ID
) > High(e_TextureFonts
) then
1841 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1842 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1845 procedure e_RemoveAllTextureFont();
1849 if e_NoGraphics
then Exit
;
1850 if e_TextureFonts
= nil then Exit
;
1852 for i
:= 0 to High(e_TextureFonts
) do
1853 if e_TextureFonts
[i
].Base
<> 0 then
1855 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1856 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1858 e_TextureFonts
[i
].Base
:= 0;
1861 e_TextureFonts
:= nil;
1864 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1871 function _Point(X
, Y
: Integer): TPoint2i
;
1877 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1881 Result
.Width
:= Width
;
1882 Result
.Height
:= Height
;
1885 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1894 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1896 pixels
, obuf
, scln
, ps
, pd
: PByte;
1899 i
, x
, y
, res
: Integer;
1900 sign
: array [0..7] of Byte;
1901 hbuf
: array [0..12] of Byte;
1906 if e_NoGraphics
then Exit
;
1909 // first, extract and pack graphics data
1910 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1912 GetMem(pixels
, Width
*Height
*3);
1914 FillChar(pixels
^, Width
*Height
*3, 0);
1915 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1916 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1918 if e_FastScreenshots
then
1921 GetMem(scln
, (Width
*3+1)*Height
);
1925 Inc(ps
, (Width
*3)*(Height
-1));
1926 for i
:= 0 to Height
-1 do
1930 Move(ps
^, pd
^, Width
*3);
1942 obufsize
:= (Width
*3+1)*Height
*2;
1943 GetMem(obuf
, obufsize
);
1948 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1949 if res
= Z_OK
then break
;
1950 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1951 obufsize
:= obufsize
*2;
1954 GetMem(obuf
, obufsize
);
1956 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1969 st
.writeBuffer(sign
, 8);
1970 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1973 writeIntBE(st
, LongWord(13));
1978 st
.writeBuffer(sign
, 4);
1979 crc
:= crc32(0, @sign
[0], 4);
1982 hbuf
[2] := (Width
shr 8) and $ff;
1983 hbuf
[3] := Width
and $ff;
1986 hbuf
[6] := (Height
shr 8) and $ff;
1987 hbuf
[7] := Height
and $ff;
1988 hbuf
[8] := 8; // bit depth
1989 hbuf
[9] := 2; // RGB
1990 hbuf
[10] := 0; // compression method
1991 hbuf
[11] := 0; // filter method
1992 hbuf
[12] := 0; // no interlace
1993 crc
:= crc32(crc
, @hbuf
[0], 13);
1994 st
.writeBuffer(hbuf
, 13);
1995 writeIntBE(st
, crc
);
1996 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1999 writeIntBE(st
, LongWord(dlen
));
2004 st
.writeBuffer(sign
, 4);
2005 crc
:= crc32(0, @sign
[0], 4);
2006 crc
:= crc32(crc
, obuf
, dlen
);
2007 st
.writeBuffer(obuf
^, dlen
);
2008 writeIntBE(st
, crc
);
2009 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2012 writeIntBE(st
, LongWord(0));
2017 st
.writeBuffer(sign
, 4);
2018 crc
:= crc32(0, @sign
[0], 4);
2019 writeIntBE(st
, crc
);
2020 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2022 if obuf
<> nil then FreeMem(obuf
);
2027 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2028 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2031 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2033 //writeln(stderr, 'moving pixels...');
2034 for y
:= Height
-1 downto 0 do
2036 for x
:= 0 to Width
-1 do
2038 clr
.r
:= ps
^; Inc(ps
);
2039 clr
.g
:= ps
^; Inc(ps
);
2040 clr
.b
:= ps
^; Inc(ps
);
2042 SetPixel32(img
, x
, y
, clr
);
2045 GlobalMetadata
.ClearMetaItems();
2046 GlobalMetadata
.ClearMetaItemsForSaving();
2047 //writeln(stderr, 'compressing image...');
2048 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2049 //writeln(stderr, 'done!');