6362cd11437986e6eba5c5da168560e11145f06c
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
=(None
, Horizontal
, Vertical
);
26 TBlending
=(None
, Blend
, Filter
, 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
= TMirrorType
.None
);
64 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
65 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
66 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
67 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
68 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
69 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
71 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
72 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
74 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
75 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
77 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
79 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
80 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
81 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
82 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
83 Blending
: TBlending
= TBlending
.None
);
84 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
85 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
87 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
88 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
89 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
90 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
91 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
92 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
93 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
94 procedure e_DeleteTexture(ID
: DWORD
);
95 procedure e_RemoveAllTextures();
98 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
99 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
100 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
101 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
102 Color
: TRGB
; Scale
: Single = 1.0);
103 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
104 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
105 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
106 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
107 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
108 procedure e_CharFont_Remove(FontID
: DWORD
);
109 procedure e_CharFont_RemoveAll();
112 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
114 procedure e_TextureFontKill(FontID
: DWORD
);
115 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
116 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
117 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
118 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
119 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
120 procedure e_RemoveAllTextureFont();
122 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
123 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
125 procedure e_ReleaseEngine();
126 procedure e_BeginRender();
127 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
128 procedure e_Clear(); overload
;
129 procedure e_EndRender();
131 function e_GetGamma(win
: PSDL_Window
): Byte;
132 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
134 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
136 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
137 function _Point(X
, Y
: Integer): TPoint2i
;
138 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
139 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
141 //function e_getTextGLId (ID: DWORD): GLuint;
145 e_NoGraphics
: Boolean = False;
146 e_FastScreenshots
: Boolean = true; // it's REALLY SLOW with `false`
147 g_dbg_scale
: Single = 1.0;
161 TTextureFont
= record
171 Chars
: array[0..255] of
181 TSavedTexture
= record
188 e_Textures
: array of TTexture
= nil;
189 e_TextureFonts
: array of TTextureFont
= nil;
190 e_CharFonts
: array of TCharFont
;
191 //e_SavedTextures: array of TSavedTexture;
193 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
195 //------------------------------------------------------------------
196 // Èíèöèàëèçèðóåò OpenGL
197 //------------------------------------------------------------------
198 procedure e_InitGL();
202 e_DummyTextures
:= True;
208 glDisable(GL_DEPTH_TEST
);
209 glEnable(GL_SCISSOR_TEST
);
210 glClearColor(0, 0, 0, 0);
213 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
215 mat
: Array [0..15] of GLDouble
;
218 if e_NoGraphics
then Exit
;
220 glScissor(X
, Y
, Width
, Height
);
221 glViewport(X
, Y
, Width
, Height
);
222 //gluOrtho2D(0, Width, Height, 0);
224 glMatrixMode(GL_PROJECTION
);
226 mat
[ 0] := 2.0 / Width
;
232 mat
[ 5] := -2.0 / Height
;
246 glLoadMatrixd(@mat
[0]);
248 glMatrixMode(GL_MODELVIEW
);
252 //------------------------------------------------------------------
253 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
254 //------------------------------------------------------------------
255 function FindTexture(): DWORD
;
259 if e_Textures
<> nil then
260 for i
:= 0 to High(e_Textures
) do
261 if e_Textures
[i
].tx
.Width
= 0 then
267 if e_Textures
= nil then
269 SetLength(e_Textures
, 32);
274 Result
:= High(e_Textures
) + 1;
275 SetLength(e_Textures
, Length(e_Textures
) + 32);
279 //------------------------------------------------------------------
281 //------------------------------------------------------------------
282 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
289 e_WriteLog('Loading texture from '+FileName
, TMsgType
.Notify
);
291 find_id
:= FindTexture();
293 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
294 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
301 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
308 find_id
:= FindTexture();
310 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
317 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
324 find_id
:= FindTexture
;
326 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
;
333 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
340 find_id
:= FindTexture();
342 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
349 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
355 find_id
:= FindTexture();
356 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
361 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
363 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
364 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
367 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
375 w
:= e_Textures
[ID
].tx
.Width
;
376 h
:= e_Textures
[ID
].tx
.Height
;
383 if e_NoGraphics
then Exit
;
385 data
:= GetMemory(w
*h
*4);
386 glEnable(GL_TEXTURE_2D
);
387 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
388 glGetTexImage(GL_TEXTURE_2D
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
390 for y
:= h
-1 downto 0 do
397 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
403 Result
.Y
:= h
-lastline
;
415 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
421 Result
.Height
:= h
-lastline
-Result
.Y
;
433 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
439 Result
.X
:= lastline
+1;
444 for x
:= w
-1 downto 0 do
451 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
457 Result
.Width
:= lastline
-Result
.X
+1;
465 procedure e_ResizeWindow(Width
, Height
: Integer);
469 e_SetViewPort(0, 0, Width
, Height
);
472 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
474 x1
, y1
, tmp
: Integer;
476 if (w
< 1) or (h
< 1) then exit
;
479 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
480 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
481 //HACK: make texture one pixel shorter, so it won't wrap
482 if (g_dbg_scale
<> 1.0) then
487 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
488 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
489 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
490 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
493 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
494 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
496 if e_NoGraphics
then Exit
;
497 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
499 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
504 if (AlphaChannel
) or (Alpha
> 0) then
505 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
508 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
511 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
513 glEnable(GL_TEXTURE_2D
);
514 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
517 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
);
519 //u := e_Textures[ID].tx.u;
520 //v := e_Textures[ID].tx.v;
523 if Mirror = M_NONE then
525 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
526 glTexCoord2f(0, 0); glVertex2i(X, Y);
527 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
528 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
531 if Mirror = M_HORIZONTAL then
533 glTexCoord2f(u, 0); glVertex2i(X, Y);
534 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
535 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
536 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
539 if Mirror = M_VERTICAL then
541 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
542 glTexCoord2f(0, -v); glVertex2i(X, Y);
543 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
544 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
553 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
554 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
558 if e_NoGraphics
then Exit
;
559 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
561 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
566 if (AlphaChannel
) or (Alpha
> 0) then
567 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
570 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
573 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
575 glEnable(GL_TEXTURE_2D
);
576 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
578 u
:= e_Textures
[ID
].tx
.u
;
579 v
:= e_Textures
[ID
].tx
.v
;
582 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
583 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
584 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
585 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
591 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
592 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
594 if e_NoGraphics
then Exit
;
595 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
597 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
602 if (AlphaChannel
) or (Alpha
> 0) then
603 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
606 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
609 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
611 glEnable(GL_TEXTURE_2D
);
612 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
614 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
);
620 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
621 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
623 X2
, Y2
, dx
, w
, h
: Integer;
626 if e_NoGraphics
then Exit
;
627 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
628 ambientBlendMode
:= false;
630 if (Alpha
> 0) or AlphaChannel
or Blending
then
636 if not ambientBlendMode
then glDisable(GL_BLEND
);
638 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
639 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
640 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
642 if (XCount
= 0) then XCount
:= 1;
643 if (YCount
= 0) then YCount
:= 1;
645 glEnable(GL_TEXTURE_2D
);
646 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
648 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
649 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
651 //k8: this SHOULD work... i hope
652 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
655 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
656 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
657 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
658 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
665 u
:= e_Textures
[ID
].tx
.u
;
666 v
:= e_Textures
[ID
].tx
.v
;
667 w
:= e_Textures
[ID
].tx
.width
;
668 h
:= e_Textures
[ID
].tx
.height
;
675 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
676 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
677 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
678 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
693 //TODO: overflow checks
694 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
699 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
700 // check for intersection
701 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
702 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
706 if (x0
< x1
) then x0
:= x1
;
707 if (y0
< y1
) then y0
:= y1
;
708 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
709 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
712 result
:= (w0
> 0) and (h0
> 0);
716 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
717 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
722 scxywh: array[0..3] of GLint;
723 vpxywh: array[0..3] of GLint;
725 w
, h
, dw
, cw
, ch
, yofs
: Integer;
726 u
, v
, cu
, cv
: Single;
730 procedure setScissorGLInternal (x, y, w, h: Integer);
732 //if not scallowed then exit;
737 y := vpxywh[3]-(y+h);
738 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
740 glScissor(0, 0, 0, 0);
744 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
745 glScissor(x, y, w, h);
751 if e_NoGraphics
then exit
;
752 ambientBlendMode
:= false;
754 if (wdt
< 1) or (hgt
< 1) then exit
;
756 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
758 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
762 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
764 if (Alpha
> 0) or AlphaChannel
or Blending
then
770 if not ambientBlendMode
then glDisable(GL_BLEND
);
772 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
773 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
774 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
776 glEnable(GL_TEXTURE_2D
);
777 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
782 //k8: this SHOULD work... i hope
783 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
786 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
787 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
788 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
789 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
794 // hard day's night; setup scissor
796 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
797 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
798 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
799 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
800 //glEnable(GL_SCISSOR_TEST);
801 setScissorGLInternal(x, y, wdt, hgt);
804 u
:= e_Textures
[ID
].tx
.u
;
805 v
:= e_Textures
[ID
].tx
.v
;
806 w
:= e_Textures
[ID
].tx
.width
;
807 h
:= e_Textures
[ID
].tx
.height
;
809 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
813 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
814 if onlyOneY
then yofs
:= 0;
820 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
822 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
823 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
824 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
825 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
831 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
838 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
840 if e_NoGraphics
then exit
;
841 if (w
< 1) or (h
< 1) then exit
;
842 if (a
<> 255) or ((r
or g
or b
) <> 0) then
845 glDisable(GL_TEXTURE_2D
);
846 glColor4ub(r
, g
, b
, a
);
847 if ((r
or g
or b
) <> 0) then
849 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
853 glVertex2i(x
+w
, y
+h
);
857 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
861 glVertex2i(x
+w
, y
+h
);
869 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
870 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
872 if e_NoGraphics
then Exit
;
874 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
876 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
881 if (AlphaChannel
) or (Alpha
> 0) then
882 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
885 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
888 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
890 if (Angle
<> 0) and (RC
<> nil) then
893 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
894 glRotatef(Angle
, 0, 0, 1);
895 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
898 glEnable(GL_TEXTURE_2D
);
899 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
900 glBegin(GL_QUADS
); //0-1 1-1
902 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
);
911 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
913 if e_NoGraphics
then Exit
;
914 glDisable(GL_TEXTURE_2D
);
915 glColor3ub(Red
, Green
, Blue
);
918 if (Size
= 2) or (Size
= 4) then
922 glVertex2f(X
+0.3, Y
+1.0);
925 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
928 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
930 // Make lines only top-left/bottom-right and top-right/bottom-left
942 // Pixel-perfect hack
950 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
952 nX1
, nY1
, nX2
, nY2
: Integer;
954 if e_NoGraphics
then Exit
;
955 // Only top-left/bottom-right quad
972 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
976 glDisable(GL_TEXTURE_2D
);
977 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
981 nX1
:= X1
; nY1
:= Y1
;
982 nX2
:= X2
; nY2
:= Y1
;
983 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
984 glVertex2i(nX1
, nY1
);
985 glVertex2i(nX2
, nY2
);
987 nX1
:= X2
; nY1
:= Y1
;
988 nX2
:= X2
; nY2
:= Y2
;
989 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
990 glVertex2i(nX1
, nY1
);
991 glVertex2i(nX2
, nY2
);
993 nX1
:= X2
; nY1
:= Y2
;
994 nX2
:= X1
; nY2
:= Y2
;
995 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
996 glVertex2i(nX1
, nY1
);
997 glVertex2i(nX2
, nY2
);
999 nX1
:= X1
; nY1
:= Y2
;
1000 nX2
:= X1
; nY2
:= Y1
;
1001 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
1002 glVertex2i(nX1
, nY1
);
1003 glVertex2i(nX2
, nY2
);
1006 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1008 glDisable(GL_BLEND
);
1011 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
1012 Blending
: TBlending
= TBlending
.None
);
1014 if e_NoGraphics
then Exit
;
1015 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
1018 glDisable(GL_BLEND
);
1020 if Blending
= TBlending
.Blend
then
1021 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
1023 if Blending
= TBlending
.Filter
then
1024 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
1026 if Blending
= TBlending
.Invert
then
1027 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
1030 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1032 glDisable(GL_TEXTURE_2D
);
1033 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1045 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1047 glDisable(GL_BLEND
);
1051 // ////////////////////////////////////////////////////////////////////////// //
1052 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
1054 if (a
< 0) then a
:= 0;
1055 if (a
> 255) then a
:= 255;
1057 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1058 glDisable(GL_TEXTURE_2D
);
1059 glColor4ub(0, 0, 0, Byte(255-a
));
1066 //glRect(x, y, x+w, y+h);
1067 glColor4ub(1, 1, 1, 1);
1068 glDisable(GL_BLEND
);
1069 //glBlendEquation(GL_FUNC_ADD);
1072 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1074 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1078 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1080 if e_NoGraphics
then Exit
;
1081 // Pixel-perfect lines
1083 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1088 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1090 glDisable(GL_BLEND
);
1092 glDisable(GL_TEXTURE_2D
);
1093 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1101 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1103 glDisable(GL_BLEND
);
1106 //------------------------------------------------------------------
1107 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1108 //------------------------------------------------------------------
1109 procedure e_DeleteTexture(ID
: DWORD
);
1111 if not e_NoGraphics
then
1112 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1113 e_Textures
[ID
].tx
.id
:= 0;
1114 e_Textures
[ID
].tx
.Width
:= 0;
1115 e_Textures
[ID
].tx
.Height
:= 0;
1118 //------------------------------------------------------------------
1119 // Óäàëÿåò âñå òåêñòóðû
1120 //------------------------------------------------------------------
1121 procedure e_RemoveAllTextures();
1125 if e_Textures
= nil then Exit
;
1127 for i
:= 0 to High(e_Textures
) do
1128 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1132 //------------------------------------------------------------------
1134 //------------------------------------------------------------------
1135 procedure e_ReleaseEngine();
1137 e_RemoveAllTextures
;
1138 e_RemoveAllTextureFont
;
1141 procedure e_BeginRender();
1143 if e_NoGraphics
then Exit
;
1144 glEnable(GL_ALPHA_TEST
);
1145 glAlphaFunc(GL_GREATER
, 0.0);
1148 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1150 if e_NoGraphics
then Exit
;
1151 glClearColor(Red
, Green
, Blue
, 0);
1155 procedure e_Clear(); overload
;
1157 if e_NoGraphics
then Exit
;
1158 glClearColor(0, 0, 0, 0);
1159 glClear(GL_COLOR_BUFFER_BIT
);
1162 procedure e_EndRender();
1164 if e_NoGraphics
then Exit
;
1168 function e_GetGamma(win
: PSDL_Window
): Byte;
1170 ramp
: array [0..256*3-1] of Word;
1171 rgb
: array [0..2] of Double;
1180 if e_NoGraphics
then Exit
;
1185 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1194 for j
:= min
to max
- 1 do
1197 B
:= (j
mod 256)/256;
1199 sum
:= sum
+ ln(A
)/ln(B
);
1202 rgb
[i
] := sum
/ count
;
1205 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1208 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1210 ramp
: array [0..256*3-1] of Word;
1215 if e_NoGraphics
then Exit
;
1216 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1218 for i
:= 0 to 255 do
1220 r
:= Exp(g
* ln(i
/256))*65536;
1221 if r
< 0 then r
:= 0
1222 else if r
> 65535 then r
:= 65535;
1223 ramp
[i
] := trunc(r
);
1224 ramp
[i
+ 256] := trunc(r
);
1225 ramp
[i
+ 512] := trunc(r
);
1228 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1231 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1235 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1239 if e_CharFonts
<> nil then
1240 for i
:= 0 to High(e_CharFonts
) do
1241 if not e_CharFonts
[i
].alive
then
1247 if id
= DWORD(-1) then
1249 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1250 id
:= High(e_CharFonts
);
1253 with e_CharFonts
[id
] do
1255 for i
:= 0 to High(Chars
) do
1269 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1271 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1273 TextureID
:= Texture
;
1278 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1282 if e_NoGraphics
then Exit
;
1283 if Text = '' then Exit
;
1284 if e_CharFonts
= nil then Exit
;
1285 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1287 with e_CharFonts
[FontID
] do
1289 for a
:= 1 to Length(Text) do
1290 with Chars
[Ord(Text[a
])] do
1291 if TextureID
<> -1 then
1293 e_Draw(TextureID
, X
, Y
, 0, True, False);
1294 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1299 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1300 Color
: TRGB
; Scale
: Single = 1.0);
1305 if e_NoGraphics
then Exit
;
1306 if Text = '' then Exit
;
1307 if e_CharFonts
= nil then Exit
;
1308 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1310 with e_CharFonts
[FontID
] do
1312 for a
:= 1 to Length(Text) do
1313 with Chars
[Ord(Text[a
])] do
1314 if TextureID
<> -1 then
1316 if Scale
<> 1.0 then
1319 glScalef(Scale
, Scale
, 0);
1324 e_Draw(TextureID
, X
, Y
, 0, True, False);
1327 if Scale
<> 1.0 then glPopMatrix
;
1329 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1334 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1336 a
, TX
, TY
, len
: Integer;
1340 if e_NoGraphics
then Exit
;
1341 if Text = '' then Exit
;
1342 if e_CharFonts
= nil then Exit
;
1343 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1351 len
:= Length(Text);
1353 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1355 with e_CharFonts
[FontID
] do
1357 for a
:= 1 to len
do
1368 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1373 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1378 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1383 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1388 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1393 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1398 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1403 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1408 with Chars
[Ord(Text[a
])] do
1409 if TextureID
<> -1 then
1413 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1416 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1422 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1430 if Text = '' then Exit
;
1431 if e_CharFonts
= nil then Exit
;
1432 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1434 with e_CharFonts
[FontID
] do
1436 for a
:= 1 to Length(Text) do
1437 with Chars
[Ord(Text[a
])] do
1438 if TextureID
<> -1 then
1440 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1441 e_GetTextureSize(TextureID
, nil, @h2
);
1442 if h2
> h
then h
:= h2
;
1447 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1449 a
, lines
, len
: Integer;
1456 if Text = '' then Exit
;
1457 if e_CharFonts
= nil then Exit
;
1458 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1461 len
:= Length(Text);
1463 with e_CharFonts
[FontID
] do
1465 for a
:= 1 to len
do
1467 if Text[a
] = #10 then
1477 else if Text[a
] in [#1, #2, #3, #4, #18, #19, #20, #21] then
1480 with Chars
[Ord(Text[a
])] do
1481 if TextureID
<> -1 then
1483 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1484 e_GetTextureSize(TextureID
, nil, @h2
);
1485 if h2
> h
then h
:= h2
;
1495 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1501 if e_CharFonts
= nil then Exit
;
1502 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1504 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1505 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1508 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1515 if e_CharFonts
= nil then Exit
;
1516 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1518 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1520 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1521 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1523 if h2
> Result
then Result
:= h2
;
1527 procedure e_CharFont_Remove(FontID
: DWORD
);
1531 with e_CharFonts
[FontID
] do
1532 for a
:= 0 to High(Chars
) do
1533 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1535 e_CharFonts
[FontID
].alive
:= False;
1538 procedure e_CharFont_RemoveAll();
1542 if e_CharFonts
= nil then Exit
;
1544 for a
:= 0 to High(e_CharFonts
) do
1545 e_CharFont_Remove(a
);
1550 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1557 if e_NoGraphics
then Exit
;
1558 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1562 if e_TextureFonts
<> nil then
1563 for i
:= 0 to High(e_TextureFonts
) do
1564 if e_TextureFonts
[i
].Base
= 0 then
1570 if id
= DWORD(-1) then
1572 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1573 id
:= High(e_TextureFonts
);
1576 with e_TextureFonts
[id
] do
1578 Base
:= glGenLists(XCount
*YCount
);
1579 TextureID
:= e_Textures
[Tex
].tx
.id
;
1580 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1581 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1588 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1589 for loop1
:= 0 to XCount
*YCount
-1 do
1591 cx
:= (loop1
mod XCount
)/XCount
;
1592 cy
:= (loop1
div YCount
)/YCount
;
1594 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1596 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1597 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1599 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1600 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1602 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1603 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1605 glTexCoord2f(cx
, 1.0-cy
);
1608 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1615 procedure e_TextureFontKill(FontID
: DWORD
);
1617 if e_NoGraphics
then Exit
;
1618 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1619 e_TextureFonts
[FontID
].Base
:= 0;
1622 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1624 if e_NoGraphics
then Exit
;
1625 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1626 if Text = '' then Exit
;
1628 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1631 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1634 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1635 glEnable(GL_TEXTURE_2D
);
1636 glTranslated(x
, y
, 0);
1637 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1638 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1639 glDisable(GL_TEXTURE_2D
);
1642 glDisable(GL_BLEND
);
1645 // god forgive me for this, but i cannot figure out how to do it without lists
1646 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1648 if e_NoGraphics
then Exit
;
1653 glColor4ub(0, 0, 0, 128);
1654 glTranslated(X
+1, Y
+1, 0);
1655 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1660 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1661 glTranslated(X
, Y
, 0);
1662 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1667 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1669 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1670 glEnable(GL_TEXTURE_2D
);
1671 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1673 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1675 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1676 glDisable(GL_TEXTURE_2D
);
1677 glDisable(GL_BLEND
);
1680 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1682 result
:= e_TextureFonts
[FontID
].CharWidth
;
1685 procedure e_TextureFontPrintFmt(X
, Y
: Integer; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1687 a
, TX
, TY
, len
: Integer;
1691 if e_NoGraphics
then Exit
;
1692 if Text = '' then Exit
;
1693 if e_TextureFonts
= nil then Exit
;
1694 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1702 len
:= Length(Text);
1704 w
:= e_TextureFonts
[FontID
].CharWidth
;
1706 with e_TextureFonts
[FontID
] do
1708 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1709 glEnable(GL_TEXTURE_2D
);
1710 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1712 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1715 for a
:= 1 to len
do
1726 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1731 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1736 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1741 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1746 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1751 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1756 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1761 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1768 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1773 glDisable(GL_TEXTURE_2D
);
1774 glDisable(GL_BLEND
);
1778 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1779 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1781 if e_NoGraphics
then Exit
;
1782 if Text = '' then Exit
;
1785 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1786 glEnable(GL_TEXTURE_2D
);
1787 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1789 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1794 glColor4ub(0, 0, 0, 128);
1795 glTranslated(x
+1, y
+1, 0);
1796 glScalef(Scale
, Scale
, 0);
1797 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1802 glColor4ub(Red
, Green
, Blue
, 255);
1803 glTranslated(x
, y
, 0);
1804 glScalef(Scale
, Scale
, 0);
1805 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1807 glDisable(GL_TEXTURE_2D
);
1809 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1810 glDisable(GL_BLEND
);
1813 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1817 if e_NoGraphics
then Exit
;
1818 if Integer(ID
) > High(e_TextureFonts
) then
1820 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1821 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1824 procedure e_RemoveAllTextureFont();
1828 if e_NoGraphics
then Exit
;
1829 if e_TextureFonts
= nil then Exit
;
1831 for i
:= 0 to High(e_TextureFonts
) do
1832 if e_TextureFonts
[i
].Base
<> 0 then
1834 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1835 e_TextureFonts
[i
].Base
:= 0;
1838 e_TextureFonts
:= nil;
1841 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1848 function _Point(X
, Y
: Integer): TPoint2i
;
1854 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1858 Result
.Width
:= Width
;
1859 Result
.Height
:= Height
;
1862 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1871 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1873 pixels
, obuf
, scln
, ps
, pd
: PByte;
1876 i
, x
, y
, res
: Integer;
1877 sign
: array [0..7] of Byte;
1878 hbuf
: array [0..12] of Byte;
1883 if e_NoGraphics
then Exit
;
1886 // first, extract and pack graphics data
1887 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1889 GetMem(pixels
, Width
*Height
*3);
1891 FillChar(pixels
^, Width
*Height
*3, 0);
1892 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1893 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1895 if e_FastScreenshots
then
1898 GetMem(scln
, (Width
*3+1)*Height
);
1902 Inc(ps
, (Width
*3)*(Height
-1));
1903 for i
:= 0 to Height
-1 do
1907 Move(ps
^, pd
^, Width
*3);
1919 obufsize
:= (Width
*3+1)*Height
*2;
1920 GetMem(obuf
, obufsize
);
1925 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1926 if res
= Z_OK
then break
;
1927 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1928 obufsize
:= obufsize
*2;
1931 GetMem(obuf
, obufsize
);
1933 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1946 st
.writeBuffer(sign
, 8);
1947 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1950 writeIntBE(st
, LongWord(13));
1955 st
.writeBuffer(sign
, 4);
1956 crc
:= crc32(0, @sign
[0], 4);
1959 hbuf
[2] := (Width
shr 8) and $ff;
1960 hbuf
[3] := Width
and $ff;
1963 hbuf
[6] := (Height
shr 8) and $ff;
1964 hbuf
[7] := Height
and $ff;
1965 hbuf
[8] := 8; // bit depth
1966 hbuf
[9] := 2; // RGB
1967 hbuf
[10] := 0; // compression method
1968 hbuf
[11] := 0; // filter method
1969 hbuf
[12] := 0; // no interlace
1970 crc
:= crc32(crc
, @hbuf
[0], 13);
1971 st
.writeBuffer(hbuf
, 13);
1972 writeIntBE(st
, crc
);
1973 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1976 writeIntBE(st
, LongWord(dlen
));
1981 st
.writeBuffer(sign
, 4);
1982 crc
:= crc32(0, @sign
[0], 4);
1983 crc
:= crc32(crc
, obuf
, dlen
);
1984 st
.writeBuffer(obuf
^, dlen
);
1985 writeIntBE(st
, crc
);
1986 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
1989 writeIntBE(st
, LongWord(0));
1994 st
.writeBuffer(sign
, 4);
1995 crc
:= crc32(0, @sign
[0], 4);
1996 writeIntBE(st
, crc
);
1997 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
1999 if obuf
<> nil then FreeMem(obuf
);
2004 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2005 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2008 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2010 //writeln(stderr, 'moving pixels...');
2011 for y
:= Height
-1 downto 0 do
2013 for x
:= 0 to Width
-1 do
2015 clr
.r
:= ps
^; Inc(ps
);
2016 clr
.g
:= ps
^; Inc(ps
);
2017 clr
.b
:= ps
^; Inc(ps
);
2019 SetPixel32(img
, x
, y
, clr
);
2022 GlobalMetadata
.ClearMetaItems();
2023 GlobalMetadata
.ClearMetaItemsForSaving();
2024 //writeln(stderr, 'compressing image...');
2025 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2026 //writeln(stderr, 'done!');