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;
206 e_FrameW
: Integer = -1;
207 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 e_ResizeFramebuffer(Width
, Height
: Integer);
387 if e_NoGraphics
then Exit
;
389 glBindTexture(GL_TEXTURE_2D
, 0);
390 glBindRenderbuffer(GL_RENDERBUFFER
, 0);
391 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
395 glDeleteTextures(1, @e_Frame
);
401 glDeleteRenderbuffers(1, @e_RBO
);
407 glDeleteFramebuffers(1, @e_FBO
);
414 glGenFramebuffers(1, @e_FBO
);
416 glGenTextures(1, @e_Frame
);
417 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
418 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, Width
, Height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, nil);
419 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
420 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
422 glGenRenderbuffers(1, @e_RBO
);
423 glBindRenderbuffer(GL_RENDERBUFFER
, e_RBO
);
424 glRenderbufferStorage(GL_RENDERBUFFER
, GL_DEPTH24_STENCIL8
, Width
, Height
);
426 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
427 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, e_Frame
, 0);
428 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, e_RBO
);
432 procedure e_ResizeWindow(Width
, Height
: Integer);
436 e_SetViewPort(0, 0, Width
, Height
);
439 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
441 x1
, y1
, tmp
: Integer;
443 if (w
< 1) or (h
< 1) then exit
;
446 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
447 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
448 //HACK: make texture one pixel shorter, so it won't wrap
449 if (g_dbg_scale
<> 1.0) then
454 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
455 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
456 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
457 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
460 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
463 if (e_FBO
= 0) or (e_Frame
= 0) or e_NoGraphics
then exit
;
465 glEnable(GL_TEXTURE_2D
);
466 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
467 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
468 glColor4ub(255, 255, 255, 255);
469 e_SetViewPort(0, 0, WinWidth
, WinHeight
);
471 drawTxQuad(0, 0, WinWidth
, WinHeight
, e_FrameW
, e_FrameH
, 1, 1, TMirrorType
.None
);
473 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
474 e_SetViewPort(0, 0, e_FrameW
, e_FrameH
);
478 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
479 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
481 if e_NoGraphics
then Exit
;
482 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
484 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
489 if (AlphaChannel
) or (Alpha
> 0) then
490 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
493 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
496 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
498 glEnable(GL_TEXTURE_2D
);
499 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
502 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
);
504 //u := e_Textures[ID].tx.u;
505 //v := e_Textures[ID].tx.v;
508 if Mirror = M_NONE then
510 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
511 glTexCoord2f(0, 0); glVertex2i(X, Y);
512 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
513 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
516 if Mirror = M_HORIZONTAL then
518 glTexCoord2f(u, 0); glVertex2i(X, Y);
519 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
520 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
521 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
524 if Mirror = M_VERTICAL then
526 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
527 glTexCoord2f(0, -v); glVertex2i(X, Y);
528 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
529 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
538 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
539 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
543 if e_NoGraphics
then Exit
;
544 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
546 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
551 if (AlphaChannel
) or (Alpha
> 0) then
552 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
555 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
558 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
560 glEnable(GL_TEXTURE_2D
);
561 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
563 u
:= e_Textures
[ID
].tx
.u
;
564 v
:= e_Textures
[ID
].tx
.v
;
567 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
568 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
569 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
570 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
576 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
577 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
579 if e_NoGraphics
then Exit
;
580 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
582 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
587 if (AlphaChannel
) or (Alpha
> 0) then
588 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
591 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
594 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
596 glEnable(GL_TEXTURE_2D
);
597 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
599 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
);
605 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
606 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
608 X2
, Y2
, dx
, w
, h
: Integer;
611 if e_NoGraphics
then Exit
;
612 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
613 ambientBlendMode
:= false;
615 if (Alpha
> 0) or AlphaChannel
or Blending
then
621 if not ambientBlendMode
then glDisable(GL_BLEND
);
623 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
624 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
625 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
627 if (XCount
= 0) then XCount
:= 1;
628 if (YCount
= 0) then YCount
:= 1;
630 glEnable(GL_TEXTURE_2D
);
631 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
633 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
634 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
636 //k8: this SHOULD work... i hope
637 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
640 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
641 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
642 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
643 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
650 u
:= e_Textures
[ID
].tx
.u
;
651 v
:= e_Textures
[ID
].tx
.v
;
652 w
:= e_Textures
[ID
].tx
.width
;
653 h
:= e_Textures
[ID
].tx
.height
;
660 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
661 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
662 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
663 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
678 //TODO: overflow checks
679 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
684 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
685 // check for intersection
686 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
687 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
691 if (x0
< x1
) then x0
:= x1
;
692 if (y0
< y1
) then y0
:= y1
;
693 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
694 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
697 result
:= (w0
> 0) and (h0
> 0);
701 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
702 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
707 scxywh: array[0..3] of GLint;
708 vpxywh: array[0..3] of GLint;
710 w
, h
, dw
, cw
, ch
, yofs
: Integer;
711 u
, v
, cu
, cv
: Single;
715 procedure setScissorGLInternal (x, y, w, h: Integer);
717 //if not scallowed then exit;
722 y := vpxywh[3]-(y+h);
723 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
725 glScissor(0, 0, 0, 0);
729 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
730 glScissor(x, y, w, h);
736 if e_NoGraphics
then exit
;
737 ambientBlendMode
:= false;
739 if (wdt
< 1) or (hgt
< 1) then exit
;
741 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
743 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
747 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
749 if (Alpha
> 0) or AlphaChannel
or Blending
then
755 if not ambientBlendMode
then glDisable(GL_BLEND
);
757 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
758 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
759 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
761 glEnable(GL_TEXTURE_2D
);
762 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
767 //k8: this SHOULD work... i hope
768 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
771 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
772 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
773 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
774 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
779 // hard day's night; setup scissor
781 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
782 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
783 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
784 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
785 //glEnable(GL_SCISSOR_TEST);
786 setScissorGLInternal(x, y, wdt, hgt);
789 u
:= e_Textures
[ID
].tx
.u
;
790 v
:= e_Textures
[ID
].tx
.v
;
791 w
:= e_Textures
[ID
].tx
.width
;
792 h
:= e_Textures
[ID
].tx
.height
;
794 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
798 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
799 if onlyOneY
then yofs
:= 0;
805 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
807 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
808 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
809 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
810 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
816 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
823 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
825 if e_NoGraphics
then exit
;
826 if (w
< 1) or (h
< 1) then exit
;
827 if (a
<> 255) or ((r
or g
or b
) <> 0) then
830 glDisable(GL_TEXTURE_2D
);
831 glColor4ub(r
, g
, b
, a
);
832 if ((r
or g
or b
) <> 0) then
834 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
838 glVertex2i(x
+w
, y
+h
);
842 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
846 glVertex2i(x
+w
, y
+h
);
854 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
855 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
857 if e_NoGraphics
then Exit
;
859 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
861 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
866 if (AlphaChannel
) or (Alpha
> 0) then
867 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
870 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
873 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
875 if (Angle
<> 0) and (RC
<> nil) then
878 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
879 glRotatef(Angle
, 0, 0, 1);
880 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
883 glEnable(GL_TEXTURE_2D
);
884 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
885 glBegin(GL_QUADS
); //0-1 1-1
887 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
);
896 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
898 if e_NoGraphics
then Exit
;
899 glDisable(GL_TEXTURE_2D
);
900 glColor3ub(Red
, Green
, Blue
);
903 if (Size
= 2) or (Size
= 4) then
907 glVertex2f(X
+0.3, Y
+1.0);
910 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
913 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
915 // Make lines only top-left/bottom-right and top-right/bottom-left
927 // Pixel-perfect hack
935 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
937 nX1
, nY1
, nX2
, nY2
: Integer;
939 if e_NoGraphics
then Exit
;
940 // Only top-left/bottom-right quad
957 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
962 glDisable(GL_TEXTURE_2D
);
963 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
966 nX1
:= X1
; nY1
:= Y1
;
967 nX2
:= X2
; nY2
:= Y1
;
968 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
969 glVertex2i(nX1
, nY1
);
970 glVertex2i(nX2
, nY2
);
972 nX1
:= X2
; nY1
:= Y1
;
973 nX2
:= X2
; nY2
:= Y2
;
974 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
975 glVertex2i(nX1
, nY1
);
976 glVertex2i(nX2
, nY2
);
978 nX1
:= X2
; nY1
:= Y2
;
979 nX2
:= X1
; nY2
:= Y2
;
980 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
981 glVertex2i(nX1
, nY1
);
982 glVertex2i(nX2
, nY2
);
984 nX1
:= X1
; nY1
:= Y2
;
985 nX2
:= X1
; nY2
:= Y1
;
986 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
987 glVertex2i(nX1
, nY1
);
988 glVertex2i(nX2
, nY2
);
990 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
994 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
995 Blending
: TBlending
= TBlending
.None
);
997 if e_NoGraphics
then Exit
;
998 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
1001 glDisable(GL_BLEND
);
1003 if Blending
= TBlending
.Blend
then
1004 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
1006 if Blending
= TBlending
.Filter
then
1007 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
1009 if Blending
= TBlending
.Invert
then
1010 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
1013 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1015 glDisable(GL_TEXTURE_2D
);
1016 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1028 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1030 glDisable(GL_BLEND
);
1034 // ////////////////////////////////////////////////////////////////////////// //
1035 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1037 if (a
< 0) then a
:= 0;
1038 if (a
> 255) then a
:= 255;
1040 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1041 glDisable(GL_TEXTURE_2D
);
1042 glColor4ub(0, 0, 0, Byte(255-a
));
1049 //glRect(x, y, x+w, y+h);
1050 glColor4ub(1, 1, 1, 1);
1051 glDisable(GL_BLEND
);
1052 //glBlendEquation(GL_FUNC_ADD);
1055 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1057 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1061 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1063 if e_NoGraphics
then Exit
;
1064 // Pixel-perfect lines
1066 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1071 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1073 glDisable(GL_BLEND
);
1075 glDisable(GL_TEXTURE_2D
);
1076 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1082 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1084 glDisable(GL_BLEND
);
1087 //------------------------------------------------------------------
1088 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1089 //------------------------------------------------------------------
1090 procedure e_DeleteTexture(ID
: DWORD
);
1092 if not e_NoGraphics
then
1093 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1094 e_Textures
[ID
].tx
.id
:= 0;
1095 e_Textures
[ID
].tx
.Width
:= 0;
1096 e_Textures
[ID
].tx
.Height
:= 0;
1099 //------------------------------------------------------------------
1100 // Óäàëÿåò âñå òåêñòóðû
1101 //------------------------------------------------------------------
1102 procedure e_RemoveAllTextures();
1106 if e_Textures
= nil then Exit
;
1108 for i
:= 0 to High(e_Textures
) do
1109 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1113 //------------------------------------------------------------------
1115 //------------------------------------------------------------------
1116 procedure e_ReleaseEngine();
1118 e_RemoveAllTextures
;
1119 e_RemoveAllTextureFont
;
1122 procedure e_BeginRender();
1124 if e_NoGraphics
then Exit
;
1125 glEnable(GL_ALPHA_TEST
);
1126 glAlphaFunc(GL_GREATER
, 0.0);
1129 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1131 if e_NoGraphics
then Exit
;
1132 glClearColor(Red
, Green
, Blue
, 0);
1136 procedure e_Clear(); overload
;
1138 if e_NoGraphics
then Exit
;
1139 glClearColor(0, 0, 0, 0);
1140 glClear(GL_COLOR_BUFFER_BIT
);
1143 procedure e_EndRender();
1145 if e_NoGraphics
then Exit
;
1150 function e_GetGamma(win
: PSDL_Window
): Byte;
1152 ramp
: array [0..256*3-1] of Word;
1153 rgb
: array [0..2] of Double;
1162 if e_NoGraphics
then Exit
;
1167 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1176 for j
:= min
to max
- 1 do
1179 B
:= (j
mod 256)/256;
1181 sum
:= sum
+ ln(A
)/ln(B
);
1184 rgb
[i
] := sum
/ count
;
1187 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1190 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1192 ramp
: array [0..256*3-1] of Word;
1197 if e_NoGraphics
then Exit
;
1198 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1200 for i
:= 0 to 255 do
1202 r
:= Exp(g
* ln(i
/256))*65536;
1203 if r
< 0 then r
:= 0
1204 else if r
> 65535 then r
:= 65535;
1205 ramp
[i
] := trunc(r
);
1206 ramp
[i
+ 256] := trunc(r
);
1207 ramp
[i
+ 512] := trunc(r
);
1210 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1214 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1218 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1222 if e_CharFonts
<> nil then
1223 for i
:= 0 to High(e_CharFonts
) do
1224 if not e_CharFonts
[i
].alive
then
1230 if id
= DWORD(-1) then
1232 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1233 id
:= High(e_CharFonts
);
1236 with e_CharFonts
[id
] do
1238 for i
:= 0 to High(Chars
) do
1252 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1254 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1256 TextureID
:= Texture
;
1261 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1265 if e_NoGraphics
then Exit
;
1266 if Text = '' then Exit
;
1267 if e_CharFonts
= nil then Exit
;
1268 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1270 with e_CharFonts
[FontID
] do
1272 for a
:= 1 to Length(Text) do
1273 with Chars
[Ord(Text[a
])] do
1274 if TextureID
<> -1 then
1276 e_Draw(TextureID
, X
, Y
, 0, True, False);
1277 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1282 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1283 Color
: TRGB
; Scale
: Single = 1.0);
1288 if e_NoGraphics
then Exit
;
1289 if Text = '' then Exit
;
1290 if e_CharFonts
= nil then Exit
;
1291 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1293 with e_CharFonts
[FontID
] do
1295 for a
:= 1 to Length(Text) do
1296 with Chars
[Ord(Text[a
])] do
1297 if TextureID
<> -1 then
1299 if Scale
<> 1.0 then
1302 glScalef(Scale
, Scale
, 0);
1307 e_Draw(TextureID
, X
, Y
, 0, True, False);
1310 if Scale
<> 1.0 then glPopMatrix
;
1312 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1317 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1319 a
, TX
, TY
, len
: Integer;
1323 if e_NoGraphics
then Exit
;
1324 if Text = '' then Exit
;
1325 if e_CharFonts
= nil then Exit
;
1326 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1334 len
:= Length(Text);
1336 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1338 with e_CharFonts
[FontID
] do
1340 for a
:= 1 to len
do
1351 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1356 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1361 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1366 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1371 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1376 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1381 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1386 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1391 with Chars
[Ord(Text[a
])] do
1392 if TextureID
<> -1 then
1396 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1399 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1405 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1413 if Text = '' then Exit
;
1414 if e_CharFonts
= nil then Exit
;
1415 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1417 with e_CharFonts
[FontID
] do
1419 for a
:= 1 to Length(Text) do
1420 with Chars
[Ord(Text[a
])] do
1421 if TextureID
<> -1 then
1423 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1424 e_GetTextureSize(TextureID
, nil, @h2
);
1425 if h2
> h
then h
:= h2
;
1430 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1432 a
, lines
, len
: Integer;
1433 h2
, w2
, tw
, th
: Word;
1440 if Text = '' then Exit
;
1441 if e_CharFonts
= nil then Exit
;
1442 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1445 len
:= Length(Text);
1447 with e_CharFonts
[FontID
] do
1449 for a
:= 1 to len
do
1451 if Text[a
] = #10 then
1454 if w2
> tw
then tw
:= w2
;
1459 with Chars
[Ord(Text[a
])] do
1460 if TextureID
<> -1 then
1462 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1463 e_GetTextureSize(TextureID
, nil, @h2
);
1464 if h2
> th
then th
:= h2
;
1476 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1482 if e_CharFonts
= nil then Exit
;
1483 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1485 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1486 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1489 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1496 if e_CharFonts
= nil then Exit
;
1497 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1499 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1501 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1502 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1504 if h2
> Result
then Result
:= h2
;
1508 procedure e_CharFont_Remove(FontID
: DWORD
);
1512 with e_CharFonts
[FontID
] do
1513 for a
:= 0 to High(Chars
) do
1514 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1516 e_CharFonts
[FontID
].alive
:= False;
1519 procedure e_CharFont_RemoveAll();
1523 if e_CharFonts
= nil then Exit
;
1525 for a
:= 0 to High(e_CharFonts
) do
1526 e_CharFont_Remove(a
);
1531 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1540 if e_NoGraphics
then Exit
;
1541 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1545 if e_TextureFonts
<> nil then
1546 for i
:= 0 to High(e_TextureFonts
) do
1547 if e_TextureFonts
[i
].Base
= 0 then
1553 if id
= DWORD(-1) then
1555 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1556 id
:= High(e_TextureFonts
);
1559 with e_TextureFonts
[id
] do
1562 Base
:= glGenLists(XCount
*YCount
);
1564 TextureID
:= e_Textures
[Tex
].tx
.id
;
1565 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1566 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1574 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1575 for loop1
:= 0 to XCount
*YCount
-1 do
1577 cx
:= (loop1
mod XCount
)/XCount
;
1578 cy
:= (loop1
div YCount
)/YCount
;
1580 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1582 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1583 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1585 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1586 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1588 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1589 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1591 glTexCoord2f(cx
, 1.0-cy
);
1594 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1602 procedure e_TextureFontKill(FontID
: DWORD
);
1604 if e_NoGraphics
then Exit
;
1606 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1608 e_TextureFonts
[FontID
].Base
:= 0;
1611 {$IFNDEF NOGL_LISTS}
1612 procedure e_TextureFontDrawChar(ch
: Char; FontID
: DWORD
);
1617 Width
, Height
: Integer;
1618 XCount
, YCount
: Integer;
1620 index
:= Ord(ch
) - 32;
1621 Tex
:= e_TextureFonts
[FontID
].Texture
;
1622 Width
:= e_Textures
[Tex
].tx
.Width
;
1623 Height
:= e_Textures
[Tex
].tx
.Height
;
1624 XCount
:= e_TextureFonts
[FontID
].XC
;
1625 YCount
:= e_TextureFonts
[FontID
].YC
;
1626 cx
:= (index
mod XCount
)/XCount
;
1627 cy
:= (index
div YCount
)/YCount
;
1629 glTexCoord2f(cx
, 1 - cy
- 1/YCount
);
1630 glVertex2i(0, Height
div YCount
);
1631 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
- 1/YCount
);
1632 glVertex2i(Width
div XCount
, Height
div YCount
);
1633 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
);
1634 glVertex2i(Width
div XCount
, 0);
1635 glTexCoord2f(cx
, 1 - cy
);
1638 glTranslatef((e_Textures
[Tex
].tx
.Width
div XCount
) + e_TextureFonts
[FontID
].SPC
, 0, 0);
1641 procedure e_TextureFontDrawString(Text: String; FontID
: DWORD
);
1645 for i
:= 1 to High(Text) do
1646 e_TextureFontDrawChar(Text[i
], FontID
);
1650 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1652 if e_NoGraphics
then Exit
;
1653 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1654 if Text = '' then Exit
;
1656 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1659 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1662 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1663 glEnable(GL_TEXTURE_2D
);
1664 glTranslatef(x
, y
, 0);
1666 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1667 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1669 e_TextureFontDrawString(Text, FontID
);
1671 glDisable(GL_TEXTURE_2D
);
1674 glDisable(GL_BLEND
);
1677 // god forgive me for this, but i cannot figure out how to do it without lists
1678 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1680 if e_NoGraphics
then Exit
;
1685 glColor4ub(0, 0, 0, 128);
1686 glTranslatef(X
+1, Y
+1, 0);
1688 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1690 e_TextureFontDrawChar(Ch
, FontID
);
1696 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1697 glTranslatef(X
, Y
, 0);
1699 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1701 e_TextureFontDrawChar(Ch
, FontID
);
1707 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1709 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1710 glEnable(GL_TEXTURE_2D
);
1711 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1713 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1715 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1716 glDisable(GL_TEXTURE_2D
);
1717 glDisable(GL_BLEND
);
1720 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1722 result
:= e_TextureFonts
[FontID
].CharWidth
;
1725 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
;
1726 Shadow
: Boolean = False; Newlines
: Boolean = False);
1728 a
, TX
, TY
, len
: Integer;
1732 if e_NoGraphics
then Exit
;
1733 if Text = '' then Exit
;
1734 if e_TextureFonts
= nil then Exit
;
1735 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1743 len
:= Length(Text);
1745 w
:= e_TextureFonts
[FontID
].CharWidth
;
1746 h
:= e_TextureFonts
[FontID
].CharHeight
;
1748 with e_TextureFonts
[FontID
] do
1750 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1751 glEnable(GL_TEXTURE_2D
);
1754 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1757 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1760 for a
:= 1 to len
do
1774 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1779 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1784 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1789 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1794 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1799 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1804 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1809 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1816 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1821 glDisable(GL_TEXTURE_2D
);
1822 glDisable(GL_BLEND
);
1826 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1827 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1829 if e_NoGraphics
then Exit
;
1830 if Text = '' then Exit
;
1833 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1834 glEnable(GL_TEXTURE_2D
);
1837 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1840 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1845 glColor4ub(0, 0, 0, 128);
1846 glTranslatef(x
+1, y
+1, 0);
1847 glScalef(Scale
, Scale
, 0);
1849 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1851 e_TextureFontDrawString(Text, FontID
);
1857 glColor4ub(Red
, Green
, Blue
, 255);
1858 glTranslatef(x
, y
, 0);
1859 glScalef(Scale
, Scale
, 0);
1861 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1863 e_TextureFontDrawString(Text, FontID
);
1866 glDisable(GL_TEXTURE_2D
);
1868 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1869 glDisable(GL_BLEND
);
1872 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1876 if e_NoGraphics
then Exit
;
1877 if Integer(ID
) > High(e_TextureFonts
) then
1879 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1880 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1883 procedure e_RemoveAllTextureFont();
1887 if e_NoGraphics
then Exit
;
1888 if e_TextureFonts
= nil then Exit
;
1890 for i
:= 0 to High(e_TextureFonts
) do
1891 if e_TextureFonts
[i
].Base
<> 0 then
1894 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1896 e_TextureFonts
[i
].Base
:= 0;
1899 e_TextureFonts
:= nil;
1902 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1909 function _Point(X
, Y
: Integer): TPoint2i
;
1915 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1919 Result
.Width
:= Width
;
1920 Result
.Height
:= Height
;
1923 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1932 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1934 pixels
, obuf
, scln
, ps
, pd
: PByte;
1937 i
, x
, y
, res
: Integer;
1938 sign
: array [0..7] of Byte;
1939 hbuf
: array [0..12] of Byte;
1944 if e_NoGraphics
then Exit
;
1947 // first, extract and pack graphics data
1948 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1950 GetMem(pixels
, Width
*Height
*3);
1952 FillChar(pixels
^, Width
*Height
*3, 0);
1953 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1954 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1956 if e_FastScreenshots
then
1959 GetMem(scln
, (Width
*3+1)*Height
);
1963 Inc(ps
, (Width
*3)*(Height
-1));
1964 for i
:= 0 to Height
-1 do
1968 Move(ps
^, pd
^, Width
*3);
1980 obufsize
:= (Width
*3+1)*Height
*2;
1981 GetMem(obuf
, obufsize
);
1986 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1987 if res
= Z_OK
then break
;
1988 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1989 obufsize
:= obufsize
*2;
1992 GetMem(obuf
, obufsize
);
1994 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
2007 st
.writeBuffer(sign
, 8);
2008 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2011 writeIntBE(st
, LongWord(13));
2016 st
.writeBuffer(sign
, 4);
2017 crc
:= crc32(0, @sign
[0], 4);
2020 hbuf
[2] := (Width
shr 8) and $ff;
2021 hbuf
[3] := Width
and $ff;
2024 hbuf
[6] := (Height
shr 8) and $ff;
2025 hbuf
[7] := Height
and $ff;
2026 hbuf
[8] := 8; // bit depth
2027 hbuf
[9] := 2; // RGB
2028 hbuf
[10] := 0; // compression method
2029 hbuf
[11] := 0; // filter method
2030 hbuf
[12] := 0; // no interlace
2031 crc
:= crc32(crc
, @hbuf
[0], 13);
2032 st
.writeBuffer(hbuf
, 13);
2033 writeIntBE(st
, crc
);
2034 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2037 writeIntBE(st
, LongWord(dlen
));
2042 st
.writeBuffer(sign
, 4);
2043 crc
:= crc32(0, @sign
[0], 4);
2044 crc
:= crc32(crc
, obuf
, dlen
);
2045 st
.writeBuffer(obuf
^, dlen
);
2046 writeIntBE(st
, crc
);
2047 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2050 writeIntBE(st
, LongWord(0));
2055 st
.writeBuffer(sign
, 4);
2056 crc
:= crc32(0, @sign
[0], 4);
2057 writeIntBE(st
, crc
);
2058 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2060 if obuf
<> nil then FreeMem(obuf
);
2065 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2066 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2069 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2071 //writeln(stderr, 'moving pixels...');
2072 for y
:= Height
-1 downto 0 do
2074 for x
:= 0 to Width
-1 do
2076 clr
.r
:= ps
^; Inc(ps
);
2077 clr
.g
:= ps
^; Inc(ps
);
2078 clr
.b
:= ps
^; Inc(ps
);
2080 SetPixel32(img
, x
, y
, clr
);
2083 GlobalMetadata
.ClearMetaItems();
2084 GlobalMetadata
.ClearMetaItemsForSaving();
2085 //writeln(stderr, 'compressing image...');
2086 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2087 //writeln(stderr, 'done!');