87b31a5b22dcaea9db8f8628a1d275266f896efe
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, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
22 SysUtils
, Classes
, Math
, e_log
, e_texture
, SDL2
, GL
, GLExt
, MAPDEF
, ImagingTypes
, Imaging
, ImagingUtility
;
25 TMirrorType
=(M_NONE
, M_HORIZONTAL
, M_VERTICAL
);
26 TBlending
=(B_NONE
, B_BLEND
, B_FILTER
, B_INVERT
);
37 Left
, Top
, Right
, Bottom
: Integer;
55 //------------------------------------------------------------------
57 //------------------------------------------------------------------
59 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
60 procedure e_ResizeWindow(Width
, Height
: Integer);
62 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
63 Blending
: Boolean; Mirror
: TMirrorType
= M_NONE
);
64 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
65 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= M_NONE
);
66 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
67 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
68 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
69 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
71 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
72 AlphaChannel
: Boolean; Blending
: Boolean);
74 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean; blending
: Boolean; scale
: Single);
76 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
77 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
78 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
79 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
80 Blending
: TBlending
= B_NONE
);
81 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
82 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
84 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
85 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
86 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
87 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
88 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
89 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
90 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
91 procedure e_DeleteTexture(ID
: DWORD
);
92 procedure e_RemoveAllTextures();
95 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
96 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
97 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
98 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
99 Color
: TRGB
; Scale
: Single = 1.0);
100 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
101 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
102 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
103 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
104 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
105 procedure e_CharFont_Remove(FontID
: DWORD
);
106 procedure e_CharFont_RemoveAll();
109 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
111 procedure e_TextureFontKill(FontID
: DWORD
);
112 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
113 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
114 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
115 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
116 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
117 procedure e_RemoveAllTextureFont();
119 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
120 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
122 procedure e_ReleaseEngine();
123 procedure e_BeginRender();
124 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
125 procedure e_Clear(); overload
;
126 procedure e_EndRender();
128 function e_GetGamma(win
: PSDL_Window
): Byte;
129 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
131 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
133 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
134 function _Point(X
, Y
: Integer): TPoint2i
;
135 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
136 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
138 //function e_getTextGLId (ID: DWORD): GLuint;
142 e_NoGraphics
: Boolean = False;
143 e_FastScreenshots
: Boolean = true; // it's REALLY SLOW with `false`
157 TTextureFont
= record
167 Chars
: array[0..255] of
177 TSavedTexture
= record
184 e_Textures
: array of TTexture
= nil;
185 e_TextureFonts
: array of TTextureFont
= nil;
186 e_CharFonts
: array of TCharFont
;
187 //e_SavedTextures: array of TSavedTexture;
189 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
191 //------------------------------------------------------------------
192 // Èíèöèàëèçèðóåò OpenGL
193 //------------------------------------------------------------------
194 procedure e_InitGL();
198 e_DummyTextures
:= True;
204 glDisable(GL_DEPTH_TEST
);
205 glEnable(GL_SCISSOR_TEST
);
206 glClearColor(0, 0, 0, 0);
209 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
211 mat
: Array [0..15] of GLDouble
;
214 if e_NoGraphics
then Exit
;
216 glScissor(X
, Y
, Width
, Height
);
217 glViewport(X
, Y
, Width
, Height
);
218 //gluOrtho2D(0, Width, Height, 0);
220 glMatrixMode(GL_PROJECTION
);
222 mat
[ 0] := 2.0 / Width
;
228 mat
[ 5] := -2.0 / Height
;
242 glLoadMatrixd(@mat
[0]);
244 glMatrixMode(GL_MODELVIEW
);
248 //------------------------------------------------------------------
249 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
250 //------------------------------------------------------------------
251 function FindTexture(): DWORD
;
255 if e_Textures
<> nil then
256 for i
:= 0 to High(e_Textures
) do
257 if e_Textures
[i
].tx
.Width
= 0 then
263 if e_Textures
= nil then
265 SetLength(e_Textures
, 32);
270 Result
:= High(e_Textures
) + 1;
271 SetLength(e_Textures
, Length(e_Textures
) + 32);
275 //------------------------------------------------------------------
277 //------------------------------------------------------------------
278 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
285 e_WriteLog('Loading texture from '+FileName
, MSG_NOTIFY
);
287 find_id
:= FindTexture();
289 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
290 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
297 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
304 find_id
:= FindTexture();
306 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
313 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
320 find_id
:= FindTexture
;
322 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
;
329 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
336 find_id
:= FindTexture();
338 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
345 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
351 find_id
:= FindTexture();
352 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
357 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
359 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
360 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
363 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
371 w
:= e_Textures
[ID
].tx
.Width
;
372 h
:= e_Textures
[ID
].tx
.Height
;
379 if e_NoGraphics
then Exit
;
381 data
:= GetMemory(w
*h
*4);
382 glEnable(GL_TEXTURE_2D
);
383 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
384 glGetTexImage(GL_TEXTURE_2D
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
386 for y
:= h
-1 downto 0 do
393 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
399 Result
.Y
:= h
-lastline
;
411 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
417 Result
.Height
:= h
-lastline
-Result
.Y
;
429 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
435 Result
.X
:= lastline
+1;
440 for x
:= w
-1 downto 0 do
447 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
453 Result
.Width
:= lastline
-Result
.X
+1;
461 procedure e_ResizeWindow(Width
, Height
: Integer);
465 e_SetViewPort(0, 0, Width
, Height
);
468 procedure drawTxQuad (x0
, y0
, w
, h
: Integer; u
, v
: single; Mirror
: TMirrorType
);
470 x1
, y1
, tmp
: Integer;
472 if (w
< 1) or (h
< 1) then exit
;
475 if Mirror
= M_HORIZONTAL
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
476 else if Mirror
= M_VERTICAL
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
477 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
478 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
479 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
480 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
483 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
484 Blending
: Boolean; Mirror
: TMirrorType
= M_NONE
);
486 if e_NoGraphics
then Exit
;
487 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
489 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
494 if (AlphaChannel
) or (Alpha
> 0) then
495 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
498 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
501 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
503 glEnable(GL_TEXTURE_2D
);
504 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
507 drawTxQuad(X
, Y
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
509 //u := e_Textures[ID].tx.u;
510 //v := e_Textures[ID].tx.v;
513 if Mirror = M_NONE then
515 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
516 glTexCoord2f(0, 0); glVertex2i(X, Y);
517 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
518 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
521 if Mirror = M_HORIZONTAL then
523 glTexCoord2f(u, 0); glVertex2i(X, Y);
524 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
525 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
526 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
529 if Mirror = M_VERTICAL then
531 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
532 glTexCoord2f(0, -v); glVertex2i(X, Y);
533 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
534 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
543 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
544 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
548 if e_NoGraphics
then Exit
;
549 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
551 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
556 if (AlphaChannel
) or (Alpha
> 0) then
557 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
560 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
563 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
565 glEnable(GL_TEXTURE_2D
);
566 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
568 u
:= e_Textures
[ID
].tx
.u
;
569 v
:= e_Textures
[ID
].tx
.v
;
572 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
573 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
574 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
575 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
581 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
582 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
584 if e_NoGraphics
then Exit
;
585 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
587 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
592 if (AlphaChannel
) or (Alpha
> 0) then
593 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
596 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
599 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
601 glEnable(GL_TEXTURE_2D
);
602 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
604 drawTxQuad(X
, Y
, Width
, Height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
610 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
611 AlphaChannel
: Boolean; Blending
: Boolean);
613 X2
, Y2
, dx
, w
, h
: Integer;
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
);
639 glEnable(GL_TEXTURE_2D
);
640 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
642 X2
:= X
+ e_Textures
[ID
].tx
.width
* XCount
;
643 Y2
:= Y
+ e_Textures
[ID
].tx
.height
* YCount
;
645 //k8: this SHOULD work... i hope
646 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
649 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
650 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
651 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
652 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
659 u
:= e_Textures
[ID
].tx
.u
;
660 v
:= e_Textures
[ID
].tx
.v
;
661 w
:= e_Textures
[ID
].tx
.width
;
662 h
:= e_Textures
[ID
].tx
.height
;
669 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
670 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
671 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
672 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
687 //TODO: overflow checks
688 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
693 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
694 // check for intersection
695 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
696 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
700 if (x0
< x1
) then x0
:= x1
;
701 if (y0
< y1
) then y0
:= y1
;
702 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
703 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
706 result
:= (w0
> 0) and (h0
> 0);
710 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean; blending
: Boolean; scale
: Single);
715 scxywh: array[0..3] of GLint;
716 vpxywh: array[0..3] of GLint;
718 w
, h
, dw
, cw
, ch
: Integer;
719 u
, v
, cu
, cv
: Single;
722 procedure setScissorGLInternal (x, y, w, h: Integer);
724 //if not scallowed then exit;
729 y := vpxywh[3]-(y+h);
730 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
732 glScissor(0, 0, 0, 0);
736 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
737 glScissor(x, y, w, h);
743 if e_NoGraphics
then exit
;
745 if (wdt
< 1) or (hgt
< 1) then exit
;
747 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
749 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
);
753 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
755 if (Alpha
> 0) or AlphaChannel
or Blending
then
760 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
762 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
764 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
766 glEnable(GL_TEXTURE_2D
);
767 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
772 //k8: this SHOULD work... i hope
773 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
776 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
777 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
778 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
779 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
784 // hard day's night; setup scissor
786 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
787 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
788 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
789 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
790 //glEnable(GL_SCISSOR_TEST);
791 setScissorGLInternal(x, y, wdt, hgt);
794 u
:= e_Textures
[ID
].tx
.u
;
795 v
:= e_Textures
[ID
].tx
.v
;
796 w
:= e_Textures
[ID
].tx
.width
;
797 h
:= e_Textures
[ID
].tx
.height
;
802 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
) end;
808 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
) end;
810 glTexCoord2f(0, cv
); glVertex2i(X
, Y
);
811 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
);
812 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
);
813 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
);
819 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
826 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
827 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= M_NONE
);
829 if e_NoGraphics
then Exit
;
831 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
833 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
838 if (AlphaChannel
) or (Alpha
> 0) then
839 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
842 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
845 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
847 if (Angle
<> 0) and (RC
<> nil) then
850 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
851 glRotatef(Angle
, 0, 0, 1);
852 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
855 glEnable(GL_TEXTURE_2D
);
856 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
857 glBegin(GL_QUADS
); //0-1 1-1
859 drawTxQuad(X
, Y
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
868 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
870 if e_NoGraphics
then Exit
;
871 glDisable(GL_TEXTURE_2D
);
872 glColor3ub(Red
, Green
, Blue
);
875 if (Size
= 2) or (Size
= 4) then
879 glVertex2f(X
+0.3, Y
+1.0);
882 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
885 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
887 // Make lines only top-left/bottom-right and top-right/bottom-left
899 // Pixel-perfect hack
907 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
909 nX1
, nY1
, nX2
, nY2
: Integer;
911 if e_NoGraphics
then Exit
;
912 // Only top-left/bottom-right quad
929 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
933 glDisable(GL_TEXTURE_2D
);
934 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
938 nX1
:= X1
; nY1
:= Y1
;
939 nX2
:= X2
; nY2
:= Y1
;
940 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
941 glVertex2i(nX1
, nY1
);
942 glVertex2i(nX2
, nY2
);
944 nX1
:= X2
; nY1
:= Y1
;
945 nX2
:= X2
; nY2
:= Y2
;
946 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
947 glVertex2i(nX1
, nY1
);
948 glVertex2i(nX2
, nY2
);
950 nX1
:= X2
; nY1
:= Y2
;
951 nX2
:= X1
; nY2
:= Y2
;
952 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
953 glVertex2i(nX1
, nY1
);
954 glVertex2i(nX2
, nY2
);
956 nX1
:= X1
; nY1
:= Y2
;
957 nX2
:= X1
; nY2
:= Y1
;
958 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
959 glVertex2i(nX1
, nY1
);
960 glVertex2i(nX2
, nY2
);
963 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
968 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
969 Blending
: TBlending
= B_NONE
);
971 if e_NoGraphics
then Exit
;
972 if (Alpha
> 0) or (Blending
<> B_NONE
) then
977 if Blending
= B_BLEND
then
978 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
980 if Blending
= B_FILTER
then
981 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
983 if Blending
= B_INVERT
then
984 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
987 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
989 glDisable(GL_TEXTURE_2D
);
990 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1002 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1004 glDisable(GL_BLEND
);
1008 // ////////////////////////////////////////////////////////////////////////// //
1009 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1011 if (a
< 0) then a
:= 0;
1012 if (a
> 255) then a
:= 255;
1014 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1015 glDisable(GL_TEXTURE_2D
);
1016 glColor4ub(0, 0, 0, Byte(255-a
));
1023 //glRect(x, y, x+w, y+h);
1024 glColor4ub(1, 1, 1, 1);
1025 glDisable(GL_BLEND
);
1026 //glBlendEquation(GL_FUNC_ADD);
1029 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1031 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1035 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1037 if e_NoGraphics
then Exit
;
1038 // Pixel-perfect lines
1040 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1045 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1047 glDisable(GL_BLEND
);
1049 glDisable(GL_TEXTURE_2D
);
1050 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1058 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1060 glDisable(GL_BLEND
);
1063 //------------------------------------------------------------------
1064 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1065 //------------------------------------------------------------------
1066 procedure e_DeleteTexture(ID
: DWORD
);
1068 if not e_NoGraphics
then
1069 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1070 e_Textures
[ID
].tx
.id
:= 0;
1071 e_Textures
[ID
].tx
.Width
:= 0;
1072 e_Textures
[ID
].tx
.Height
:= 0;
1075 //------------------------------------------------------------------
1076 // Óäàëÿåò âñå òåêñòóðû
1077 //------------------------------------------------------------------
1078 procedure e_RemoveAllTextures();
1082 if e_Textures
= nil then Exit
;
1084 for i
:= 0 to High(e_Textures
) do
1085 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1089 //------------------------------------------------------------------
1091 //------------------------------------------------------------------
1092 procedure e_ReleaseEngine();
1094 e_RemoveAllTextures
;
1095 e_RemoveAllTextureFont
;
1098 procedure e_BeginRender();
1100 if e_NoGraphics
then Exit
;
1101 glEnable(GL_ALPHA_TEST
);
1102 glAlphaFunc(GL_GREATER
, 0.0);
1105 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1107 if e_NoGraphics
then Exit
;
1108 glClearColor(Red
, Green
, Blue
, 0);
1112 procedure e_Clear(); overload
;
1114 if e_NoGraphics
then Exit
;
1115 glClearColor(0, 0, 0, 0);
1116 glClear(GL_COLOR_BUFFER_BIT
);
1119 procedure e_EndRender();
1121 if e_NoGraphics
then Exit
;
1125 function e_GetGamma(win
: PSDL_Window
): Byte;
1127 ramp
: array [0..256*3-1] of Word;
1128 rgb
: array [0..2] of Double;
1137 if e_NoGraphics
then Exit
;
1142 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1151 for j
:= min
to max
- 1 do
1154 B
:= (j
mod 256)/256;
1156 sum
:= sum
+ ln(A
)/ln(B
);
1159 rgb
[i
] := sum
/ count
;
1162 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1165 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1167 ramp
: array [0..256*3-1] of Word;
1172 if e_NoGraphics
then Exit
;
1173 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1175 for i
:= 0 to 255 do
1177 r
:= Exp(g
* ln(i
/256))*65536;
1178 if r
< 0 then r
:= 0
1179 else if r
> 65535 then r
:= 65535;
1180 ramp
[i
] := trunc(r
);
1181 ramp
[i
+ 256] := trunc(r
);
1182 ramp
[i
+ 512] := trunc(r
);
1185 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1188 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1192 e_WriteLog('Creating CharFont...', MSG_NOTIFY
);
1196 if e_CharFonts
<> nil then
1197 for i
:= 0 to High(e_CharFonts
) do
1198 if not e_CharFonts
[i
].alive
then
1204 if id
= DWORD(-1) then
1206 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1207 id
:= High(e_CharFonts
);
1210 with e_CharFonts
[id
] do
1212 for i
:= 0 to High(Chars
) do
1226 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1228 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1230 TextureID
:= Texture
;
1235 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1239 if e_NoGraphics
then Exit
;
1240 if Text = '' then Exit
;
1241 if e_CharFonts
= nil then Exit
;
1242 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1244 with e_CharFonts
[FontID
] do
1246 for a
:= 1 to Length(Text) do
1247 with Chars
[Ord(Text[a
])] do
1248 if TextureID
<> -1 then
1250 e_Draw(TextureID
, X
, Y
, 0, True, False);
1251 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1256 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1257 Color
: TRGB
; Scale
: Single = 1.0);
1262 if e_NoGraphics
then Exit
;
1263 if Text = '' then Exit
;
1264 if e_CharFonts
= nil then Exit
;
1265 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1267 with e_CharFonts
[FontID
] do
1269 for a
:= 1 to Length(Text) do
1270 with Chars
[Ord(Text[a
])] do
1271 if TextureID
<> -1 then
1273 if Scale
<> 1.0 then
1276 glScalef(Scale
, Scale
, 0);
1281 e_Draw(TextureID
, X
, Y
, 0, True, False);
1284 if Scale
<> 1.0 then glPopMatrix
;
1286 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1291 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1293 a
, TX
, TY
, len
: Integer;
1297 if e_NoGraphics
then Exit
;
1298 if Text = '' then Exit
;
1299 if e_CharFonts
= nil then Exit
;
1300 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1308 len
:= Length(Text);
1310 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1312 with e_CharFonts
[FontID
] do
1314 for a
:= 1 to len
do
1325 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1330 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1335 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1340 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1345 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1350 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1355 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1360 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1365 with Chars
[Ord(Text[a
])] do
1366 if TextureID
<> -1 then
1370 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1373 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1379 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1387 if Text = '' then Exit
;
1388 if e_CharFonts
= nil then Exit
;
1389 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1391 with e_CharFonts
[FontID
] do
1393 for a
:= 1 to Length(Text) do
1394 with Chars
[Ord(Text[a
])] do
1395 if TextureID
<> -1 then
1397 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1398 e_GetTextureSize(TextureID
, nil, @h2
);
1399 if h2
> h
then h
:= h2
;
1404 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1406 a
, lines
, len
: Integer;
1413 if Text = '' then Exit
;
1414 if e_CharFonts
= nil then Exit
;
1415 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1418 len
:= Length(Text);
1420 with e_CharFonts
[FontID
] do
1422 for a
:= 1 to len
do
1424 if Text[a
] = #10 then
1434 else if Text[a
] in [#1, #2, #3, #4, #18, #19, #20, #21] then
1437 with Chars
[Ord(Text[a
])] do
1438 if TextureID
<> -1 then
1440 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1441 e_GetTextureSize(TextureID
, nil, @h2
);
1442 if h2
> h
then h
:= h2
;
1452 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1458 if e_CharFonts
= nil then Exit
;
1459 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1461 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1462 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1465 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1472 if e_CharFonts
= nil then Exit
;
1473 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1475 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1477 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1478 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1480 if h2
> Result
then Result
:= h2
;
1484 procedure e_CharFont_Remove(FontID
: DWORD
);
1488 with e_CharFonts
[FontID
] do
1489 for a
:= 0 to High(Chars
) do
1490 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1492 e_CharFonts
[FontID
].alive
:= False;
1495 procedure e_CharFont_RemoveAll();
1499 if e_CharFonts
= nil then Exit
;
1501 for a
:= 0 to High(e_CharFonts
) do
1502 e_CharFont_Remove(a
);
1507 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1514 if e_NoGraphics
then Exit
;
1515 e_WriteLog('Creating texture font...', MSG_NOTIFY
);
1519 if e_TextureFonts
<> nil then
1520 for i
:= 0 to High(e_TextureFonts
) do
1521 if e_TextureFonts
[i
].Base
= 0 then
1527 if id
= DWORD(-1) then
1529 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1530 id
:= High(e_TextureFonts
);
1533 with e_TextureFonts
[id
] do
1535 Base
:= glGenLists(XCount
*YCount
);
1536 TextureID
:= e_Textures
[Tex
].tx
.id
;
1537 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1538 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1545 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1546 for loop1
:= 0 to XCount
*YCount
-1 do
1548 cx
:= (loop1
mod XCount
)/XCount
;
1549 cy
:= (loop1
div YCount
)/YCount
;
1551 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1553 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1554 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1556 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1557 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1559 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1560 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1562 glTexCoord2f(cx
, 1.0-cy
);
1565 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1572 procedure e_TextureFontKill(FontID
: DWORD
);
1574 if e_NoGraphics
then Exit
;
1575 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1576 e_TextureFonts
[FontID
].Base
:= 0;
1579 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1581 if e_NoGraphics
then Exit
;
1582 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1583 if Text = '' then Exit
;
1585 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1588 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1591 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1592 glEnable(GL_TEXTURE_2D
);
1593 glTranslated(x
, y
, 0);
1594 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1595 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1596 glDisable(GL_TEXTURE_2D
);
1599 glDisable(GL_BLEND
);
1602 // god forgive me for this, but i cannot figure out how to do it without lists
1603 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1605 if e_NoGraphics
then Exit
;
1610 glColor4ub(0, 0, 0, 128);
1611 glTranslated(X
+1, Y
+1, 0);
1612 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1617 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1618 glTranslated(X
, Y
, 0);
1619 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1624 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1626 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1627 glEnable(GL_TEXTURE_2D
);
1628 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1630 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1632 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1633 glDisable(GL_TEXTURE_2D
);
1634 glDisable(GL_BLEND
);
1637 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1639 result
:= e_TextureFonts
[FontID
].CharWidth
;
1642 procedure e_TextureFontPrintFmt(X
, Y
: Integer; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1644 a
, TX
, TY
, len
: Integer;
1648 if e_NoGraphics
then Exit
;
1649 if Text = '' then Exit
;
1650 if e_TextureFonts
= nil then Exit
;
1651 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1659 len
:= Length(Text);
1661 w
:= e_TextureFonts
[FontID
].CharWidth
;
1663 with e_TextureFonts
[FontID
] do
1665 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1666 glEnable(GL_TEXTURE_2D
);
1667 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1669 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1672 for a
:= 1 to len
do
1683 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1688 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1693 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1698 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1703 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1708 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1713 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1718 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1725 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1730 glDisable(GL_TEXTURE_2D
);
1731 glDisable(GL_BLEND
);
1735 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1736 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1738 if e_NoGraphics
then Exit
;
1739 if Text = '' then Exit
;
1742 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1743 glEnable(GL_TEXTURE_2D
);
1744 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1746 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1751 glColor4ub(0, 0, 0, 128);
1752 glTranslated(x
+1, y
+1, 0);
1753 glScalef(Scale
, Scale
, 0);
1754 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1759 glColor4ub(Red
, Green
, Blue
, 255);
1760 glTranslated(x
, y
, 0);
1761 glScalef(Scale
, Scale
, 0);
1762 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1764 glDisable(GL_TEXTURE_2D
);
1766 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1767 glDisable(GL_BLEND
);
1770 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1774 if e_NoGraphics
then Exit
;
1775 if Integer(ID
) > High(e_TextureFonts
) then
1777 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1778 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1781 procedure e_RemoveAllTextureFont();
1785 if e_NoGraphics
then Exit
;
1786 if e_TextureFonts
= nil then Exit
;
1788 for i
:= 0 to High(e_TextureFonts
) do
1789 if e_TextureFonts
[i
].Base
<> 0 then
1791 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1792 e_TextureFonts
[i
].Base
:= 0;
1795 e_TextureFonts
:= nil;
1798 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1805 function _Point(X
, Y
: Integer): TPoint2i
;
1811 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1815 Result
.Width
:= Width
;
1816 Result
.Height
:= Height
;
1819 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1828 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1830 pixels
, obuf
, scln
, ps
, pd
: PByte;
1833 i
, x
, y
, res
: Integer;
1834 sign
: array [0..7] of Byte;
1835 hbuf
: array [0..12] of Byte;
1840 if e_NoGraphics
then Exit
;
1843 // first, extract and pack graphics data
1844 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1846 GetMem(pixels
, Width
*Height
*3);
1848 FillChar(pixels
^, Width
*Height
*3, 0);
1849 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1850 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1852 if e_FastScreenshots
then
1855 GetMem(scln
, (Width
*3+1)*Height
);
1859 Inc(ps
, (Width
*3)*(Height
-1));
1860 for i
:= 0 to Height
-1 do
1864 Move(ps
^, pd
^, Width
*3);
1876 obufsize
:= (Width
*3+1)*Height
*2;
1877 GetMem(obuf
, obufsize
);
1882 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1883 if res
= Z_OK
then break
;
1884 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1885 obufsize
:= obufsize
*2;
1888 GetMem(obuf
, obufsize
);
1890 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1903 st
.writeBuffer(sign
, 8);
1904 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1907 writeIntBE(st
, LongWord(13));
1912 st
.writeBuffer(sign
, 4);
1913 crc
:= crc32(0, @sign
[0], 4);
1916 hbuf
[2] := (Width
shr 8) and $ff;
1917 hbuf
[3] := Width
and $ff;
1920 hbuf
[6] := (Height
shr 8) and $ff;
1921 hbuf
[7] := Height
and $ff;
1922 hbuf
[8] := 8; // bit depth
1923 hbuf
[9] := 2; // RGB
1924 hbuf
[10] := 0; // compression method
1925 hbuf
[11] := 0; // filter method
1926 hbuf
[12] := 0; // no interlace
1927 crc
:= crc32(crc
, @hbuf
[0], 13);
1928 st
.writeBuffer(hbuf
, 13);
1929 writeIntBE(st
, crc
);
1930 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1933 writeIntBE(st
, LongWord(dlen
));
1938 st
.writeBuffer(sign
, 4);
1939 crc
:= crc32(0, @sign
[0], 4);
1940 crc
:= crc32(crc
, obuf
, dlen
);
1941 st
.writeBuffer(obuf
^, dlen
);
1942 writeIntBE(st
, crc
);
1943 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
1946 writeIntBE(st
, LongWord(0));
1951 st
.writeBuffer(sign
, 4);
1952 crc
:= crc32(0, @sign
[0], 4);
1953 writeIntBE(st
, crc
);
1954 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
1956 if obuf
<> nil then FreeMem(obuf
);
1961 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
1962 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
1965 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
1967 //writeln(stderr, 'moving pixels...');
1968 for y
:= Height
-1 downto 0 do
1970 for x
:= 0 to Width
-1 do
1972 clr
.r
:= ps
^; Inc(ps
);
1973 clr
.g
:= ps
^; Inc(ps
);
1974 clr
.b
:= ps
^; Inc(ps
);
1976 SetPixel32(img
, x
, y
, clr
);
1979 GlobalMetadata
.ClearMetaItems();
1980 GlobalMetadata
.ClearMetaItemsForSaving();
1981 //writeln(stderr, 'compressing image...');
1982 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
1983 //writeln(stderr, 'done!');