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
, yofs
: Integer;
719 u
, v
, cu
, cv
: Single;
723 procedure setScissorGLInternal (x, y, w, h: Integer);
725 //if not scallowed then exit;
730 y := vpxywh[3]-(y+h);
731 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
733 glScissor(0, 0, 0, 0);
737 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
738 glScissor(x, y, w, h);
744 if e_NoGraphics
then exit
;
746 if (wdt
< 1) or (hgt
< 1) then exit
;
748 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
750 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
);
754 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
756 if (Alpha
> 0) or AlphaChannel
or Blending
then
761 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
763 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
765 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
767 glEnable(GL_TEXTURE_2D
);
768 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
773 //k8: this SHOULD work... i hope
774 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
777 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
778 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
779 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
780 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
785 // hard day's night; setup scissor
787 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
788 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
789 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
790 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
791 //glEnable(GL_SCISSOR_TEST);
792 setScissorGLInternal(x, y, wdt, hgt);
795 u
:= e_Textures
[ID
].tx
.u
;
796 v
:= e_Textures
[ID
].tx
.v
;
797 w
:= e_Textures
[ID
].tx
.width
;
798 h
:= e_Textures
[ID
].tx
.height
;
800 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
804 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
805 if onlyOneY
then yofs
:= 0;
811 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
813 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
814 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
815 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
816 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
822 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
829 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
830 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= M_NONE
);
832 if e_NoGraphics
then Exit
;
834 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
836 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
841 if (AlphaChannel
) or (Alpha
> 0) then
842 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
845 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
848 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
850 if (Angle
<> 0) and (RC
<> nil) then
853 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
854 glRotatef(Angle
, 0, 0, 1);
855 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
858 glEnable(GL_TEXTURE_2D
);
859 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
860 glBegin(GL_QUADS
); //0-1 1-1
862 drawTxQuad(X
, Y
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
871 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
873 if e_NoGraphics
then Exit
;
874 glDisable(GL_TEXTURE_2D
);
875 glColor3ub(Red
, Green
, Blue
);
878 if (Size
= 2) or (Size
= 4) then
882 glVertex2f(X
+0.3, Y
+1.0);
885 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
888 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
890 // Make lines only top-left/bottom-right and top-right/bottom-left
902 // Pixel-perfect hack
910 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
912 nX1
, nY1
, nX2
, nY2
: Integer;
914 if e_NoGraphics
then Exit
;
915 // Only top-left/bottom-right quad
932 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
936 glDisable(GL_TEXTURE_2D
);
937 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
941 nX1
:= X1
; nY1
:= Y1
;
942 nX2
:= X2
; nY2
:= Y1
;
943 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
944 glVertex2i(nX1
, nY1
);
945 glVertex2i(nX2
, nY2
);
947 nX1
:= X2
; nY1
:= Y1
;
948 nX2
:= X2
; nY2
:= Y2
;
949 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
950 glVertex2i(nX1
, nY1
);
951 glVertex2i(nX2
, nY2
);
953 nX1
:= X2
; nY1
:= Y2
;
954 nX2
:= X1
; nY2
:= Y2
;
955 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
956 glVertex2i(nX1
, nY1
);
957 glVertex2i(nX2
, nY2
);
959 nX1
:= X1
; nY1
:= Y2
;
960 nX2
:= X1
; nY2
:= Y1
;
961 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
962 glVertex2i(nX1
, nY1
);
963 glVertex2i(nX2
, nY2
);
966 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
971 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
972 Blending
: TBlending
= B_NONE
);
974 if e_NoGraphics
then Exit
;
975 if (Alpha
> 0) or (Blending
<> B_NONE
) then
980 if Blending
= B_BLEND
then
981 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
983 if Blending
= B_FILTER
then
984 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
986 if Blending
= B_INVERT
then
987 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
990 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
992 glDisable(GL_TEXTURE_2D
);
993 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1005 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1007 glDisable(GL_BLEND
);
1011 // ////////////////////////////////////////////////////////////////////////// //
1012 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1014 if (a
< 0) then a
:= 0;
1015 if (a
> 255) then a
:= 255;
1017 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1018 glDisable(GL_TEXTURE_2D
);
1019 glColor4ub(0, 0, 0, Byte(255-a
));
1026 //glRect(x, y, x+w, y+h);
1027 glColor4ub(1, 1, 1, 1);
1028 glDisable(GL_BLEND
);
1029 //glBlendEquation(GL_FUNC_ADD);
1032 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1034 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1038 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1040 if e_NoGraphics
then Exit
;
1041 // Pixel-perfect lines
1043 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1048 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1050 glDisable(GL_BLEND
);
1052 glDisable(GL_TEXTURE_2D
);
1053 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1061 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1063 glDisable(GL_BLEND
);
1066 //------------------------------------------------------------------
1067 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1068 //------------------------------------------------------------------
1069 procedure e_DeleteTexture(ID
: DWORD
);
1071 if not e_NoGraphics
then
1072 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1073 e_Textures
[ID
].tx
.id
:= 0;
1074 e_Textures
[ID
].tx
.Width
:= 0;
1075 e_Textures
[ID
].tx
.Height
:= 0;
1078 //------------------------------------------------------------------
1079 // Óäàëÿåò âñå òåêñòóðû
1080 //------------------------------------------------------------------
1081 procedure e_RemoveAllTextures();
1085 if e_Textures
= nil then Exit
;
1087 for i
:= 0 to High(e_Textures
) do
1088 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1092 //------------------------------------------------------------------
1094 //------------------------------------------------------------------
1095 procedure e_ReleaseEngine();
1097 e_RemoveAllTextures
;
1098 e_RemoveAllTextureFont
;
1101 procedure e_BeginRender();
1103 if e_NoGraphics
then Exit
;
1104 glEnable(GL_ALPHA_TEST
);
1105 glAlphaFunc(GL_GREATER
, 0.0);
1108 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1110 if e_NoGraphics
then Exit
;
1111 glClearColor(Red
, Green
, Blue
, 0);
1115 procedure e_Clear(); overload
;
1117 if e_NoGraphics
then Exit
;
1118 glClearColor(0, 0, 0, 0);
1119 glClear(GL_COLOR_BUFFER_BIT
);
1122 procedure e_EndRender();
1124 if e_NoGraphics
then Exit
;
1128 function e_GetGamma(win
: PSDL_Window
): Byte;
1130 ramp
: array [0..256*3-1] of Word;
1131 rgb
: array [0..2] of Double;
1140 if e_NoGraphics
then Exit
;
1145 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1154 for j
:= min
to max
- 1 do
1157 B
:= (j
mod 256)/256;
1159 sum
:= sum
+ ln(A
)/ln(B
);
1162 rgb
[i
] := sum
/ count
;
1165 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1168 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1170 ramp
: array [0..256*3-1] of Word;
1175 if e_NoGraphics
then Exit
;
1176 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1178 for i
:= 0 to 255 do
1180 r
:= Exp(g
* ln(i
/256))*65536;
1181 if r
< 0 then r
:= 0
1182 else if r
> 65535 then r
:= 65535;
1183 ramp
[i
] := trunc(r
);
1184 ramp
[i
+ 256] := trunc(r
);
1185 ramp
[i
+ 512] := trunc(r
);
1188 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1191 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1195 e_WriteLog('Creating CharFont...', MSG_NOTIFY
);
1199 if e_CharFonts
<> nil then
1200 for i
:= 0 to High(e_CharFonts
) do
1201 if not e_CharFonts
[i
].alive
then
1207 if id
= DWORD(-1) then
1209 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1210 id
:= High(e_CharFonts
);
1213 with e_CharFonts
[id
] do
1215 for i
:= 0 to High(Chars
) do
1229 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1231 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1233 TextureID
:= Texture
;
1238 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1242 if e_NoGraphics
then Exit
;
1243 if Text = '' then Exit
;
1244 if e_CharFonts
= nil then Exit
;
1245 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1247 with e_CharFonts
[FontID
] do
1249 for a
:= 1 to Length(Text) do
1250 with Chars
[Ord(Text[a
])] do
1251 if TextureID
<> -1 then
1253 e_Draw(TextureID
, X
, Y
, 0, True, False);
1254 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1259 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1260 Color
: TRGB
; Scale
: Single = 1.0);
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 if Scale
<> 1.0 then
1279 glScalef(Scale
, Scale
, 0);
1284 e_Draw(TextureID
, X
, Y
, 0, True, False);
1287 if Scale
<> 1.0 then glPopMatrix
;
1289 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1294 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1296 a
, TX
, TY
, len
: Integer;
1300 if e_NoGraphics
then Exit
;
1301 if Text = '' then Exit
;
1302 if e_CharFonts
= nil then Exit
;
1303 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1311 len
:= Length(Text);
1313 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1315 with e_CharFonts
[FontID
] do
1317 for a
:= 1 to len
do
1328 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1333 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1338 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1343 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1348 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1353 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1358 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1363 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1368 with Chars
[Ord(Text[a
])] do
1369 if TextureID
<> -1 then
1373 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1376 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1382 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1390 if Text = '' then Exit
;
1391 if e_CharFonts
= nil then Exit
;
1392 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1394 with e_CharFonts
[FontID
] do
1396 for a
:= 1 to Length(Text) do
1397 with Chars
[Ord(Text[a
])] do
1398 if TextureID
<> -1 then
1400 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1401 e_GetTextureSize(TextureID
, nil, @h2
);
1402 if h2
> h
then h
:= h2
;
1407 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1409 a
, lines
, len
: Integer;
1416 if Text = '' then Exit
;
1417 if e_CharFonts
= nil then Exit
;
1418 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1421 len
:= Length(Text);
1423 with e_CharFonts
[FontID
] do
1425 for a
:= 1 to len
do
1427 if Text[a
] = #10 then
1437 else if Text[a
] in [#1, #2, #3, #4, #18, #19, #20, #21] then
1440 with Chars
[Ord(Text[a
])] do
1441 if TextureID
<> -1 then
1443 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1444 e_GetTextureSize(TextureID
, nil, @h2
);
1445 if h2
> h
then h
:= h2
;
1455 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1461 if e_CharFonts
= nil then Exit
;
1462 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1464 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1465 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1468 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1475 if e_CharFonts
= nil then Exit
;
1476 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1478 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1480 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1481 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1483 if h2
> Result
then Result
:= h2
;
1487 procedure e_CharFont_Remove(FontID
: DWORD
);
1491 with e_CharFonts
[FontID
] do
1492 for a
:= 0 to High(Chars
) do
1493 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1495 e_CharFonts
[FontID
].alive
:= False;
1498 procedure e_CharFont_RemoveAll();
1502 if e_CharFonts
= nil then Exit
;
1504 for a
:= 0 to High(e_CharFonts
) do
1505 e_CharFont_Remove(a
);
1510 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1517 if e_NoGraphics
then Exit
;
1518 e_WriteLog('Creating texture font...', MSG_NOTIFY
);
1522 if e_TextureFonts
<> nil then
1523 for i
:= 0 to High(e_TextureFonts
) do
1524 if e_TextureFonts
[i
].Base
= 0 then
1530 if id
= DWORD(-1) then
1532 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1533 id
:= High(e_TextureFonts
);
1536 with e_TextureFonts
[id
] do
1538 Base
:= glGenLists(XCount
*YCount
);
1539 TextureID
:= e_Textures
[Tex
].tx
.id
;
1540 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1541 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1548 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1549 for loop1
:= 0 to XCount
*YCount
-1 do
1551 cx
:= (loop1
mod XCount
)/XCount
;
1552 cy
:= (loop1
div YCount
)/YCount
;
1554 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1556 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1557 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1559 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1560 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1562 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1563 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1565 glTexCoord2f(cx
, 1.0-cy
);
1568 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1575 procedure e_TextureFontKill(FontID
: DWORD
);
1577 if e_NoGraphics
then Exit
;
1578 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1579 e_TextureFonts
[FontID
].Base
:= 0;
1582 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1584 if e_NoGraphics
then Exit
;
1585 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1586 if Text = '' then Exit
;
1588 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1591 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1594 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1595 glEnable(GL_TEXTURE_2D
);
1596 glTranslated(x
, y
, 0);
1597 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1598 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1599 glDisable(GL_TEXTURE_2D
);
1602 glDisable(GL_BLEND
);
1605 // god forgive me for this, but i cannot figure out how to do it without lists
1606 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1608 if e_NoGraphics
then Exit
;
1613 glColor4ub(0, 0, 0, 128);
1614 glTranslated(X
+1, Y
+1, 0);
1615 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1620 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1621 glTranslated(X
, Y
, 0);
1622 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1627 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1629 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1630 glEnable(GL_TEXTURE_2D
);
1631 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1633 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1635 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1636 glDisable(GL_TEXTURE_2D
);
1637 glDisable(GL_BLEND
);
1640 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1642 result
:= e_TextureFonts
[FontID
].CharWidth
;
1645 procedure e_TextureFontPrintFmt(X
, Y
: Integer; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1647 a
, TX
, TY
, len
: Integer;
1651 if e_NoGraphics
then Exit
;
1652 if Text = '' then Exit
;
1653 if e_TextureFonts
= nil then Exit
;
1654 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1662 len
:= Length(Text);
1664 w
:= e_TextureFonts
[FontID
].CharWidth
;
1666 with e_TextureFonts
[FontID
] do
1668 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1669 glEnable(GL_TEXTURE_2D
);
1670 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1672 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1675 for a
:= 1 to len
do
1686 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1691 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1696 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1701 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1706 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1711 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1716 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1721 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1728 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1733 glDisable(GL_TEXTURE_2D
);
1734 glDisable(GL_BLEND
);
1738 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1739 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1741 if e_NoGraphics
then Exit
;
1742 if Text = '' then Exit
;
1745 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1746 glEnable(GL_TEXTURE_2D
);
1747 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1749 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1754 glColor4ub(0, 0, 0, 128);
1755 glTranslated(x
+1, y
+1, 0);
1756 glScalef(Scale
, Scale
, 0);
1757 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1762 glColor4ub(Red
, Green
, Blue
, 255);
1763 glTranslated(x
, y
, 0);
1764 glScalef(Scale
, Scale
, 0);
1765 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1767 glDisable(GL_TEXTURE_2D
);
1769 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1770 glDisable(GL_BLEND
);
1773 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1777 if e_NoGraphics
then Exit
;
1778 if Integer(ID
) > High(e_TextureFonts
) then
1780 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1781 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1784 procedure e_RemoveAllTextureFont();
1788 if e_NoGraphics
then Exit
;
1789 if e_TextureFonts
= nil then Exit
;
1791 for i
:= 0 to High(e_TextureFonts
) do
1792 if e_TextureFonts
[i
].Base
<> 0 then
1794 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1795 e_TextureFonts
[i
].Base
:= 0;
1798 e_TextureFonts
:= nil;
1801 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1808 function _Point(X
, Y
: Integer): TPoint2i
;
1814 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1818 Result
.Width
:= Width
;
1819 Result
.Height
:= Height
;
1822 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1831 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1833 pixels
, obuf
, scln
, ps
, pd
: PByte;
1836 i
, x
, y
, res
: Integer;
1837 sign
: array [0..7] of Byte;
1838 hbuf
: array [0..12] of Byte;
1843 if e_NoGraphics
then Exit
;
1846 // first, extract and pack graphics data
1847 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1849 GetMem(pixels
, Width
*Height
*3);
1851 FillChar(pixels
^, Width
*Height
*3, 0);
1852 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1853 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1855 if e_FastScreenshots
then
1858 GetMem(scln
, (Width
*3+1)*Height
);
1862 Inc(ps
, (Width
*3)*(Height
-1));
1863 for i
:= 0 to Height
-1 do
1867 Move(ps
^, pd
^, Width
*3);
1879 obufsize
:= (Width
*3+1)*Height
*2;
1880 GetMem(obuf
, obufsize
);
1885 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1886 if res
= Z_OK
then break
;
1887 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1888 obufsize
:= obufsize
*2;
1891 GetMem(obuf
, obufsize
);
1893 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1906 st
.writeBuffer(sign
, 8);
1907 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1910 writeIntBE(st
, LongWord(13));
1915 st
.writeBuffer(sign
, 4);
1916 crc
:= crc32(0, @sign
[0], 4);
1919 hbuf
[2] := (Width
shr 8) and $ff;
1920 hbuf
[3] := Width
and $ff;
1923 hbuf
[6] := (Height
shr 8) and $ff;
1924 hbuf
[7] := Height
and $ff;
1925 hbuf
[8] := 8; // bit depth
1926 hbuf
[9] := 2; // RGB
1927 hbuf
[10] := 0; // compression method
1928 hbuf
[11] := 0; // filter method
1929 hbuf
[12] := 0; // no interlace
1930 crc
:= crc32(crc
, @hbuf
[0], 13);
1931 st
.writeBuffer(hbuf
, 13);
1932 writeIntBE(st
, crc
);
1933 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1936 writeIntBE(st
, LongWord(dlen
));
1941 st
.writeBuffer(sign
, 4);
1942 crc
:= crc32(0, @sign
[0], 4);
1943 crc
:= crc32(crc
, obuf
, dlen
);
1944 st
.writeBuffer(obuf
^, dlen
);
1945 writeIntBE(st
, crc
);
1946 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
1949 writeIntBE(st
, LongWord(0));
1954 st
.writeBuffer(sign
, 4);
1955 crc
:= crc32(0, @sign
[0], 4);
1956 writeIntBE(st
, crc
);
1957 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
1959 if obuf
<> nil then FreeMem(obuf
);
1964 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
1965 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
1968 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
1970 //writeln(stderr, 'moving pixels...');
1971 for y
:= Height
-1 downto 0 do
1973 for x
:= 0 to Width
-1 do
1975 clr
.r
:= ps
^; Inc(ps
);
1976 clr
.g
:= ps
^; Inc(ps
);
1977 clr
.b
:= ps
^; Inc(ps
);
1979 SetPixel32(img
, x
, y
, clr
);
1982 GlobalMetadata
.ClearMetaItems();
1983 GlobalMetadata
.ClearMetaItemsForSaving();
1984 //writeln(stderr, 'compressing image...');
1985 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
1986 //writeln(stderr, 'done!');