c5ec8f36e94baae90f47ca21bbd20d32416a36d9
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 procedure e_DeleteTexture(ID
: DWORD
);
99 procedure e_RemoveAllTextures();
102 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
103 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
104 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
105 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
106 Color
: TRGB
; Scale
: Single = 1.0);
107 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
108 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
109 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
110 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
111 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
112 procedure e_CharFont_Remove(FontID
: DWORD
);
113 procedure e_CharFont_RemoveAll();
116 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
118 procedure e_TextureFontKill(FontID
: DWORD
);
119 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
120 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
121 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
122 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
123 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
124 procedure e_RemoveAllTextureFont();
126 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
127 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
129 procedure e_ReleaseEngine();
130 procedure e_BeginRender();
131 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
132 procedure e_Clear(); overload
;
133 procedure e_EndRender();
135 function e_GetGamma(win
: PSDL_Window
): Byte;
136 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
138 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
140 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
141 function _Point(X
, Y
: Integer): TPoint2i
;
142 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
143 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
145 //function e_getTextGLId (ID: DWORD): GLuint;
149 e_NoGraphics
: Boolean = False;
150 e_FastScreenshots
: Boolean = true; // it's REALLY SLOW with `false`
151 g_dbg_scale
: Single = 1.0;
165 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 procedure e_ResizeWindow(Width
, Height
: Integer);
376 e_SetViewPort(0, 0, Width
, Height
);
379 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
381 x1
, y1
, tmp
: Integer;
383 if (w
< 1) or (h
< 1) then exit
;
386 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
387 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
388 //HACK: make texture one pixel shorter, so it won't wrap
389 if (g_dbg_scale
<> 1.0) then
394 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
395 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
396 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
397 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
400 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
401 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
403 if e_NoGraphics
then Exit
;
404 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
406 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
411 if (AlphaChannel
) or (Alpha
> 0) then
412 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
415 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
418 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
420 glEnable(GL_TEXTURE_2D
);
421 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
424 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
);
426 //u := e_Textures[ID].tx.u;
427 //v := e_Textures[ID].tx.v;
430 if Mirror = M_NONE then
432 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
433 glTexCoord2f(0, 0); glVertex2i(X, Y);
434 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
435 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
438 if Mirror = M_HORIZONTAL then
440 glTexCoord2f(u, 0); glVertex2i(X, Y);
441 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
442 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
443 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
446 if Mirror = M_VERTICAL then
448 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
449 glTexCoord2f(0, -v); glVertex2i(X, Y);
450 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
451 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
460 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
461 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
465 if e_NoGraphics
then Exit
;
466 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
468 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
473 if (AlphaChannel
) or (Alpha
> 0) then
474 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
477 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
480 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
482 glEnable(GL_TEXTURE_2D
);
483 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
485 u
:= e_Textures
[ID
].tx
.u
;
486 v
:= e_Textures
[ID
].tx
.v
;
489 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
490 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
491 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
492 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
498 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
499 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
501 if e_NoGraphics
then Exit
;
502 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
504 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
509 if (AlphaChannel
) or (Alpha
> 0) then
510 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
513 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
516 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
518 glEnable(GL_TEXTURE_2D
);
519 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
521 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
);
527 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
528 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
530 X2
, Y2
, dx
, w
, h
: Integer;
533 if e_NoGraphics
then Exit
;
534 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
535 ambientBlendMode
:= false;
537 if (Alpha
> 0) or AlphaChannel
or Blending
then
543 if not ambientBlendMode
then glDisable(GL_BLEND
);
545 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
546 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
547 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
549 if (XCount
= 0) then XCount
:= 1;
550 if (YCount
= 0) then YCount
:= 1;
552 glEnable(GL_TEXTURE_2D
);
553 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
555 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
556 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
558 //k8: this SHOULD work... i hope
559 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
562 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
563 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
564 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
565 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
572 u
:= e_Textures
[ID
].tx
.u
;
573 v
:= e_Textures
[ID
].tx
.v
;
574 w
:= e_Textures
[ID
].tx
.width
;
575 h
:= e_Textures
[ID
].tx
.height
;
582 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
583 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
584 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
585 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
600 //TODO: overflow checks
601 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
606 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
607 // check for intersection
608 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
609 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
613 if (x0
< x1
) then x0
:= x1
;
614 if (y0
< y1
) then y0
:= y1
;
615 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
616 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
619 result
:= (w0
> 0) and (h0
> 0);
623 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
624 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
629 scxywh: array[0..3] of GLint;
630 vpxywh: array[0..3] of GLint;
632 w
, h
, dw
, cw
, ch
, yofs
: Integer;
633 u
, v
, cu
, cv
: Single;
637 procedure setScissorGLInternal (x, y, w, h: Integer);
639 //if not scallowed then exit;
644 y := vpxywh[3]-(y+h);
645 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
647 glScissor(0, 0, 0, 0);
651 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
652 glScissor(x, y, w, h);
658 if e_NoGraphics
then exit
;
659 ambientBlendMode
:= false;
661 if (wdt
< 1) or (hgt
< 1) then exit
;
663 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
665 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
669 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
671 if (Alpha
> 0) or AlphaChannel
or Blending
then
677 if not ambientBlendMode
then glDisable(GL_BLEND
);
679 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
680 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
681 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
683 glEnable(GL_TEXTURE_2D
);
684 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
689 //k8: this SHOULD work... i hope
690 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
693 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
694 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
695 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
696 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
701 // hard day's night; setup scissor
703 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
704 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
705 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
706 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
707 //glEnable(GL_SCISSOR_TEST);
708 setScissorGLInternal(x, y, wdt, hgt);
711 u
:= e_Textures
[ID
].tx
.u
;
712 v
:= e_Textures
[ID
].tx
.v
;
713 w
:= e_Textures
[ID
].tx
.width
;
714 h
:= e_Textures
[ID
].tx
.height
;
716 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
720 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
721 if onlyOneY
then yofs
:= 0;
727 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
729 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
730 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
731 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
732 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
738 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
745 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
747 if e_NoGraphics
then exit
;
748 if (w
< 1) or (h
< 1) then exit
;
749 if (a
<> 255) or ((r
or g
or b
) <> 0) then
752 glDisable(GL_TEXTURE_2D
);
753 glColor4ub(r
, g
, b
, a
);
754 if ((r
or g
or b
) <> 0) then
756 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
760 glVertex2i(x
+w
, y
+h
);
764 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
768 glVertex2i(x
+w
, y
+h
);
776 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
777 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
779 if e_NoGraphics
then Exit
;
781 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
783 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
788 if (AlphaChannel
) or (Alpha
> 0) then
789 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
792 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
795 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
797 if (Angle
<> 0) and (RC
<> nil) then
800 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
801 glRotatef(Angle
, 0, 0, 1);
802 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
805 glEnable(GL_TEXTURE_2D
);
806 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
807 glBegin(GL_QUADS
); //0-1 1-1
809 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
);
818 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
820 if e_NoGraphics
then Exit
;
821 glDisable(GL_TEXTURE_2D
);
822 glColor3ub(Red
, Green
, Blue
);
825 if (Size
= 2) or (Size
= 4) then
829 glVertex2f(X
+0.3, Y
+1.0);
832 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
835 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
837 // Make lines only top-left/bottom-right and top-right/bottom-left
849 // Pixel-perfect hack
857 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
859 nX1
, nY1
, nX2
, nY2
: Integer;
861 v
: array [0..15] of GLfloat
;
864 if e_NoGraphics
then Exit
;
865 // Only top-left/bottom-right quad
882 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
886 glDisable(GL_TEXTURE_2D
);
887 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
890 nX1
:= X1
; nY1
:= Y1
;
891 nX2
:= X2
; nY2
:= Y1
;
892 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
893 v
[0] := nX1
; v
[1] := nY1
; v
[2] := nX2
; v
[3] := nY2
;
895 nX1
:= X2
; nY1
:= Y1
;
896 nX2
:= X2
; nY2
:= Y2
;
897 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
898 v
[4] := nX1
; v
[5] := nY1
; v
[6] := nX2
; v
[7] := nY2
;
900 nX1
:= X2
; nY1
:= Y2
;
901 nX2
:= X1
; nY2
:= Y2
;
902 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
903 v
[8] := nX1
; v
[9] := nY1
; v
[10] := nX2
; v
[11] := nY2
;
905 nX1
:= X1
; nY1
:= Y2
;
906 nX2
:= X1
; nY2
:= Y1
;
907 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
908 v
[12] := nX1
; v
[13] := nY1
; v
[14] := nX2
; v
[15] := nY2
;
910 glVertexPointer(2, GL_FLOAT
, 0, @v
[0]);
911 glEnableClientState(GL_VERTEX_ARRAY
);
912 glDisableClientState(GL_COLOR_ARRAY
);
913 glDisableClientState(GL_NORMAL_ARRAY
);
914 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
915 glDrawArrays(GL_LINES
, 0, 16);
918 nX1
:= X1
; nY1
:= Y1
;
919 nX2
:= X2
; nY2
:= Y1
;
920 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
921 glVertex2i(nX1
, nY1
);
922 glVertex2i(nX2
, nY2
);
924 nX1
:= X2
; nY1
:= Y1
;
925 nX2
:= X2
; nY2
:= Y2
;
926 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
927 glVertex2i(nX1
, nY1
);
928 glVertex2i(nX2
, nY2
);
930 nX1
:= X2
; nY1
:= Y2
;
931 nX2
:= X1
; nY2
:= Y2
;
932 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
933 glVertex2i(nX1
, nY1
);
934 glVertex2i(nX2
, nY2
);
936 nX1
:= X1
; nY1
:= Y2
;
937 nX2
:= X1
; nY2
:= Y1
;
938 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
939 glVertex2i(nX1
, nY1
);
940 glVertex2i(nX2
, nY2
);
944 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
949 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
950 Blending
: TBlending
= TBlending
.None
);
952 if e_NoGraphics
then Exit
;
953 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
958 if Blending
= TBlending
.Blend
then
959 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
961 if Blending
= TBlending
.Filter
then
962 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
964 if Blending
= TBlending
.Invert
then
965 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
968 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
970 glDisable(GL_TEXTURE_2D
);
971 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
983 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
989 // ////////////////////////////////////////////////////////////////////////// //
990 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
992 if (a
< 0) then a
:= 0;
993 if (a
> 255) then a
:= 255;
995 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
996 glDisable(GL_TEXTURE_2D
);
997 glColor4ub(0, 0, 0, Byte(255-a
));
1004 //glRect(x, y, x+w, y+h);
1005 glColor4ub(1, 1, 1, 1);
1006 glDisable(GL_BLEND
);
1007 //glBlendEquation(GL_FUNC_ADD);
1010 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1012 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1016 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1019 v
: array [0..3] of GLfloat
;
1022 if e_NoGraphics
then Exit
;
1023 // Pixel-perfect lines
1025 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1030 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1032 glDisable(GL_BLEND
);
1034 glDisable(GL_TEXTURE_2D
);
1035 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1039 v
[0] := X1
; v
[1] := Y1
; v
[2] := X2
; v
[3] := Y2
;
1040 glVertexPointer(2, GL_FLOAT
, 0, @v
[0]);
1041 glEnableClientState(GL_VERTEX_ARRAY
);
1042 glDisableClientState(GL_COLOR_ARRAY
);
1043 glDisableClientState(GL_NORMAL_ARRAY
);
1044 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
1045 glDrawArrays(GL_LINES
, 0, 4);
1053 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1055 glDisable(GL_BLEND
);
1058 //------------------------------------------------------------------
1059 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1060 //------------------------------------------------------------------
1061 procedure e_DeleteTexture(ID
: DWORD
);
1063 if not e_NoGraphics
then
1064 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1065 e_Textures
[ID
].tx
.id
:= 0;
1066 e_Textures
[ID
].tx
.Width
:= 0;
1067 e_Textures
[ID
].tx
.Height
:= 0;
1070 //------------------------------------------------------------------
1071 // Óäàëÿåò âñå òåêñòóðû
1072 //------------------------------------------------------------------
1073 procedure e_RemoveAllTextures();
1077 if e_Textures
= nil then Exit
;
1079 for i
:= 0 to High(e_Textures
) do
1080 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1084 //------------------------------------------------------------------
1086 //------------------------------------------------------------------
1087 procedure e_ReleaseEngine();
1089 e_RemoveAllTextures
;
1090 e_RemoveAllTextureFont
;
1093 procedure e_BeginRender();
1095 if e_NoGraphics
then Exit
;
1096 glEnable(GL_ALPHA_TEST
);
1097 glAlphaFunc(GL_GREATER
, 0.0);
1100 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1102 if e_NoGraphics
then Exit
;
1103 glClearColor(Red
, Green
, Blue
, 0);
1107 procedure e_Clear(); overload
;
1109 if e_NoGraphics
then Exit
;
1110 glClearColor(0, 0, 0, 0);
1111 glClear(GL_COLOR_BUFFER_BIT
);
1114 procedure e_EndRender();
1116 if e_NoGraphics
then Exit
;
1120 function e_GetGamma(win
: PSDL_Window
): Byte;
1122 ramp
: array [0..256*3-1] of Word;
1123 rgb
: array [0..2] of Double;
1132 if e_NoGraphics
then Exit
;
1137 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1146 for j
:= min
to max
- 1 do
1149 B
:= (j
mod 256)/256;
1151 sum
:= sum
+ ln(A
)/ln(B
);
1154 rgb
[i
] := sum
/ count
;
1157 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1160 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1162 ramp
: array [0..256*3-1] of Word;
1167 if e_NoGraphics
then Exit
;
1168 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1170 for i
:= 0 to 255 do
1172 r
:= Exp(g
* ln(i
/256))*65536;
1173 if r
< 0 then r
:= 0
1174 else if r
> 65535 then r
:= 65535;
1175 ramp
[i
] := trunc(r
);
1176 ramp
[i
+ 256] := trunc(r
);
1177 ramp
[i
+ 512] := trunc(r
);
1180 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1183 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1187 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1191 if e_CharFonts
<> nil then
1192 for i
:= 0 to High(e_CharFonts
) do
1193 if not e_CharFonts
[i
].alive
then
1199 if id
= DWORD(-1) then
1201 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1202 id
:= High(e_CharFonts
);
1205 with e_CharFonts
[id
] do
1207 for i
:= 0 to High(Chars
) do
1221 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1223 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1225 TextureID
:= Texture
;
1230 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1234 if e_NoGraphics
then Exit
;
1235 if Text = '' then Exit
;
1236 if e_CharFonts
= nil then Exit
;
1237 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1239 with e_CharFonts
[FontID
] do
1241 for a
:= 1 to Length(Text) do
1242 with Chars
[Ord(Text[a
])] do
1243 if TextureID
<> -1 then
1245 e_Draw(TextureID
, X
, Y
, 0, True, False);
1246 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1251 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1252 Color
: TRGB
; Scale
: Single = 1.0);
1257 if e_NoGraphics
then Exit
;
1258 if Text = '' then Exit
;
1259 if e_CharFonts
= nil then Exit
;
1260 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1262 with e_CharFonts
[FontID
] do
1264 for a
:= 1 to Length(Text) do
1265 with Chars
[Ord(Text[a
])] do
1266 if TextureID
<> -1 then
1268 if Scale
<> 1.0 then
1271 glScalef(Scale
, Scale
, 0);
1276 e_Draw(TextureID
, X
, Y
, 0, True, False);
1279 if Scale
<> 1.0 then glPopMatrix
;
1281 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1286 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1288 a
, TX
, TY
, len
: Integer;
1292 if e_NoGraphics
then Exit
;
1293 if Text = '' then Exit
;
1294 if e_CharFonts
= nil then Exit
;
1295 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1303 len
:= Length(Text);
1305 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1307 with e_CharFonts
[FontID
] do
1309 for a
:= 1 to len
do
1320 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1325 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1330 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1335 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1340 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1345 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1350 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1355 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1360 with Chars
[Ord(Text[a
])] do
1361 if TextureID
<> -1 then
1365 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1368 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1374 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1382 if Text = '' then Exit
;
1383 if e_CharFonts
= nil then Exit
;
1384 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1386 with e_CharFonts
[FontID
] do
1388 for a
:= 1 to Length(Text) do
1389 with Chars
[Ord(Text[a
])] do
1390 if TextureID
<> -1 then
1392 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1393 e_GetTextureSize(TextureID
, nil, @h2
);
1394 if h2
> h
then h
:= h2
;
1399 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1401 a
, lines
, len
: Integer;
1402 h2
, w2
, tw
, th
: Word;
1409 if Text = '' then Exit
;
1410 if e_CharFonts
= nil then Exit
;
1411 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1414 len
:= Length(Text);
1416 with e_CharFonts
[FontID
] do
1418 for a
:= 1 to len
do
1420 if Text[a
] = #10 then
1423 if w2
> tw
then tw
:= w2
;
1428 with Chars
[Ord(Text[a
])] do
1429 if TextureID
<> -1 then
1431 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1432 e_GetTextureSize(TextureID
, nil, @h2
);
1433 if h2
> th
then th
:= h2
;
1445 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1451 if e_CharFonts
= nil then Exit
;
1452 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1454 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1455 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1458 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1465 if e_CharFonts
= nil then Exit
;
1466 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1468 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1470 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1471 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1473 if h2
> Result
then Result
:= h2
;
1477 procedure e_CharFont_Remove(FontID
: DWORD
);
1481 with e_CharFonts
[FontID
] do
1482 for a
:= 0 to High(Chars
) do
1483 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1485 e_CharFonts
[FontID
].alive
:= False;
1488 procedure e_CharFont_RemoveAll();
1492 if e_CharFonts
= nil then Exit
;
1494 for a
:= 0 to High(e_CharFonts
) do
1495 e_CharFont_Remove(a
);
1500 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1507 if e_NoGraphics
then Exit
;
1508 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1512 if e_TextureFonts
<> nil then
1513 for i
:= 0 to High(e_TextureFonts
) do
1514 if e_TextureFonts
[i
].Base
= 0 then
1520 if id
= DWORD(-1) then
1522 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1523 id
:= High(e_TextureFonts
);
1526 with e_TextureFonts
[id
] do
1528 {$IF not DEFINED(USE_NANOGL)}
1529 Base
:= glGenLists(XCount
*YCount
);
1531 TextureID
:= e_Textures
[Tex
].tx
.id
;
1532 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1533 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1540 {$IF not DEFINED(USE_NANOGL)}
1541 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1542 for loop1
:= 0 to XCount
*YCount
-1 do
1544 cx
:= (loop1
mod XCount
)/XCount
;
1545 cy
:= (loop1
div YCount
)/YCount
;
1547 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1549 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1550 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1552 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1553 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1555 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1556 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1558 glTexCoord2f(cx
, 1.0-cy
);
1561 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1569 procedure e_TextureFontKill(FontID
: DWORD
);
1571 if e_NoGraphics
then Exit
;
1572 {$IF not DEFINED(USE_NANOGL)}
1573 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1575 e_TextureFonts
[FontID
].Base
:= 0;
1579 procedure e_TextureFontDrawChar(ch
: Char; FontID
: DWORD
);
1584 Width
, Height
: Integer;
1585 XCount
, YCount
: Integer;
1587 index
:= Ord(ch
) - 32;
1588 Tex
:= e_TextureFonts
[FontID
].Texture
;
1589 Width
:= e_Textures
[Tex
].tx
.Width
;
1590 Height
:= e_Textures
[Tex
].tx
.Height
;
1591 XCount
:= e_TextureFonts
[FontID
].XC
;
1592 YCount
:= e_TextureFonts
[FontID
].YC
;
1593 cx
:= (index
mod XCount
)/XCount
;
1594 cy
:= (index
div YCount
)/YCount
;
1596 glTexCoord2f(cx
, 1 - cy
- 1/YCount
);
1597 glVertex2i(0, Height
div YCount
);
1598 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
- 1/YCount
);
1599 glVertex2i(Width
div XCount
, Height
div YCount
);
1600 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
);
1601 glVertex2i(Width
div XCount
, 0);
1602 glTexCoord2f(cx
, 1 - cy
);
1605 glTranslatef((e_Textures
[Tex
].tx
.Width
div XCount
) + e_TextureFonts
[FontID
].SPC
, 0, 0);
1608 procedure e_TextureFontDrawString(Text: String; FontID
: DWORD
);
1612 for i
:= 1 to High(Text) do
1613 e_TextureFontDrawChar(Text[i
], FontID
);
1617 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1619 if e_NoGraphics
then Exit
;
1620 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1621 if Text = '' then Exit
;
1623 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1626 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1629 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1630 glEnable(GL_TEXTURE_2D
);
1631 glTranslatef(x
, y
, 0);
1633 e_TextureFontDrawString(Text, FontID
);
1635 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1636 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1638 glDisable(GL_TEXTURE_2D
);
1641 glDisable(GL_BLEND
);
1644 // god forgive me for this, but i cannot figure out how to do it without lists
1645 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1647 if e_NoGraphics
then Exit
;
1652 glColor4ub(0, 0, 0, 128);
1653 glTranslatef(X
+1, Y
+1, 0);
1655 e_TextureFontDrawChar(Ch
, FontID
);
1657 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1663 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1664 glTranslatef(X
, Y
, 0);
1666 e_TextureFontDrawChar(Ch
, FontID
);
1668 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1674 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1676 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1677 glEnable(GL_TEXTURE_2D
);
1678 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1680 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1682 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1683 glDisable(GL_TEXTURE_2D
);
1684 glDisable(GL_BLEND
);
1687 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1689 result
:= e_TextureFonts
[FontID
].CharWidth
;
1692 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1694 a
, TX
, TY
, len
: Integer;
1698 if e_NoGraphics
then Exit
;
1699 if Text = '' then Exit
;
1700 if e_TextureFonts
= nil then Exit
;
1701 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1709 len
:= Length(Text);
1711 w
:= e_TextureFonts
[FontID
].CharWidth
;
1713 with e_TextureFonts
[FontID
] do
1715 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1716 glEnable(GL_TEXTURE_2D
);
1718 {$IF not DEFINED(USE_NANOGL)}
1719 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1722 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1725 for a
:= 1 to len
do
1736 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1741 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1746 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1751 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1756 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1761 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1766 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1771 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1778 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1783 glDisable(GL_TEXTURE_2D
);
1784 glDisable(GL_BLEND
);
1788 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1789 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1791 if e_NoGraphics
then Exit
;
1792 if Text = '' then Exit
;
1795 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1796 glEnable(GL_TEXTURE_2D
);
1798 {$IF not DEFINED(USE_NANOGL)}
1799 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1802 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1807 glColor4ub(0, 0, 0, 128);
1808 glTranslatef(x
+1, y
+1, 0);
1809 glScalef(Scale
, Scale
, 0);
1811 e_TextureFontDrawString(Text, FontID
);
1813 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1819 glColor4ub(Red
, Green
, Blue
, 255);
1820 glTranslatef(x
, y
, 0);
1821 glScalef(Scale
, Scale
, 0);
1823 e_TextureFontDrawString(Text, FontID
);
1825 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1828 glDisable(GL_TEXTURE_2D
);
1830 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1831 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}
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!');