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;
204 e_RBOSupported
: Boolean = True;
206 e_FrameW
: Integer = -1;
207 e_FrameH
: Integer = -1;
209 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
211 //------------------------------------------------------------------
212 // Èíèöèàëèçèðóåò OpenGL
213 //------------------------------------------------------------------
214 procedure e_InitGL();
218 e_DummyTextures
:= True;
224 glDisable(GL_DEPTH_TEST
);
225 glEnable(GL_SCISSOR_TEST
);
226 glClearColor(0, 0, 0, 0);
229 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
231 mat
: Array [0..15] of GLDouble
;
234 if e_NoGraphics
then Exit
;
236 glScissor(X
, Y
, Width
, Height
);
237 glViewport(X
, Y
, Width
, Height
);
238 //gluOrtho2D(0, Width, Height, 0);
240 glMatrixMode(GL_PROJECTION
);
242 mat
[ 0] := 2.0 / Width
;
248 mat
[ 5] := -2.0 / Height
;
262 glLoadMatrixd(@mat
[0]);
264 glMatrixMode(GL_MODELVIEW
);
268 //------------------------------------------------------------------
269 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
270 //------------------------------------------------------------------
271 function FindTexture(): DWORD
;
275 if e_Textures
<> nil then
276 for i
:= 0 to High(e_Textures
) do
277 if e_Textures
[i
].tx
.Width
= 0 then
283 if e_Textures
= nil then
285 SetLength(e_Textures
, 32);
290 Result
:= High(e_Textures
) + 1;
291 SetLength(e_Textures
, Length(e_Textures
) + 32);
295 //------------------------------------------------------------------
297 //------------------------------------------------------------------
298 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
305 e_WriteLog('Loading texture from '+FileName
, TMsgType
.Notify
);
307 find_id
:= FindTexture();
309 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
310 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
317 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
324 find_id
:= FindTexture();
326 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
333 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
340 find_id
:= FindTexture
;
342 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
;
349 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
356 find_id
:= FindTexture();
358 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
365 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
371 find_id
:= FindTexture();
372 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
377 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
379 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
380 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
383 procedure DestroyFramebuffer
;
385 glBindTexture(GL_TEXTURE_2D
, 0);
386 glBindRenderbuffer(GL_RENDERBUFFER
, 0);
387 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
391 glDeleteTextures(1, @e_Frame
);
397 glDeleteRenderbuffers(1, @e_RBO
);
403 glDeleteFramebuffers(1, @e_FBO
);
408 function e_ResizeFramebuffer(Width
, Height
: Integer): Boolean;
412 if e_NoGraphics
then Exit
;
421 glGenFramebuffers(1, @e_FBO
);
423 if glGetError() <> GL_NO_ERROR
then
425 e_LogWriteln('GL: glGenFramebuffers failed');
429 glGenTextures(1, @e_Frame
);
430 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
431 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, Width
, Height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, nil);
432 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
433 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
435 if glGetError() <> GL_NO_ERROR
then
437 e_LogWriteln('GL: can''t create FBO color buffer');
442 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
443 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, e_Frame
, 0);
444 if glCheckFramebufferStatus(GL_FRAMEBUFFER
) <> GL_FRAMEBUFFER_COMPLETE
then
446 e_LogWriteln('GL: can''t construct framebuffer with color attachment');
452 if e_RBOSupported
then
454 glGenRenderbuffers(1, @e_RBO
);
455 glBindRenderbuffer(GL_RENDERBUFFER
, e_RBO
);
456 glRenderbufferStorage(GL_RENDERBUFFER
, GL_DEPTH24_STENCIL8
, Width
, Height
);
457 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, e_RBO
);
458 if glCheckFramebufferStatus(GL_FRAMEBUFFER
) <> GL_FRAMEBUFFER_COMPLETE
then
460 e_LogWriteln('GL: can''t construct framebuffer with depth+stencil attachment, trying without');
461 e_RBOSupported
:= False;
462 Result
:= e_ResizeFramebuffer(Width
, Height
);
471 procedure e_ResizeWindow(Width
, Height
: Integer);
475 e_SetViewPort(0, 0, Width
, Height
);
478 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
480 x1
, y1
, tmp
: Integer;
482 if (w
< 1) or (h
< 1) then exit
;
485 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
486 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
487 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
488 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
489 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
490 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
493 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
495 if (e_FBO
= 0) or (e_Frame
= 0) or e_NoGraphics
then exit
;
498 glEnable(GL_TEXTURE_2D
);
499 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
500 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
501 e_SetViewPort(0, 0, WinWidth
, WinHeight
);
502 glColor4ub(255, 255, 255, 255);
505 glTexCoord2f(0, 1); glVertex2i( 0, 0);
506 glTexCoord2f(0, 0); glVertex2i( 0, WinHeight
);
507 glTexCoord2f(1, 0); glVertex2i(WinWidth
, WinHeight
);
508 glTexCoord2f(1, 1); glVertex2i(WinWidth
, 0);
511 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
512 e_SetViewPort(0, 0, e_FrameW
, e_FrameH
);
515 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
516 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
518 if e_NoGraphics
then Exit
;
519 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
521 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
526 if (AlphaChannel
) or (Alpha
> 0) then
527 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
530 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
533 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
535 glEnable(GL_TEXTURE_2D
);
536 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
539 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
);
541 //u := e_Textures[ID].tx.u;
542 //v := e_Textures[ID].tx.v;
545 if Mirror = M_NONE then
547 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
548 glTexCoord2f(0, 0); glVertex2i(X, Y);
549 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
550 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
553 if Mirror = M_HORIZONTAL then
555 glTexCoord2f(u, 0); glVertex2i(X, Y);
556 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
557 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
558 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
561 if Mirror = M_VERTICAL then
563 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
564 glTexCoord2f(0, -v); glVertex2i(X, Y);
565 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
566 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
575 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
576 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
580 if e_NoGraphics
then Exit
;
581 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
583 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
588 if (AlphaChannel
) or (Alpha
> 0) then
589 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
592 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
595 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
597 glEnable(GL_TEXTURE_2D
);
598 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
600 u
:= e_Textures
[ID
].tx
.u
;
601 v
:= e_Textures
[ID
].tx
.v
;
604 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
605 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
606 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
607 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
613 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
614 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
616 if e_NoGraphics
then Exit
;
617 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
619 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
624 if (AlphaChannel
) or (Alpha
> 0) then
625 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
628 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
631 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
633 glEnable(GL_TEXTURE_2D
);
634 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
636 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
);
642 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
643 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
645 X2
, Y2
, dx
, w
, h
: Integer;
648 if e_NoGraphics
then Exit
;
649 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
650 ambientBlendMode
:= false;
652 if (Alpha
> 0) or AlphaChannel
or Blending
then
658 if not ambientBlendMode
then glDisable(GL_BLEND
);
660 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
661 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
662 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
664 if (XCount
= 0) then XCount
:= 1;
665 if (YCount
= 0) then YCount
:= 1;
667 glEnable(GL_TEXTURE_2D
);
668 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
670 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
671 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
673 //k8: this SHOULD work... i hope
674 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
677 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
678 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
679 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
680 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
687 u
:= e_Textures
[ID
].tx
.u
;
688 v
:= e_Textures
[ID
].tx
.v
;
689 w
:= e_Textures
[ID
].tx
.width
;
690 h
:= e_Textures
[ID
].tx
.height
;
697 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
698 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
699 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
700 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
715 //TODO: overflow checks
716 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
721 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
722 // check for intersection
723 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
724 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
728 if (x0
< x1
) then x0
:= x1
;
729 if (y0
< y1
) then y0
:= y1
;
730 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
731 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
734 result
:= (w0
> 0) and (h0
> 0);
738 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
739 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
744 scxywh: array[0..3] of GLint;
745 vpxywh: array[0..3] of GLint;
747 w
, h
, dw
, cw
, ch
, yofs
: Integer;
748 u
, v
, cu
, cv
: Single;
752 procedure setScissorGLInternal (x, y, w, h: Integer);
754 //if not scallowed then exit;
759 y := vpxywh[3]-(y+h);
760 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
762 glScissor(0, 0, 0, 0);
766 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
767 glScissor(x, y, w, h);
773 if e_NoGraphics
then exit
;
774 ambientBlendMode
:= false;
776 if (wdt
< 1) or (hgt
< 1) then exit
;
778 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
780 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
784 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
786 if (Alpha
> 0) or AlphaChannel
or Blending
then
792 if not ambientBlendMode
then glDisable(GL_BLEND
);
794 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
795 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
796 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
798 glEnable(GL_TEXTURE_2D
);
799 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
804 //k8: this SHOULD work... i hope
805 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
808 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
809 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
810 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
811 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
816 // hard day's night; setup scissor
818 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
819 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
820 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
821 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
822 //glEnable(GL_SCISSOR_TEST);
823 setScissorGLInternal(x, y, wdt, hgt);
826 u
:= e_Textures
[ID
].tx
.u
;
827 v
:= e_Textures
[ID
].tx
.v
;
828 w
:= e_Textures
[ID
].tx
.width
;
829 h
:= e_Textures
[ID
].tx
.height
;
831 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
835 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
836 if onlyOneY
then yofs
:= 0;
842 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
844 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
845 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
846 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
847 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
853 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
860 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
862 if e_NoGraphics
then exit
;
863 if (w
< 1) or (h
< 1) then exit
;
864 if (a
<> 255) or ((r
or g
or b
) <> 0) then
867 glDisable(GL_TEXTURE_2D
);
868 glColor4ub(r
, g
, b
, a
);
869 if ((r
or g
or b
) <> 0) then
871 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
875 glVertex2i(x
+w
, y
+h
);
879 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
883 glVertex2i(x
+w
, y
+h
);
891 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
892 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
894 if e_NoGraphics
then Exit
;
896 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
898 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
903 if (AlphaChannel
) or (Alpha
> 0) then
904 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
907 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
910 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
912 if (Angle
<> 0) and (RC
<> nil) then
915 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
916 glRotatef(Angle
, 0, 0, 1);
917 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
920 glEnable(GL_TEXTURE_2D
);
921 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
922 glBegin(GL_QUADS
); //0-1 1-1
924 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
);
933 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
935 if e_NoGraphics
then Exit
;
936 glDisable(GL_TEXTURE_2D
);
937 glColor3ub(Red
, Green
, Blue
);
940 if (Size
= 2) or (Size
= 4) then
944 glVertex2f(X
+0.3, Y
+1.0);
947 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
950 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
952 // Make lines only top-left/bottom-right and top-right/bottom-left
964 // Pixel-perfect hack
972 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
974 nX1
, nY1
, nX2
, nY2
: Integer;
976 if e_NoGraphics
then Exit
;
977 // Only top-left/bottom-right quad
994 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
999 glDisable(GL_TEXTURE_2D
);
1000 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1003 nX1
:= X1
; nY1
:= Y1
;
1004 nX2
:= X2
; nY2
:= Y1
;
1005 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
1006 glVertex2i(nX1
, nY1
);
1007 glVertex2i(nX2
, nY2
);
1009 nX1
:= X2
; nY1
:= Y1
;
1010 nX2
:= X2
; nY2
:= Y2
;
1011 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1012 glVertex2i(nX1
, nY1
);
1013 glVertex2i(nX2
, nY2
);
1015 nX1
:= X2
; nY1
:= Y2
;
1016 nX2
:= X1
; nY2
:= Y2
;
1017 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1018 glVertex2i(nX1
, nY1
);
1019 glVertex2i(nX2
, nY2
);
1021 nX1
:= X1
; nY1
:= Y2
;
1022 nX2
:= X1
; nY2
:= Y1
;
1023 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1024 glVertex2i(nX1
, nY1
);
1025 glVertex2i(nX2
, nY2
);
1027 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1028 glDisable(GL_BLEND
);
1031 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
1032 Blending
: TBlending
= TBlending
.None
);
1034 if e_NoGraphics
then Exit
;
1035 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
1038 glDisable(GL_BLEND
);
1041 TBlending
.None
: if Alpha
> 0 then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1042 TBlending
.Blend
: glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
1043 TBlending
.Invert
: glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
);
1044 TBlending
.Filter
: glBlendFunc(GL_ZERO
, GL_SRC_COLOR
);
1047 glDisable(GL_TEXTURE_2D
);
1048 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1060 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1061 glDisable(GL_BLEND
);
1065 // ////////////////////////////////////////////////////////////////////////// //
1066 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1068 if (a
< 0) then a
:= 0;
1069 if (a
> 255) then a
:= 255;
1071 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1072 glDisable(GL_TEXTURE_2D
);
1073 glColor4ub(0, 0, 0, Byte(255-a
));
1080 //glRect(x, y, x+w, y+h);
1081 glColor4ub(1, 1, 1, 1);
1082 glDisable(GL_BLEND
);
1083 //glBlendEquation(GL_FUNC_ADD);
1086 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1088 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1092 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1094 if e_NoGraphics
then Exit
;
1095 // Pixel-perfect lines
1097 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1102 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1104 glDisable(GL_BLEND
);
1106 glDisable(GL_TEXTURE_2D
);
1107 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1113 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1115 glDisable(GL_BLEND
);
1118 //------------------------------------------------------------------
1119 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1120 //------------------------------------------------------------------
1121 procedure e_DeleteTexture(ID
: DWORD
);
1123 if not e_NoGraphics
then
1124 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1125 e_Textures
[ID
].tx
.id
:= 0;
1126 e_Textures
[ID
].tx
.Width
:= 0;
1127 e_Textures
[ID
].tx
.Height
:= 0;
1130 //------------------------------------------------------------------
1131 // Óäàëÿåò âñå òåêñòóðû
1132 //------------------------------------------------------------------
1133 procedure e_RemoveAllTextures();
1137 if e_Textures
= nil then Exit
;
1139 for i
:= 0 to High(e_Textures
) do
1140 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1144 //------------------------------------------------------------------
1146 //------------------------------------------------------------------
1147 procedure e_ReleaseEngine();
1149 e_RemoveAllTextures
;
1150 e_RemoveAllTextureFont
;
1153 procedure e_BeginRender();
1155 if e_NoGraphics
then Exit
;
1156 glEnable(GL_ALPHA_TEST
);
1157 glAlphaFunc(GL_GREATER
, 0.0);
1160 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1162 if e_NoGraphics
then Exit
;
1163 glClearColor(Red
, Green
, Blue
, 0);
1167 procedure e_Clear(); overload
;
1169 if e_NoGraphics
then Exit
;
1170 glClearColor(0, 0, 0, 0);
1171 glClear(GL_COLOR_BUFFER_BIT
);
1174 procedure e_EndRender();
1176 if e_NoGraphics
then Exit
;
1181 function e_GetGamma(win
: PSDL_Window
): Byte;
1183 ramp
: array [0..256*3-1] of Word;
1184 rgb
: array [0..2] of Double;
1193 if e_NoGraphics
then Exit
;
1198 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1207 for j
:= min
to max
- 1 do
1210 B
:= (j
mod 256)/256;
1212 sum
:= sum
+ ln(A
)/ln(B
);
1215 rgb
[i
] := sum
/ count
;
1218 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1221 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1223 ramp
: array [0..256*3-1] of Word;
1228 if e_NoGraphics
then Exit
;
1229 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1231 for i
:= 0 to 255 do
1233 r
:= Exp(g
* ln(i
/256))*65536;
1234 if r
< 0 then r
:= 0
1235 else if r
> 65535 then r
:= 65535;
1236 ramp
[i
] := trunc(r
);
1237 ramp
[i
+ 256] := trunc(r
);
1238 ramp
[i
+ 512] := trunc(r
);
1241 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1245 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1249 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1253 if e_CharFonts
<> nil then
1254 for i
:= 0 to High(e_CharFonts
) do
1255 if not e_CharFonts
[i
].alive
then
1261 if id
= DWORD(-1) then
1263 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1264 id
:= High(e_CharFonts
);
1267 with e_CharFonts
[id
] do
1269 for i
:= 0 to High(Chars
) do
1283 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1285 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1287 TextureID
:= Texture
;
1292 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1296 if e_NoGraphics
then Exit
;
1297 if Text = '' then Exit
;
1298 if e_CharFonts
= nil then Exit
;
1299 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1301 with e_CharFonts
[FontID
] do
1303 for a
:= 1 to Length(Text) do
1304 with Chars
[Ord(Text[a
])] do
1305 if TextureID
<> -1 then
1307 e_Draw(TextureID
, X
, Y
, 0, True, False);
1308 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1313 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1314 Color
: TRGB
; Scale
: Single = 1.0);
1319 if e_NoGraphics
then Exit
;
1320 if Text = '' then Exit
;
1321 if e_CharFonts
= nil then Exit
;
1322 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1324 with e_CharFonts
[FontID
] do
1326 for a
:= 1 to Length(Text) do
1327 with Chars
[Ord(Text[a
])] do
1328 if TextureID
<> -1 then
1330 if Scale
<> 1.0 then
1333 glScalef(Scale
, Scale
, 0);
1338 e_Draw(TextureID
, X
, Y
, 0, True, False);
1341 if Scale
<> 1.0 then glPopMatrix
;
1343 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1348 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1350 a
, TX
, TY
, len
: Integer;
1354 if e_NoGraphics
then Exit
;
1355 if Text = '' then Exit
;
1356 if e_CharFonts
= nil then Exit
;
1357 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1365 len
:= Length(Text);
1367 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1369 with e_CharFonts
[FontID
] do
1371 for a
:= 1 to len
do
1382 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1387 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1392 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1397 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1402 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1407 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1412 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1417 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1422 with Chars
[Ord(Text[a
])] do
1423 if TextureID
<> -1 then
1427 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1430 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1436 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1444 if Text = '' then Exit
;
1445 if e_CharFonts
= nil then Exit
;
1446 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1448 with e_CharFonts
[FontID
] do
1450 for a
:= 1 to Length(Text) do
1451 with Chars
[Ord(Text[a
])] do
1452 if TextureID
<> -1 then
1454 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1455 e_GetTextureSize(TextureID
, nil, @h2
);
1456 if h2
> h
then h
:= h2
;
1461 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1463 a
, lines
, len
: Integer;
1464 h2
, w2
, tw
, th
: Word;
1471 if Text = '' then Exit
;
1472 if e_CharFonts
= nil then Exit
;
1473 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1476 len
:= Length(Text);
1478 with e_CharFonts
[FontID
] do
1480 for a
:= 1 to len
do
1482 if Text[a
] = #10 then
1485 if w2
> tw
then tw
:= w2
;
1490 with Chars
[Ord(Text[a
])] do
1491 if TextureID
<> -1 then
1493 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1494 e_GetTextureSize(TextureID
, nil, @h2
);
1495 if h2
> th
then th
:= h2
;
1507 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1513 if e_CharFonts
= nil then Exit
;
1514 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1516 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1517 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1520 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1527 if e_CharFonts
= nil then Exit
;
1528 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1530 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1532 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1533 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1535 if h2
> Result
then Result
:= h2
;
1539 procedure e_CharFont_Remove(FontID
: DWORD
);
1543 with e_CharFonts
[FontID
] do
1544 for a
:= 0 to High(Chars
) do
1545 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1547 e_CharFonts
[FontID
].alive
:= False;
1550 procedure e_CharFont_RemoveAll();
1554 if e_CharFonts
= nil then Exit
;
1556 for a
:= 0 to High(e_CharFonts
) do
1557 e_CharFont_Remove(a
);
1562 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1571 if e_NoGraphics
then Exit
;
1572 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1576 if e_TextureFonts
<> nil then
1577 for i
:= 0 to High(e_TextureFonts
) do
1578 if e_TextureFonts
[i
].Base
= 0 then
1584 if id
= DWORD(-1) then
1586 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1587 id
:= High(e_TextureFonts
);
1590 with e_TextureFonts
[id
] do
1593 Base
:= glGenLists(XCount
*YCount
);
1595 TextureID
:= e_Textures
[Tex
].tx
.id
;
1596 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1597 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1605 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1606 for loop1
:= 0 to XCount
*YCount
-1 do
1608 cx
:= (loop1
mod XCount
)/XCount
;
1609 cy
:= (loop1
div YCount
)/YCount
;
1611 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1613 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1614 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1616 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1617 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1619 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1620 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1622 glTexCoord2f(cx
, 1.0-cy
);
1625 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1633 procedure e_TextureFontKill(FontID
: DWORD
);
1635 if e_NoGraphics
then Exit
;
1637 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1639 e_TextureFonts
[FontID
].Base
:= 0;
1642 {$IFNDEF NOGL_LISTS}
1643 procedure e_TextureFontDrawChar(ch
: Char; FontID
: DWORD
);
1648 Width
, Height
: Integer;
1649 XCount
, YCount
: Integer;
1651 index
:= Ord(ch
) - 32;
1652 Tex
:= e_TextureFonts
[FontID
].Texture
;
1653 Width
:= e_Textures
[Tex
].tx
.Width
;
1654 Height
:= e_Textures
[Tex
].tx
.Height
;
1655 XCount
:= e_TextureFonts
[FontID
].XC
;
1656 YCount
:= e_TextureFonts
[FontID
].YC
;
1657 cx
:= (index
mod XCount
)/XCount
;
1658 cy
:= (index
div YCount
)/YCount
;
1660 glTexCoord2f(cx
, 1 - cy
- 1/YCount
);
1661 glVertex2i(0, Height
div YCount
);
1662 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
- 1/YCount
);
1663 glVertex2i(Width
div XCount
, Height
div YCount
);
1664 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
);
1665 glVertex2i(Width
div XCount
, 0);
1666 glTexCoord2f(cx
, 1 - cy
);
1669 glTranslatef((e_Textures
[Tex
].tx
.Width
div XCount
) + e_TextureFonts
[FontID
].SPC
, 0, 0);
1672 procedure e_TextureFontDrawString(Text: String; FontID
: DWORD
);
1676 for i
:= 1 to High(Text) do
1677 e_TextureFontDrawChar(Text[i
], FontID
);
1681 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1683 if e_NoGraphics
then Exit
;
1684 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1685 if Text = '' then Exit
;
1687 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1690 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1693 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1694 glEnable(GL_TEXTURE_2D
);
1695 glTranslatef(x
, y
, 0);
1697 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1698 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1700 e_TextureFontDrawString(Text, FontID
);
1702 glDisable(GL_TEXTURE_2D
);
1705 glDisable(GL_BLEND
);
1708 // god forgive me for this, but i cannot figure out how to do it without lists
1709 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1711 if e_NoGraphics
then Exit
;
1716 glColor4ub(0, 0, 0, 128);
1717 glTranslatef(X
+1, Y
+1, 0);
1719 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1721 e_TextureFontDrawChar(Ch
, FontID
);
1727 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1728 glTranslatef(X
, Y
, 0);
1730 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1732 e_TextureFontDrawChar(Ch
, FontID
);
1738 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1740 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1741 glEnable(GL_TEXTURE_2D
);
1742 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1744 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1746 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1747 glDisable(GL_TEXTURE_2D
);
1748 glDisable(GL_BLEND
);
1751 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1753 result
:= e_TextureFonts
[FontID
].CharWidth
;
1756 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
;
1757 Shadow
: Boolean = False; Newlines
: Boolean = False);
1759 a
, TX
, TY
, len
: Integer;
1763 if e_NoGraphics
then Exit
;
1764 if Text = '' then Exit
;
1765 if e_TextureFonts
= nil then Exit
;
1766 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1774 len
:= Length(Text);
1776 w
:= e_TextureFonts
[FontID
].CharWidth
;
1777 h
:= e_TextureFonts
[FontID
].CharHeight
;
1779 with e_TextureFonts
[FontID
] do
1781 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1782 glEnable(GL_TEXTURE_2D
);
1785 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1788 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1791 for a
:= 1 to len
do
1805 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1810 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1815 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1820 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1825 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1830 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1835 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1840 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1847 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1852 glDisable(GL_TEXTURE_2D
);
1853 glDisable(GL_BLEND
);
1857 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1858 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1860 if e_NoGraphics
then Exit
;
1861 if Text = '' then Exit
;
1864 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1865 glEnable(GL_TEXTURE_2D
);
1868 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1871 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1876 glColor4ub(0, 0, 0, 128);
1877 glTranslatef(x
+1, y
+1, 0);
1878 glScalef(Scale
, Scale
, 0);
1880 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1882 e_TextureFontDrawString(Text, FontID
);
1888 glColor4ub(Red
, Green
, Blue
, 255);
1889 glTranslatef(x
, y
, 0);
1890 glScalef(Scale
, Scale
, 0);
1892 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1894 e_TextureFontDrawString(Text, FontID
);
1897 glDisable(GL_TEXTURE_2D
);
1899 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1900 glDisable(GL_BLEND
);
1903 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1907 if e_NoGraphics
then Exit
;
1908 if Integer(ID
) > High(e_TextureFonts
) then
1910 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1911 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1914 procedure e_RemoveAllTextureFont();
1918 if e_NoGraphics
then Exit
;
1919 if e_TextureFonts
= nil then Exit
;
1921 for i
:= 0 to High(e_TextureFonts
) do
1922 if e_TextureFonts
[i
].Base
<> 0 then
1925 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1927 e_TextureFonts
[i
].Base
:= 0;
1930 e_TextureFonts
:= nil;
1933 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1940 function _Point(X
, Y
: Integer): TPoint2i
;
1946 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1950 Result
.Width
:= Width
;
1951 Result
.Height
:= Height
;
1954 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1963 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1965 pixels
, obuf
, scln
, ps
, pd
: PByte;
1968 i
, x
, y
, res
: Integer;
1969 sign
: array [0..7] of Byte;
1970 hbuf
: array [0..12] of Byte;
1975 if e_NoGraphics
then Exit
;
1978 // first, extract and pack graphics data
1979 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1981 GetMem(pixels
, Width
*Height
*3);
1983 FillChar(pixels
^, Width
*Height
*3, 0);
1984 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1985 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1987 if e_FastScreenshots
then
1990 GetMem(scln
, (Width
*3+1)*Height
);
1994 Inc(ps
, (Width
*3)*(Height
-1));
1995 for i
:= 0 to Height
-1 do
1999 Move(ps
^, pd
^, Width
*3);
2011 obufsize
:= (Width
*3+1)*Height
*2;
2012 GetMem(obuf
, obufsize
);
2017 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
2018 if res
= Z_OK
then break
;
2019 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
2020 obufsize
:= obufsize
*2;
2023 GetMem(obuf
, obufsize
);
2025 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
2038 st
.writeBuffer(sign
, 8);
2039 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2042 writeIntBE(st
, LongWord(13));
2047 st
.writeBuffer(sign
, 4);
2048 crc
:= crc32(0, @sign
[0], 4);
2051 hbuf
[2] := (Width
shr 8) and $ff;
2052 hbuf
[3] := Width
and $ff;
2055 hbuf
[6] := (Height
shr 8) and $ff;
2056 hbuf
[7] := Height
and $ff;
2057 hbuf
[8] := 8; // bit depth
2058 hbuf
[9] := 2; // RGB
2059 hbuf
[10] := 0; // compression method
2060 hbuf
[11] := 0; // filter method
2061 hbuf
[12] := 0; // no interlace
2062 crc
:= crc32(crc
, @hbuf
[0], 13);
2063 st
.writeBuffer(hbuf
, 13);
2064 writeIntBE(st
, crc
);
2065 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2068 writeIntBE(st
, LongWord(dlen
));
2073 st
.writeBuffer(sign
, 4);
2074 crc
:= crc32(0, @sign
[0], 4);
2075 crc
:= crc32(crc
, obuf
, dlen
);
2076 st
.writeBuffer(obuf
^, dlen
);
2077 writeIntBE(st
, crc
);
2078 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2081 writeIntBE(st
, LongWord(0));
2086 st
.writeBuffer(sign
, 4);
2087 crc
:= crc32(0, @sign
[0], 4);
2088 writeIntBE(st
, crc
);
2089 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2091 if obuf
<> nil then FreeMem(obuf
);
2096 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2097 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2100 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2102 //writeln(stderr, 'moving pixels...');
2103 for y
:= Height
-1 downto 0 do
2105 for x
:= 0 to Width
-1 do
2107 clr
.r
:= ps
^; Inc(ps
);
2108 clr
.g
:= ps
^; Inc(ps
);
2109 clr
.b
:= ps
^; Inc(ps
);
2111 SetPixel32(img
, x
, y
, clr
);
2114 GlobalMetadata
.ClearMetaItems();
2115 GlobalMetadata
.ClearMetaItemsForSaving();
2116 //writeln(stderr, 'compressing image...');
2117 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2118 //writeln(stderr, 'done!');