1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../shared/a_modes.inc}
21 {$INCLUDE ../nogl/noGLuses.inc}
25 SysUtils
, Classes
, Math
, e_log
, e_texture
,
26 MAPDEF
, ImagingTypes
, Imaging
, ImagingUtility
;
29 TMirrorType
=(None
, Horizontal
, Vertical
);
30 TBlending
=(None
, Blend
, Filter
, Invert
);
41 Left
, Top
, Right
, Bottom
: Integer;
59 //------------------------------------------------------------------
61 //------------------------------------------------------------------
63 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
64 procedure e_ResizeWindow(Width
, Height
: Integer);
65 procedure e_ResizeFramebuffer(Width
, Height
: Integer);
66 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
68 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
69 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
70 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
71 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
72 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
73 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
74 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
75 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
77 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
78 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
80 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
81 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
83 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
85 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
86 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
87 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
88 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
89 Blending
: TBlending
= TBlending
.None
);
90 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
91 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
93 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
94 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
95 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
96 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
97 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
98 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
99 procedure e_DeleteTexture(ID
: DWORD
);
100 procedure e_RemoveAllTextures();
103 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
104 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
105 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
106 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
107 Color
: TRGB
; Scale
: Single = 1.0);
108 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
109 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
110 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
111 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
112 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
113 procedure e_CharFont_Remove(FontID
: DWORD
);
114 procedure e_CharFont_RemoveAll();
117 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
119 procedure e_TextureFontKill(FontID
: DWORD
);
120 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
121 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
122 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
123 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
;
124 Shadow
: Boolean = False; Newlines
: Boolean = False);
125 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
126 procedure e_RemoveAllTextureFont();
128 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
129 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
131 procedure e_ReleaseEngine();
132 procedure e_BeginRender();
133 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
134 procedure e_Clear(); overload
;
135 procedure e_EndRender();
138 function e_GetGamma(win
: PSDL_Window
): Byte;
139 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
142 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
144 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
145 function _Point(X
, Y
: Integer): TPoint2i
;
146 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
147 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
149 //function e_getTextGLId (ID: DWORD): GLuint;
153 e_NoGraphics
: Boolean = False;
154 e_FastScreenshots
: Boolean = true; // it's REALLY SLOW with `false`
155 g_dbg_scale
: Single = 1.0;
156 r_pixel_scale
: Single = 1.0;
170 TTextureFont
= record
181 Chars
: array[0..255] of
191 TSavedTexture
= record
198 e_Textures
: array of TTexture
= nil;
199 e_TextureFonts
: array of TTextureFont
= nil;
200 e_CharFonts
: array of TCharFont
;
201 //e_SavedTextures: array of TSavedTexture;
205 e_FrameW
: Integer = -1;
206 e_FrameH
: Integer = -1;
208 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
210 //------------------------------------------------------------------
211 // Èíèöèàëèçèðóåò OpenGL
212 //------------------------------------------------------------------
213 procedure e_InitGL();
217 e_DummyTextures
:= True;
223 glDisable(GL_DEPTH_TEST
);
224 glEnable(GL_SCISSOR_TEST
);
225 glClearColor(0, 0, 0, 0);
228 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
230 mat
: Array [0..15] of GLDouble
;
233 if e_NoGraphics
then Exit
;
235 glScissor(X
, Y
, Width
, Height
);
236 glViewport(X
, Y
, Width
, Height
);
237 //gluOrtho2D(0, Width, Height, 0);
239 glMatrixMode(GL_PROJECTION
);
241 mat
[ 0] := 2.0 / Width
;
247 mat
[ 5] := -2.0 / Height
;
261 glLoadMatrixd(@mat
[0]);
263 glMatrixMode(GL_MODELVIEW
);
267 //------------------------------------------------------------------
268 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
269 //------------------------------------------------------------------
270 function FindTexture(): DWORD
;
274 if e_Textures
<> nil then
275 for i
:= 0 to High(e_Textures
) do
276 if e_Textures
[i
].tx
.Width
= 0 then
282 if e_Textures
= nil then
284 SetLength(e_Textures
, 32);
289 Result
:= High(e_Textures
) + 1;
290 SetLength(e_Textures
, Length(e_Textures
) + 32);
294 //------------------------------------------------------------------
296 //------------------------------------------------------------------
297 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
304 e_WriteLog('Loading texture from '+FileName
, TMsgType
.Notify
);
306 find_id
:= FindTexture();
308 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
309 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
316 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
323 find_id
:= FindTexture();
325 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
332 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
339 find_id
:= FindTexture
;
341 if not LoadTextureMem(pData
, dataSize
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
, e_Textures
[find_id
].tx
.Height
, @fmt
) then exit
;
348 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
355 find_id
:= FindTexture();
357 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
364 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
370 find_id
:= FindTexture();
371 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
376 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
378 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
379 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
382 procedure e_ResizeFramebuffer(Width
, Height
: Integer);
384 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
);
409 glGenFramebuffers(1, @e_FBO
);
411 glGenTextures(1, @e_Frame
);
412 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
413 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, Width
, Height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, nil);
414 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
415 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
417 glGenRenderbuffers(1, @e_RBO
);
418 glBindRenderbuffer(GL_RENDERBUFFER
, e_RBO
);
419 glRenderbufferStorage(GL_RENDERBUFFER
, GL_DEPTH24_STENCIL8
, Width
, Height
);
421 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
422 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, e_Frame
, 0);
423 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, e_RBO
);
426 procedure e_ResizeWindow(Width
, Height
: Integer);
430 e_SetViewPort(0, 0, Width
, Height
);
433 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
435 x1
, y1
, tmp
: Integer;
437 if (w
< 1) or (h
< 1) then exit
;
440 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
441 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
442 //HACK: make texture one pixel shorter, so it won't wrap
443 if (g_dbg_scale
<> 1.0) then
448 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
449 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
450 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
451 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
454 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
456 if (e_FBO
= 0) or (e_Frame
= 0) then exit
;
458 glEnable(GL_TEXTURE_2D
);
459 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
460 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
461 glColor4ub(255, 255, 255, 255);
462 e_SetViewPort(0, 0, WinWidth
, WinHeight
);
464 drawTxQuad(0, 0, WinWidth
, WinHeight
, e_FrameW
, e_FrameH
, 1, 1, TMirrorType
.None
);
466 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
467 e_SetViewPort(0, 0, e_FrameW
, e_FrameH
);
470 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
471 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
473 if e_NoGraphics
then Exit
;
474 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
476 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
481 if (AlphaChannel
) or (Alpha
> 0) then
482 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
485 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
488 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
490 glEnable(GL_TEXTURE_2D
);
491 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
494 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
);
496 //u := e_Textures[ID].tx.u;
497 //v := e_Textures[ID].tx.v;
500 if Mirror = M_NONE then
502 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
503 glTexCoord2f(0, 0); glVertex2i(X, Y);
504 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
505 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
508 if Mirror = M_HORIZONTAL then
510 glTexCoord2f(u, 0); glVertex2i(X, Y);
511 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
512 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
513 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
516 if Mirror = M_VERTICAL then
518 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
519 glTexCoord2f(0, -v); glVertex2i(X, Y);
520 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
521 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
530 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
531 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
535 if e_NoGraphics
then Exit
;
536 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
538 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
543 if (AlphaChannel
) or (Alpha
> 0) then
544 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
547 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
550 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
552 glEnable(GL_TEXTURE_2D
);
553 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
555 u
:= e_Textures
[ID
].tx
.u
;
556 v
:= e_Textures
[ID
].tx
.v
;
559 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
560 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
561 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
562 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
568 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
569 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
571 if e_NoGraphics
then Exit
;
572 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
574 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
579 if (AlphaChannel
) or (Alpha
> 0) then
580 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
583 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
586 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
588 glEnable(GL_TEXTURE_2D
);
589 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
591 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
);
597 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
598 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
600 X2
, Y2
, dx
, w
, h
: Integer;
603 if e_NoGraphics
then Exit
;
604 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
605 ambientBlendMode
:= false;
607 if (Alpha
> 0) or AlphaChannel
or Blending
then
613 if not ambientBlendMode
then glDisable(GL_BLEND
);
615 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
616 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
617 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
619 if (XCount
= 0) then XCount
:= 1;
620 if (YCount
= 0) then YCount
:= 1;
622 glEnable(GL_TEXTURE_2D
);
623 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
625 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
626 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
628 //k8: this SHOULD work... i hope
629 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
632 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
633 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
634 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
635 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
642 u
:= e_Textures
[ID
].tx
.u
;
643 v
:= e_Textures
[ID
].tx
.v
;
644 w
:= e_Textures
[ID
].tx
.width
;
645 h
:= e_Textures
[ID
].tx
.height
;
652 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
653 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
654 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
655 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
670 //TODO: overflow checks
671 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
676 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
677 // check for intersection
678 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
679 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
683 if (x0
< x1
) then x0
:= x1
;
684 if (y0
< y1
) then y0
:= y1
;
685 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
686 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
689 result
:= (w0
> 0) and (h0
> 0);
693 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
694 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
699 scxywh: array[0..3] of GLint;
700 vpxywh: array[0..3] of GLint;
702 w
, h
, dw
, cw
, ch
, yofs
: Integer;
703 u
, v
, cu
, cv
: Single;
707 procedure setScissorGLInternal (x, y, w, h: Integer);
709 //if not scallowed then exit;
714 y := vpxywh[3]-(y+h);
715 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
717 glScissor(0, 0, 0, 0);
721 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
722 glScissor(x, y, w, h);
728 if e_NoGraphics
then exit
;
729 ambientBlendMode
:= false;
731 if (wdt
< 1) or (hgt
< 1) then exit
;
733 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
735 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
739 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
741 if (Alpha
> 0) or AlphaChannel
or Blending
then
747 if not ambientBlendMode
then glDisable(GL_BLEND
);
749 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
750 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
751 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
753 glEnable(GL_TEXTURE_2D
);
754 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
759 //k8: this SHOULD work... i hope
760 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
763 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
764 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
765 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
766 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
771 // hard day's night; setup scissor
773 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
774 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
775 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
776 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
777 //glEnable(GL_SCISSOR_TEST);
778 setScissorGLInternal(x, y, wdt, hgt);
781 u
:= e_Textures
[ID
].tx
.u
;
782 v
:= e_Textures
[ID
].tx
.v
;
783 w
:= e_Textures
[ID
].tx
.width
;
784 h
:= e_Textures
[ID
].tx
.height
;
786 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
790 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
791 if onlyOneY
then yofs
:= 0;
797 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
799 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
800 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
801 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
802 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
808 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
815 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
817 if e_NoGraphics
then exit
;
818 if (w
< 1) or (h
< 1) then exit
;
819 if (a
<> 255) or ((r
or g
or b
) <> 0) then
822 glDisable(GL_TEXTURE_2D
);
823 glColor4ub(r
, g
, b
, a
);
824 if ((r
or g
or b
) <> 0) then
826 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
830 glVertex2i(x
+w
, y
+h
);
834 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
838 glVertex2i(x
+w
, y
+h
);
846 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
847 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
849 if e_NoGraphics
then Exit
;
851 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
853 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
858 if (AlphaChannel
) or (Alpha
> 0) then
859 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
862 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
865 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
867 if (Angle
<> 0) and (RC
<> nil) then
870 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
871 glRotatef(Angle
, 0, 0, 1);
872 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
875 glEnable(GL_TEXTURE_2D
);
876 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
877 glBegin(GL_QUADS
); //0-1 1-1
879 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
);
888 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
890 if e_NoGraphics
then Exit
;
891 glDisable(GL_TEXTURE_2D
);
892 glColor3ub(Red
, Green
, Blue
);
895 if (Size
= 2) or (Size
= 4) then
899 glVertex2f(X
+0.3, Y
+1.0);
902 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
905 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
907 // Make lines only top-left/bottom-right and top-right/bottom-left
919 // Pixel-perfect hack
927 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
929 nX1
, nY1
, nX2
, nY2
: Integer;
931 if e_NoGraphics
then Exit
;
932 // Only top-left/bottom-right quad
949 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
954 glDisable(GL_TEXTURE_2D
);
955 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
958 nX1
:= X1
; nY1
:= Y1
;
959 nX2
:= X2
; nY2
:= Y1
;
960 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
961 glVertex2i(nX1
, nY1
);
962 glVertex2i(nX2
, nY2
);
964 nX1
:= X2
; nY1
:= Y1
;
965 nX2
:= X2
; nY2
:= Y2
;
966 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
967 glVertex2i(nX1
, nY1
);
968 glVertex2i(nX2
, nY2
);
970 nX1
:= X2
; nY1
:= Y2
;
971 nX2
:= X1
; nY2
:= Y2
;
972 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
973 glVertex2i(nX1
, nY1
);
974 glVertex2i(nX2
, nY2
);
976 nX1
:= X1
; nY1
:= Y2
;
977 nX2
:= X1
; nY2
:= Y1
;
978 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
979 glVertex2i(nX1
, nY1
);
980 glVertex2i(nX2
, nY2
);
982 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
986 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
987 Blending
: TBlending
= TBlending
.None
);
989 if e_NoGraphics
then Exit
;
990 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
995 if Blending
= TBlending
.Blend
then
996 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
998 if Blending
= TBlending
.Filter
then
999 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
1001 if Blending
= TBlending
.Invert
then
1002 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
1005 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1007 glDisable(GL_TEXTURE_2D
);
1008 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1020 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1022 glDisable(GL_BLEND
);
1026 // ////////////////////////////////////////////////////////////////////////// //
1027 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1029 if (a
< 0) then a
:= 0;
1030 if (a
> 255) then a
:= 255;
1032 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1033 glDisable(GL_TEXTURE_2D
);
1034 glColor4ub(0, 0, 0, Byte(255-a
));
1041 //glRect(x, y, x+w, y+h);
1042 glColor4ub(1, 1, 1, 1);
1043 glDisable(GL_BLEND
);
1044 //glBlendEquation(GL_FUNC_ADD);
1047 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1049 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1053 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1055 if e_NoGraphics
then Exit
;
1056 // Pixel-perfect lines
1058 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1063 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1065 glDisable(GL_BLEND
);
1067 glDisable(GL_TEXTURE_2D
);
1068 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1074 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1076 glDisable(GL_BLEND
);
1079 //------------------------------------------------------------------
1080 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1081 //------------------------------------------------------------------
1082 procedure e_DeleteTexture(ID
: DWORD
);
1084 if not e_NoGraphics
then
1085 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1086 e_Textures
[ID
].tx
.id
:= 0;
1087 e_Textures
[ID
].tx
.Width
:= 0;
1088 e_Textures
[ID
].tx
.Height
:= 0;
1091 //------------------------------------------------------------------
1092 // Óäàëÿåò âñå òåêñòóðû
1093 //------------------------------------------------------------------
1094 procedure e_RemoveAllTextures();
1098 if e_Textures
= nil then Exit
;
1100 for i
:= 0 to High(e_Textures
) do
1101 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1105 //------------------------------------------------------------------
1107 //------------------------------------------------------------------
1108 procedure e_ReleaseEngine();
1110 e_RemoveAllTextures
;
1111 e_RemoveAllTextureFont
;
1114 procedure e_BeginRender();
1116 if e_NoGraphics
then Exit
;
1117 glEnable(GL_ALPHA_TEST
);
1118 glAlphaFunc(GL_GREATER
, 0.0);
1121 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1123 if e_NoGraphics
then Exit
;
1124 glClearColor(Red
, Green
, Blue
, 0);
1128 procedure e_Clear(); overload
;
1130 if e_NoGraphics
then Exit
;
1131 glClearColor(0, 0, 0, 0);
1132 glClear(GL_COLOR_BUFFER_BIT
);
1135 procedure e_EndRender();
1137 if e_NoGraphics
then Exit
;
1142 function e_GetGamma(win
: PSDL_Window
): Byte;
1144 ramp
: array [0..256*3-1] of Word;
1145 rgb
: array [0..2] of Double;
1154 if e_NoGraphics
then Exit
;
1159 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1168 for j
:= min
to max
- 1 do
1171 B
:= (j
mod 256)/256;
1173 sum
:= sum
+ ln(A
)/ln(B
);
1176 rgb
[i
] := sum
/ count
;
1179 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1182 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1184 ramp
: array [0..256*3-1] of Word;
1189 if e_NoGraphics
then Exit
;
1190 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1192 for i
:= 0 to 255 do
1194 r
:= Exp(g
* ln(i
/256))*65536;
1195 if r
< 0 then r
:= 0
1196 else if r
> 65535 then r
:= 65535;
1197 ramp
[i
] := trunc(r
);
1198 ramp
[i
+ 256] := trunc(r
);
1199 ramp
[i
+ 512] := trunc(r
);
1202 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1206 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1210 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1214 if e_CharFonts
<> nil then
1215 for i
:= 0 to High(e_CharFonts
) do
1216 if not e_CharFonts
[i
].alive
then
1222 if id
= DWORD(-1) then
1224 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1225 id
:= High(e_CharFonts
);
1228 with e_CharFonts
[id
] do
1230 for i
:= 0 to High(Chars
) do
1244 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1246 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1248 TextureID
:= Texture
;
1253 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1257 if e_NoGraphics
then Exit
;
1258 if Text = '' then Exit
;
1259 if e_CharFonts
= nil then Exit
;
1260 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1262 with e_CharFonts
[FontID
] do
1264 for a
:= 1 to Length(Text) do
1265 with Chars
[Ord(Text[a
])] do
1266 if TextureID
<> -1 then
1268 e_Draw(TextureID
, X
, Y
, 0, True, False);
1269 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1274 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1275 Color
: TRGB
; Scale
: Single = 1.0);
1280 if e_NoGraphics
then Exit
;
1281 if Text = '' then Exit
;
1282 if e_CharFonts
= nil then Exit
;
1283 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1285 with e_CharFonts
[FontID
] do
1287 for a
:= 1 to Length(Text) do
1288 with Chars
[Ord(Text[a
])] do
1289 if TextureID
<> -1 then
1291 if Scale
<> 1.0 then
1294 glScalef(Scale
, Scale
, 0);
1299 e_Draw(TextureID
, X
, Y
, 0, True, False);
1302 if Scale
<> 1.0 then glPopMatrix
;
1304 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1309 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1311 a
, TX
, TY
, len
: Integer;
1315 if e_NoGraphics
then Exit
;
1316 if Text = '' then Exit
;
1317 if e_CharFonts
= nil then Exit
;
1318 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1326 len
:= Length(Text);
1328 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1330 with e_CharFonts
[FontID
] do
1332 for a
:= 1 to len
do
1343 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1348 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1353 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1358 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1363 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1368 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1373 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1378 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1383 with Chars
[Ord(Text[a
])] do
1384 if TextureID
<> -1 then
1388 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1391 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1397 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1405 if Text = '' then Exit
;
1406 if e_CharFonts
= nil then Exit
;
1407 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1409 with e_CharFonts
[FontID
] do
1411 for a
:= 1 to Length(Text) do
1412 with Chars
[Ord(Text[a
])] do
1413 if TextureID
<> -1 then
1415 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1416 e_GetTextureSize(TextureID
, nil, @h2
);
1417 if h2
> h
then h
:= h2
;
1422 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1424 a
, lines
, len
: Integer;
1425 h2
, w2
, tw
, th
: Word;
1432 if Text = '' then Exit
;
1433 if e_CharFonts
= nil then Exit
;
1434 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1437 len
:= Length(Text);
1439 with e_CharFonts
[FontID
] do
1441 for a
:= 1 to len
do
1443 if Text[a
] = #10 then
1446 if w2
> tw
then tw
:= w2
;
1451 with Chars
[Ord(Text[a
])] do
1452 if TextureID
<> -1 then
1454 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1455 e_GetTextureSize(TextureID
, nil, @h2
);
1456 if h2
> th
then th
:= h2
;
1468 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1474 if e_CharFonts
= nil then Exit
;
1475 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1477 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1478 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1481 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1488 if e_CharFonts
= nil then Exit
;
1489 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1491 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1493 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1494 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1496 if h2
> Result
then Result
:= h2
;
1500 procedure e_CharFont_Remove(FontID
: DWORD
);
1504 with e_CharFonts
[FontID
] do
1505 for a
:= 0 to High(Chars
) do
1506 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1508 e_CharFonts
[FontID
].alive
:= False;
1511 procedure e_CharFont_RemoveAll();
1515 if e_CharFonts
= nil then Exit
;
1517 for a
:= 0 to High(e_CharFonts
) do
1518 e_CharFont_Remove(a
);
1523 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1532 if e_NoGraphics
then Exit
;
1533 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1537 if e_TextureFonts
<> nil then
1538 for i
:= 0 to High(e_TextureFonts
) do
1539 if e_TextureFonts
[i
].Base
= 0 then
1545 if id
= DWORD(-1) then
1547 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1548 id
:= High(e_TextureFonts
);
1551 with e_TextureFonts
[id
] do
1554 Base
:= glGenLists(XCount
*YCount
);
1556 TextureID
:= e_Textures
[Tex
].tx
.id
;
1557 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1558 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1566 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1567 for loop1
:= 0 to XCount
*YCount
-1 do
1569 cx
:= (loop1
mod XCount
)/XCount
;
1570 cy
:= (loop1
div YCount
)/YCount
;
1572 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1574 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1575 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1577 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1578 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1580 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1581 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1583 glTexCoord2f(cx
, 1.0-cy
);
1586 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1594 procedure e_TextureFontKill(FontID
: DWORD
);
1596 if e_NoGraphics
then Exit
;
1598 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1600 e_TextureFonts
[FontID
].Base
:= 0;
1603 {$IFNDEF NOGL_LISTS}
1604 procedure e_TextureFontDrawChar(ch
: Char; FontID
: DWORD
);
1609 Width
, Height
: Integer;
1610 XCount
, YCount
: Integer;
1612 index
:= Ord(ch
) - 32;
1613 Tex
:= e_TextureFonts
[FontID
].Texture
;
1614 Width
:= e_Textures
[Tex
].tx
.Width
;
1615 Height
:= e_Textures
[Tex
].tx
.Height
;
1616 XCount
:= e_TextureFonts
[FontID
].XC
;
1617 YCount
:= e_TextureFonts
[FontID
].YC
;
1618 cx
:= (index
mod XCount
)/XCount
;
1619 cy
:= (index
div YCount
)/YCount
;
1621 glTexCoord2f(cx
, 1 - cy
- 1/YCount
);
1622 glVertex2i(0, Height
div YCount
);
1623 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
- 1/YCount
);
1624 glVertex2i(Width
div XCount
, Height
div YCount
);
1625 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
);
1626 glVertex2i(Width
div XCount
, 0);
1627 glTexCoord2f(cx
, 1 - cy
);
1630 glTranslatef((e_Textures
[Tex
].tx
.Width
div XCount
) + e_TextureFonts
[FontID
].SPC
, 0, 0);
1633 procedure e_TextureFontDrawString(Text: String; FontID
: DWORD
);
1637 for i
:= 1 to High(Text) do
1638 e_TextureFontDrawChar(Text[i
], FontID
);
1642 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1644 if e_NoGraphics
then Exit
;
1645 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1646 if Text = '' then Exit
;
1648 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1651 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1654 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1655 glEnable(GL_TEXTURE_2D
);
1656 glTranslatef(x
, y
, 0);
1658 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1659 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1661 e_TextureFontDrawString(Text, FontID
);
1663 glDisable(GL_TEXTURE_2D
);
1666 glDisable(GL_BLEND
);
1669 // god forgive me for this, but i cannot figure out how to do it without lists
1670 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1672 if e_NoGraphics
then Exit
;
1677 glColor4ub(0, 0, 0, 128);
1678 glTranslatef(X
+1, Y
+1, 0);
1680 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1682 e_TextureFontDrawChar(Ch
, FontID
);
1688 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1689 glTranslatef(X
, Y
, 0);
1691 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1693 e_TextureFontDrawChar(Ch
, FontID
);
1699 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1701 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1702 glEnable(GL_TEXTURE_2D
);
1703 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1705 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1707 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1708 glDisable(GL_TEXTURE_2D
);
1709 glDisable(GL_BLEND
);
1712 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1714 result
:= e_TextureFonts
[FontID
].CharWidth
;
1717 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
;
1718 Shadow
: Boolean = False; Newlines
: Boolean = False);
1720 a
, TX
, TY
, len
: Integer;
1724 if e_NoGraphics
then Exit
;
1725 if Text = '' then Exit
;
1726 if e_TextureFonts
= nil then Exit
;
1727 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1735 len
:= Length(Text);
1737 w
:= e_TextureFonts
[FontID
].CharWidth
;
1738 h
:= e_TextureFonts
[FontID
].CharHeight
;
1740 with e_TextureFonts
[FontID
] do
1742 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1743 glEnable(GL_TEXTURE_2D
);
1746 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1749 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1752 for a
:= 1 to len
do
1766 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1771 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1776 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1781 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1786 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1791 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1796 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1801 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1808 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1813 glDisable(GL_TEXTURE_2D
);
1814 glDisable(GL_BLEND
);
1818 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1819 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1821 if e_NoGraphics
then Exit
;
1822 if Text = '' then Exit
;
1825 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1826 glEnable(GL_TEXTURE_2D
);
1829 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1832 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1837 glColor4ub(0, 0, 0, 128);
1838 glTranslatef(x
+1, y
+1, 0);
1839 glScalef(Scale
, Scale
, 0);
1841 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1843 e_TextureFontDrawString(Text, FontID
);
1849 glColor4ub(Red
, Green
, Blue
, 255);
1850 glTranslatef(x
, y
, 0);
1851 glScalef(Scale
, Scale
, 0);
1853 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1855 e_TextureFontDrawString(Text, FontID
);
1858 glDisable(GL_TEXTURE_2D
);
1860 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1861 glDisable(GL_BLEND
);
1864 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1868 if e_NoGraphics
then Exit
;
1869 if Integer(ID
) > High(e_TextureFonts
) then
1871 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1872 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1875 procedure e_RemoveAllTextureFont();
1879 if e_NoGraphics
then Exit
;
1880 if e_TextureFonts
= nil then Exit
;
1882 for i
:= 0 to High(e_TextureFonts
) do
1883 if e_TextureFonts
[i
].Base
<> 0 then
1886 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1888 e_TextureFonts
[i
].Base
:= 0;
1891 e_TextureFonts
:= nil;
1894 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1901 function _Point(X
, Y
: Integer): TPoint2i
;
1907 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1911 Result
.Width
:= Width
;
1912 Result
.Height
:= Height
;
1915 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1924 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1926 pixels
, obuf
, scln
, ps
, pd
: PByte;
1929 i
, x
, y
, res
: Integer;
1930 sign
: array [0..7] of Byte;
1931 hbuf
: array [0..12] of Byte;
1936 if e_NoGraphics
then Exit
;
1939 // first, extract and pack graphics data
1940 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1942 GetMem(pixels
, Width
*Height
*3);
1944 FillChar(pixels
^, Width
*Height
*3, 0);
1945 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1946 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1948 if e_FastScreenshots
then
1951 GetMem(scln
, (Width
*3+1)*Height
);
1955 Inc(ps
, (Width
*3)*(Height
-1));
1956 for i
:= 0 to Height
-1 do
1960 Move(ps
^, pd
^, Width
*3);
1972 obufsize
:= (Width
*3+1)*Height
*2;
1973 GetMem(obuf
, obufsize
);
1978 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1979 if res
= Z_OK
then break
;
1980 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1981 obufsize
:= obufsize
*2;
1984 GetMem(obuf
, obufsize
);
1986 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1999 st
.writeBuffer(sign
, 8);
2000 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2003 writeIntBE(st
, LongWord(13));
2008 st
.writeBuffer(sign
, 4);
2009 crc
:= crc32(0, @sign
[0], 4);
2012 hbuf
[2] := (Width
shr 8) and $ff;
2013 hbuf
[3] := Width
and $ff;
2016 hbuf
[6] := (Height
shr 8) and $ff;
2017 hbuf
[7] := Height
and $ff;
2018 hbuf
[8] := 8; // bit depth
2019 hbuf
[9] := 2; // RGB
2020 hbuf
[10] := 0; // compression method
2021 hbuf
[11] := 0; // filter method
2022 hbuf
[12] := 0; // no interlace
2023 crc
:= crc32(crc
, @hbuf
[0], 13);
2024 st
.writeBuffer(hbuf
, 13);
2025 writeIntBE(st
, crc
);
2026 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2029 writeIntBE(st
, LongWord(dlen
));
2034 st
.writeBuffer(sign
, 4);
2035 crc
:= crc32(0, @sign
[0], 4);
2036 crc
:= crc32(crc
, obuf
, dlen
);
2037 st
.writeBuffer(obuf
^, dlen
);
2038 writeIntBE(st
, crc
);
2039 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2042 writeIntBE(st
, LongWord(0));
2047 st
.writeBuffer(sign
, 4);
2048 crc
:= crc32(0, @sign
[0], 4);
2049 writeIntBE(st
, crc
);
2050 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2052 if obuf
<> nil then FreeMem(obuf
);
2057 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2058 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2061 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2063 //writeln(stderr, 'moving pixels...');
2064 for y
:= Height
-1 downto 0 do
2066 for x
:= 0 to Width
-1 do
2068 clr
.r
:= ps
^; Inc(ps
);
2069 clr
.g
:= ps
^; Inc(ps
);
2070 clr
.b
:= ps
^; Inc(ps
);
2072 SetPixel32(img
, x
, y
, clr
);
2075 GlobalMetadata
.ClearMetaItems();
2076 GlobalMetadata
.ClearMetaItemsForSaving();
2077 //writeln(stderr, 'compressing image...');
2078 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2079 //writeln(stderr, 'done!');