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
);
32 TPoint
= MAPDEF
.TPoint
; // TODO: create an utiltypes.pas or something
33 // for other types like rect as well
40 Left
, Top
, Right
, Bottom
: Integer;
58 //------------------------------------------------------------------
60 //------------------------------------------------------------------
62 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
63 procedure e_ResizeWindow(Width
, Height
: Integer);
65 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
66 Blending
: Boolean; Mirror
: TMirrorType
= M_NONE
);
67 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
68 Blending
: Boolean; Angle
: Single; RC
: PPoint
; Mirror
: TMirrorType
= M_NONE
);
69 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
70 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
71 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
72 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
73 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
74 AlphaChannel
: Boolean; Blending
: Boolean);
75 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
76 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
77 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
78 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
79 Blending
: TBlending
= B_NONE
);
81 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
82 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
83 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
84 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
85 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
86 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
87 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
88 procedure e_DeleteTexture(ID
: DWORD
);
89 procedure e_RemoveAllTextures();
92 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
93 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
94 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
95 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
96 Color
: TRGB
; Scale
: Single = 1.0);
97 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
98 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
99 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
100 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
101 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
102 procedure e_CharFont_Remove(FontID
: DWORD
);
103 procedure e_CharFont_RemoveAll();
106 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
108 procedure e_TextureFontKill(FontID
: DWORD
);
109 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
110 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
111 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
112 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
113 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
114 procedure e_RemoveAllTextureFont();
116 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
117 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
119 procedure e_ReleaseEngine();
120 procedure e_BeginRender();
121 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
122 procedure e_Clear(); overload
;
123 procedure e_EndRender();
125 function e_GetGamma(win
: PSDL_Window
): Byte;
126 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
128 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
130 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
131 function _Point(X
, Y
: Integer): TPoint2i
;
132 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
133 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
135 //function e_getTextGLId (ID: DWORD): GLuint;
139 e_NoGraphics
: Boolean = False;
140 e_FastScreenshots
: Boolean = true; // it's REALLY SLOW with `false`
154 TTextureFont
= record
164 Chars
: array[0..255] of
174 TSavedTexture
= record
181 e_Textures
: array of TTexture
= nil;
182 e_TextureFonts
: array of TTextureFont
= nil;
183 e_CharFonts
: array of TCharFont
;
184 //e_SavedTextures: array of TSavedTexture;
186 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
188 //------------------------------------------------------------------
189 // Èíèöèàëèçèðóåò OpenGL
190 //------------------------------------------------------------------
191 procedure e_InitGL();
195 e_DummyTextures
:= True;
201 glDisable(GL_DEPTH_TEST
);
202 glEnable(GL_SCISSOR_TEST
);
203 glClearColor(0, 0, 0, 0);
206 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
208 mat
: Array [0..15] of GLDouble
;
211 if e_NoGraphics
then Exit
;
213 glScissor(X
, Y
, Width
, Height
);
214 glViewport(X
, Y
, Width
, Height
);
215 //gluOrtho2D(0, Width, Height, 0);
217 glMatrixMode(GL_PROJECTION
);
219 mat
[ 0] := 2.0 / Width
;
225 mat
[ 5] := -2.0 / Height
;
239 glLoadMatrixd(@mat
[0]);
241 glMatrixMode(GL_MODELVIEW
);
245 //------------------------------------------------------------------
246 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
247 //------------------------------------------------------------------
248 function FindTexture(): DWORD
;
252 if e_Textures
<> nil then
253 for i
:= 0 to High(e_Textures
) do
254 if e_Textures
[i
].tx
.Width
= 0 then
260 if e_Textures
= nil then
262 SetLength(e_Textures
, 32);
267 Result
:= High(e_Textures
) + 1;
268 SetLength(e_Textures
, Length(e_Textures
) + 32);
272 //------------------------------------------------------------------
274 //------------------------------------------------------------------
275 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
282 e_WriteLog('Loading texture from '+FileName
, MSG_NOTIFY
);
284 find_id
:= FindTexture();
286 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
287 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
294 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
301 find_id
:= FindTexture();
303 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
310 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
317 find_id
:= FindTexture
;
319 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
;
326 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
333 find_id
:= FindTexture();
335 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
342 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
348 find_id
:= FindTexture();
349 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
354 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
356 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
357 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
360 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
368 w
:= e_Textures
[ID
].tx
.Width
;
369 h
:= e_Textures
[ID
].tx
.Height
;
376 if e_NoGraphics
then Exit
;
378 data
:= GetMemory(w
*h
*4);
379 glEnable(GL_TEXTURE_2D
);
380 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
381 glGetTexImage(GL_TEXTURE_2D
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
383 for y
:= h
-1 downto 0 do
390 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
396 Result
.Y
:= h
-lastline
;
408 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
414 Result
.Height
:= h
-lastline
-Result
.Y
;
426 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
432 Result
.X
:= lastline
+1;
437 for x
:= w
-1 downto 0 do
444 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
450 Result
.Width
:= lastline
-Result
.X
+1;
458 procedure e_ResizeWindow(Width
, Height
: Integer);
462 e_SetViewPort(0, 0, Width
, Height
);
465 procedure drawTxQuad (x0
, y0
, w
, h
: Integer; u
, v
: single; Mirror
: TMirrorType
);
467 x1
, y1
, tmp
: Integer;
469 if (w
< 1) or (h
< 1) then exit
;
472 if Mirror
= M_HORIZONTAL
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
473 else if Mirror
= M_VERTICAL
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
474 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
475 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
476 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
477 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
480 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
481 Blending
: Boolean; Mirror
: TMirrorType
= M_NONE
);
483 if e_NoGraphics
then Exit
;
484 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
486 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
491 if (AlphaChannel
) or (Alpha
> 0) then
492 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
495 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
498 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
500 glEnable(GL_TEXTURE_2D
);
501 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
504 drawTxQuad(X
, Y
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
506 //u := e_Textures[ID].tx.u;
507 //v := e_Textures[ID].tx.v;
510 if Mirror = M_NONE then
512 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
513 glTexCoord2f(0, 0); glVertex2i(X, Y);
514 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
515 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
518 if Mirror = M_HORIZONTAL then
520 glTexCoord2f(u, 0); glVertex2i(X, Y);
521 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
522 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
523 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
526 if Mirror = M_VERTICAL then
528 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
529 glTexCoord2f(0, -v); glVertex2i(X, Y);
530 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
531 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
540 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
541 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
545 if e_NoGraphics
then Exit
;
546 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
548 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
553 if (AlphaChannel
) or (Alpha
> 0) then
554 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
557 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
560 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
562 glEnable(GL_TEXTURE_2D
);
563 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
565 u
:= e_Textures
[ID
].tx
.u
;
566 v
:= e_Textures
[ID
].tx
.v
;
569 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
570 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
571 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
572 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
578 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
579 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
581 if e_NoGraphics
then Exit
;
582 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
584 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
589 if (AlphaChannel
) or (Alpha
> 0) then
590 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
593 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
596 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
598 glEnable(GL_TEXTURE_2D
);
599 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
601 drawTxQuad(X
, Y
, Width
, Height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
607 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
608 AlphaChannel
: Boolean; Blending
: Boolean);
610 X2
, Y2
, dx
, w
, h
: Integer;
613 if e_NoGraphics
then Exit
;
614 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
616 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
621 if (AlphaChannel
) or (Alpha
> 0) then
622 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
625 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
628 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
636 glEnable(GL_TEXTURE_2D
);
637 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
639 X2
:= X
+ e_Textures
[ID
].tx
.width
* XCount
;
640 Y2
:= Y
+ e_Textures
[ID
].tx
.height
* YCount
;
642 //k8: this SHOULD work... i hope
643 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
646 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
647 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
648 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
649 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
656 u
:= e_Textures
[ID
].tx
.u
;
657 v
:= e_Textures
[ID
].tx
.v
;
658 w
:= e_Textures
[ID
].tx
.width
;
659 h
:= e_Textures
[ID
].tx
.height
;
666 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
667 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
668 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
669 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
683 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
684 Blending
: Boolean; Angle
: Single; RC
: PPoint
; Mirror
: TMirrorType
= M_NONE
);
686 if e_NoGraphics
then Exit
;
688 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
690 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
695 if (AlphaChannel
) or (Alpha
> 0) then
696 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
699 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
702 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
704 if (Angle
<> 0) and (RC
<> nil) then
707 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
708 glRotatef(Angle
, 0, 0, 1);
709 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
712 glEnable(GL_TEXTURE_2D
);
713 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
714 glBegin(GL_QUADS
); //0-1 1-1
716 drawTxQuad(X
, Y
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
725 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
727 if e_NoGraphics
then Exit
;
728 glDisable(GL_TEXTURE_2D
);
729 glColor3ub(Red
, Green
, Blue
);
732 if (Size
= 2) or (Size
= 4) then
736 glVertex2f(X
+0.3, Y
+1.0);
739 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
742 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
744 // Make lines only top-left/bottom-right and top-right/bottom-left
756 // Pixel-perfect hack
764 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
766 nX1
, nY1
, nX2
, nY2
: Integer;
768 if e_NoGraphics
then Exit
;
769 // Only top-left/bottom-right quad
786 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
790 glDisable(GL_TEXTURE_2D
);
791 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
795 nX1
:= X1
; nY1
:= Y1
;
796 nX2
:= X2
; nY2
:= Y1
;
797 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
798 glVertex2i(nX1
, nY1
);
799 glVertex2i(nX2
, nY2
);
801 nX1
:= X2
; nY1
:= Y1
;
802 nX2
:= X2
; nY2
:= Y2
;
803 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
804 glVertex2i(nX1
, nY1
);
805 glVertex2i(nX2
, nY2
);
807 nX1
:= X2
; nY1
:= Y2
;
808 nX2
:= X1
; nY2
:= Y2
;
809 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
810 glVertex2i(nX1
, nY1
);
811 glVertex2i(nX2
, nY2
);
813 nX1
:= X1
; nY1
:= Y2
;
814 nX2
:= X1
; nY2
:= Y1
;
815 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
816 glVertex2i(nX1
, nY1
);
817 glVertex2i(nX2
, nY2
);
820 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
825 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
826 Blending
: TBlending
= B_NONE
);
828 if e_NoGraphics
then Exit
;
829 if (Alpha
> 0) or (Blending
<> B_NONE
) then
834 if Blending
= B_BLEND
then
835 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
837 if Blending
= B_FILTER
then
838 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
840 if Blending
= B_INVERT
then
841 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
844 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
846 glDisable(GL_TEXTURE_2D
);
847 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
859 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
864 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
866 if e_NoGraphics
then Exit
;
867 // Pixel-perfect lines
869 e_LineCorrection(X1
, Y1
, X2
, Y2
);
874 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
878 glDisable(GL_TEXTURE_2D
);
879 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
887 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
892 //------------------------------------------------------------------
893 // Óäàëÿåò òåêñòóðó èç ìàññèâà
894 //------------------------------------------------------------------
895 procedure e_DeleteTexture(ID
: DWORD
);
897 if not e_NoGraphics
then
898 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
899 e_Textures
[ID
].tx
.id
:= 0;
900 e_Textures
[ID
].tx
.Width
:= 0;
901 e_Textures
[ID
].tx
.Height
:= 0;
904 //------------------------------------------------------------------
905 // Óäàëÿåò âñå òåêñòóðû
906 //------------------------------------------------------------------
907 procedure e_RemoveAllTextures();
911 if e_Textures
= nil then Exit
;
913 for i
:= 0 to High(e_Textures
) do
914 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
918 //------------------------------------------------------------------
920 //------------------------------------------------------------------
921 procedure e_ReleaseEngine();
924 e_RemoveAllTextureFont
;
927 procedure e_BeginRender();
929 if e_NoGraphics
then Exit
;
930 glEnable(GL_ALPHA_TEST
);
931 glAlphaFunc(GL_GREATER
, 0.0);
934 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
936 if e_NoGraphics
then Exit
;
937 glClearColor(Red
, Green
, Blue
, 0);
941 procedure e_Clear(); overload
;
943 if e_NoGraphics
then Exit
;
944 glClearColor(0, 0, 0, 0);
945 glClear(GL_COLOR_BUFFER_BIT
);
948 procedure e_EndRender();
950 if e_NoGraphics
then Exit
;
954 function e_GetGamma(win
: PSDL_Window
): Byte;
956 ramp
: array [0..256*3-1] of Word;
957 rgb
: array [0..2] of Double;
966 if e_NoGraphics
then Exit
;
971 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
980 for j
:= min
to max
- 1 do
983 B
:= (j
mod 256)/256;
985 sum
:= sum
+ ln(A
)/ln(B
);
988 rgb
[i
] := sum
/ count
;
991 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
994 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
996 ramp
: array [0..256*3-1] of Word;
1001 if e_NoGraphics
then Exit
;
1002 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1004 for i
:= 0 to 255 do
1006 r
:= Exp(g
* ln(i
/256))*65536;
1007 if r
< 0 then r
:= 0
1008 else if r
> 65535 then r
:= 65535;
1009 ramp
[i
] := trunc(r
);
1010 ramp
[i
+ 256] := trunc(r
);
1011 ramp
[i
+ 512] := trunc(r
);
1014 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1017 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1021 e_WriteLog('Creating CharFont...', MSG_NOTIFY
);
1025 if e_CharFonts
<> nil then
1026 for i
:= 0 to High(e_CharFonts
) do
1027 if not e_CharFonts
[i
].Live
then
1033 if id
= DWORD(-1) then
1035 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1036 id
:= High(e_CharFonts
);
1039 with e_CharFonts
[id
] do
1041 for i
:= 0 to High(Chars
) do
1055 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1057 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1059 TextureID
:= Texture
;
1064 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1068 if e_NoGraphics
then Exit
;
1069 if Text = '' then Exit
;
1070 if e_CharFonts
= nil then Exit
;
1071 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1073 with e_CharFonts
[FontID
] do
1075 for a
:= 1 to Length(Text) do
1076 with Chars
[Ord(Text[a
])] do
1077 if TextureID
<> -1 then
1079 e_Draw(TextureID
, X
, Y
, 0, True, False);
1080 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1085 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1086 Color
: TRGB
; Scale
: Single = 1.0);
1091 if e_NoGraphics
then Exit
;
1092 if Text = '' then Exit
;
1093 if e_CharFonts
= nil then Exit
;
1094 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1096 with e_CharFonts
[FontID
] do
1098 for a
:= 1 to Length(Text) do
1099 with Chars
[Ord(Text[a
])] do
1100 if TextureID
<> -1 then
1102 if Scale
<> 1.0 then
1105 glScalef(Scale
, Scale
, 0);
1110 e_Draw(TextureID
, X
, Y
, 0, True, False);
1113 if Scale
<> 1.0 then glPopMatrix
;
1115 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1120 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1122 a
, TX
, TY
, len
: Integer;
1126 if e_NoGraphics
then Exit
;
1127 if Text = '' then Exit
;
1128 if e_CharFonts
= nil then Exit
;
1129 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1137 len
:= Length(Text);
1139 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1141 with e_CharFonts
[FontID
] do
1143 for a
:= 1 to len
do
1154 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1159 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1164 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1169 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1174 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1179 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1184 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1189 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1194 with Chars
[Ord(Text[a
])] do
1195 if TextureID
<> -1 then
1199 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1202 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1208 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1216 if Text = '' then Exit
;
1217 if e_CharFonts
= nil then Exit
;
1218 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1220 with e_CharFonts
[FontID
] do
1222 for a
:= 1 to Length(Text) do
1223 with Chars
[Ord(Text[a
])] do
1224 if TextureID
<> -1 then
1226 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1227 e_GetTextureSize(TextureID
, nil, @h2
);
1228 if h2
> h
then h
:= h2
;
1233 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1235 a
, lines
, len
: Integer;
1242 if Text = '' then Exit
;
1243 if e_CharFonts
= nil then Exit
;
1244 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1247 len
:= Length(Text);
1249 with e_CharFonts
[FontID
] do
1251 for a
:= 1 to len
do
1253 if Text[a
] = #10 then
1263 else if Text[a
] in [#1, #2, #3, #4, #18, #19, #20, #21] then
1266 with Chars
[Ord(Text[a
])] do
1267 if TextureID
<> -1 then
1269 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1270 e_GetTextureSize(TextureID
, nil, @h2
);
1271 if h2
> h
then h
:= h2
;
1281 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1287 if e_CharFonts
= nil then Exit
;
1288 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1290 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1291 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1294 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1301 if e_CharFonts
= nil then Exit
;
1302 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1304 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1306 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1307 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1309 if h2
> Result
then Result
:= h2
;
1313 procedure e_CharFont_Remove(FontID
: DWORD
);
1317 with e_CharFonts
[FontID
] do
1318 for a
:= 0 to High(Chars
) do
1319 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1321 e_CharFonts
[FontID
].Live
:= False;
1324 procedure e_CharFont_RemoveAll();
1328 if e_CharFonts
= nil then Exit
;
1330 for a
:= 0 to High(e_CharFonts
) do
1331 e_CharFont_Remove(a
);
1336 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1343 if e_NoGraphics
then Exit
;
1344 e_WriteLog('Creating texture font...', MSG_NOTIFY
);
1348 if e_TextureFonts
<> nil then
1349 for i
:= 0 to High(e_TextureFonts
) do
1350 if e_TextureFonts
[i
].Base
= 0 then
1356 if id
= DWORD(-1) then
1358 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1359 id
:= High(e_TextureFonts
);
1362 with e_TextureFonts
[id
] do
1364 Base
:= glGenLists(XCount
*YCount
);
1365 TextureID
:= e_Textures
[Tex
].tx
.id
;
1366 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1367 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1374 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1375 for loop1
:= 0 to XCount
*YCount
-1 do
1377 cx
:= (loop1
mod XCount
)/XCount
;
1378 cy
:= (loop1
div YCount
)/YCount
;
1380 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1382 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1383 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1385 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1386 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1388 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1389 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1391 glTexCoord2f(cx
, 1.0-cy
);
1394 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1401 procedure e_TextureFontKill(FontID
: DWORD
);
1403 if e_NoGraphics
then Exit
;
1404 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1405 e_TextureFonts
[FontID
].Base
:= 0;
1408 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1410 if e_NoGraphics
then Exit
;
1411 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1412 if Text = '' then Exit
;
1414 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1417 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1420 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1421 glEnable(GL_TEXTURE_2D
);
1422 glTranslated(x
, y
, 0);
1423 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1424 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1425 glDisable(GL_TEXTURE_2D
);
1428 glDisable(GL_BLEND
);
1431 // god forgive me for this, but i cannot figure out how to do it without lists
1432 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1434 if e_NoGraphics
then Exit
;
1439 glColor4ub(0, 0, 0, 128);
1440 glTranslated(X
+1, Y
+1, 0);
1441 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1446 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1447 glTranslated(X
, Y
, 0);
1448 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1453 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1455 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1456 glEnable(GL_TEXTURE_2D
);
1457 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1459 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1461 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1462 glDisable(GL_TEXTURE_2D
);
1463 glDisable(GL_BLEND
);
1466 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1468 result
:= e_TextureFonts
[FontID
].CharWidth
;
1471 procedure e_TextureFontPrintFmt(X
, Y
: Integer; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1473 a
, TX
, TY
, len
: Integer;
1477 if e_NoGraphics
then Exit
;
1478 if Text = '' then Exit
;
1479 if e_TextureFonts
= nil then Exit
;
1480 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1488 len
:= Length(Text);
1490 w
:= e_TextureFonts
[FontID
].CharWidth
;
1492 with e_TextureFonts
[FontID
] do
1494 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1495 glEnable(GL_TEXTURE_2D
);
1496 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1498 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1501 for a
:= 1 to len
do
1512 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1517 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1522 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1527 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1532 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1537 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1542 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1547 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1554 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1559 glDisable(GL_TEXTURE_2D
);
1560 glDisable(GL_BLEND
);
1564 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1565 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1567 if e_NoGraphics
then Exit
;
1568 if Text = '' then Exit
;
1571 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1572 glEnable(GL_TEXTURE_2D
);
1573 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1575 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1580 glColor4ub(0, 0, 0, 128);
1581 glTranslated(x
+1, y
+1, 0);
1582 glScalef(Scale
, Scale
, 0);
1583 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1588 glColor4ub(Red
, Green
, Blue
, 255);
1589 glTranslated(x
, y
, 0);
1590 glScalef(Scale
, Scale
, 0);
1591 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1593 glDisable(GL_TEXTURE_2D
);
1595 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1596 glDisable(GL_BLEND
);
1599 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1603 if e_NoGraphics
then Exit
;
1604 if Integer(ID
) > High(e_TextureFonts
) then
1606 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1607 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1610 procedure e_RemoveAllTextureFont();
1614 if e_NoGraphics
then Exit
;
1615 if e_TextureFonts
= nil then Exit
;
1617 for i
:= 0 to High(e_TextureFonts
) do
1618 if e_TextureFonts
[i
].Base
<> 0 then
1620 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1621 e_TextureFonts
[i
].Base
:= 0;
1624 e_TextureFonts
:= nil;
1627 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1634 function _Point(X
, Y
: Integer): TPoint2i
;
1640 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1644 Result
.Width
:= Width
;
1645 Result
.Height
:= Height
;
1648 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1657 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1659 pixels
, obuf
, scln
, ps
, pd
: PByte;
1662 i
, x
, y
, res
: Integer;
1663 sign
: array [0..7] of Byte;
1664 hbuf
: array [0..12] of Byte;
1669 if e_NoGraphics
then Exit
;
1672 // first, extract and pack graphics data
1673 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1675 GetMem(pixels
, Width
*Height
*3);
1677 FillChar(pixels
^, Width
*Height
*3, 0);
1678 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1679 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1681 if e_FastScreenshots
then
1684 GetMem(scln
, (Width
*3+1)*Height
);
1688 Inc(ps
, (Width
*3)*(Height
-1));
1689 for i
:= 0 to Height
-1 do
1693 Move(ps
^, pd
^, Width
*3);
1705 obufsize
:= (Width
*3+1)*Height
*2;
1706 GetMem(obuf
, obufsize
);
1711 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1712 if res
= Z_OK
then break
;
1713 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1714 obufsize
:= obufsize
*2;
1717 GetMem(obuf
, obufsize
);
1719 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1732 st
.writeBuffer(sign
, 8);
1733 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1736 writeIntBE(st
, LongWord(13));
1741 st
.writeBuffer(sign
, 4);
1742 crc
:= crc32(0, @sign
[0], 4);
1745 hbuf
[2] := (Width
shr 8) and $ff;
1746 hbuf
[3] := Width
and $ff;
1749 hbuf
[6] := (Height
shr 8) and $ff;
1750 hbuf
[7] := Height
and $ff;
1751 hbuf
[8] := 8; // bit depth
1752 hbuf
[9] := 2; // RGB
1753 hbuf
[10] := 0; // compression method
1754 hbuf
[11] := 0; // filter method
1755 hbuf
[12] := 0; // no interlace
1756 crc
:= crc32(crc
, @hbuf
[0], 13);
1757 st
.writeBuffer(hbuf
, 13);
1758 writeIntBE(st
, crc
);
1759 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1762 writeIntBE(st
, LongWord(dlen
));
1767 st
.writeBuffer(sign
, 4);
1768 crc
:= crc32(0, @sign
[0], 4);
1769 crc
:= crc32(crc
, obuf
, dlen
);
1770 st
.writeBuffer(obuf
^, dlen
);
1771 writeIntBE(st
, crc
);
1772 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
1775 writeIntBE(st
, LongWord(0));
1780 st
.writeBuffer(sign
, 4);
1781 crc
:= crc32(0, @sign
[0], 4);
1782 writeIntBE(st
, crc
);
1783 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
1785 if obuf
<> nil then FreeMem(obuf
);
1790 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
1791 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
1794 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
1796 //writeln(stderr, 'moving pixels...');
1797 for y
:= Height
-1 downto 0 do
1799 for x
:= 0 to Width
-1 do
1801 clr
.r
:= ps
^; Inc(ps
);
1802 clr
.g
:= ps
^; Inc(ps
);
1803 clr
.b
:= ps
^; Inc(ps
);
1805 SetPixel32(img
, x
, y
, clr
);
1808 GlobalMetadata
.ClearMetaItems();
1809 GlobalMetadata
.ClearMetaItemsForSaving();
1810 //writeln(stderr, 'compressing image...');
1811 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
1812 //writeln(stderr, 'done!');