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, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../shared/a_modes.inc}
21 {$INCLUDE ../nogl/noGLuses.inc}
25 SysUtils
, Classes
, Math
, e_log
, e_texture
,
26 MAPDEF
, ImagingTypes
, Imaging
, ImagingUtility
;
29 TMirrorType
=(None
, Horizontal
, Vertical
);
30 TBlending
=(None
, Blend
, Filter
, Invert
);
41 Left
, Top
, Right
, Bottom
: Integer;
59 //------------------------------------------------------------------
61 //------------------------------------------------------------------
63 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
64 procedure e_ResizeWindow(Width
, Height
: Integer);
65 procedure e_ResizeFramebuffer(Width
, Height
: Integer);
66 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
68 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
69 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
70 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
71 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
72 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
73 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
74 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
75 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
77 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
78 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
80 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
81 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
83 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
85 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
86 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
87 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
88 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
89 Blending
: TBlending
= TBlending
.None
);
90 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
91 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
93 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
94 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
95 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
96 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
97 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
98 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
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
;
124 Shadow
: Boolean = False; Newlines
: Boolean = False);
125 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
126 procedure e_RemoveAllTextureFont();
128 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
129 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
131 procedure e_ReleaseEngine();
132 procedure e_BeginRender();
133 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
134 procedure e_Clear(); overload
;
135 procedure e_EndRender();
138 function e_GetGamma(win
: PSDL_Window
): Byte;
139 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
142 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
144 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
145 function _Point(X
, Y
: Integer): TPoint2i
;
146 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
147 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
149 //function e_getTextGLId (ID: DWORD): GLuint;
153 e_NoGraphics
: Boolean = False;
154 e_FastScreenshots
: Boolean = true; // it's REALLY SLOW with `false`
155 g_dbg_scale
: Single = 1.0;
156 r_pixel_scale
: Single = 1.0;
170 TTextureFont
= record
181 Chars
: array[0..255] of
191 TSavedTexture
= record
198 e_Textures
: array of TTexture
= nil;
199 e_TextureFonts
: array of TTextureFont
= nil;
200 e_CharFonts
: array of TCharFont
;
201 //e_SavedTextures: array of TSavedTexture;
205 e_FrameW
: Integer = -1;
206 e_FrameH
: Integer = -1;
208 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
210 //------------------------------------------------------------------
211 // Èíèöèàëèçèðóåò OpenGL
212 //------------------------------------------------------------------
213 procedure e_InitGL();
217 e_DummyTextures
:= True;
223 glDisable(GL_DEPTH_TEST
);
224 glEnable(GL_SCISSOR_TEST
);
225 glClearColor(0, 0, 0, 0);
228 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
230 mat
: Array [0..15] of GLDouble
;
233 if e_NoGraphics
then Exit
;
235 glScissor(X
, Y
, Width
, Height
);
236 glViewport(X
, Y
, Width
, Height
);
237 //gluOrtho2D(0, Width, Height, 0);
239 glMatrixMode(GL_PROJECTION
);
241 mat
[ 0] := 2.0 / Width
;
247 mat
[ 5] := -2.0 / Height
;
261 glLoadMatrixd(@mat
[0]);
263 glMatrixMode(GL_MODELVIEW
);
267 //------------------------------------------------------------------
268 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
269 //------------------------------------------------------------------
270 function FindTexture(): DWORD
;
274 if e_Textures
<> nil then
275 for i
:= 0 to High(e_Textures
) do
276 if e_Textures
[i
].tx
.Width
= 0 then
282 if e_Textures
= nil then
284 SetLength(e_Textures
, 32);
289 Result
:= High(e_Textures
) + 1;
290 SetLength(e_Textures
, Length(e_Textures
) + 32);
294 //------------------------------------------------------------------
296 //------------------------------------------------------------------
297 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
304 e_WriteLog('Loading texture from '+FileName
, TMsgType
.Notify
);
306 find_id
:= FindTexture();
308 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
309 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
316 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
323 find_id
:= FindTexture();
325 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
332 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
339 find_id
:= FindTexture
;
341 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
;
348 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
355 find_id
:= FindTexture();
357 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
364 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
370 find_id
:= FindTexture();
371 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
376 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
378 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
379 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
382 procedure e_ResizeFramebuffer(Width
, Height
: Integer);
384 if e_NoGraphics
then Exit
;
386 glBindTexture(GL_TEXTURE_2D
, 0);
387 glBindRenderbuffer(GL_RENDERBUFFER
, 0);
388 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
392 glDeleteTextures(1, @e_Frame
);
398 glDeleteRenderbuffers(1, @e_RBO
);
404 glDeleteFramebuffers(1, @e_FBO
);
411 glGenFramebuffers(1, @e_FBO
);
413 glGenTextures(1, @e_Frame
);
414 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
415 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, Width
, Height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, nil);
416 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
417 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
419 glGenRenderbuffers(1, @e_RBO
);
420 glBindRenderbuffer(GL_RENDERBUFFER
, e_RBO
);
421 glRenderbufferStorage(GL_RENDERBUFFER
, {$IFNDEF USE_GLES1}GL_DEPTH24_STENCIL8
{$ELSE}GL_DEPTH_COMPONENT16
{$ENDIF}, Width
, Height
);
423 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
424 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, e_Frame
, 0);
425 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, {$IFNDEF USE_GLES1}GL_DEPTH_STENCIL_ATTACHMENT
{$ELSE}GL_DEPTH_ATTACHMENT
{$ENDIF}, GL_RENDERBUFFER
, e_RBO
);
428 procedure e_ResizeWindow(Width
, Height
: Integer);
432 e_SetViewPort(0, 0, Width
, Height
);
435 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
437 x1
, y1
, tmp
: Integer;
439 if (w
< 1) or (h
< 1) then exit
;
442 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
443 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
444 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
445 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
446 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
447 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
450 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
452 if (e_FBO
= 0) or (e_Frame
= 0) or e_NoGraphics
then exit
;
455 glEnable(GL_TEXTURE_2D
);
456 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
457 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
458 e_SetViewPort(0, 0, WinWidth
, WinHeight
);
459 glColor4ub(255, 255, 255, 255);
462 glTexCoord2f(0, 1); glVertex2i( 0, 0);
463 glTexCoord2f(0, 0); glVertex2i( 0, WinHeight
);
464 glTexCoord2f(1, 0); glVertex2i(WinWidth
, WinHeight
);
465 glTexCoord2f(1, 1); glVertex2i(WinWidth
, 0);
468 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
469 e_SetViewPort(0, 0, e_FrameW
, e_FrameH
);
472 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
473 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
475 if e_NoGraphics
then Exit
;
476 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
478 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
483 if (AlphaChannel
) or (Alpha
> 0) then
484 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
487 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
490 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
492 glEnable(GL_TEXTURE_2D
);
493 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
496 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
);
498 //u := e_Textures[ID].tx.u;
499 //v := e_Textures[ID].tx.v;
502 if Mirror = M_NONE then
504 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
505 glTexCoord2f(0, 0); glVertex2i(X, Y);
506 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
507 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
510 if Mirror = M_HORIZONTAL then
512 glTexCoord2f(u, 0); glVertex2i(X, Y);
513 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
514 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
515 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
518 if Mirror = M_VERTICAL then
520 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
521 glTexCoord2f(0, -v); glVertex2i(X, Y);
522 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
523 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
532 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
533 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
537 if e_NoGraphics
then Exit
;
538 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
540 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
545 if (AlphaChannel
) or (Alpha
> 0) then
546 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
549 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
552 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
554 glEnable(GL_TEXTURE_2D
);
555 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
557 u
:= e_Textures
[ID
].tx
.u
;
558 v
:= e_Textures
[ID
].tx
.v
;
561 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
562 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
563 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
564 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
570 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
571 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
573 if e_NoGraphics
then Exit
;
574 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
576 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
581 if (AlphaChannel
) or (Alpha
> 0) then
582 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
585 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
588 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
590 glEnable(GL_TEXTURE_2D
);
591 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
593 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
);
599 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
600 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
602 X2
, Y2
, dx
, w
, h
: Integer;
605 if e_NoGraphics
then Exit
;
606 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
607 ambientBlendMode
:= false;
609 if (Alpha
> 0) or AlphaChannel
or Blending
then
615 if not ambientBlendMode
then glDisable(GL_BLEND
);
617 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
618 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
619 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
621 if (XCount
= 0) then XCount
:= 1;
622 if (YCount
= 0) then YCount
:= 1;
624 glEnable(GL_TEXTURE_2D
);
625 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
627 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
628 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
630 //k8: this SHOULD work... i hope
631 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
634 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
635 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
636 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
637 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
644 u
:= e_Textures
[ID
].tx
.u
;
645 v
:= e_Textures
[ID
].tx
.v
;
646 w
:= e_Textures
[ID
].tx
.width
;
647 h
:= e_Textures
[ID
].tx
.height
;
654 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
655 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
656 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
657 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
672 //TODO: overflow checks
673 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
678 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
679 // check for intersection
680 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
681 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
685 if (x0
< x1
) then x0
:= x1
;
686 if (y0
< y1
) then y0
:= y1
;
687 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
688 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
691 result
:= (w0
> 0) and (h0
> 0);
695 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
696 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
701 scxywh: array[0..3] of GLint;
702 vpxywh: array[0..3] of GLint;
704 w
, h
, dw
, cw
, ch
, yofs
: Integer;
705 u
, v
, cu
, cv
: Single;
709 procedure setScissorGLInternal (x, y, w, h: Integer);
711 //if not scallowed then exit;
716 y := vpxywh[3]-(y+h);
717 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
719 glScissor(0, 0, 0, 0);
723 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
724 glScissor(x, y, w, h);
730 if e_NoGraphics
then exit
;
731 ambientBlendMode
:= false;
733 if (wdt
< 1) or (hgt
< 1) then exit
;
735 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
737 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
741 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
743 if (Alpha
> 0) or AlphaChannel
or Blending
then
749 if not ambientBlendMode
then glDisable(GL_BLEND
);
751 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
752 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
753 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
755 glEnable(GL_TEXTURE_2D
);
756 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
761 //k8: this SHOULD work... i hope
762 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
765 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
766 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
767 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
768 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
773 // hard day's night; setup scissor
775 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
776 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
777 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
778 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
779 //glEnable(GL_SCISSOR_TEST);
780 setScissorGLInternal(x, y, wdt, hgt);
783 u
:= e_Textures
[ID
].tx
.u
;
784 v
:= e_Textures
[ID
].tx
.v
;
785 w
:= e_Textures
[ID
].tx
.width
;
786 h
:= e_Textures
[ID
].tx
.height
;
788 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
792 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
793 if onlyOneY
then yofs
:= 0;
799 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
801 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
802 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
803 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
804 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
810 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
817 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
819 if e_NoGraphics
then exit
;
820 if (w
< 1) or (h
< 1) then exit
;
821 if (a
<> 255) or ((r
or g
or b
) <> 0) then
824 glDisable(GL_TEXTURE_2D
);
825 glColor4ub(r
, g
, b
, a
);
826 if ((r
or g
or b
) <> 0) then
828 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
832 glVertex2i(x
+w
, y
+h
);
836 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
840 glVertex2i(x
+w
, y
+h
);
848 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
849 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
851 if e_NoGraphics
then Exit
;
853 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
855 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
860 if (AlphaChannel
) or (Alpha
> 0) then
861 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
864 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
867 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
869 if (Angle
<> 0) and (RC
<> nil) then
872 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
873 glRotatef(Angle
, 0, 0, 1);
874 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
877 glEnable(GL_TEXTURE_2D
);
878 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
879 glBegin(GL_QUADS
); //0-1 1-1
881 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
);
890 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
892 if e_NoGraphics
then Exit
;
893 glDisable(GL_TEXTURE_2D
);
894 glColor3ub(Red
, Green
, Blue
);
897 if (Size
= 2) or (Size
= 4) then
901 glVertex2f(X
+0.3, Y
+1.0);
904 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
907 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
909 // Make lines only top-left/bottom-right and top-right/bottom-left
921 // Pixel-perfect hack
929 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
931 nX1
, nY1
, nX2
, nY2
: Integer;
933 if e_NoGraphics
then Exit
;
934 // Only top-left/bottom-right quad
951 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
956 glDisable(GL_TEXTURE_2D
);
957 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
960 nX1
:= X1
; nY1
:= Y1
;
961 nX2
:= X2
; nY2
:= Y1
;
962 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
963 glVertex2i(nX1
, nY1
);
964 glVertex2i(nX2
, nY2
);
966 nX1
:= X2
; nY1
:= Y1
;
967 nX2
:= X2
; nY2
:= Y2
;
968 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
969 glVertex2i(nX1
, nY1
);
970 glVertex2i(nX2
, nY2
);
972 nX1
:= X2
; nY1
:= Y2
;
973 nX2
:= X1
; nY2
:= Y2
;
974 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
975 glVertex2i(nX1
, nY1
);
976 glVertex2i(nX2
, nY2
);
978 nX1
:= X1
; nY1
:= Y2
;
979 nX2
:= X1
; nY2
:= Y1
;
980 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
981 glVertex2i(nX1
, nY1
);
982 glVertex2i(nX2
, nY2
);
984 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
988 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
989 Blending
: TBlending
= TBlending
.None
);
991 if e_NoGraphics
then Exit
;
992 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
998 TBlending
.None
: if Alpha
> 0 then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
999 TBlending
.Blend
: glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
1000 TBlending
.Invert
: glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
);
1001 TBlending
.Filter
: glBlendFunc(GL_ZERO
, GL_SRC_COLOR
);
1004 glDisable(GL_TEXTURE_2D
);
1005 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1017 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1018 glDisable(GL_BLEND
);
1022 // ////////////////////////////////////////////////////////////////////////// //
1023 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1025 if (a
< 0) then a
:= 0;
1026 if (a
> 255) then a
:= 255;
1028 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1029 glDisable(GL_TEXTURE_2D
);
1030 glColor4ub(0, 0, 0, Byte(255-a
));
1037 //glRect(x, y, x+w, y+h);
1038 glColor4ub(1, 1, 1, 1);
1039 glDisable(GL_BLEND
);
1040 //glBlendEquation(GL_FUNC_ADD);
1043 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1045 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1049 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1051 if e_NoGraphics
then Exit
;
1052 // Pixel-perfect lines
1054 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1059 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1061 glDisable(GL_BLEND
);
1063 glDisable(GL_TEXTURE_2D
);
1064 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1070 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1072 glDisable(GL_BLEND
);
1075 //------------------------------------------------------------------
1076 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1077 //------------------------------------------------------------------
1078 procedure e_DeleteTexture(ID
: DWORD
);
1080 if not e_NoGraphics
then
1081 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1082 e_Textures
[ID
].tx
.id
:= 0;
1083 e_Textures
[ID
].tx
.Width
:= 0;
1084 e_Textures
[ID
].tx
.Height
:= 0;
1087 //------------------------------------------------------------------
1088 // Óäàëÿåò âñå òåêñòóðû
1089 //------------------------------------------------------------------
1090 procedure e_RemoveAllTextures();
1094 if e_Textures
= nil then Exit
;
1096 for i
:= 0 to High(e_Textures
) do
1097 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1101 //------------------------------------------------------------------
1103 //------------------------------------------------------------------
1104 procedure e_ReleaseEngine();
1106 e_RemoveAllTextures
;
1107 e_RemoveAllTextureFont
;
1110 procedure e_BeginRender();
1112 if e_NoGraphics
then Exit
;
1113 glEnable(GL_ALPHA_TEST
);
1114 glAlphaFunc(GL_GREATER
, 0.0);
1117 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1119 if e_NoGraphics
then Exit
;
1120 glClearColor(Red
, Green
, Blue
, 0);
1124 procedure e_Clear(); overload
;
1126 if e_NoGraphics
then Exit
;
1127 glClearColor(0, 0, 0, 0);
1128 glClear(GL_COLOR_BUFFER_BIT
);
1131 procedure e_EndRender();
1133 if e_NoGraphics
then Exit
;
1138 function e_GetGamma(win
: PSDL_Window
): Byte;
1140 ramp
: array [0..256*3-1] of Word;
1141 rgb
: array [0..2] of Double;
1150 if e_NoGraphics
then Exit
;
1155 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1164 for j
:= min
to max
- 1 do
1167 B
:= (j
mod 256)/256;
1169 sum
:= sum
+ ln(A
)/ln(B
);
1172 rgb
[i
] := sum
/ count
;
1175 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1178 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1180 ramp
: array [0..256*3-1] of Word;
1185 if e_NoGraphics
then Exit
;
1186 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1188 for i
:= 0 to 255 do
1190 r
:= Exp(g
* ln(i
/256))*65536;
1191 if r
< 0 then r
:= 0
1192 else if r
> 65535 then r
:= 65535;
1193 ramp
[i
] := trunc(r
);
1194 ramp
[i
+ 256] := trunc(r
);
1195 ramp
[i
+ 512] := trunc(r
);
1198 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1202 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1206 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1210 if e_CharFonts
<> nil then
1211 for i
:= 0 to High(e_CharFonts
) do
1212 if not e_CharFonts
[i
].alive
then
1218 if id
= DWORD(-1) then
1220 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1221 id
:= High(e_CharFonts
);
1224 with e_CharFonts
[id
] do
1226 for i
:= 0 to High(Chars
) do
1240 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1242 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1244 TextureID
:= Texture
;
1249 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1253 if e_NoGraphics
then Exit
;
1254 if Text = '' then Exit
;
1255 if e_CharFonts
= nil then Exit
;
1256 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1258 with e_CharFonts
[FontID
] do
1260 for a
:= 1 to Length(Text) do
1261 with Chars
[Ord(Text[a
])] do
1262 if TextureID
<> -1 then
1264 e_Draw(TextureID
, X
, Y
, 0, True, False);
1265 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1270 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1271 Color
: TRGB
; Scale
: Single = 1.0);
1276 if e_NoGraphics
then Exit
;
1277 if Text = '' then Exit
;
1278 if e_CharFonts
= nil then Exit
;
1279 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1281 with e_CharFonts
[FontID
] do
1283 for a
:= 1 to Length(Text) do
1284 with Chars
[Ord(Text[a
])] do
1285 if TextureID
<> -1 then
1287 if Scale
<> 1.0 then
1290 glScalef(Scale
, Scale
, 0);
1295 e_Draw(TextureID
, X
, Y
, 0, True, False);
1298 if Scale
<> 1.0 then glPopMatrix
;
1300 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1305 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1307 a
, TX
, TY
, len
: Integer;
1311 if e_NoGraphics
then Exit
;
1312 if Text = '' then Exit
;
1313 if e_CharFonts
= nil then Exit
;
1314 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1322 len
:= Length(Text);
1324 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1326 with e_CharFonts
[FontID
] do
1328 for a
:= 1 to len
do
1339 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1344 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1349 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1354 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1359 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1364 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1369 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1374 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1379 with Chars
[Ord(Text[a
])] do
1380 if TextureID
<> -1 then
1384 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1387 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1393 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1401 if Text = '' then Exit
;
1402 if e_CharFonts
= nil then Exit
;
1403 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1405 with e_CharFonts
[FontID
] do
1407 for a
:= 1 to Length(Text) do
1408 with Chars
[Ord(Text[a
])] do
1409 if TextureID
<> -1 then
1411 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1412 e_GetTextureSize(TextureID
, nil, @h2
);
1413 if h2
> h
then h
:= h2
;
1418 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1420 a
, lines
, len
: Integer;
1421 h2
, w2
, tw
, th
: Word;
1428 if Text = '' then Exit
;
1429 if e_CharFonts
= nil then Exit
;
1430 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1433 len
:= Length(Text);
1435 with e_CharFonts
[FontID
] do
1437 for a
:= 1 to len
do
1439 if Text[a
] = #10 then
1442 if w2
> tw
then tw
:= w2
;
1447 with Chars
[Ord(Text[a
])] do
1448 if TextureID
<> -1 then
1450 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1451 e_GetTextureSize(TextureID
, nil, @h2
);
1452 if h2
> th
then th
:= h2
;
1464 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1470 if e_CharFonts
= nil then Exit
;
1471 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1473 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1474 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1477 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1484 if e_CharFonts
= nil then Exit
;
1485 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1487 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1489 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1490 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1492 if h2
> Result
then Result
:= h2
;
1496 procedure e_CharFont_Remove(FontID
: DWORD
);
1500 with e_CharFonts
[FontID
] do
1501 for a
:= 0 to High(Chars
) do
1502 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1504 e_CharFonts
[FontID
].alive
:= False;
1507 procedure e_CharFont_RemoveAll();
1511 if e_CharFonts
= nil then Exit
;
1513 for a
:= 0 to High(e_CharFonts
) do
1514 e_CharFont_Remove(a
);
1519 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1528 if e_NoGraphics
then Exit
;
1529 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1533 if e_TextureFonts
<> nil then
1534 for i
:= 0 to High(e_TextureFonts
) do
1535 if e_TextureFonts
[i
].Base
= 0 then
1541 if id
= DWORD(-1) then
1543 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1544 id
:= High(e_TextureFonts
);
1547 with e_TextureFonts
[id
] do
1550 Base
:= glGenLists(XCount
*YCount
);
1552 TextureID
:= e_Textures
[Tex
].tx
.id
;
1553 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1554 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1562 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1563 for loop1
:= 0 to XCount
*YCount
-1 do
1565 cx
:= (loop1
mod XCount
)/XCount
;
1566 cy
:= (loop1
div YCount
)/YCount
;
1568 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1570 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1571 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1573 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1574 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1576 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1577 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1579 glTexCoord2f(cx
, 1.0-cy
);
1582 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1590 procedure e_TextureFontKill(FontID
: DWORD
);
1592 if e_NoGraphics
then Exit
;
1594 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1596 e_TextureFonts
[FontID
].Base
:= 0;
1599 {$IFNDEF NOGL_LISTS}
1600 procedure e_TextureFontDrawChar(ch
: Char; FontID
: DWORD
);
1605 Width
, Height
: Integer;
1606 XCount
, YCount
: Integer;
1608 index
:= Ord(ch
) - 32;
1609 Tex
:= e_TextureFonts
[FontID
].Texture
;
1610 Width
:= e_Textures
[Tex
].tx
.Width
;
1611 Height
:= e_Textures
[Tex
].tx
.Height
;
1612 XCount
:= e_TextureFonts
[FontID
].XC
;
1613 YCount
:= e_TextureFonts
[FontID
].YC
;
1614 cx
:= (index
mod XCount
)/XCount
;
1615 cy
:= (index
div YCount
)/YCount
;
1617 glTexCoord2f(cx
, 1 - cy
- 1/YCount
);
1618 glVertex2i(0, Height
div YCount
);
1619 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
- 1/YCount
);
1620 glVertex2i(Width
div XCount
, Height
div YCount
);
1621 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
);
1622 glVertex2i(Width
div XCount
, 0);
1623 glTexCoord2f(cx
, 1 - cy
);
1626 glTranslatef((e_Textures
[Tex
].tx
.Width
div XCount
) + e_TextureFonts
[FontID
].SPC
, 0, 0);
1629 procedure e_TextureFontDrawString(Text: String; FontID
: DWORD
);
1633 for i
:= 1 to High(Text) do
1634 e_TextureFontDrawChar(Text[i
], FontID
);
1638 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1640 if e_NoGraphics
then Exit
;
1641 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1642 if Text = '' then Exit
;
1644 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1647 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1650 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1651 glEnable(GL_TEXTURE_2D
);
1652 glTranslatef(x
, y
, 0);
1654 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1655 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1657 e_TextureFontDrawString(Text, FontID
);
1659 glDisable(GL_TEXTURE_2D
);
1662 glDisable(GL_BLEND
);
1665 // god forgive me for this, but i cannot figure out how to do it without lists
1666 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1668 if e_NoGraphics
then Exit
;
1673 glColor4ub(0, 0, 0, 128);
1674 glTranslatef(X
+1, Y
+1, 0);
1676 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1678 e_TextureFontDrawChar(Ch
, FontID
);
1684 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1685 glTranslatef(X
, Y
, 0);
1687 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1689 e_TextureFontDrawChar(Ch
, FontID
);
1695 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1697 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1698 glEnable(GL_TEXTURE_2D
);
1699 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1701 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1703 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1704 glDisable(GL_TEXTURE_2D
);
1705 glDisable(GL_BLEND
);
1708 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1710 result
:= e_TextureFonts
[FontID
].CharWidth
;
1713 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
;
1714 Shadow
: Boolean = False; Newlines
: Boolean = False);
1716 a
, TX
, TY
, len
: Integer;
1720 if e_NoGraphics
then Exit
;
1721 if Text = '' then Exit
;
1722 if e_TextureFonts
= nil then Exit
;
1723 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1731 len
:= Length(Text);
1733 w
:= e_TextureFonts
[FontID
].CharWidth
;
1734 h
:= e_TextureFonts
[FontID
].CharHeight
;
1736 with e_TextureFonts
[FontID
] do
1738 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1739 glEnable(GL_TEXTURE_2D
);
1742 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1745 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1748 for a
:= 1 to len
do
1762 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1767 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1772 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1777 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1782 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1787 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1792 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1797 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1804 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1809 glDisable(GL_TEXTURE_2D
);
1810 glDisable(GL_BLEND
);
1814 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1815 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1817 if e_NoGraphics
then Exit
;
1818 if Text = '' then Exit
;
1821 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1822 glEnable(GL_TEXTURE_2D
);
1825 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1828 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1833 glColor4ub(0, 0, 0, 128);
1834 glTranslatef(x
+1, y
+1, 0);
1835 glScalef(Scale
, Scale
, 0);
1837 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1839 e_TextureFontDrawString(Text, FontID
);
1845 glColor4ub(Red
, Green
, Blue
, 255);
1846 glTranslatef(x
, y
, 0);
1847 glScalef(Scale
, Scale
, 0);
1849 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1851 e_TextureFontDrawString(Text, FontID
);
1854 glDisable(GL_TEXTURE_2D
);
1856 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1857 glDisable(GL_BLEND
);
1860 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1864 if e_NoGraphics
then Exit
;
1865 if Integer(ID
) > High(e_TextureFonts
) then
1867 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1868 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1871 procedure e_RemoveAllTextureFont();
1875 if e_NoGraphics
then Exit
;
1876 if e_TextureFonts
= nil then Exit
;
1878 for i
:= 0 to High(e_TextureFonts
) do
1879 if e_TextureFonts
[i
].Base
<> 0 then
1882 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1884 e_TextureFonts
[i
].Base
:= 0;
1887 e_TextureFonts
:= nil;
1890 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1897 function _Point(X
, Y
: Integer): TPoint2i
;
1903 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1907 Result
.Width
:= Width
;
1908 Result
.Height
:= Height
;
1911 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1920 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1922 pixels
, obuf
, scln
, ps
, pd
: PByte;
1925 i
, x
, y
, res
: Integer;
1926 sign
: array [0..7] of Byte;
1927 hbuf
: array [0..12] of Byte;
1932 if e_NoGraphics
then Exit
;
1935 // first, extract and pack graphics data
1936 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1938 GetMem(pixels
, Width
*Height
*3);
1940 FillChar(pixels
^, Width
*Height
*3, 0);
1941 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1942 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1944 if e_FastScreenshots
then
1947 GetMem(scln
, (Width
*3+1)*Height
);
1951 Inc(ps
, (Width
*3)*(Height
-1));
1952 for i
:= 0 to Height
-1 do
1956 Move(ps
^, pd
^, Width
*3);
1968 obufsize
:= (Width
*3+1)*Height
*2;
1969 GetMem(obuf
, obufsize
);
1974 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1975 if res
= Z_OK
then break
;
1976 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1977 obufsize
:= obufsize
*2;
1980 GetMem(obuf
, obufsize
);
1982 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1995 st
.writeBuffer(sign
, 8);
1996 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1999 writeIntBE(st
, LongWord(13));
2004 st
.writeBuffer(sign
, 4);
2005 crc
:= crc32(0, @sign
[0], 4);
2008 hbuf
[2] := (Width
shr 8) and $ff;
2009 hbuf
[3] := Width
and $ff;
2012 hbuf
[6] := (Height
shr 8) and $ff;
2013 hbuf
[7] := Height
and $ff;
2014 hbuf
[8] := 8; // bit depth
2015 hbuf
[9] := 2; // RGB
2016 hbuf
[10] := 0; // compression method
2017 hbuf
[11] := 0; // filter method
2018 hbuf
[12] := 0; // no interlace
2019 crc
:= crc32(crc
, @hbuf
[0], 13);
2020 st
.writeBuffer(hbuf
, 13);
2021 writeIntBE(st
, crc
);
2022 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2025 writeIntBE(st
, LongWord(dlen
));
2030 st
.writeBuffer(sign
, 4);
2031 crc
:= crc32(0, @sign
[0], 4);
2032 crc
:= crc32(crc
, obuf
, dlen
);
2033 st
.writeBuffer(obuf
^, dlen
);
2034 writeIntBE(st
, crc
);
2035 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2038 writeIntBE(st
, LongWord(0));
2043 st
.writeBuffer(sign
, 4);
2044 crc
:= crc32(0, @sign
[0], 4);
2045 writeIntBE(st
, crc
);
2046 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2048 if obuf
<> nil then FreeMem(obuf
);
2053 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2054 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2057 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2059 //writeln(stderr, 'moving pixels...');
2060 for y
:= Height
-1 downto 0 do
2062 for x
:= 0 to Width
-1 do
2064 clr
.r
:= ps
^; Inc(ps
);
2065 clr
.g
:= ps
^; Inc(ps
);
2066 clr
.b
:= ps
^; Inc(ps
);
2068 SetPixel32(img
, x
, y
, clr
);
2071 GlobalMetadata
.ClearMetaItems();
2072 GlobalMetadata
.ClearMetaItemsForSaving();
2073 //writeln(stderr, 'compressing image...');
2074 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2075 //writeln(stderr, 'done!');