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
, r_texture
, g_base
,
26 MAPDEF
, ImagingTypes
, Imaging
, ImagingUtility
;
31 //------------------------------------------------------------------
33 //------------------------------------------------------------------
35 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
36 procedure e_ResizeWindow(Width
, Height
: Integer);
37 function e_ResizeFramebuffer(Width
, Height
: Integer): Boolean;
38 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
39 procedure e_SetRenderTarget(Framebuffer
: Boolean);
41 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
42 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
43 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
44 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
45 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
46 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
47 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
48 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
50 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
51 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
53 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
54 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
56 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
58 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
59 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
60 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
61 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
62 Blending
: TBlending
= TBlending
.None
);
63 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
64 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
66 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
67 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
68 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
69 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; filter
: Boolean = False): Boolean;
70 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word; filter
: Boolean = False): Boolean;
71 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
72 procedure e_DeleteTexture(ID
: DWORD
);
73 procedure e_RemoveAllTextures();
76 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
77 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
78 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
79 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
80 Color
: TRGB
; Scale
: Single = 1.0);
81 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
82 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
83 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
84 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
85 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
86 procedure e_CharFont_Remove(FontID
: DWORD
);
87 procedure e_CharFont_RemoveAll();
90 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
92 procedure e_TextureFontKill(FontID
: DWORD
);
93 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
94 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
95 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
96 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
;
97 Shadow
: Boolean = False; Newlines
: Boolean = False);
98 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
99 procedure e_RemoveAllTextureFont();
101 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
102 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
104 procedure e_ReleaseEngine();
105 procedure e_BeginRender();
106 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
107 procedure e_Clear(Red
, Green
, Blue
: Single); overload
;
108 procedure e_Clear(); overload
;
109 procedure e_EndRender();
112 function e_GetGamma(win
: PSDL_Window
): Byte;
113 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
116 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
118 //function e_getTextGLId (ID: DWORD): GLuint;
126 paszlib
, crc
, utils
, g_options
;
133 TTextureFont
= record
144 Chars
: array[0..255] of
154 TSavedTexture
= record
161 e_Textures
: array of TTexture
= nil;
162 e_TextureFonts
: array of TTextureFont
= nil;
163 e_CharFonts
: array of TCharFont
;
164 //e_SavedTextures: array of TSavedTexture;
167 e_RBOSupported
: Boolean = True;
170 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
172 //------------------------------------------------------------------
173 // Инициализирует OpenGL
174 //------------------------------------------------------------------
175 procedure e_InitGL();
179 e_DummyTextures
:= True;
185 glDisable(GL_DEPTH_TEST
);
186 glEnable(GL_SCISSOR_TEST
);
187 glClearColor(0, 0, 0, 0);
190 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
192 mat
: Array [0..15] of GLDouble
;
195 if e_NoGraphics
then Exit
;
197 glScissor(X
, Y
, Width
, Height
);
198 glViewport(X
, Y
, Width
, Height
);
199 //gluOrtho2D(0, Width, Height, 0);
201 glMatrixMode(GL_PROJECTION
);
203 mat
[ 0] := 2.0 / Width
;
209 mat
[ 5] := -2.0 / Height
;
223 glLoadMatrixd(@mat
[0]);
225 glMatrixMode(GL_MODELVIEW
);
229 //------------------------------------------------------------------
230 // Ищет свободный элемент в массиве текстур
231 //------------------------------------------------------------------
232 function FindTexture(): DWORD
;
236 if e_Textures
<> nil then
237 for i
:= 0 to High(e_Textures
) do
238 if e_Textures
[i
].tx
.Width
= 0 then
244 if e_Textures
= nil then
246 SetLength(e_Textures
, 32);
251 Result
:= High(e_Textures
) + 1;
252 SetLength(e_Textures
, Length(e_Textures
) + 32);
256 //------------------------------------------------------------------
258 //------------------------------------------------------------------
259 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
266 e_WriteLog('Loading texture from '+FileName
, TMsgType
.Notify
);
268 find_id
:= FindTexture();
270 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
271 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
278 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
285 find_id
:= FindTexture();
287 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
294 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; filter
: Boolean = False): Boolean;
301 find_id
:= FindTexture
;
303 if not LoadTextureMem(pData
, dataSize
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
, e_Textures
[find_id
].tx
.Height
, @fmt
, filter
) then exit
;
310 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word; filter
: Boolean = False): Boolean;
317 find_id
:= FindTexture();
319 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
, filter
) then exit
;
326 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
332 find_id
:= FindTexture();
333 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
338 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
340 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
341 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
344 procedure DestroyFramebuffer
;
346 glBindTexture(GL_TEXTURE_2D
, 0);
347 glBindRenderbuffer(GL_RENDERBUFFER
, 0);
348 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
352 glDeleteTextures(1, @e_Frame
);
358 glDeleteRenderbuffers(1, @e_RBO
);
364 glDeleteFramebuffers(1, @e_FBO
);
369 function e_ResizeFramebuffer(Width
, Height
: Integer): Boolean;
373 if e_NoGraphics
then Exit
;
379 glGenFramebuffers(1, @e_FBO
);
381 if glGetError() <> GL_NO_ERROR
then
383 e_LogWriteln('GL: glGenFramebuffers failed');
387 glGenTextures(1, @e_Frame
);
388 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
389 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, Width
, Height
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, nil);
390 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
391 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
393 if glGetError() <> GL_NO_ERROR
then
395 e_LogWriteln('GL: can''t create FBO color buffer');
400 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
);
401 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, e_Frame
, 0);
402 if glCheckFramebufferStatus(GL_FRAMEBUFFER
) <> GL_FRAMEBUFFER_COMPLETE
then
404 e_LogWriteln('GL: can''t construct framebuffer with color attachment');
410 if e_RBOSupported
then
412 glGenRenderbuffers(1, @e_RBO
);
413 glBindRenderbuffer(GL_RENDERBUFFER
, e_RBO
);
414 glRenderbufferStorage(GL_RENDERBUFFER
, GL_DEPTH24_STENCIL8
, Width
, Height
);
415 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, e_RBO
);
416 if glCheckFramebufferStatus(GL_FRAMEBUFFER
) <> GL_FRAMEBUFFER_COMPLETE
then
418 e_LogWriteln('GL: can''t construct framebuffer with depth+stencil attachment, trying without');
419 e_RBOSupported
:= False;
420 Result
:= e_ResizeFramebuffer(Width
, Height
);
429 procedure e_ResizeWindow(Width
, Height
: Integer);
433 e_SetViewPort(0, 0, Width
, Height
);
436 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
438 x1
, y1
, tmp
: Integer;
440 if (w
< 1) or (h
< 1) then exit
;
443 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
444 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
445 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
446 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
447 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
448 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
451 procedure e_SetRenderTarget(Framebuffer
: Boolean);
453 if (e_FBO
= 0) or e_NoGraphics
then exit
;
455 glBindFramebuffer(GL_FRAMEBUFFER
, e_FBO
)
457 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
460 procedure e_BlitFramebuffer(WinWidth
, WinHeight
: Integer);
462 if (e_FBO
= 0) or (e_Frame
= 0) or e_NoGraphics
then exit
;
465 glEnable(GL_TEXTURE_2D
);
466 glBindTexture(GL_TEXTURE_2D
, e_Frame
);
467 glColor4ub(255, 255, 255, 255);
470 glTexCoord2f(0, 1); glVertex2i( 0, 0);
471 glTexCoord2f(0, 0); glVertex2i( 0, WinHeight
);
472 glTexCoord2f(1, 0); glVertex2i(WinWidth
, WinHeight
);
473 glTexCoord2f(1, 1); glVertex2i(WinWidth
, 0);
477 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
478 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
480 if e_NoGraphics
then Exit
;
481 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
483 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
488 if (AlphaChannel
) or (Alpha
> 0) then
489 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
492 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
495 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
497 glEnable(GL_TEXTURE_2D
);
498 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
501 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
);
503 //u := e_Textures[ID].tx.u;
504 //v := e_Textures[ID].tx.v;
507 if Mirror = M_NONE then
509 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
510 glTexCoord2f(0, 0); glVertex2i(X, Y);
511 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
512 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
515 if Mirror = M_HORIZONTAL then
517 glTexCoord2f(u, 0); glVertex2i(X, Y);
518 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
519 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
520 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
523 if Mirror = M_VERTICAL then
525 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
526 glTexCoord2f(0, -v); glVertex2i(X, Y);
527 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
528 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
537 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
538 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
542 if e_NoGraphics
then Exit
;
543 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
545 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
550 if (AlphaChannel
) or (Alpha
> 0) then
551 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
554 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
557 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
559 glEnable(GL_TEXTURE_2D
);
560 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
562 u
:= e_Textures
[ID
].tx
.u
;
563 v
:= e_Textures
[ID
].tx
.v
;
566 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
567 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
568 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
569 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
575 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
576 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
578 if e_NoGraphics
then Exit
;
579 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
581 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
586 if (AlphaChannel
) or (Alpha
> 0) then
587 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
590 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
593 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
595 glEnable(GL_TEXTURE_2D
);
596 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
598 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
);
604 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
605 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
607 X2
, Y2
, dx
, w
, h
: Integer;
610 if e_NoGraphics
then Exit
;
611 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
612 ambientBlendMode
:= false;
614 if (Alpha
> 0) or AlphaChannel
or Blending
then
620 if not ambientBlendMode
then glDisable(GL_BLEND
);
622 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
623 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
624 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
626 if (XCount
= 0) then XCount
:= 1;
627 if (YCount
= 0) then YCount
:= 1;
629 glEnable(GL_TEXTURE_2D
);
630 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
632 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
633 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
635 //k8: this SHOULD work... i hope
636 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
639 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
640 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
641 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
642 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
649 u
:= e_Textures
[ID
].tx
.u
;
650 v
:= e_Textures
[ID
].tx
.v
;
651 w
:= e_Textures
[ID
].tx
.width
;
652 h
:= e_Textures
[ID
].tx
.height
;
659 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
660 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
661 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
662 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
677 //TODO: overflow checks
678 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
683 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
684 // check for intersection
685 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
686 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
690 if (x0
< x1
) then x0
:= x1
;
691 if (y0
< y1
) then y0
:= y1
;
692 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
693 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
696 result
:= (w0
> 0) and (h0
> 0);
700 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
701 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
706 scxywh: array[0..3] of GLint;
707 vpxywh: array[0..3] of GLint;
709 w
, h
, dw
, cw
, ch
, yofs
: Integer;
710 u
, v
, cu
, cv
: Single;
714 procedure setScissorGLInternal (x, y, w, h: Integer);
716 //if not scallowed then exit;
721 y := vpxywh[3]-(y+h);
722 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
724 glScissor(0, 0, 0, 0);
728 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
729 glScissor(x, y, w, h);
735 if e_NoGraphics
then exit
;
736 ambientBlendMode
:= false;
738 if (wdt
< 1) or (hgt
< 1) then exit
;
740 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
742 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
746 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
748 if (Alpha
> 0) or AlphaChannel
or Blending
then
754 if not ambientBlendMode
then glDisable(GL_BLEND
);
756 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
757 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
758 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
760 glEnable(GL_TEXTURE_2D
);
761 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
766 //k8: this SHOULD work... i hope
767 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
770 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
771 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
772 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
773 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
778 // hard day's night; setup scissor
780 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
781 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
782 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
783 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
784 //glEnable(GL_SCISSOR_TEST);
785 setScissorGLInternal(x, y, wdt, hgt);
788 u
:= e_Textures
[ID
].tx
.u
;
789 v
:= e_Textures
[ID
].tx
.v
;
790 w
:= e_Textures
[ID
].tx
.width
;
791 h
:= e_Textures
[ID
].tx
.height
;
793 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
797 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
798 if onlyOneY
then yofs
:= 0;
804 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
806 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
807 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
808 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
809 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
815 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
822 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
824 if e_NoGraphics
then exit
;
825 if (w
< 1) or (h
< 1) then exit
;
826 if (a
<> 255) or ((r
or g
or b
) <> 0) then
829 glDisable(GL_TEXTURE_2D
);
830 glColor4ub(r
, g
, b
, a
);
831 if ((r
or g
or b
) <> 0) then
833 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
837 glVertex2i(x
+w
, y
+h
);
841 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
845 glVertex2i(x
+w
, y
+h
);
853 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
854 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
856 if e_NoGraphics
then Exit
;
858 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
860 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
865 if (AlphaChannel
) or (Alpha
> 0) then
866 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
869 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
872 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
874 if (Angle
<> 0) and (RC
<> nil) then
877 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
878 glRotatef(Angle
, 0, 0, 1);
879 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
882 glEnable(GL_TEXTURE_2D
);
883 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
884 glBegin(GL_QUADS
); //0-1 1-1
886 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
);
895 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
897 if e_NoGraphics
then Exit
;
898 glDisable(GL_TEXTURE_2D
);
899 glColor3ub(Red
, Green
, Blue
);
902 if (Size
= 2) or (Size
= 4) then
906 glVertex2f(X
+0.3, Y
+1.0);
909 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
912 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
914 // Make lines only top-left/bottom-right and top-right/bottom-left
926 // Pixel-perfect hack
934 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
936 nX1
, nY1
, nX2
, nY2
: Integer;
938 if e_NoGraphics
then Exit
;
939 // Only top-left/bottom-right quad
956 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
961 glDisable(GL_TEXTURE_2D
);
962 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
965 nX1
:= X1
; nY1
:= Y1
;
966 nX2
:= X2
; nY2
:= Y1
;
967 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
968 glVertex2i(nX1
, nY1
);
969 glVertex2i(nX2
, nY2
);
971 nX1
:= X2
; nY1
:= Y1
;
972 nX2
:= X2
; nY2
:= Y2
;
973 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
974 glVertex2i(nX1
, nY1
);
975 glVertex2i(nX2
, nY2
);
977 nX1
:= X2
; nY1
:= Y2
;
978 nX2
:= X1
; nY2
:= Y2
;
979 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
980 glVertex2i(nX1
, nY1
);
981 glVertex2i(nX2
, nY2
);
983 nX1
:= X1
; nY1
:= Y2
;
984 nX2
:= X1
; nY2
:= Y1
;
985 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
986 glVertex2i(nX1
, nY1
);
987 glVertex2i(nX2
, nY2
);
989 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
993 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
994 Blending
: TBlending
= TBlending
.None
);
996 if e_NoGraphics
then Exit
;
997 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
1000 glDisable(GL_BLEND
);
1003 TBlending
.None
: if Alpha
> 0 then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1004 TBlending
.Blend
: glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
1005 TBlending
.Invert
: glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
);
1006 TBlending
.Filter
: glBlendFunc(GL_ZERO
, GL_SRC_COLOR
);
1009 glDisable(GL_TEXTURE_2D
);
1010 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1022 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1023 glDisable(GL_BLEND
);
1027 // ////////////////////////////////////////////////////////////////////////// //
1028 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1030 if (a
< 0) then a
:= 0;
1031 if (a
> 255) then a
:= 255;
1033 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1034 glDisable(GL_TEXTURE_2D
);
1035 glColor4ub(0, 0, 0, Byte(255-a
));
1042 //glRect(x, y, x+w, y+h);
1043 glColor4ub(1, 1, 1, 1);
1044 glDisable(GL_BLEND
);
1045 //glBlendEquation(GL_FUNC_ADD);
1048 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1050 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1054 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1056 if e_NoGraphics
then Exit
;
1057 // Pixel-perfect lines
1059 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1064 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1066 glDisable(GL_BLEND
);
1068 glDisable(GL_TEXTURE_2D
);
1069 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1075 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1077 glDisable(GL_BLEND
);
1080 //------------------------------------------------------------------
1081 // Удаляет текстуру из массива
1082 //------------------------------------------------------------------
1083 procedure e_DeleteTexture(ID
: DWORD
);
1085 if not e_NoGraphics
then
1086 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1087 e_Textures
[ID
].tx
.id
:= 0;
1088 e_Textures
[ID
].tx
.Width
:= 0;
1089 e_Textures
[ID
].tx
.Height
:= 0;
1092 //------------------------------------------------------------------
1093 // Удаляет все текстуры
1094 //------------------------------------------------------------------
1095 procedure e_RemoveAllTextures();
1099 if e_Textures
= nil then Exit
;
1101 for i
:= 0 to High(e_Textures
) do
1102 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1106 //------------------------------------------------------------------
1108 //------------------------------------------------------------------
1109 procedure e_ReleaseEngine();
1111 e_RemoveAllTextures
;
1112 e_RemoveAllTextureFont
;
1115 procedure e_BeginRender();
1117 if e_NoGraphics
then Exit
;
1118 glEnable(GL_ALPHA_TEST
);
1119 glAlphaFunc(GL_GREATER
, 0.0);
1122 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1124 if e_NoGraphics
then Exit
;
1125 glClearColor(Red
, Green
, Blue
, 0);
1129 procedure e_Clear(Red
, Green
, Blue
: Single); overload
;
1131 if e_NoGraphics
then Exit
;
1132 glClearColor(Red
, Green
, Blue
, 0);
1133 glClear(GL_COLOR_BUFFER_BIT
);
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 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1904 pixels
, obuf
, scln
, ps
, pd
: PByte;
1907 i
, x
, y
, res
: Integer;
1908 sign
: array [0..7] of Byte;
1909 hbuf
: array [0..12] of Byte;
1914 if e_NoGraphics
then Exit
;
1917 // first, extract and pack graphics data
1918 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1920 GetMem(pixels
, Width
*Height
*3);
1922 FillChar(pixels
^, Width
*Height
*3, 0);
1923 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1924 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1926 if e_FastScreenshots
then
1929 GetMem(scln
, (Width
*3+1)*Height
);
1933 Inc(ps
, (Width
*3)*(Height
-1));
1934 for i
:= 0 to Height
-1 do
1938 Move(ps
^, pd
^, Width
*3);
1950 obufsize
:= (Width
*3+1)*Height
*2;
1951 GetMem(obuf
, obufsize
);
1956 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1957 if res
= Z_OK
then break
;
1958 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1959 obufsize
:= obufsize
*2;
1962 GetMem(obuf
, obufsize
);
1964 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1977 st
.writeBuffer(sign
, 8);
1978 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1981 writeIntBE(st
, LongWord(13));
1986 st
.writeBuffer(sign
, 4);
1987 crc
:= crc32(0, @sign
[0], 4);
1990 hbuf
[2] := (Width
shr 8) and $ff;
1991 hbuf
[3] := Width
and $ff;
1994 hbuf
[6] := (Height
shr 8) and $ff;
1995 hbuf
[7] := Height
and $ff;
1996 hbuf
[8] := 8; // bit depth
1997 hbuf
[9] := 2; // RGB
1998 hbuf
[10] := 0; // compression method
1999 hbuf
[11] := 0; // filter method
2000 hbuf
[12] := 0; // no interlace
2001 crc
:= crc32(crc
, @hbuf
[0], 13);
2002 st
.writeBuffer(hbuf
, 13);
2003 writeIntBE(st
, crc
);
2004 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2007 writeIntBE(st
, LongWord(dlen
));
2012 st
.writeBuffer(sign
, 4);
2013 crc
:= crc32(0, @sign
[0], 4);
2014 crc
:= crc32(crc
, obuf
, dlen
);
2015 st
.writeBuffer(obuf
^, dlen
);
2016 writeIntBE(st
, crc
);
2017 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2020 writeIntBE(st
, LongWord(0));
2025 st
.writeBuffer(sign
, 4);
2026 crc
:= crc32(0, @sign
[0], 4);
2027 writeIntBE(st
, crc
);
2028 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2030 if obuf
<> nil then FreeMem(obuf
);
2035 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2036 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2039 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2041 //writeln(stderr, 'moving pixels...');
2042 for y
:= Height
-1 downto 0 do
2044 for x
:= 0 to Width
-1 do
2046 clr
.r
:= ps
^; Inc(ps
);
2047 clr
.g
:= ps
^; Inc(ps
);
2048 clr
.b
:= ps
^; Inc(ps
);
2050 SetPixel32(img
, x
, y
, clr
);
2053 GlobalMetadata
.ClearMetaItems();
2054 GlobalMetadata
.ClearMetaItemsForSaving();
2055 //writeln(stderr, 'compressing image...');
2056 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2057 //writeln(stderr, 'done!');