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);
1090 v
: array [0..3] of GLfloat
;
1093 if e_NoGraphics
then Exit
;
1094 // Pixel-perfect lines
1096 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1101 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1103 glDisable(GL_BLEND
);
1105 glDisable(GL_TEXTURE_2D
);
1106 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1110 v
[0] := X1
; v
[1] := Y1
; v
[2] := X2
; v
[3] := Y2
;
1111 glVertexPointer(2, GL_FLOAT
, 0, @v
[0]);
1112 glEnableClientState(GL_VERTEX_ARRAY
);
1113 glDisableClientState(GL_COLOR_ARRAY
);
1114 glDisableClientState(GL_NORMAL_ARRAY
);
1115 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
1116 glDrawArrays(GL_LINES
, 0, 4);
1124 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1126 glDisable(GL_BLEND
);
1129 //------------------------------------------------------------------
1130 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1131 //------------------------------------------------------------------
1132 procedure e_DeleteTexture(ID
: DWORD
);
1134 if not e_NoGraphics
then
1135 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1136 e_Textures
[ID
].tx
.id
:= 0;
1137 e_Textures
[ID
].tx
.Width
:= 0;
1138 e_Textures
[ID
].tx
.Height
:= 0;
1141 //------------------------------------------------------------------
1142 // Óäàëÿåò âñå òåêñòóðû
1143 //------------------------------------------------------------------
1144 procedure e_RemoveAllTextures();
1148 if e_Textures
= nil then Exit
;
1150 for i
:= 0 to High(e_Textures
) do
1151 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1155 //------------------------------------------------------------------
1157 //------------------------------------------------------------------
1158 procedure e_ReleaseEngine();
1160 e_RemoveAllTextures
;
1161 e_RemoveAllTextureFont
;
1164 procedure e_BeginRender();
1166 if e_NoGraphics
then Exit
;
1167 glEnable(GL_ALPHA_TEST
);
1168 glAlphaFunc(GL_GREATER
, 0.0);
1171 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1173 if e_NoGraphics
then Exit
;
1174 glClearColor(Red
, Green
, Blue
, 0);
1178 procedure e_Clear(); overload
;
1180 if e_NoGraphics
then Exit
;
1181 glClearColor(0, 0, 0, 0);
1182 glClear(GL_COLOR_BUFFER_BIT
);
1185 procedure e_EndRender();
1187 if e_NoGraphics
then Exit
;
1191 function e_GetGamma(win
: PSDL_Window
): Byte;
1193 ramp
: array [0..256*3-1] of Word;
1194 rgb
: array [0..2] of Double;
1203 if e_NoGraphics
then Exit
;
1208 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1217 for j
:= min
to max
- 1 do
1220 B
:= (j
mod 256)/256;
1222 sum
:= sum
+ ln(A
)/ln(B
);
1225 rgb
[i
] := sum
/ count
;
1228 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1231 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1233 ramp
: array [0..256*3-1] of Word;
1238 if e_NoGraphics
then Exit
;
1239 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1241 for i
:= 0 to 255 do
1243 r
:= Exp(g
* ln(i
/256))*65536;
1244 if r
< 0 then r
:= 0
1245 else if r
> 65535 then r
:= 65535;
1246 ramp
[i
] := trunc(r
);
1247 ramp
[i
+ 256] := trunc(r
);
1248 ramp
[i
+ 512] := trunc(r
);
1251 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1254 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1258 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1262 if e_CharFonts
<> nil then
1263 for i
:= 0 to High(e_CharFonts
) do
1264 if not e_CharFonts
[i
].alive
then
1270 if id
= DWORD(-1) then
1272 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1273 id
:= High(e_CharFonts
);
1276 with e_CharFonts
[id
] do
1278 for i
:= 0 to High(Chars
) do
1292 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1294 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1296 TextureID
:= Texture
;
1301 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1305 if e_NoGraphics
then Exit
;
1306 if Text = '' then Exit
;
1307 if e_CharFonts
= nil then Exit
;
1308 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1310 with e_CharFonts
[FontID
] do
1312 for a
:= 1 to Length(Text) do
1313 with Chars
[Ord(Text[a
])] do
1314 if TextureID
<> -1 then
1316 e_Draw(TextureID
, X
, Y
, 0, True, False);
1317 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1322 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1323 Color
: TRGB
; Scale
: Single = 1.0);
1328 if e_NoGraphics
then Exit
;
1329 if Text = '' then Exit
;
1330 if e_CharFonts
= nil then Exit
;
1331 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1333 with e_CharFonts
[FontID
] do
1335 for a
:= 1 to Length(Text) do
1336 with Chars
[Ord(Text[a
])] do
1337 if TextureID
<> -1 then
1339 if Scale
<> 1.0 then
1342 glScalef(Scale
, Scale
, 0);
1347 e_Draw(TextureID
, X
, Y
, 0, True, False);
1350 if Scale
<> 1.0 then glPopMatrix
;
1352 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1357 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1359 a
, TX
, TY
, len
: Integer;
1363 if e_NoGraphics
then Exit
;
1364 if Text = '' then Exit
;
1365 if e_CharFonts
= nil then Exit
;
1366 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1374 len
:= Length(Text);
1376 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1378 with e_CharFonts
[FontID
] do
1380 for a
:= 1 to len
do
1391 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1396 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1401 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1406 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1411 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1416 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1421 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1426 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1431 with Chars
[Ord(Text[a
])] do
1432 if TextureID
<> -1 then
1436 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1439 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1445 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1453 if Text = '' then Exit
;
1454 if e_CharFonts
= nil then Exit
;
1455 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1457 with e_CharFonts
[FontID
] do
1459 for a
:= 1 to Length(Text) do
1460 with Chars
[Ord(Text[a
])] do
1461 if TextureID
<> -1 then
1463 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1464 e_GetTextureSize(TextureID
, nil, @h2
);
1465 if h2
> h
then h
:= h2
;
1470 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1472 a
, lines
, len
: Integer;
1473 h2
, w2
, tw
, th
: Word;
1480 if Text = '' then Exit
;
1481 if e_CharFonts
= nil then Exit
;
1482 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1485 len
:= Length(Text);
1487 with e_CharFonts
[FontID
] do
1489 for a
:= 1 to len
do
1491 if Text[a
] = #10 then
1494 if w2
> tw
then tw
:= w2
;
1499 with Chars
[Ord(Text[a
])] do
1500 if TextureID
<> -1 then
1502 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1503 e_GetTextureSize(TextureID
, nil, @h2
);
1504 if h2
> th
then th
:= h2
;
1516 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1522 if e_CharFonts
= nil then Exit
;
1523 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1525 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1526 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1529 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1536 if e_CharFonts
= nil then Exit
;
1537 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1539 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1541 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1542 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1544 if h2
> Result
then Result
:= h2
;
1548 procedure e_CharFont_Remove(FontID
: DWORD
);
1552 with e_CharFonts
[FontID
] do
1553 for a
:= 0 to High(Chars
) do
1554 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1556 e_CharFonts
[FontID
].alive
:= False;
1559 procedure e_CharFont_RemoveAll();
1563 if e_CharFonts
= nil then Exit
;
1565 for a
:= 0 to High(e_CharFonts
) do
1566 e_CharFont_Remove(a
);
1571 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1578 if e_NoGraphics
then Exit
;
1579 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1583 if e_TextureFonts
<> nil then
1584 for i
:= 0 to High(e_TextureFonts
) do
1585 if e_TextureFonts
[i
].Base
= 0 then
1591 if id
= DWORD(-1) then
1593 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1594 id
:= High(e_TextureFonts
);
1597 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1598 with e_TextureFonts
[id
] do
1600 Base
:= glGenLists(XCount
*YCount
);
1601 TextureID
:= e_Textures
[Tex
].tx
.id
;
1602 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1603 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1610 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1611 for loop1
:= 0 to XCount
*YCount
-1 do
1613 cx
:= (loop1
mod XCount
)/XCount
;
1614 cy
:= (loop1
div YCount
)/YCount
;
1616 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1618 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1619 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1621 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1622 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1624 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1625 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1627 glTexCoord2f(cx
, 1.0-cy
);
1630 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1638 procedure e_TextureFontKill(FontID
: DWORD
);
1640 if e_NoGraphics
then Exit
;
1641 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1642 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1644 e_TextureFonts
[FontID
].Base
:= 0;
1647 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1649 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1650 if e_NoGraphics
then Exit
;
1651 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1652 if Text = '' then Exit
;
1654 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1657 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1660 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1661 glEnable(GL_TEXTURE_2D
);
1662 glTranslated(x
, y
, 0);
1663 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1664 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1665 glDisable(GL_TEXTURE_2D
);
1668 glDisable(GL_BLEND
);
1672 // god forgive me for this, but i cannot figure out how to do it without lists
1673 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1675 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1676 if e_NoGraphics
then Exit
;
1681 glColor4ub(0, 0, 0, 128);
1682 glTranslated(X
+1, Y
+1, 0);
1683 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1688 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1689 glTranslated(X
, Y
, 0);
1690 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1696 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1698 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1699 glEnable(GL_TEXTURE_2D
);
1700 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1702 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1704 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1705 glDisable(GL_TEXTURE_2D
);
1706 glDisable(GL_BLEND
);
1709 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1711 result
:= e_TextureFonts
[FontID
].CharWidth
;
1714 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1716 a
, TX
, TY
, len
: Integer;
1720 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1721 if e_NoGraphics
then Exit
;
1722 if Text = '' then Exit
;
1723 if e_TextureFonts
= nil then Exit
;
1724 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1732 len
:= Length(Text);
1734 w
:= e_TextureFonts
[FontID
].CharWidth
;
1736 with e_TextureFonts
[FontID
] do
1738 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1739 glEnable(GL_TEXTURE_2D
);
1740 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1742 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1745 for a
:= 1 to len
do
1756 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1761 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1766 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1771 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1776 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1781 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1786 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1791 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1798 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1803 glDisable(GL_TEXTURE_2D
);
1804 glDisable(GL_BLEND
);
1809 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1810 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1812 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1813 if e_NoGraphics
then Exit
;
1814 if Text = '' then Exit
;
1817 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1818 glEnable(GL_TEXTURE_2D
);
1819 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1821 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1826 glColor4ub(0, 0, 0, 128);
1827 glTranslated(x
+1, y
+1, 0);
1828 glScalef(Scale
, Scale
, 0);
1829 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1834 glColor4ub(Red
, Green
, Blue
, 255);
1835 glTranslated(x
, y
, 0);
1836 glScalef(Scale
, Scale
, 0);
1837 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1839 glDisable(GL_TEXTURE_2D
);
1841 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1842 glDisable(GL_BLEND
);
1846 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1850 if e_NoGraphics
then Exit
;
1851 if Integer(ID
) > High(e_TextureFonts
) then
1853 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1854 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1857 procedure e_RemoveAllTextureFont();
1861 if e_NoGraphics
then Exit
;
1862 if e_TextureFonts
= nil then Exit
;
1864 for i
:= 0 to High(e_TextureFonts
) do
1865 if e_TextureFonts
[i
].Base
<> 0 then
1867 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support gl-lists
1868 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1870 e_TextureFonts
[i
].Base
:= 0;
1873 e_TextureFonts
:= nil;
1876 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1883 function _Point(X
, Y
: Integer): TPoint2i
;
1889 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1893 Result
.Width
:= Width
;
1894 Result
.Height
:= Height
;
1897 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1906 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1908 pixels
, obuf
, scln
, ps
, pd
: PByte;
1911 i
, x
, y
, res
: Integer;
1912 sign
: array [0..7] of Byte;
1913 hbuf
: array [0..12] of Byte;
1918 if e_NoGraphics
then Exit
;
1921 // first, extract and pack graphics data
1922 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1924 GetMem(pixels
, Width
*Height
*3);
1926 FillChar(pixels
^, Width
*Height
*3, 0);
1927 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1928 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1930 if e_FastScreenshots
then
1933 GetMem(scln
, (Width
*3+1)*Height
);
1937 Inc(ps
, (Width
*3)*(Height
-1));
1938 for i
:= 0 to Height
-1 do
1942 Move(ps
^, pd
^, Width
*3);
1954 obufsize
:= (Width
*3+1)*Height
*2;
1955 GetMem(obuf
, obufsize
);
1960 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1961 if res
= Z_OK
then break
;
1962 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1963 obufsize
:= obufsize
*2;
1966 GetMem(obuf
, obufsize
);
1968 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1981 st
.writeBuffer(sign
, 8);
1982 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1985 writeIntBE(st
, LongWord(13));
1990 st
.writeBuffer(sign
, 4);
1991 crc
:= crc32(0, @sign
[0], 4);
1994 hbuf
[2] := (Width
shr 8) and $ff;
1995 hbuf
[3] := Width
and $ff;
1998 hbuf
[6] := (Height
shr 8) and $ff;
1999 hbuf
[7] := Height
and $ff;
2000 hbuf
[8] := 8; // bit depth
2001 hbuf
[9] := 2; // RGB
2002 hbuf
[10] := 0; // compression method
2003 hbuf
[11] := 0; // filter method
2004 hbuf
[12] := 0; // no interlace
2005 crc
:= crc32(crc
, @hbuf
[0], 13);
2006 st
.writeBuffer(hbuf
, 13);
2007 writeIntBE(st
, crc
);
2008 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2011 writeIntBE(st
, LongWord(dlen
));
2016 st
.writeBuffer(sign
, 4);
2017 crc
:= crc32(0, @sign
[0], 4);
2018 crc
:= crc32(crc
, obuf
, dlen
);
2019 st
.writeBuffer(obuf
^, dlen
);
2020 writeIntBE(st
, crc
);
2021 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2024 writeIntBE(st
, LongWord(0));
2029 st
.writeBuffer(sign
, 4);
2030 crc
:= crc32(0, @sign
[0], 4);
2031 writeIntBE(st
, crc
);
2032 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2034 if obuf
<> nil then FreeMem(obuf
);
2039 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2040 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2043 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2045 //writeln(stderr, 'moving pixels...');
2046 for y
:= Height
-1 downto 0 do
2048 for x
:= 0 to Width
-1 do
2050 clr
.r
:= ps
^; Inc(ps
);
2051 clr
.g
:= ps
^; Inc(ps
);
2052 clr
.b
:= ps
^; Inc(ps
);
2054 SetPixel32(img
, x
, y
, clr
);
2057 GlobalMetadata
.ClearMetaItems();
2058 GlobalMetadata
.ClearMetaItemsForSaving();
2059 //writeln(stderr, 'compressing image...');
2060 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2061 //writeln(stderr, 'done!');