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 function e_ResizeFramebuffer(Width
, Height
: Integer): Boolean;
66 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
67 procedure e_SetRenderTarget(Framebuffer
: Boolean);
69 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
70 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
71 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
72 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
73 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
74 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
75 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
76 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
78 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
79 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
81 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
82 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
84 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
86 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
87 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
88 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
89 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
90 Blending
: TBlending
= TBlending
.None
);
91 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
92 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
94 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
95 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
96 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
97 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
98 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
99 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
100 procedure e_DeleteTexture(ID
: DWORD
);
101 procedure e_RemoveAllTextures();
104 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
105 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
106 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
107 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
108 Color
: TRGB
; Scale
: Single = 1.0);
109 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
110 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
111 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
112 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
113 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
114 procedure e_CharFont_Remove(FontID
: DWORD
);
115 procedure e_CharFont_RemoveAll();
118 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
120 procedure e_TextureFontKill(FontID
: DWORD
);
121 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
122 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
123 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
124 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
;
125 Shadow
: Boolean = False; Newlines
: Boolean = False);
126 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
127 procedure e_RemoveAllTextureFont();
129 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
130 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
132 procedure e_ReleaseEngine();
133 procedure e_BeginRender();
134 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
135 procedure e_Clear(); overload
;
136 procedure e_EndRender();
139 function e_GetGamma(win
: PSDL_Window
): Byte;
140 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
143 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
145 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
146 function _Point(X
, Y
: Integer): TPoint2i
;
147 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
148 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
150 //function e_getTextGLId (ID: DWORD): GLuint;
154 e_NoGraphics
: Boolean = False;
155 e_FastScreenshots
: Boolean = true; // it's REALLY SLOW with `false`
156 g_dbg_scale
: Single = 1.0;
157 r_pixel_scale
: Single = 1.0;
171 TTextureFont
= record
182 Chars
: array[0..255] of
192 TSavedTexture
= record
199 e_Textures
: array of TTexture
= nil;
200 e_TextureFonts
: array of TTextureFont
= nil;
201 e_CharFonts
: array of TCharFont
;
202 //e_SavedTextures: array of TSavedTexture;
205 e_RBOSupported
: Boolean = True;
207 e_FrameW
: Integer = -1;
208 e_FrameH
: Integer = -1;
210 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
212 //------------------------------------------------------------------
213 // Èíèöèàëèçèðóåò OpenGL
214 //------------------------------------------------------------------
215 procedure e_InitGL();
219 e_DummyTextures
:= True;
225 glDisable(GL_DEPTH_TEST
);
226 glEnable(GL_SCISSOR_TEST
);
227 glClearColor(0, 0, 0, 0);
230 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
232 mat
: Array [0..15] of GLDouble
;
235 if e_NoGraphics
then Exit
;
237 glScissor(X
, Y
, Width
, Height
);
238 glViewport(X
, Y
, Width
, Height
);
239 //gluOrtho2D(0, Width, Height, 0);
241 glMatrixMode(GL_PROJECTION
);
243 mat
[ 0] := 2.0 / Width
;
249 mat
[ 5] := -2.0 / Height
;
263 glLoadMatrixd(@mat
[0]);
265 glMatrixMode(GL_MODELVIEW
);
269 //------------------------------------------------------------------
270 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
271 //------------------------------------------------------------------
272 function FindTexture(): DWORD
;
276 if e_Textures
<> nil then
277 for i
:= 0 to High(e_Textures
) do
278 if e_Textures
[i
].tx
.Width
= 0 then
284 if e_Textures
= nil then
286 SetLength(e_Textures
, 32);
291 Result
:= High(e_Textures
) + 1;
292 SetLength(e_Textures
, Length(e_Textures
) + 32);
296 //------------------------------------------------------------------
298 //------------------------------------------------------------------
299 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
306 e_WriteLog('Loading texture from '+FileName
, TMsgType
.Notify
);
308 find_id
:= FindTexture();
310 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
311 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
318 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
325 find_id
:= FindTexture();
327 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
334 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
341 find_id
:= FindTexture
;
343 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
;
350 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
357 find_id
:= FindTexture();
359 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
366 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
372 find_id
:= FindTexture();
373 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
378 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
380 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
381 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
384 procedure DestroyFramebuffer
;
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
);
409 function e_ResizeFramebuffer(Width
, Height
: Integer): Boolean;
413 if e_NoGraphics
then Exit
;
422 glGenFramebuffers(1, @e_FBO
);
424 if glGetError() <> GL_NO_ERROR
then
426 e_LogWriteln('GL: glGenFramebuffers failed');
430 glGenTextures(1, @e_Frame
);
431 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
432 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, Width
, Height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, nil);
433 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
434 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
436 if glGetError() <> GL_NO_ERROR
then
438 e_LogWriteln('GL: can''t create FBO color buffer');
443 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
444 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, e_Frame
, 0);
445 if glCheckFramebufferStatus(GL_FRAMEBUFFER
) <> GL_FRAMEBUFFER_COMPLETE
then
447 e_LogWriteln('GL: can''t construct framebuffer with color attachment');
453 if e_RBOSupported
then
455 glGenRenderbuffers(1, @e_RBO
);
456 glBindRenderbuffer(GL_RENDERBUFFER
, e_RBO
);
457 glRenderbufferStorage(GL_RENDERBUFFER
, GL_DEPTH24_STENCIL8
, Width
, Height
);
458 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, e_RBO
);
459 if glCheckFramebufferStatus(GL_FRAMEBUFFER
) <> GL_FRAMEBUFFER_COMPLETE
then
461 e_LogWriteln('GL: can''t construct framebuffer with depth+stencil attachment, trying without');
462 e_RBOSupported
:= False;
463 Result
:= e_ResizeFramebuffer(Width
, Height
);
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 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
489 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
490 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
491 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
494 procedure e_SetRenderTarget(Framebuffer
: Boolean);
496 if (e_FBO
= 0) or e_NoGraphics
then exit
;
498 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
)
500 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
503 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
505 if (e_FBO
= 0) or (e_Frame
= 0) or e_NoGraphics
then exit
;
508 glEnable(GL_TEXTURE_2D
);
509 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
510 glColor4ub(255, 255, 255, 255);
513 glTexCoord2f(0, 1); glVertex2i( 0, 0);
514 glTexCoord2f(0, 0); glVertex2i( 0, WinHeight
);
515 glTexCoord2f(1, 0); glVertex2i(WinWidth
, WinHeight
);
516 glTexCoord2f(1, 1); glVertex2i(WinWidth
, 0);
520 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
521 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
523 if e_NoGraphics
then Exit
;
524 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
526 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
531 if (AlphaChannel
) or (Alpha
> 0) then
532 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
535 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
538 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
540 glEnable(GL_TEXTURE_2D
);
541 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
544 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
);
546 //u := e_Textures[ID].tx.u;
547 //v := e_Textures[ID].tx.v;
550 if Mirror = M_NONE then
552 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
553 glTexCoord2f(0, 0); glVertex2i(X, Y);
554 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
555 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
558 if Mirror = M_HORIZONTAL then
560 glTexCoord2f(u, 0); glVertex2i(X, Y);
561 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
562 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
563 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
566 if Mirror = M_VERTICAL then
568 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
569 glTexCoord2f(0, -v); glVertex2i(X, Y);
570 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
571 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
580 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
581 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
585 if e_NoGraphics
then Exit
;
586 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
588 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
593 if (AlphaChannel
) or (Alpha
> 0) then
594 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
597 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
600 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
602 glEnable(GL_TEXTURE_2D
);
603 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
605 u
:= e_Textures
[ID
].tx
.u
;
606 v
:= e_Textures
[ID
].tx
.v
;
609 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
610 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
611 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
612 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
618 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
619 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
621 if e_NoGraphics
then Exit
;
622 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
624 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
629 if (AlphaChannel
) or (Alpha
> 0) then
630 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
633 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
636 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
638 glEnable(GL_TEXTURE_2D
);
639 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
641 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
);
647 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
648 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
650 X2
, Y2
, dx
, w
, h
: Integer;
653 if e_NoGraphics
then Exit
;
654 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
655 ambientBlendMode
:= false;
657 if (Alpha
> 0) or AlphaChannel
or Blending
then
663 if not ambientBlendMode
then glDisable(GL_BLEND
);
665 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
666 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
667 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
669 if (XCount
= 0) then XCount
:= 1;
670 if (YCount
= 0) then YCount
:= 1;
672 glEnable(GL_TEXTURE_2D
);
673 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
675 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
676 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
678 //k8: this SHOULD work... i hope
679 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
682 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
683 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
684 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
685 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
692 u
:= e_Textures
[ID
].tx
.u
;
693 v
:= e_Textures
[ID
].tx
.v
;
694 w
:= e_Textures
[ID
].tx
.width
;
695 h
:= e_Textures
[ID
].tx
.height
;
702 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
703 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
704 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
705 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
720 //TODO: overflow checks
721 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
726 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
727 // check for intersection
728 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
729 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
733 if (x0
< x1
) then x0
:= x1
;
734 if (y0
< y1
) then y0
:= y1
;
735 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
736 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
739 result
:= (w0
> 0) and (h0
> 0);
743 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
744 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
749 scxywh: array[0..3] of GLint;
750 vpxywh: array[0..3] of GLint;
752 w
, h
, dw
, cw
, ch
, yofs
: Integer;
753 u
, v
, cu
, cv
: Single;
757 procedure setScissorGLInternal (x, y, w, h: Integer);
759 //if not scallowed then exit;
764 y := vpxywh[3]-(y+h);
765 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
767 glScissor(0, 0, 0, 0);
771 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
772 glScissor(x, y, w, h);
778 if e_NoGraphics
then exit
;
779 ambientBlendMode
:= false;
781 if (wdt
< 1) or (hgt
< 1) then exit
;
783 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
785 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
789 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
791 if (Alpha
> 0) or AlphaChannel
or Blending
then
797 if not ambientBlendMode
then glDisable(GL_BLEND
);
799 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
800 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
801 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
803 glEnable(GL_TEXTURE_2D
);
804 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
809 //k8: this SHOULD work... i hope
810 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
813 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
814 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
815 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
816 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
821 // hard day's night; setup scissor
823 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
824 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
825 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
826 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
827 //glEnable(GL_SCISSOR_TEST);
828 setScissorGLInternal(x, y, wdt, hgt);
831 u
:= e_Textures
[ID
].tx
.u
;
832 v
:= e_Textures
[ID
].tx
.v
;
833 w
:= e_Textures
[ID
].tx
.width
;
834 h
:= e_Textures
[ID
].tx
.height
;
836 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
840 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
841 if onlyOneY
then yofs
:= 0;
847 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
849 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
850 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
851 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
852 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
858 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
865 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
867 if e_NoGraphics
then exit
;
868 if (w
< 1) or (h
< 1) then exit
;
869 if (a
<> 255) or ((r
or g
or b
) <> 0) then
872 glDisable(GL_TEXTURE_2D
);
873 glColor4ub(r
, g
, b
, a
);
874 if ((r
or g
or b
) <> 0) then
876 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
880 glVertex2i(x
+w
, y
+h
);
884 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
888 glVertex2i(x
+w
, y
+h
);
896 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
897 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
899 if e_NoGraphics
then Exit
;
901 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
903 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
908 if (AlphaChannel
) or (Alpha
> 0) then
909 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
912 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
915 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
917 if (Angle
<> 0) and (RC
<> nil) then
920 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
921 glRotatef(Angle
, 0, 0, 1);
922 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
925 glEnable(GL_TEXTURE_2D
);
926 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
927 glBegin(GL_QUADS
); //0-1 1-1
929 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
);
938 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
940 if e_NoGraphics
then Exit
;
941 glDisable(GL_TEXTURE_2D
);
942 glColor3ub(Red
, Green
, Blue
);
945 if (Size
= 2) or (Size
= 4) then
949 glVertex2f(X
+0.3, Y
+1.0);
952 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
955 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
957 // Make lines only top-left/bottom-right and top-right/bottom-left
969 // Pixel-perfect hack
977 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
979 nX1
, nY1
, nX2
, nY2
: Integer;
981 if e_NoGraphics
then Exit
;
982 // Only top-left/bottom-right quad
999 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1002 glDisable(GL_BLEND
);
1004 glDisable(GL_TEXTURE_2D
);
1005 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1008 nX1
:= X1
; nY1
:= Y1
;
1009 nX2
:= X2
; nY2
:= Y1
;
1010 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
1011 glVertex2i(nX1
, nY1
);
1012 glVertex2i(nX2
, nY2
);
1014 nX1
:= X2
; nY1
:= Y1
;
1015 nX2
:= X2
; nY2
:= Y2
;
1016 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1017 glVertex2i(nX1
, nY1
);
1018 glVertex2i(nX2
, nY2
);
1020 nX1
:= X2
; nY1
:= Y2
;
1021 nX2
:= X1
; nY2
:= Y2
;
1022 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1023 glVertex2i(nX1
, nY1
);
1024 glVertex2i(nX2
, nY2
);
1026 nX1
:= X1
; nY1
:= Y2
;
1027 nX2
:= X1
; nY2
:= Y1
;
1028 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1029 glVertex2i(nX1
, nY1
);
1030 glVertex2i(nX2
, nY2
);
1032 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1033 glDisable(GL_BLEND
);
1036 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
1037 Blending
: TBlending
= TBlending
.None
);
1039 if e_NoGraphics
then Exit
;
1040 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
1043 glDisable(GL_BLEND
);
1046 TBlending
.None
: if Alpha
> 0 then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1047 TBlending
.Blend
: glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
1048 TBlending
.Invert
: glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
);
1049 TBlending
.Filter
: glBlendFunc(GL_ZERO
, GL_SRC_COLOR
);
1052 glDisable(GL_TEXTURE_2D
);
1053 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1065 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1066 glDisable(GL_BLEND
);
1070 // ////////////////////////////////////////////////////////////////////////// //
1071 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1073 if (a
< 0) then a
:= 0;
1074 if (a
> 255) then a
:= 255;
1076 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1077 glDisable(GL_TEXTURE_2D
);
1078 glColor4ub(0, 0, 0, Byte(255-a
));
1085 //glRect(x, y, x+w, y+h);
1086 glColor4ub(1, 1, 1, 1);
1087 glDisable(GL_BLEND
);
1088 //glBlendEquation(GL_FUNC_ADD);
1091 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1093 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1097 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1099 if e_NoGraphics
then Exit
;
1100 // Pixel-perfect lines
1102 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1107 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1109 glDisable(GL_BLEND
);
1111 glDisable(GL_TEXTURE_2D
);
1112 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1118 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1120 glDisable(GL_BLEND
);
1123 //------------------------------------------------------------------
1124 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1125 //------------------------------------------------------------------
1126 procedure e_DeleteTexture(ID
: DWORD
);
1128 if not e_NoGraphics
then
1129 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1130 e_Textures
[ID
].tx
.id
:= 0;
1131 e_Textures
[ID
].tx
.Width
:= 0;
1132 e_Textures
[ID
].tx
.Height
:= 0;
1135 //------------------------------------------------------------------
1136 // Óäàëÿåò âñå òåêñòóðû
1137 //------------------------------------------------------------------
1138 procedure e_RemoveAllTextures();
1142 if e_Textures
= nil then Exit
;
1144 for i
:= 0 to High(e_Textures
) do
1145 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1149 //------------------------------------------------------------------
1151 //------------------------------------------------------------------
1152 procedure e_ReleaseEngine();
1154 e_RemoveAllTextures
;
1155 e_RemoveAllTextureFont
;
1158 procedure e_BeginRender();
1160 if e_NoGraphics
then Exit
;
1161 glEnable(GL_ALPHA_TEST
);
1162 glAlphaFunc(GL_GREATER
, 0.0);
1165 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1167 if e_NoGraphics
then Exit
;
1168 glClearColor(Red
, Green
, Blue
, 0);
1172 procedure e_Clear(); overload
;
1174 if e_NoGraphics
then Exit
;
1175 glClearColor(0, 0, 0, 0);
1176 glClear(GL_COLOR_BUFFER_BIT
);
1179 procedure e_EndRender();
1181 if e_NoGraphics
then Exit
;
1186 function e_GetGamma(win
: PSDL_Window
): Byte;
1188 ramp
: array [0..256*3-1] of Word;
1189 rgb
: array [0..2] of Double;
1198 if e_NoGraphics
then Exit
;
1203 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1212 for j
:= min
to max
- 1 do
1215 B
:= (j
mod 256)/256;
1217 sum
:= sum
+ ln(A
)/ln(B
);
1220 rgb
[i
] := sum
/ count
;
1223 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1226 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1228 ramp
: array [0..256*3-1] of Word;
1233 if e_NoGraphics
then Exit
;
1234 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1236 for i
:= 0 to 255 do
1238 r
:= Exp(g
* ln(i
/256))*65536;
1239 if r
< 0 then r
:= 0
1240 else if r
> 65535 then r
:= 65535;
1241 ramp
[i
] := trunc(r
);
1242 ramp
[i
+ 256] := trunc(r
);
1243 ramp
[i
+ 512] := trunc(r
);
1246 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1250 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1254 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1258 if e_CharFonts
<> nil then
1259 for i
:= 0 to High(e_CharFonts
) do
1260 if not e_CharFonts
[i
].alive
then
1266 if id
= DWORD(-1) then
1268 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1269 id
:= High(e_CharFonts
);
1272 with e_CharFonts
[id
] do
1274 for i
:= 0 to High(Chars
) do
1288 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1290 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1292 TextureID
:= Texture
;
1297 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1301 if e_NoGraphics
then Exit
;
1302 if Text = '' then Exit
;
1303 if e_CharFonts
= nil then Exit
;
1304 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1306 with e_CharFonts
[FontID
] do
1308 for a
:= 1 to Length(Text) do
1309 with Chars
[Ord(Text[a
])] do
1310 if TextureID
<> -1 then
1312 e_Draw(TextureID
, X
, Y
, 0, True, False);
1313 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1318 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1319 Color
: TRGB
; Scale
: Single = 1.0);
1324 if e_NoGraphics
then Exit
;
1325 if Text = '' then Exit
;
1326 if e_CharFonts
= nil then Exit
;
1327 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1329 with e_CharFonts
[FontID
] do
1331 for a
:= 1 to Length(Text) do
1332 with Chars
[Ord(Text[a
])] do
1333 if TextureID
<> -1 then
1335 if Scale
<> 1.0 then
1338 glScalef(Scale
, Scale
, 0);
1343 e_Draw(TextureID
, X
, Y
, 0, True, False);
1346 if Scale
<> 1.0 then glPopMatrix
;
1348 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1353 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1355 a
, TX
, TY
, len
: Integer;
1359 if e_NoGraphics
then Exit
;
1360 if Text = '' then Exit
;
1361 if e_CharFonts
= nil then Exit
;
1362 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1370 len
:= Length(Text);
1372 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1374 with e_CharFonts
[FontID
] do
1376 for a
:= 1 to len
do
1387 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1392 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1397 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1402 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1407 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1412 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1417 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1422 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1427 with Chars
[Ord(Text[a
])] do
1428 if TextureID
<> -1 then
1432 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1435 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1441 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1449 if Text = '' then Exit
;
1450 if e_CharFonts
= nil then Exit
;
1451 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1453 with e_CharFonts
[FontID
] do
1455 for a
:= 1 to Length(Text) do
1456 with Chars
[Ord(Text[a
])] do
1457 if TextureID
<> -1 then
1459 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1460 e_GetTextureSize(TextureID
, nil, @h2
);
1461 if h2
> h
then h
:= h2
;
1466 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1468 a
, lines
, len
: Integer;
1469 h2
, w2
, tw
, th
: Word;
1476 if Text = '' then Exit
;
1477 if e_CharFonts
= nil then Exit
;
1478 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1481 len
:= Length(Text);
1483 with e_CharFonts
[FontID
] do
1485 for a
:= 1 to len
do
1487 if Text[a
] = #10 then
1490 if w2
> tw
then tw
:= w2
;
1495 with Chars
[Ord(Text[a
])] do
1496 if TextureID
<> -1 then
1498 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1499 e_GetTextureSize(TextureID
, nil, @h2
);
1500 if h2
> th
then th
:= h2
;
1512 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1518 if e_CharFonts
= nil then Exit
;
1519 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1521 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1522 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1525 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1532 if e_CharFonts
= nil then Exit
;
1533 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1535 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1537 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1538 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1540 if h2
> Result
then Result
:= h2
;
1544 procedure e_CharFont_Remove(FontID
: DWORD
);
1548 with e_CharFonts
[FontID
] do
1549 for a
:= 0 to High(Chars
) do
1550 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1552 e_CharFonts
[FontID
].alive
:= False;
1555 procedure e_CharFont_RemoveAll();
1559 if e_CharFonts
= nil then Exit
;
1561 for a
:= 0 to High(e_CharFonts
) do
1562 e_CharFont_Remove(a
);
1567 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1576 if e_NoGraphics
then Exit
;
1577 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1581 if e_TextureFonts
<> nil then
1582 for i
:= 0 to High(e_TextureFonts
) do
1583 if e_TextureFonts
[i
].Base
= 0 then
1589 if id
= DWORD(-1) then
1591 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1592 id
:= High(e_TextureFonts
);
1595 with e_TextureFonts
[id
] do
1598 Base
:= glGenLists(XCount
*YCount
);
1600 TextureID
:= e_Textures
[Tex
].tx
.id
;
1601 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1602 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
;
1642 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1644 e_TextureFonts
[FontID
].Base
:= 0;
1647 {$IFNDEF NOGL_LISTS}
1648 procedure e_TextureFontDrawChar(ch
: Char; FontID
: DWORD
);
1653 Width
, Height
: Integer;
1654 XCount
, YCount
: Integer;
1656 index
:= Ord(ch
) - 32;
1657 Tex
:= e_TextureFonts
[FontID
].Texture
;
1658 Width
:= e_Textures
[Tex
].tx
.Width
;
1659 Height
:= e_Textures
[Tex
].tx
.Height
;
1660 XCount
:= e_TextureFonts
[FontID
].XC
;
1661 YCount
:= e_TextureFonts
[FontID
].YC
;
1662 cx
:= (index
mod XCount
)/XCount
;
1663 cy
:= (index
div YCount
)/YCount
;
1665 glTexCoord2f(cx
, 1 - cy
- 1/YCount
);
1666 glVertex2i(0, Height
div YCount
);
1667 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
- 1/YCount
);
1668 glVertex2i(Width
div XCount
, Height
div YCount
);
1669 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
);
1670 glVertex2i(Width
div XCount
, 0);
1671 glTexCoord2f(cx
, 1 - cy
);
1674 glTranslatef((e_Textures
[Tex
].tx
.Width
div XCount
) + e_TextureFonts
[FontID
].SPC
, 0, 0);
1677 procedure e_TextureFontDrawString(Text: String; FontID
: DWORD
);
1681 for i
:= 1 to High(Text) do
1682 e_TextureFontDrawChar(Text[i
], FontID
);
1686 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1688 if e_NoGraphics
then Exit
;
1689 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1690 if Text = '' then Exit
;
1692 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1695 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1698 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1699 glEnable(GL_TEXTURE_2D
);
1700 glTranslatef(x
, y
, 0);
1702 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1703 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1705 e_TextureFontDrawString(Text, FontID
);
1707 glDisable(GL_TEXTURE_2D
);
1710 glDisable(GL_BLEND
);
1713 // god forgive me for this, but i cannot figure out how to do it without lists
1714 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1716 if e_NoGraphics
then Exit
;
1721 glColor4ub(0, 0, 0, 128);
1722 glTranslatef(X
+1, Y
+1, 0);
1724 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1726 e_TextureFontDrawChar(Ch
, FontID
);
1732 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1733 glTranslatef(X
, Y
, 0);
1735 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1737 e_TextureFontDrawChar(Ch
, FontID
);
1743 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1745 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1746 glEnable(GL_TEXTURE_2D
);
1747 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1749 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1751 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1752 glDisable(GL_TEXTURE_2D
);
1753 glDisable(GL_BLEND
);
1756 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1758 result
:= e_TextureFonts
[FontID
].CharWidth
;
1761 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
;
1762 Shadow
: Boolean = False; Newlines
: Boolean = False);
1764 a
, TX
, TY
, len
: Integer;
1768 if e_NoGraphics
then Exit
;
1769 if Text = '' then Exit
;
1770 if e_TextureFonts
= nil then Exit
;
1771 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1779 len
:= Length(Text);
1781 w
:= e_TextureFonts
[FontID
].CharWidth
;
1782 h
:= e_TextureFonts
[FontID
].CharHeight
;
1784 with e_TextureFonts
[FontID
] do
1786 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1787 glEnable(GL_TEXTURE_2D
);
1790 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1793 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1796 for a
:= 1 to len
do
1810 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1815 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1820 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1825 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1830 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1835 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1840 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1845 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1852 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1857 glDisable(GL_TEXTURE_2D
);
1858 glDisable(GL_BLEND
);
1862 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1863 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1865 if e_NoGraphics
then Exit
;
1866 if Text = '' then Exit
;
1869 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1870 glEnable(GL_TEXTURE_2D
);
1873 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1876 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1881 glColor4ub(0, 0, 0, 128);
1882 glTranslatef(x
+1, y
+1, 0);
1883 glScalef(Scale
, Scale
, 0);
1885 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1887 e_TextureFontDrawString(Text, FontID
);
1893 glColor4ub(Red
, Green
, Blue
, 255);
1894 glTranslatef(x
, y
, 0);
1895 glScalef(Scale
, Scale
, 0);
1897 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1899 e_TextureFontDrawString(Text, FontID
);
1902 glDisable(GL_TEXTURE_2D
);
1904 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1905 glDisable(GL_BLEND
);
1908 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1912 if e_NoGraphics
then Exit
;
1913 if Integer(ID
) > High(e_TextureFonts
) then
1915 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1916 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1919 procedure e_RemoveAllTextureFont();
1923 if e_NoGraphics
then Exit
;
1924 if e_TextureFonts
= nil then Exit
;
1926 for i
:= 0 to High(e_TextureFonts
) do
1927 if e_TextureFonts
[i
].Base
<> 0 then
1930 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1932 e_TextureFonts
[i
].Base
:= 0;
1935 e_TextureFonts
:= nil;
1938 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1945 function _Point(X
, Y
: Integer): TPoint2i
;
1951 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1955 Result
.Width
:= Width
;
1956 Result
.Height
:= Height
;
1959 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1968 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1970 pixels
, obuf
, scln
, ps
, pd
: PByte;
1973 i
, x
, y
, res
: Integer;
1974 sign
: array [0..7] of Byte;
1975 hbuf
: array [0..12] of Byte;
1980 if e_NoGraphics
then Exit
;
1983 // first, extract and pack graphics data
1984 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1986 GetMem(pixels
, Width
*Height
*3);
1988 FillChar(pixels
^, Width
*Height
*3, 0);
1989 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1990 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1992 if e_FastScreenshots
then
1995 GetMem(scln
, (Width
*3+1)*Height
);
1999 Inc(ps
, (Width
*3)*(Height
-1));
2000 for i
:= 0 to Height
-1 do
2004 Move(ps
^, pd
^, Width
*3);
2016 obufsize
:= (Width
*3+1)*Height
*2;
2017 GetMem(obuf
, obufsize
);
2022 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
2023 if res
= Z_OK
then break
;
2024 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
2025 obufsize
:= obufsize
*2;
2028 GetMem(obuf
, obufsize
);
2030 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
2043 st
.writeBuffer(sign
, 8);
2044 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2047 writeIntBE(st
, LongWord(13));
2052 st
.writeBuffer(sign
, 4);
2053 crc
:= crc32(0, @sign
[0], 4);
2056 hbuf
[2] := (Width
shr 8) and $ff;
2057 hbuf
[3] := Width
and $ff;
2060 hbuf
[6] := (Height
shr 8) and $ff;
2061 hbuf
[7] := Height
and $ff;
2062 hbuf
[8] := 8; // bit depth
2063 hbuf
[9] := 2; // RGB
2064 hbuf
[10] := 0; // compression method
2065 hbuf
[11] := 0; // filter method
2066 hbuf
[12] := 0; // no interlace
2067 crc
:= crc32(crc
, @hbuf
[0], 13);
2068 st
.writeBuffer(hbuf
, 13);
2069 writeIntBE(st
, crc
);
2070 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2073 writeIntBE(st
, LongWord(dlen
));
2078 st
.writeBuffer(sign
, 4);
2079 crc
:= crc32(0, @sign
[0], 4);
2080 crc
:= crc32(crc
, obuf
, dlen
);
2081 st
.writeBuffer(obuf
^, dlen
);
2082 writeIntBE(st
, crc
);
2083 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2086 writeIntBE(st
, LongWord(0));
2091 st
.writeBuffer(sign
, 4);
2092 crc
:= crc32(0, @sign
[0], 4);
2093 writeIntBE(st
, crc
);
2094 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2096 if obuf
<> nil then FreeMem(obuf
);
2101 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2102 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2105 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2107 //writeln(stderr, 'moving pixels...');
2108 for y
:= Height
-1 downto 0 do
2110 for x
:= 0 to Width
-1 do
2112 clr
.r
:= ps
^; Inc(ps
);
2113 clr
.g
:= ps
^; Inc(ps
);
2114 clr
.b
:= ps
^; Inc(ps
);
2116 SetPixel32(img
, x
, y
, clr
);
2119 GlobalMetadata
.ClearMetaItems();
2120 GlobalMetadata
.ClearMetaItemsForSaving();
2121 //writeln(stderr, 'compressing image...');
2122 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2123 //writeln(stderr, 'done!');