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 {$INCLUDE ../nogl/noGLuses.inc}
23 SysUtils
, Classes
, Math
, e_log
, e_texture
, SDL2
, MAPDEF
, ImagingTypes
, Imaging
, ImagingUtility
;
26 TMirrorType
=(None
, Horizontal
, Vertical
);
27 TBlending
=(None
, Blend
, Filter
, Invert
);
38 Left
, Top
, Right
, Bottom
: Integer;
56 //------------------------------------------------------------------
58 //------------------------------------------------------------------
60 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
61 procedure e_ResizeWindow(Width
, Height
: Integer);
63 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
64 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
65 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
66 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
67 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
68 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
69 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
70 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
72 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
73 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
75 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
76 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
78 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
80 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
81 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
82 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
83 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
84 Blending
: TBlending
= TBlending
.None
);
85 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
86 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
88 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
89 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
90 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
91 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
92 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
93 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
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
172 Chars
: array[0..255] of
182 TSavedTexture
= record
189 e_Textures
: array of TTexture
= nil;
190 e_TextureFonts
: array of TTextureFont
= nil;
191 e_CharFonts
: array of TCharFont
;
192 //e_SavedTextures: array of TSavedTexture;
194 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
196 //------------------------------------------------------------------
197 // Èíèöèàëèçèðóåò OpenGL
198 //------------------------------------------------------------------
199 procedure e_InitGL();
203 e_DummyTextures
:= True;
209 glDisable(GL_DEPTH_TEST
);
210 glEnable(GL_SCISSOR_TEST
);
211 glClearColor(0, 0, 0, 0);
214 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
216 mat
: Array [0..15] of GLDouble
;
219 if e_NoGraphics
then Exit
;
221 glScissor(X
, Y
, Width
, Height
);
222 glViewport(X
, Y
, Width
, Height
);
223 //gluOrtho2D(0, Width, Height, 0);
225 glMatrixMode(GL_PROJECTION
);
227 mat
[ 0] := 2.0 / Width
;
233 mat
[ 5] := -2.0 / Height
;
247 glLoadMatrixd(@mat
[0]);
249 glMatrixMode(GL_MODELVIEW
);
253 //------------------------------------------------------------------
254 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
255 //------------------------------------------------------------------
256 function FindTexture(): DWORD
;
260 if e_Textures
<> nil then
261 for i
:= 0 to High(e_Textures
) do
262 if e_Textures
[i
].tx
.Width
= 0 then
268 if e_Textures
= nil then
270 SetLength(e_Textures
, 32);
275 Result
:= High(e_Textures
) + 1;
276 SetLength(e_Textures
, Length(e_Textures
) + 32);
280 //------------------------------------------------------------------
282 //------------------------------------------------------------------
283 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
290 e_WriteLog('Loading texture from '+FileName
, TMsgType
.Notify
);
292 find_id
:= FindTexture();
294 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
295 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
302 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
309 find_id
:= FindTexture();
311 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
318 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
325 find_id
:= FindTexture
;
327 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
;
334 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
341 find_id
:= FindTexture();
343 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
350 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
356 find_id
:= FindTexture();
357 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
362 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
364 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
365 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
368 procedure e_ResizeWindow(Width
, Height
: Integer);
372 e_SetViewPort(0, 0, Width
, Height
);
375 procedure drawTxQuad (x0
, y0
, w
, h
, tw
, th
: Integer; u
, v
: single; Mirror
: TMirrorType
);
377 x1
, y1
, tmp
: Integer;
379 if (w
< 1) or (h
< 1) then exit
;
382 if Mirror
= TMirrorType
.Horizontal
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
383 else if Mirror
= TMirrorType
.Vertical
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
384 //HACK: make texture one pixel shorter, so it won't wrap
385 if (g_dbg_scale
<> 1.0) then
390 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
391 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
392 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
393 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
396 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
397 Blending
: Boolean; Mirror
: TMirrorType
= TMirrorType
.None
);
399 if e_NoGraphics
then Exit
;
400 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
402 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
407 if (AlphaChannel
) or (Alpha
> 0) then
408 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
411 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
414 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
416 glEnable(GL_TEXTURE_2D
);
417 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
420 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
);
422 //u := e_Textures[ID].tx.u;
423 //v := e_Textures[ID].tx.v;
426 if Mirror = M_NONE then
428 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
429 glTexCoord2f(0, 0); glVertex2i(X, Y);
430 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
431 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
434 if Mirror = M_HORIZONTAL then
436 glTexCoord2f(u, 0); glVertex2i(X, Y);
437 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
438 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
439 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
442 if Mirror = M_VERTICAL then
444 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
445 glTexCoord2f(0, -v); glVertex2i(X, Y);
446 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
447 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
456 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
457 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
461 if e_NoGraphics
then Exit
;
462 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
464 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
469 if (AlphaChannel
) or (Alpha
> 0) then
470 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
473 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
476 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
478 glEnable(GL_TEXTURE_2D
);
479 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
481 u
:= e_Textures
[ID
].tx
.u
;
482 v
:= e_Textures
[ID
].tx
.v
;
485 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
486 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
487 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
488 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
494 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
495 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= TMirrorType
.None
);
497 if e_NoGraphics
then Exit
;
498 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
500 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
505 if (AlphaChannel
) or (Alpha
> 0) then
506 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
509 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
512 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
514 glEnable(GL_TEXTURE_2D
);
515 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
517 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
);
523 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
524 AlphaChannel
: Boolean; Blending
: Boolean; ambientBlendMode
: Boolean=false);
526 X2
, Y2
, dx
, w
, h
: Integer;
529 if e_NoGraphics
then Exit
;
530 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
531 ambientBlendMode
:= false;
533 if (Alpha
> 0) or AlphaChannel
or Blending
then
539 if not ambientBlendMode
then glDisable(GL_BLEND
);
541 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
542 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
543 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
545 if (XCount
= 0) then XCount
:= 1;
546 if (YCount
= 0) then YCount
:= 1;
548 glEnable(GL_TEXTURE_2D
);
549 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
551 X2
:= X
+e_Textures
[ID
].tx
.width
*XCount
;
552 Y2
:= Y
+e_Textures
[ID
].tx
.height
*YCount
;
554 //k8: this SHOULD work... i hope
555 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
558 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
559 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
560 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
561 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
568 u
:= e_Textures
[ID
].tx
.u
;
569 v
:= e_Textures
[ID
].tx
.v
;
570 w
:= e_Textures
[ID
].tx
.width
;
571 h
:= e_Textures
[ID
].tx
.height
;
578 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
579 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
580 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
581 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
596 //TODO: overflow checks
597 function intersectRect (var x0
, y0
, w0
, h0
: Integer; const x1
, y1
, w1
, h1
: Integer): Boolean;
602 if (w0
< 1) or (h0
< 1) or (w1
< 1) or (h1
< 1) then exit
;
603 // check for intersection
604 if (x0
+w0
<= x1
) or (y0
+h0
<= y1
) or (x1
+w1
<= x0
) or (y1
+h1
<= y0
) then exit
;
605 if (x0
>= x1
+w1
) or (y0
>= y1
+h1
) or (x1
>= x0
+h0
) or (y1
>= y0
+h0
) then exit
;
609 if (x0
< x1
) then x0
:= x1
;
610 if (y0
< y1
) then y0
:= y1
;
611 if (ex0
> x1
+w1
) then ex0
:= x1
+w1
;
612 if (ey0
> y1
+h1
) then ey0
:= y1
+h1
;
615 result
:= (w0
> 0) and (h0
> 0);
619 procedure e_DrawFillX (id
: DWORD
; x
, y
, wdt
, hgt
: Integer; alpha
: Integer; alphachannel
: Boolean;
620 blending
: Boolean; scale
: Single; ambientBlendMode
: Boolean=false);
625 scxywh: array[0..3] of GLint;
626 vpxywh: array[0..3] of GLint;
628 w
, h
, dw
, cw
, ch
, yofs
: Integer;
629 u
, v
, cu
, cv
: Single;
633 procedure setScissorGLInternal (x, y, w, h: Integer);
635 //if not scallowed then exit;
640 y := vpxywh[3]-(y+h);
641 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
643 glScissor(0, 0, 0, 0);
647 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
648 glScissor(x, y, w, h);
654 if e_NoGraphics
then exit
;
655 ambientBlendMode
:= false;
657 if (wdt
< 1) or (hgt
< 1) then exit
;
659 if (wdt
mod e_Textures
[ID
].tx
.width
= 0) and (hgt
mod e_Textures
[ID
].tx
.height
= 0) then
661 e_DrawFill(id
, x
, y
, wdt
div e_Textures
[ID
].tx
.width
, hgt
div e_Textures
[ID
].tx
.height
, alpha
, alphachannel
, blending
, ambientBlendMode
);
665 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
667 if (Alpha
> 0) or AlphaChannel
or Blending
then
673 if not ambientBlendMode
then glDisable(GL_BLEND
);
675 if AlphaChannel
or (Alpha
> 0) then glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
676 if (Alpha
> 0) then glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
677 if Blending
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
679 glEnable(GL_TEXTURE_2D
);
680 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
685 //k8: this SHOULD work... i hope
686 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
689 glTexCoord2f(0, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x
, y
);
690 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, hgt
/e_Textures
[ID
].tx
.height
); glVertex2i(x2
, y
);
691 glTexCoord2f(wdt
/e_Textures
[ID
].tx
.width
, 0); glVertex2i(x2
, y2
);
692 glTexCoord2f(0, 0); glVertex2i(x
, y2
);
697 // hard day's night; setup scissor
699 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
700 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
701 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
702 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
703 //glEnable(GL_SCISSOR_TEST);
704 setScissorGLInternal(x, y, wdt, hgt);
707 u
:= e_Textures
[ID
].tx
.u
;
708 v
:= e_Textures
[ID
].tx
.v
;
709 w
:= e_Textures
[ID
].tx
.width
;
710 h
:= e_Textures
[ID
].tx
.height
;
712 if (hgt
> h
) then begin y
+= hgt
-h
; onlyOneY
:= false; end else onlyOneY
:= true;
716 if (hgt
>= h
) then begin ch
:= h
; cv
:= v
; yofs
:= 0; end else begin ch
:= hgt
; cv
:= v
/(h
/hgt
); yofs
:= h
-hgt
; end;
717 if onlyOneY
then yofs
:= 0;
723 if (dw
>= w
) then begin cw
:= w
; cu
:= u
; end else begin cw
:= dw
; cu
:= u
/(w
/dw
); end;
725 glTexCoord2f(0, cv
); glVertex2i(X
, Y
+yofs
);
726 glTexCoord2f(cu
, cv
); glVertex2i(X
+cw
, Y
+yofs
);
727 glTexCoord2f(cu
, 0); glVertex2i(X
+cw
, Y
+ch
+yofs
);
728 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ch
+yofs
);
734 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
741 procedure e_AmbientQuad (x
, y
, w
, h
: Integer; r
, g
, b
, a
: Byte);
743 if e_NoGraphics
then exit
;
744 if (w
< 1) or (h
< 1) then exit
;
745 if (a
<> 255) or ((r
or g
or b
) <> 0) then
748 glDisable(GL_TEXTURE_2D
);
749 glColor4ub(r
, g
, b
, a
);
750 if ((r
or g
or b
) <> 0) then
752 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
756 glVertex2i(x
+w
, y
+h
);
760 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
764 glVertex2i(x
+w
, y
+h
);
772 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
773 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= TMirrorType
.None
);
775 if e_NoGraphics
then Exit
;
777 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
779 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
784 if (AlphaChannel
) or (Alpha
> 0) then
785 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
788 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
791 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
793 if (Angle
<> 0) and (RC
<> nil) then
796 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
797 glRotatef(Angle
, 0, 0, 1);
798 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
801 glEnable(GL_TEXTURE_2D
);
802 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
803 glBegin(GL_QUADS
); //0-1 1-1
805 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
);
814 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
816 if e_NoGraphics
then Exit
;
817 glDisable(GL_TEXTURE_2D
);
818 glColor3ub(Red
, Green
, Blue
);
821 if (Size
= 2) or (Size
= 4) then
825 glVertex2f(X
+0.3, Y
+1.0);
828 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
831 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
833 // Make lines only top-left/bottom-right and top-right/bottom-left
845 // Pixel-perfect hack
853 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
855 nX1
, nY1
, nX2
, nY2
: Integer;
857 v
: array [0..15] of GLfloat
;
860 if e_NoGraphics
then Exit
;
861 // Only top-left/bottom-right quad
878 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
882 glDisable(GL_TEXTURE_2D
);
883 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
886 nX1
:= X1
; nY1
:= Y1
;
887 nX2
:= X2
; nY2
:= Y1
;
888 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
889 v
[0] := nX1
; v
[1] := nY1
; v
[2] := nX2
; v
[3] := nY2
;
891 nX1
:= X2
; nY1
:= Y1
;
892 nX2
:= X2
; nY2
:= Y2
;
893 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
894 v
[4] := nX1
; v
[5] := nY1
; v
[6] := nX2
; v
[7] := nY2
;
896 nX1
:= X2
; nY1
:= Y2
;
897 nX2
:= X1
; nY2
:= Y2
;
898 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
899 v
[8] := nX1
; v
[9] := nY1
; v
[10] := nX2
; v
[11] := nY2
;
901 nX1
:= X1
; nY1
:= Y2
;
902 nX2
:= X1
; nY2
:= Y1
;
903 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
904 v
[12] := nX1
; v
[13] := nY1
; v
[14] := nX2
; v
[15] := nY2
;
906 glVertexPointer(2, GL_FLOAT
, 0, @v
[0]);
907 glEnableClientState(GL_VERTEX_ARRAY
);
908 glDisableClientState(GL_COLOR_ARRAY
);
909 glDisableClientState(GL_NORMAL_ARRAY
);
910 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
911 glDrawArrays(GL_LINES
, 0, 16);
914 nX1
:= X1
; nY1
:= Y1
;
915 nX2
:= X2
; nY2
:= Y1
;
916 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
917 glVertex2i(nX1
, nY1
);
918 glVertex2i(nX2
, nY2
);
920 nX1
:= X2
; nY1
:= Y1
;
921 nX2
:= X2
; nY2
:= Y2
;
922 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
923 glVertex2i(nX1
, nY1
);
924 glVertex2i(nX2
, nY2
);
926 nX1
:= X2
; nY1
:= Y2
;
927 nX2
:= X1
; nY2
:= Y2
;
928 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
929 glVertex2i(nX1
, nY1
);
930 glVertex2i(nX2
, nY2
);
932 nX1
:= X1
; nY1
:= Y2
;
933 nX2
:= X1
; nY2
:= Y1
;
934 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
935 glVertex2i(nX1
, nY1
);
936 glVertex2i(nX2
, nY2
);
940 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
945 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
946 Blending
: TBlending
= TBlending
.None
);
948 if e_NoGraphics
then Exit
;
949 if (Alpha
> 0) or (Blending
<> TBlending
.None
) then
954 if Blending
= TBlending
.Blend
then
955 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
957 if Blending
= TBlending
.Filter
then
958 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
960 if Blending
= TBlending
.Invert
then
961 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
964 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
966 glDisable(GL_TEXTURE_2D
);
967 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
979 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
985 // ////////////////////////////////////////////////////////////////////////// //
986 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
988 if (a
< 0) then a
:= 0;
989 if (a
> 255) then a
:= 255;
991 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
992 glDisable(GL_TEXTURE_2D
);
993 glColor4ub(0, 0, 0, Byte(255-a
));
1000 //glRect(x, y, x+w, y+h);
1001 glColor4ub(1, 1, 1, 1);
1002 glDisable(GL_BLEND
);
1003 //glBlendEquation(GL_FUNC_ADD);
1006 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
1008 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
1012 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
1015 v
: array [0..3] of GLfloat
;
1018 if e_NoGraphics
then Exit
;
1019 // Pixel-perfect lines
1021 e_LineCorrection(X1
, Y1
, X2
, Y2
);
1026 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1028 glDisable(GL_BLEND
);
1030 glDisable(GL_TEXTURE_2D
);
1031 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
1035 v
[0] := X1
; v
[1] := Y1
; v
[2] := X2
; v
[3] := Y2
;
1036 glVertexPointer(2, GL_FLOAT
, 0, @v
[0]);
1037 glEnableClientState(GL_VERTEX_ARRAY
);
1038 glDisableClientState(GL_COLOR_ARRAY
);
1039 glDisableClientState(GL_NORMAL_ARRAY
);
1040 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
1041 glDrawArrays(GL_LINES
, 0, 4);
1049 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1051 glDisable(GL_BLEND
);
1054 //------------------------------------------------------------------
1055 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1056 //------------------------------------------------------------------
1057 procedure e_DeleteTexture(ID
: DWORD
);
1059 if not e_NoGraphics
then
1060 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
1061 e_Textures
[ID
].tx
.id
:= 0;
1062 e_Textures
[ID
].tx
.Width
:= 0;
1063 e_Textures
[ID
].tx
.Height
:= 0;
1066 //------------------------------------------------------------------
1067 // Óäàëÿåò âñå òåêñòóðû
1068 //------------------------------------------------------------------
1069 procedure e_RemoveAllTextures();
1073 if e_Textures
= nil then Exit
;
1075 for i
:= 0 to High(e_Textures
) do
1076 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
1080 //------------------------------------------------------------------
1082 //------------------------------------------------------------------
1083 procedure e_ReleaseEngine();
1085 e_RemoveAllTextures
;
1086 e_RemoveAllTextureFont
;
1089 procedure e_BeginRender();
1091 if e_NoGraphics
then Exit
;
1092 glEnable(GL_ALPHA_TEST
);
1093 glAlphaFunc(GL_GREATER
, 0.0);
1096 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
1098 if e_NoGraphics
then Exit
;
1099 glClearColor(Red
, Green
, Blue
, 0);
1103 procedure e_Clear(); overload
;
1105 if e_NoGraphics
then Exit
;
1106 glClearColor(0, 0, 0, 0);
1107 glClear(GL_COLOR_BUFFER_BIT
);
1110 procedure e_EndRender();
1112 if e_NoGraphics
then Exit
;
1116 function e_GetGamma(win
: PSDL_Window
): Byte;
1118 ramp
: array [0..256*3-1] of Word;
1119 rgb
: array [0..2] of Double;
1128 if e_NoGraphics
then Exit
;
1133 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1142 for j
:= min
to max
- 1 do
1145 B
:= (j
mod 256)/256;
1147 sum
:= sum
+ ln(A
)/ln(B
);
1150 rgb
[i
] := sum
/ count
;
1153 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1156 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1158 ramp
: array [0..256*3-1] of Word;
1163 if e_NoGraphics
then Exit
;
1164 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1166 for i
:= 0 to 255 do
1168 r
:= Exp(g
* ln(i
/256))*65536;
1169 if r
< 0 then r
:= 0
1170 else if r
> 65535 then r
:= 65535;
1171 ramp
[i
] := trunc(r
);
1172 ramp
[i
+ 256] := trunc(r
);
1173 ramp
[i
+ 512] := trunc(r
);
1176 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1179 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1183 e_WriteLog('Creating CharFont...', TMsgType
.Notify
);
1187 if e_CharFonts
<> nil then
1188 for i
:= 0 to High(e_CharFonts
) do
1189 if not e_CharFonts
[i
].alive
then
1195 if id
= DWORD(-1) then
1197 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1198 id
:= High(e_CharFonts
);
1201 with e_CharFonts
[id
] do
1203 for i
:= 0 to High(Chars
) do
1217 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1219 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1221 TextureID
:= Texture
;
1226 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1230 if e_NoGraphics
then Exit
;
1231 if Text = '' then Exit
;
1232 if e_CharFonts
= nil then Exit
;
1233 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1235 with e_CharFonts
[FontID
] do
1237 for a
:= 1 to Length(Text) do
1238 with Chars
[Ord(Text[a
])] do
1239 if TextureID
<> -1 then
1241 e_Draw(TextureID
, X
, Y
, 0, True, False);
1242 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1247 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1248 Color
: TRGB
; Scale
: Single = 1.0);
1253 if e_NoGraphics
then Exit
;
1254 if Text = '' then Exit
;
1255 if e_CharFonts
= nil then Exit
;
1256 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1258 with e_CharFonts
[FontID
] do
1260 for a
:= 1 to Length(Text) do
1261 with Chars
[Ord(Text[a
])] do
1262 if TextureID
<> -1 then
1264 if Scale
<> 1.0 then
1267 glScalef(Scale
, Scale
, 0);
1272 e_Draw(TextureID
, X
, Y
, 0, True, False);
1275 if Scale
<> 1.0 then glPopMatrix
;
1277 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1282 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1284 a
, TX
, TY
, len
: Integer;
1288 if e_NoGraphics
then Exit
;
1289 if Text = '' then Exit
;
1290 if e_CharFonts
= nil then Exit
;
1291 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1299 len
:= Length(Text);
1301 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1303 with e_CharFonts
[FontID
] do
1305 for a
:= 1 to len
do
1316 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1321 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1326 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1331 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1336 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1341 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1346 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1351 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1356 with Chars
[Ord(Text[a
])] do
1357 if TextureID
<> -1 then
1361 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1364 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1370 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1378 if Text = '' then Exit
;
1379 if e_CharFonts
= nil then Exit
;
1380 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1382 with e_CharFonts
[FontID
] do
1384 for a
:= 1 to Length(Text) do
1385 with Chars
[Ord(Text[a
])] do
1386 if TextureID
<> -1 then
1388 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1389 e_GetTextureSize(TextureID
, nil, @h2
);
1390 if h2
> h
then h
:= h2
;
1395 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1397 a
, lines
, len
: Integer;
1398 h2
, w2
, tw
, th
: Word;
1405 if Text = '' then Exit
;
1406 if e_CharFonts
= nil then Exit
;
1407 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1410 len
:= Length(Text);
1412 with e_CharFonts
[FontID
] do
1414 for a
:= 1 to len
do
1416 if Text[a
] = #10 then
1419 if w2
> tw
then tw
:= w2
;
1424 with Chars
[Ord(Text[a
])] do
1425 if TextureID
<> -1 then
1427 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1428 e_GetTextureSize(TextureID
, nil, @h2
);
1429 if h2
> th
then th
:= h2
;
1441 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1447 if e_CharFonts
= nil then Exit
;
1448 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1450 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1451 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1454 function e_CharFont_GetMaxHeight(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
1466 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1467 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1469 if h2
> Result
then Result
:= h2
;
1473 procedure e_CharFont_Remove(FontID
: DWORD
);
1477 with e_CharFonts
[FontID
] do
1478 for a
:= 0 to High(Chars
) do
1479 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1481 e_CharFonts
[FontID
].alive
:= False;
1484 procedure e_CharFont_RemoveAll();
1488 if e_CharFonts
= nil then Exit
;
1490 for a
:= 0 to High(e_CharFonts
) do
1491 e_CharFont_Remove(a
);
1496 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1503 if e_NoGraphics
then Exit
;
1504 e_WriteLog('Creating texture font...', TMsgType
.Notify
);
1508 if e_TextureFonts
<> nil then
1509 for i
:= 0 to High(e_TextureFonts
) do
1510 if e_TextureFonts
[i
].Base
= 0 then
1516 if id
= DWORD(-1) then
1518 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1519 id
:= High(e_TextureFonts
);
1522 with e_TextureFonts
[id
] do
1524 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1525 Base
:= glGenLists(XCount
*YCount
);
1527 TextureID
:= e_Textures
[Tex
].tx
.id
;
1528 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1529 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1536 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1537 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1538 for loop1
:= 0 to XCount
*YCount
-1 do
1540 cx
:= (loop1
mod XCount
)/XCount
;
1541 cy
:= (loop1
div YCount
)/YCount
;
1543 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1545 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1546 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1548 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1549 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1551 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1552 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1554 glTexCoord2f(cx
, 1.0-cy
);
1557 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1565 procedure e_TextureFontKill(FontID
: DWORD
);
1567 if e_NoGraphics
then Exit
;
1568 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1569 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1571 e_TextureFonts
[FontID
].Base
:= 0;
1574 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1575 procedure e_TextureFontDrawChar(ch
: Char; FontID
: DWORD
);
1580 Width
, Height
: Integer;
1581 XCount
, YCount
: Integer;
1583 index
:= Ord(ch
) - 32;
1584 Tex
:= e_TextureFonts
[FontID
].Texture
;
1585 Width
:= e_Textures
[Tex
].tx
.Width
;
1586 Height
:= e_Textures
[Tex
].tx
.Height
;
1587 XCount
:= e_TextureFonts
[FontID
].XC
;
1588 YCount
:= e_TextureFonts
[FontID
].YC
;
1589 cx
:= (index
mod XCount
)/XCount
;
1590 cy
:= (index
div YCount
)/YCount
;
1592 glTexCoord2f(cx
, 1 - cy
- 1/YCount
);
1593 glVertex2i(0, Height
div YCount
);
1594 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
- 1/YCount
);
1595 glVertex2i(Width
div XCount
, Height
div YCount
);
1596 glTexCoord2f(cx
+ 1/XCount
, 1 - cy
);
1597 glVertex2i(Width
div XCount
, 0);
1598 glTexCoord2f(cx
, 1 - cy
);
1601 glTranslatef((e_Textures
[Tex
].tx
.Width
div XCount
) + e_TextureFonts
[FontID
].SPC
, 0, 0);
1604 procedure e_TextureFontDrawString(Text: String; FontID
: DWORD
);
1608 for i
:= 1 to High(Text) do
1609 e_TextureFontDrawChar(Text[i
], FontID
);
1613 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1615 if e_NoGraphics
then Exit
;
1616 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1617 if Text = '' then Exit
;
1619 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1622 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1625 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1626 glEnable(GL_TEXTURE_2D
);
1627 glTranslatef(x
, y
, 0);
1628 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1629 e_TextureFontDrawString(Text, FontID
);
1631 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1632 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1634 glDisable(GL_TEXTURE_2D
);
1637 glDisable(GL_BLEND
);
1640 // god forgive me for this, but i cannot figure out how to do it without lists
1641 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1643 if e_NoGraphics
then Exit
;
1648 glColor4ub(0, 0, 0, 128);
1649 glTranslatef(X
+1, Y
+1, 0);
1650 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1651 e_TextureFontDrawChar(Ch
, FontID
);
1653 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1659 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1660 glTranslatef(X
, Y
, 0);
1661 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1662 e_TextureFontDrawChar(Ch
, FontID
);
1664 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1670 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1672 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1673 glEnable(GL_TEXTURE_2D
);
1674 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1676 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1678 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1679 glDisable(GL_TEXTURE_2D
);
1680 glDisable(GL_BLEND
);
1683 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1685 result
:= e_TextureFonts
[FontID
].CharWidth
;
1688 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1690 a
, TX
, TY
, len
: Integer;
1694 if e_NoGraphics
then Exit
;
1695 if Text = '' then Exit
;
1696 if e_TextureFonts
= nil then Exit
;
1697 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1705 len
:= Length(Text);
1707 w
:= e_TextureFonts
[FontID
].CharWidth
;
1709 with e_TextureFonts
[FontID
] do
1711 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1712 glEnable(GL_TEXTURE_2D
);
1714 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1715 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1718 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1721 for a
:= 1 to len
do
1732 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1737 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1742 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1747 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1752 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1757 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1762 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1767 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1774 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1779 glDisable(GL_TEXTURE_2D
);
1780 glDisable(GL_BLEND
);
1784 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1785 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1787 if e_NoGraphics
then Exit
;
1788 if Text = '' then Exit
;
1791 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1792 glEnable(GL_TEXTURE_2D
);
1794 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1795 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1798 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1803 glColor4ub(0, 0, 0, 128);
1804 glTranslatef(x
+1, y
+1, 0);
1805 glScalef(Scale
, Scale
, 0);
1806 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1807 e_TextureFontDrawString(Text, FontID
);
1809 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1815 glColor4ub(Red
, Green
, Blue
, 255);
1816 glTranslatef(x
, y
, 0);
1817 glScalef(Scale
, Scale
, 0);
1818 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1819 e_TextureFontDrawString(Text, FontID
);
1821 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1824 glDisable(GL_TEXTURE_2D
);
1826 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1827 glDisable(GL_BLEND
);
1830 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1834 if e_NoGraphics
then Exit
;
1835 if Integer(ID
) > High(e_TextureFonts
) then
1837 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1838 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1841 procedure e_RemoveAllTextureFont();
1845 if e_NoGraphics
then Exit
;
1846 if e_TextureFonts
= nil then Exit
;
1848 for i
:= 0 to High(e_TextureFonts
) do
1849 if e_TextureFonts
[i
].Base
<> 0 then
1851 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1852 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1854 e_TextureFonts
[i
].Base
:= 0;
1857 e_TextureFonts
:= nil;
1860 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1867 function _Point(X
, Y
: Integer): TPoint2i
;
1873 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1877 Result
.Width
:= Width
;
1878 Result
.Height
:= Height
;
1881 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1890 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1892 pixels
, obuf
, scln
, ps
, pd
: PByte;
1895 i
, x
, y
, res
: Integer;
1896 sign
: array [0..7] of Byte;
1897 hbuf
: array [0..12] of Byte;
1902 if e_NoGraphics
then Exit
;
1905 // first, extract and pack graphics data
1906 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1908 GetMem(pixels
, Width
*Height
*3);
1910 FillChar(pixels
^, Width
*Height
*3, 0);
1911 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1912 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1914 if e_FastScreenshots
then
1917 GetMem(scln
, (Width
*3+1)*Height
);
1921 Inc(ps
, (Width
*3)*(Height
-1));
1922 for i
:= 0 to Height
-1 do
1926 Move(ps
^, pd
^, Width
*3);
1938 obufsize
:= (Width
*3+1)*Height
*2;
1939 GetMem(obuf
, obufsize
);
1944 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1945 if res
= Z_OK
then break
;
1946 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1947 obufsize
:= obufsize
*2;
1950 GetMem(obuf
, obufsize
);
1952 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1965 st
.writeBuffer(sign
, 8);
1966 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1969 writeIntBE(st
, LongWord(13));
1974 st
.writeBuffer(sign
, 4);
1975 crc
:= crc32(0, @sign
[0], 4);
1978 hbuf
[2] := (Width
shr 8) and $ff;
1979 hbuf
[3] := Width
and $ff;
1982 hbuf
[6] := (Height
shr 8) and $ff;
1983 hbuf
[7] := Height
and $ff;
1984 hbuf
[8] := 8; // bit depth
1985 hbuf
[9] := 2; // RGB
1986 hbuf
[10] := 0; // compression method
1987 hbuf
[11] := 0; // filter method
1988 hbuf
[12] := 0; // no interlace
1989 crc
:= crc32(crc
, @hbuf
[0], 13);
1990 st
.writeBuffer(hbuf
, 13);
1991 writeIntBE(st
, crc
);
1992 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1995 writeIntBE(st
, LongWord(dlen
));
2000 st
.writeBuffer(sign
, 4);
2001 crc
:= crc32(0, @sign
[0], 4);
2002 crc
:= crc32(crc
, obuf
, dlen
);
2003 st
.writeBuffer(obuf
^, dlen
);
2004 writeIntBE(st
, crc
);
2005 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2008 writeIntBE(st
, LongWord(0));
2013 st
.writeBuffer(sign
, 4);
2014 crc
:= crc32(0, @sign
[0], 4);
2015 writeIntBE(st
, crc
);
2016 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2018 if obuf
<> nil then FreeMem(obuf
);
2023 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
2024 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
2027 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
2029 //writeln(stderr, 'moving pixels...');
2030 for y
:= Height
-1 downto 0 do
2032 for x
:= 0 to Width
-1 do
2034 clr
.r
:= ps
^; Inc(ps
);
2035 clr
.g
:= ps
^; Inc(ps
);
2036 clr
.b
:= ps
^; Inc(ps
);
2038 SetPixel32(img
, x
, y
, clr
);
2041 GlobalMetadata
.ClearMetaItems();
2042 GlobalMetadata
.ClearMetaItemsForSaving();
2043 //writeln(stderr, 'compressing image...');
2044 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
2045 //writeln(stderr, 'done!');