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);
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 DestroyFramebuffer
;
384 glBindTexture(GL_TEXTURE_2D
, 0);
385 glBindRenderbuffer(GL_RENDERBUFFER
, 0);
386 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
390 glDeleteTextures(1, @e_Frame
);
396 glDeleteRenderbuffers(1, @e_RBO
);
402 glDeleteFramebuffers(1, @e_FBO
);
407 function e_ResizeFramebuffer(Width
, Height
: Integer): Boolean;
411 if e_NoGraphics
then Exit
;
420 glGenFramebuffers(1, @e_FBO
);
422 if glGetError() <> GL_NO_ERROR
then
424 e_LogWriteln('GL: glGenFramebuffers failed');
428 glGenTextures(1, @e_Frame
);
429 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
430 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, Width
, Height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, nil);
431 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
432 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
434 if glGetError() <> GL_NO_ERROR
then
436 e_LogWriteln('GL: can''t create FBO color buffer');
441 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
442 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, e_Frame
, 0);
445 glGenRenderbuffers(1, @e_RBO
);
446 glBindRenderbuffer(GL_RENDERBUFFER
, e_RBO
);
447 glRenderbufferStorage(GL_RENDERBUFFER
, GL_DEPTH24_STENCIL8
, Width
, Height
);
448 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, e_RBO
);
449 if glCheckFramebufferStatus(GL_FRAMEBUFFER
) <> GL_FRAMEBUFFER_COMPLETE
then
451 e_LogWriteln('GL: can''t construct framebuffer with depth+stencil attachment');
452 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, 0);
453 glDeleteRenderbuffers(1, @e_RBO
); e_RBO
:= 0;
458 if glCheckFramebufferStatus(GL_FRAMEBUFFER
) <> GL_FRAMEBUFFER_COMPLETE
then
460 e_LogWriteln('GL: can''t construct framebuffer with color attachment');
468 procedure e_ResizeWindow(Width
, Height
: Integer);
472 e_SetViewPort(0, 0, Width
, Height
);
475 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
477 x1
, y1
, tmp
: Integer;
479 if (w
< 1) or (h
< 1) then exit
;
482 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
483 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
484 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
485 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
486 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
487 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
490 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
492 if (e_FBO
= 0) or (e_Frame
= 0) or e_NoGraphics
then exit
;
495 glEnable(GL_TEXTURE_2D
);
496 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
497 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
498 e_SetViewPort(0, 0, WinWidth
, WinHeight
);
499 glColor4ub(255, 255, 255, 255);
502 glTexCoord2f(0, 1); glVertex2i( 0, 0);
503 glTexCoord2f(0, 0); glVertex2i( 0, WinHeight
);
504 glTexCoord2f(1, 0); glVertex2i(WinWidth
, WinHeight
);
505 glTexCoord2f(1, 1); glVertex2i(WinWidth
, 0);
508 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
509 e_SetViewPort(0, 0, e_FrameW
, e_FrameH
);
512 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
513 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
515 if e_NoGraphics
then Exit
;
516 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
518 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
523 if (AlphaChannel
) or (Alpha
> 0) then
524 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
527 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
530 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
532 glEnable(GL_TEXTURE_2D
);
533 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
536 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
);
538 //u := e_Textures[ID].tx.u;
539 //v := e_Textures[ID].tx.v;
542 if Mirror = M_NONE then
544 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
545 glTexCoord2f(0, 0); glVertex2i(X, Y);
546 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
547 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
550 if Mirror = M_HORIZONTAL then
552 glTexCoord2f(u, 0); glVertex2i(X, Y);
553 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
554 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
555 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
558 if Mirror = M_VERTICAL then
560 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
561 glTexCoord2f(0, -v); glVertex2i(X, Y);
562 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
563 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
572 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
573 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
577 if e_NoGraphics
then Exit
;
578 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
580 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
585 if (AlphaChannel
) or (Alpha
> 0) then
586 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
589 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
592 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
594 glEnable(GL_TEXTURE_2D
);
595 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
597 u
:= e_Textures
[ID
].tx
.u
;
598 v
:= e_Textures
[ID
].tx
.v
;
601 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
602 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
603 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
604 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
610 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
611 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
613 if e_NoGraphics
then Exit
;
614 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
616 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
621 if (AlphaChannel
) or (Alpha
> 0) then
622 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
625 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
628 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
630 glEnable(GL_TEXTURE_2D
);
631 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
633 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
);
639 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
640 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
642 X2
, Y2
, dx
, w
, h
: Integer;
645 if e_NoGraphics
then Exit
;
646 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
647 ambientBlendMode
:= false;
649 if (Alpha
> 0) or AlphaChannel
or Blending
then
655 if not ambientBlendMode
then glDisable(GL_BLEND
);
657 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
658 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
659 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
661 if (XCount
= 0) then XCount
:= 1;
662 if (YCount
= 0) then YCount
:= 1;
664 glEnable(GL_TEXTURE_2D
);
665 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
667 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
668 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
670 //k8: this SHOULD work... i hope
671 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
674 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
675 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
676 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
677 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
684 u
:= e_Textures
[ID
].tx
.u
;
685 v
:= e_Textures
[ID
].tx
.v
;
686 w
:= e_Textures
[ID
].tx
.width
;
687 h
:= e_Textures
[ID
].tx
.height
;
694 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
695 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
696 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
697 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
712 //TODO: overflow checks
713 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
718 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
719 // check for intersection
720 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
721 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
725 if (x0
< x1
) then x0
:= x1
;
726 if (y0
< y1
) then y0
:= y1
;
727 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
728 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
731 result
:= (w0
> 0) and (h0
> 0);
735 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
736 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
741 scxywh: array[0..3] of GLint;
742 vpxywh: array[0..3] of GLint;
744 w
, h
, dw
, cw
, ch
, yofs
: Integer;
745 u
, v
, cu
, cv
: Single;
749 procedure setScissorGLInternal (x, y, w, h: Integer);
751 //if not scallowed then exit;
756 y := vpxywh[3]-(y+h);
757 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
759 glScissor(0, 0, 0, 0);
763 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
764 glScissor(x, y, w, h);
770 if e_NoGraphics
then exit
;
771 ambientBlendMode
:= false;
773 if (wdt
< 1) or (hgt
< 1) then exit
;
775 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
777 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
781 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
783 if (Alpha
> 0) or AlphaChannel
or Blending
then
789 if not ambientBlendMode
then glDisable(GL_BLEND
);
791 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
792 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
793 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
795 glEnable(GL_TEXTURE_2D
);
796 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
801 //k8: this SHOULD work... i hope
802 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
805 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
806 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
807 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
808 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
813 // hard day's night; setup scissor
815 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
816 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
817 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
818 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
819 //glEnable(GL_SCISSOR_TEST);
820 setScissorGLInternal(x, y, wdt, hgt);
823 u
:= e_Textures
[ID
].tx
.u
;
824 v
:= e_Textures
[ID
].tx
.v
;
825 w
:= e_Textures
[ID
].tx
.width
;
826 h
:= e_Textures
[ID
].tx
.height
;
828 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
832 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
833 if onlyOneY
then yofs
:= 0;
839 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
841 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
842 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
843 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
844 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
850 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
857 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
859 if e_NoGraphics
then exit
;
860 if (w
< 1) or (h
< 1) then exit
;
861 if (a
<> 255) or ((r
or g
or b
) <> 0) then
864 glDisable(GL_TEXTURE_2D
);
865 glColor4ub(r
, g
, b
, a
);
866 if ((r
or g
or b
) <> 0) then
868 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
872 glVertex2i(x
+w
, y
+h
);
876 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
880 glVertex2i(x
+w
, y
+h
);
888 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
889 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
891 if e_NoGraphics
then Exit
;
893 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
895 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
900 if (AlphaChannel
) or (Alpha
> 0) then
901 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
904 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
907 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
909 if (Angle
<> 0) and (RC
<> nil) then
912 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
913 glRotatef(Angle
, 0, 0, 1);
914 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
917 glEnable(GL_TEXTURE_2D
);
918 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
919 glBegin(GL_QUADS
); //0-1 1-1
921 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
);
930 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
932 if e_NoGraphics
then Exit
;
933 glDisable(GL_TEXTURE_2D
);
934 glColor3ub(Red
, Green
, Blue
);
937 if (Size
= 2) or (Size
= 4) then
941 glVertex2f(X
+0.3, Y
+1.0);
944 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
947 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
949 // Make lines only top-left/bottom-right and top-right/bottom-left
961 // Pixel-perfect hack
969 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
971 nX1
, nY1
, nX2
, nY2
: Integer;
973 if e_NoGraphics
then Exit
;
974 // Only top-left/bottom-right quad
991 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
996 glDisable(GL_TEXTURE_2D
);
997 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1000 nX1
:= X1
; nY1
:= Y1
;
1001 nX2
:= X2
; nY2
:= Y1
;
1002 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
1003 glVertex2i(nX1
, nY1
);
1004 glVertex2i(nX2
, nY2
);
1006 nX1
:= X2
; nY1
:= Y1
;
1007 nX2
:= X2
; nY2
:= Y2
;
1008 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1009 glVertex2i(nX1
, nY1
);
1010 glVertex2i(nX2
, nY2
);
1012 nX1
:= X2
; nY1
:= Y2
;
1013 nX2
:= X1
; nY2
:= Y2
;
1014 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1015 glVertex2i(nX1
, nY1
);
1016 glVertex2i(nX2
, nY2
);
1018 nX1
:= X1
; nY1
:= Y2
;
1019 nX2
:= X1
; nY2
:= Y1
;
1020 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1021 glVertex2i(nX1
, nY1
);
1022 glVertex2i(nX2
, nY2
);
1024 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1025 glDisable(GL_BLEND
);
1028 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
1029 Blending
: TBlending
= TBlending
.None
);
1031 if e_NoGraphics
then Exit
;
1032 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
1035 glDisable(GL_BLEND
);
1038 TBlending
.None
: if Alpha
> 0 then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1039 TBlending
.Blend
: glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
1040 TBlending
.Invert
: glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
);
1041 TBlending
.Filter
: glBlendFunc(GL_ZERO
, GL_SRC_COLOR
);
1044 glDisable(GL_TEXTURE_2D
);
1045 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1057 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1058 glDisable(GL_BLEND
);
1062 // ////////////////////////////////////////////////////////////////////////// //
1063 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1065 if (a
< 0) then a
:= 0;
1066 if (a
> 255) then a
:= 255;
1068 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1069 glDisable(GL_TEXTURE_2D
);
1070 glColor4ub(0, 0, 0, Byte(255-a
));
1077 //glRect(x, y, x+w, y+h);
1078 glColor4ub(1, 1, 1, 1);
1079 glDisable(GL_BLEND
);
1080 //glBlendEquation(GL_FUNC_ADD);
1083 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1085 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1089 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1091 if e_NoGraphics
then Exit
;
1092 // Pixel-perfect lines
1094 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1099 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1101 glDisable(GL_BLEND
);
1103 glDisable(GL_TEXTURE_2D
);
1104 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1110 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1112 glDisable(GL_BLEND
);
1115 //------------------------------------------------------------------
1116 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1117 //------------------------------------------------------------------
1118 procedure e_DeleteTexture(ID
: DWORD
);
1120 if not e_NoGraphics
then
1121 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1122 e_Textures
[ID
].tx
.id
:= 0;
1123 e_Textures
[ID
].tx
.Width
:= 0;
1124 e_Textures
[ID
].tx
.Height
:= 0;
1127 //------------------------------------------------------------------
1128 // Óäàëÿåò âñå òåêñòóðû
1129 //------------------------------------------------------------------
1130 procedure e_RemoveAllTextures();
1134 if e_Textures
= nil then Exit
;
1136 for i
:= 0 to High(e_Textures
) do
1137 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1141 //------------------------------------------------------------------
1143 //------------------------------------------------------------------
1144 procedure e_ReleaseEngine();
1146 e_RemoveAllTextures
;
1147 e_RemoveAllTextureFont
;
1150 procedure e_BeginRender();
1152 if e_NoGraphics
then Exit
;
1153 glEnable(GL_ALPHA_TEST
);
1154 glAlphaFunc(GL_GREATER
, 0.0);
1157 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1159 if e_NoGraphics
then Exit
;
1160 glClearColor(Red
, Green
, Blue
, 0);
1164 procedure e_Clear(); overload
;
1166 if e_NoGraphics
then Exit
;
1167 glClearColor(0, 0, 0, 0);
1168 glClear(GL_COLOR_BUFFER_BIT
);
1171 procedure e_EndRender();
1173 if e_NoGraphics
then Exit
;
1178 function e_GetGamma(win
: PSDL_Window
): Byte;
1180 ramp
: array [0..256*3-1] of Word;
1181 rgb
: array [0..2] of Double;
1190 if e_NoGraphics
then Exit
;
1195 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1204 for j
:= min
to max
- 1 do
1207 B
:= (j
mod 256)/256;
1209 sum
:= sum
+ ln(A
)/ln(B
);
1212 rgb
[i
] := sum
/ count
;
1215 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1218 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1220 ramp
: array [0..256*3-1] of Word;
1225 if e_NoGraphics
then Exit
;
1226 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1228 for i
:= 0 to 255 do
1230 r
:= Exp(g
* ln(i
/256))*65536;
1231 if r
< 0 then r
:= 0
1232 else if r
> 65535 then r
:= 65535;
1233 ramp
[i
] := trunc(r
);
1234 ramp
[i
+ 256] := trunc(r
);
1235 ramp
[i
+ 512] := trunc(r
);
1238 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1242 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1246 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1250 if e_CharFonts
<> nil then
1251 for i
:= 0 to High(e_CharFonts
) do
1252 if not e_CharFonts
[i
].alive
then
1258 if id
= DWORD(-1) then
1260 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1261 id
:= High(e_CharFonts
);
1264 with e_CharFonts
[id
] do
1266 for i
:= 0 to High(Chars
) do
1280 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1282 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1284 TextureID
:= Texture
;
1289 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1293 if e_NoGraphics
then Exit
;
1294 if Text = '' then Exit
;
1295 if e_CharFonts
= nil then Exit
;
1296 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1298 with e_CharFonts
[FontID
] do
1300 for a
:= 1 to Length(Text) do
1301 with Chars
[Ord(Text[a
])] do
1302 if TextureID
<> -1 then
1304 e_Draw(TextureID
, X
, Y
, 0, True, False);
1305 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1310 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1311 Color
: TRGB
; Scale
: Single = 1.0);
1316 if e_NoGraphics
then Exit
;
1317 if Text = '' then Exit
;
1318 if e_CharFonts
= nil then Exit
;
1319 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1321 with e_CharFonts
[FontID
] do
1323 for a
:= 1 to Length(Text) do
1324 with Chars
[Ord(Text[a
])] do
1325 if TextureID
<> -1 then
1327 if Scale
<> 1.0 then
1330 glScalef(Scale
, Scale
, 0);
1335 e_Draw(TextureID
, X
, Y
, 0, True, False);
1338 if Scale
<> 1.0 then glPopMatrix
;
1340 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1345 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1347 a
, TX
, TY
, len
: Integer;
1351 if e_NoGraphics
then Exit
;
1352 if Text = '' then Exit
;
1353 if e_CharFonts
= nil then Exit
;
1354 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1362 len
:= Length(Text);
1364 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1366 with e_CharFonts
[FontID
] do
1368 for a
:= 1 to len
do
1379 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1384 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1389 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1394 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1399 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1404 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1409 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1414 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1419 with Chars
[Ord(Text[a
])] do
1420 if TextureID
<> -1 then
1424 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1427 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1433 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1441 if Text = '' then Exit
;
1442 if e_CharFonts
= nil then Exit
;
1443 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1445 with e_CharFonts
[FontID
] do
1447 for a
:= 1 to Length(Text) do
1448 with Chars
[Ord(Text[a
])] do
1449 if TextureID
<> -1 then
1451 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1452 e_GetTextureSize(TextureID
, nil, @h2
);
1453 if h2
> h
then h
:= h2
;
1458 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1460 a
, lines
, len
: Integer;
1461 h2
, w2
, tw
, th
: Word;
1468 if Text = '' then Exit
;
1469 if e_CharFonts
= nil then Exit
;
1470 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1473 len
:= Length(Text);
1475 with e_CharFonts
[FontID
] do
1477 for a
:= 1 to len
do
1479 if Text[a
] = #10 then
1482 if w2
> tw
then tw
:= w2
;
1487 with Chars
[Ord(Text[a
])] do
1488 if TextureID
<> -1 then
1490 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1491 e_GetTextureSize(TextureID
, nil, @h2
);
1492 if h2
> th
then th
:= h2
;
1504 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1510 if e_CharFonts
= nil then Exit
;
1511 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1513 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1514 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1517 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1524 if e_CharFonts
= nil then Exit
;
1525 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1527 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1529 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1530 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1532 if h2
> Result
then Result
:= h2
;
1536 procedure e_CharFont_Remove(FontID
: DWORD
);
1540 with e_CharFonts
[FontID
] do
1541 for a
:= 0 to High(Chars
) do
1542 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1544 e_CharFonts
[FontID
].alive
:= False;
1547 procedure e_CharFont_RemoveAll();
1551 if e_CharFonts
= nil then Exit
;
1553 for a
:= 0 to High(e_CharFonts
) do
1554 e_CharFont_Remove(a
);
1559 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1568 if e_NoGraphics
then Exit
;
1569 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1573 if e_TextureFonts
<> nil then
1574 for i
:= 0 to High(e_TextureFonts
) do
1575 if e_TextureFonts
[i
].Base
= 0 then
1581 if id
= DWORD(-1) then
1583 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1584 id
:= High(e_TextureFonts
);
1587 with e_TextureFonts
[id
] do
1590 Base
:= glGenLists(XCount
*YCount
);
1592 TextureID
:= e_Textures
[Tex
].tx
.id
;
1593 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1594 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1602 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1603 for loop1
:= 0 to XCount
*YCount
-1 do
1605 cx
:= (loop1
mod XCount
)/XCount
;
1606 cy
:= (loop1
div YCount
)/YCount
;
1608 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1610 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1611 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1613 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1614 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1616 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1617 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1619 glTexCoord2f(cx
, 1.0-cy
);
1622 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1630 procedure e_TextureFontKill(FontID
: DWORD
);
1632 if e_NoGraphics
then Exit
;
1634 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1636 e_TextureFonts
[FontID
].Base
:= 0;
1639 {$IFNDEF NOGL_LISTS}
1640 procedure e_TextureFontDrawChar(ch
: Char; FontID
: DWORD
);
1645 Width
, Height
: Integer;
1646 XCount
, YCount
: Integer;
1648 index
:= Ord(ch
) - 32;
1649 Tex
:= e_TextureFonts
[FontID
].Texture
;
1650 Width
:= e_Textures
[Tex
].tx
.Width
;
1651 Height
:= e_Textures
[Tex
].tx
.Height
;
1652 XCount
:= e_TextureFonts
[FontID
].XC
;
1653 YCount
:= e_TextureFonts
[FontID
].YC
;
1654 cx
:= (index
mod XCount
)/XCount
;
1655 cy
:= (index
div YCount
)/YCount
;
1657 glTexCoord2f(cx
, 1 - cy
- 1/YCount
);
1658 glVertex2i(0, Height
div YCount
);
1659 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
- 1/YCount
);
1660 glVertex2i(Width
div XCount
, Height
div YCount
);
1661 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
);
1662 glVertex2i(Width
div XCount
, 0);
1663 glTexCoord2f(cx
, 1 - cy
);
1666 glTranslatef((e_Textures
[Tex
].tx
.Width
div XCount
) + e_TextureFonts
[FontID
].SPC
, 0, 0);
1669 procedure e_TextureFontDrawString(Text: String; FontID
: DWORD
);
1673 for i
:= 1 to High(Text) do
1674 e_TextureFontDrawChar(Text[i
], FontID
);
1678 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1680 if e_NoGraphics
then Exit
;
1681 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1682 if Text = '' then Exit
;
1684 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1687 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1690 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1691 glEnable(GL_TEXTURE_2D
);
1692 glTranslatef(x
, y
, 0);
1694 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1695 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1697 e_TextureFontDrawString(Text, FontID
);
1699 glDisable(GL_TEXTURE_2D
);
1702 glDisable(GL_BLEND
);
1705 // god forgive me for this, but i cannot figure out how to do it without lists
1706 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1708 if e_NoGraphics
then Exit
;
1713 glColor4ub(0, 0, 0, 128);
1714 glTranslatef(X
+1, Y
+1, 0);
1716 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1718 e_TextureFontDrawChar(Ch
, FontID
);
1724 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1725 glTranslatef(X
, Y
, 0);
1727 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1729 e_TextureFontDrawChar(Ch
, FontID
);
1735 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1737 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1738 glEnable(GL_TEXTURE_2D
);
1739 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1741 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1743 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1744 glDisable(GL_TEXTURE_2D
);
1745 glDisable(GL_BLEND
);
1748 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1750 result
:= e_TextureFonts
[FontID
].CharWidth
;
1753 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
;
1754 Shadow
: Boolean = False; Newlines
: Boolean = False);
1756 a
, TX
, TY
, len
: Integer;
1760 if e_NoGraphics
then Exit
;
1761 if Text = '' then Exit
;
1762 if e_TextureFonts
= nil then Exit
;
1763 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1771 len
:= Length(Text);
1773 w
:= e_TextureFonts
[FontID
].CharWidth
;
1774 h
:= e_TextureFonts
[FontID
].CharHeight
;
1776 with e_TextureFonts
[FontID
] do
1778 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1779 glEnable(GL_TEXTURE_2D
);
1782 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1785 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1788 for a
:= 1 to len
do
1802 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1807 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1812 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1817 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1822 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1827 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1832 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1837 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1844 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1849 glDisable(GL_TEXTURE_2D
);
1850 glDisable(GL_BLEND
);
1854 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1855 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1857 if e_NoGraphics
then Exit
;
1858 if Text = '' then Exit
;
1861 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1862 glEnable(GL_TEXTURE_2D
);
1865 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1868 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1873 glColor4ub(0, 0, 0, 128);
1874 glTranslatef(x
+1, y
+1, 0);
1875 glScalef(Scale
, Scale
, 0);
1877 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1879 e_TextureFontDrawString(Text, FontID
);
1885 glColor4ub(Red
, Green
, Blue
, 255);
1886 glTranslatef(x
, y
, 0);
1887 glScalef(Scale
, Scale
, 0);
1889 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1891 e_TextureFontDrawString(Text, FontID
);
1894 glDisable(GL_TEXTURE_2D
);
1896 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1897 glDisable(GL_BLEND
);
1900 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1904 if e_NoGraphics
then Exit
;
1905 if Integer(ID
) > High(e_TextureFonts
) then
1907 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1908 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1911 procedure e_RemoveAllTextureFont();
1915 if e_NoGraphics
then Exit
;
1916 if e_TextureFonts
= nil then Exit
;
1918 for i
:= 0 to High(e_TextureFonts
) do
1919 if e_TextureFonts
[i
].Base
<> 0 then
1922 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1924 e_TextureFonts
[i
].Base
:= 0;
1927 e_TextureFonts
:= nil;
1930 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1937 function _Point(X
, Y
: Integer): TPoint2i
;
1943 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1947 Result
.Width
:= Width
;
1948 Result
.Height
:= Height
;
1951 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1960 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1962 pixels
, obuf
, scln
, ps
, pd
: PByte;
1965 i
, x
, y
, res
: Integer;
1966 sign
: array [0..7] of Byte;
1967 hbuf
: array [0..12] of Byte;
1972 if e_NoGraphics
then Exit
;
1975 // first, extract and pack graphics data
1976 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1978 GetMem(pixels
, Width
*Height
*3);
1980 FillChar(pixels
^, Width
*Height
*3, 0);
1981 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1982 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1984 if e_FastScreenshots
then
1987 GetMem(scln
, (Width
*3+1)*Height
);
1991 Inc(ps
, (Width
*3)*(Height
-1));
1992 for i
:= 0 to Height
-1 do
1996 Move(ps
^, pd
^, Width
*3);
2008 obufsize
:= (Width
*3+1)*Height
*2;
2009 GetMem(obuf
, obufsize
);
2014 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
2015 if res
= Z_OK
then break
;
2016 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
2017 obufsize
:= obufsize
*2;
2020 GetMem(obuf
, obufsize
);
2022 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
2035 st
.writeBuffer(sign
, 8);
2036 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2039 writeIntBE(st
, LongWord(13));
2044 st
.writeBuffer(sign
, 4);
2045 crc
:= crc32(0, @sign
[0], 4);
2048 hbuf
[2] := (Width
shr 8) and $ff;
2049 hbuf
[3] := Width
and $ff;
2052 hbuf
[6] := (Height
shr 8) and $ff;
2053 hbuf
[7] := Height
and $ff;
2054 hbuf
[8] := 8; // bit depth
2055 hbuf
[9] := 2; // RGB
2056 hbuf
[10] := 0; // compression method
2057 hbuf
[11] := 0; // filter method
2058 hbuf
[12] := 0; // no interlace
2059 crc
:= crc32(crc
, @hbuf
[0], 13);
2060 st
.writeBuffer(hbuf
, 13);
2061 writeIntBE(st
, crc
);
2062 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2065 writeIntBE(st
, LongWord(dlen
));
2070 st
.writeBuffer(sign
, 4);
2071 crc
:= crc32(0, @sign
[0], 4);
2072 crc
:= crc32(crc
, obuf
, dlen
);
2073 st
.writeBuffer(obuf
^, dlen
);
2074 writeIntBE(st
, crc
);
2075 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2078 writeIntBE(st
, LongWord(0));
2083 st
.writeBuffer(sign
, 4);
2084 crc
:= crc32(0, @sign
[0], 4);
2085 writeIntBE(st
, crc
);
2086 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2088 if obuf
<> nil then FreeMem(obuf
);
2093 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2094 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2097 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2099 //writeln(stderr, 'moving pixels...');
2100 for y
:= Height
-1 downto 0 do
2102 for x
:= 0 to Width
-1 do
2104 clr
.r
:= ps
^; Inc(ps
);
2105 clr
.g
:= ps
^; Inc(ps
);
2106 clr
.b
:= ps
^; Inc(ps
);
2108 SetPixel32(img
, x
, y
, clr
);
2111 GlobalMetadata
.ClearMetaItems();
2112 GlobalMetadata
.ClearMetaItemsForSaving();
2113 //writeln(stderr, 'compressing image...');
2114 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2115 //writeln(stderr, 'done!');