1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
22 SysUtils
, Classes
, Math
, e_log
, e_texture
, SDL2
, GL
, GLExt
, MAPDEF
, ImagingTypes
, Imaging
, ImagingUtility
;
25 TMirrorType
=(M_NONE
, M_HORIZONTAL
, M_VERTICAL
);
26 TBlending
=(B_NONE
, B_BLEND
, B_FILTER
, B_INVERT
);
37 Left
, Top
, Right
, Bottom
: Integer;
55 //------------------------------------------------------------------
57 //------------------------------------------------------------------
59 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
60 procedure e_ResizeWindow(Width
, Height
: Integer);
62 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
63 Blending
: Boolean; Mirror
: TMirrorType
= M_NONE
);
64 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
65 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= M_NONE
);
66 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
67 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
68 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
69 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
70 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
71 AlphaChannel
: Boolean; Blending
: Boolean);
72 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
73 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
74 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
75 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
76 Blending
: TBlending
= B_NONE
);
77 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
78 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
80 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
81 function e_CreateTexture(FileName
: string; var ID
: DWORD
): Boolean;
82 function e_CreateTextureEx(FileName
: string; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
83 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
84 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
85 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
86 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
87 procedure e_DeleteTexture(ID
: DWORD
);
88 procedure e_RemoveAllTextures();
91 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
92 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
93 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
94 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
95 Color
: TRGB
; Scale
: Single = 1.0);
96 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
97 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
98 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
99 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
100 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
101 procedure e_CharFont_Remove(FontID
: DWORD
);
102 procedure e_CharFont_RemoveAll();
105 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
107 procedure e_TextureFontKill(FontID
: DWORD
);
108 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
109 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
110 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
111 procedure e_TextureFontPrintFmt(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
112 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
113 procedure e_RemoveAllTextureFont();
115 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
116 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
118 procedure e_ReleaseEngine();
119 procedure e_BeginRender();
120 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
121 procedure e_Clear(); overload
;
122 procedure e_EndRender();
124 function e_GetGamma(win
: PSDL_Window
): Byte;
125 procedure e_SetGamma(win
: PSDL_Window
;Gamma
: Byte);
127 procedure e_MakeScreenshot(st
: TStream
; Width
, Height
: Word);
129 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
130 function _Point(X
, Y
: Integer): TPoint2i
;
131 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
132 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
134 //function e_getTextGLId (ID: DWORD): GLuint;
138 e_NoGraphics
: Boolean = False;
139 e_FastScreenshots
: Boolean = true; // it's REALLY SLOW with `false`
153 TTextureFont
= record
163 Chars
: array[0..255] of
173 TSavedTexture
= record
180 e_Textures
: array of TTexture
= nil;
181 e_TextureFonts
: array of TTextureFont
= nil;
182 e_CharFonts
: array of TCharFont
;
183 //e_SavedTextures: array of TSavedTexture;
185 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
187 //------------------------------------------------------------------
188 // Èíèöèàëèçèðóåò OpenGL
189 //------------------------------------------------------------------
190 procedure e_InitGL();
194 e_DummyTextures
:= True;
200 glDisable(GL_DEPTH_TEST
);
201 glEnable(GL_SCISSOR_TEST
);
202 glClearColor(0, 0, 0, 0);
205 procedure e_SetViewPort(X
, Y
, Width
, Height
: Word);
207 mat
: Array [0..15] of GLDouble
;
210 if e_NoGraphics
then Exit
;
212 glScissor(X
, Y
, Width
, Height
);
213 glViewport(X
, Y
, Width
, Height
);
214 //gluOrtho2D(0, Width, Height, 0);
216 glMatrixMode(GL_PROJECTION
);
218 mat
[ 0] := 2.0 / Width
;
224 mat
[ 5] := -2.0 / Height
;
238 glLoadMatrixd(@mat
[0]);
240 glMatrixMode(GL_MODELVIEW
);
244 //------------------------------------------------------------------
245 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
246 //------------------------------------------------------------------
247 function FindTexture(): DWORD
;
251 if e_Textures
<> nil then
252 for i
:= 0 to High(e_Textures
) do
253 if e_Textures
[i
].tx
.Width
= 0 then
259 if e_Textures
= nil then
261 SetLength(e_Textures
, 32);
266 Result
:= High(e_Textures
) + 1;
267 SetLength(e_Textures
, Length(e_Textures
) + 32);
271 //------------------------------------------------------------------
273 //------------------------------------------------------------------
274 function e_CreateTexture(FileName
: String; var ID
: DWORD
): Boolean;
281 e_WriteLog('Loading texture from '+FileName
, MSG_NOTIFY
);
283 find_id
:= FindTexture();
285 if not LoadTexture(FileName
, e_Textures
[find_id
].tx
, e_Textures
[find_id
].tx
.Width
,
286 e_Textures
[find_id
].tx
.Height
, @fmt
) then Exit
;
293 function e_CreateTextureEx(FileName
: String; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
300 find_id
:= FindTexture();
302 if not LoadTextureEx(FileName
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
309 function e_CreateTextureMem(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
): Boolean;
316 find_id
:= FindTexture
;
318 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
;
325 function e_CreateTextureMemEx(pData
: Pointer; dataSize
: LongInt; var ID
: DWORD
; fX
, fY
, fWidth
, fHeight
: Word): Boolean;
332 find_id
:= FindTexture();
334 if not LoadTextureMemEx(pData
, dataSize
, e_Textures
[find_id
].tx
, fX
, fY
, fWidth
, fHeight
, @fmt
) then exit
;
341 function e_CreateTextureImg (var img
: TImageData
; var ID
: DWORD
): Boolean;
347 find_id
:= FindTexture();
348 if not LoadTextureImg(img
, e_Textures
[find_id
].tx
, tw
, th
, @fmt
) then exit
;
353 procedure e_GetTextureSize(ID
: DWORD
; Width
, Height
: PWord);
355 if Width
<> nil then Width
^ := e_Textures
[ID
].tx
.Width
;
356 if Height
<> nil then Height
^ := e_Textures
[ID
].tx
.Height
;
359 function e_GetTextureSize2(ID
: DWORD
): TRectWH
;
367 w
:= e_Textures
[ID
].tx
.Width
;
368 h
:= e_Textures
[ID
].tx
.Height
;
375 if e_NoGraphics
then Exit
;
377 data
:= GetMemory(w
*h
*4);
378 glEnable(GL_TEXTURE_2D
);
379 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
380 glGetTexImage(GL_TEXTURE_2D
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
382 for y
:= h
-1 downto 0 do
389 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
395 Result
.Y
:= h
-lastline
;
407 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
413 Result
.Height
:= h
-lastline
-Result
.Y
;
425 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
431 Result
.X
:= lastline
+1;
436 for x
:= w
-1 downto 0 do
443 a
:= Byte((data
+y
*w
*4+x
*4+3)^) <> 0;
449 Result
.Width
:= lastline
-Result
.X
+1;
457 procedure e_ResizeWindow(Width
, Height
: Integer);
461 e_SetViewPort(0, 0, Width
, Height
);
464 procedure drawTxQuad (x0
, y0
, w
, h
: Integer; u
, v
: single; Mirror
: TMirrorType
);
466 x1
, y1
, tmp
: Integer;
468 if (w
< 1) or (h
< 1) then exit
;
471 if Mirror
= M_HORIZONTAL
then begin tmp
:= x1
; x1
:= x0
; x0
:= tmp
; end
472 else if Mirror
= M_VERTICAL
then begin tmp
:= y1
; y1
:= y0
; y0
:= tmp
; end;
473 glTexCoord2f(0, v
); glVertex2i(x0
, y0
);
474 glTexCoord2f(0, 0); glVertex2i(x0
, y1
);
475 glTexCoord2f(u
, 0); glVertex2i(x1
, y1
);
476 glTexCoord2f(u
, v
); glVertex2i(x1
, y0
);
479 procedure e_Draw(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
480 Blending
: Boolean; Mirror
: TMirrorType
= M_NONE
);
482 if e_NoGraphics
then Exit
;
483 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
485 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
490 if (AlphaChannel
) or (Alpha
> 0) then
491 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
494 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
497 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
499 glEnable(GL_TEXTURE_2D
);
500 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
503 drawTxQuad(X
, Y
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
505 //u := e_Textures[ID].tx.u;
506 //v := e_Textures[ID].tx.v;
509 if Mirror = M_NONE then
511 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
512 glTexCoord2f(0, 0); glVertex2i(X, Y);
513 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
514 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
517 if Mirror = M_HORIZONTAL then
519 glTexCoord2f(u, 0); glVertex2i(X, Y);
520 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
521 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
522 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
525 if Mirror = M_VERTICAL then
527 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
528 glTexCoord2f(0, -v); glVertex2i(X, Y);
529 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
530 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
539 procedure e_DrawSize(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
540 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
544 if e_NoGraphics
then Exit
;
545 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
547 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
552 if (AlphaChannel
) or (Alpha
> 0) then
553 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
556 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
559 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
561 glEnable(GL_TEXTURE_2D
);
562 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
564 u
:= e_Textures
[ID
].tx
.u
;
565 v
:= e_Textures
[ID
].tx
.v
;
568 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
569 glTexCoord2f(u
, v
); glVertex2i(X
+ Width
, Y
);
570 glTexCoord2f(u
, 0); glVertex2i(X
+ Width
, Y
+ Height
);
571 glTexCoord2f(0, 0); glVertex2i(X
, Y
+ Height
);
577 procedure e_DrawSizeMirror(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
578 Blending
: Boolean; Width
, Height
: Word; Mirror
: TMirrorType
= M_NONE
);
580 if e_NoGraphics
then Exit
;
581 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
583 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
588 if (AlphaChannel
) or (Alpha
> 0) then
589 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
592 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
595 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
597 glEnable(GL_TEXTURE_2D
);
598 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
600 drawTxQuad(X
, Y
, Width
, Height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
606 procedure e_DrawFill(ID
: DWORD
; X
, Y
: Integer; XCount
, YCount
: Word; Alpha
: Integer;
607 AlphaChannel
: Boolean; Blending
: Boolean);
609 X2
, Y2
, dx
, w
, h
: Integer;
612 if e_NoGraphics
then Exit
;
613 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
615 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
620 if (AlphaChannel
) or (Alpha
> 0) then
621 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
624 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
627 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
635 glEnable(GL_TEXTURE_2D
);
636 glBindTexture(GL_TEXTURE_2D
, e_Textures
[ID
].tx
.id
);
638 X2
:= X
+ e_Textures
[ID
].tx
.width
* XCount
;
639 Y2
:= Y
+ e_Textures
[ID
].tx
.height
* YCount
;
641 //k8: this SHOULD work... i hope
642 if (e_Textures
[ID
].tx
.width
= e_Textures
[ID
].tx
.glwidth
) and (e_Textures
[ID
].tx
.height
= e_Textures
[ID
].tx
.glheight
) then
645 glTexCoord2i(0, YCount
); glVertex2i(X
, Y
);
646 glTexCoord2i(XCount
, YCount
); glVertex2i(X2
, Y
);
647 glTexCoord2i(XCount
, 0); glVertex2i(X2
, Y2
);
648 glTexCoord2i(0, 0); glVertex2i(X
, Y2
);
655 u
:= e_Textures
[ID
].tx
.u
;
656 v
:= e_Textures
[ID
].tx
.v
;
657 w
:= e_Textures
[ID
].tx
.width
;
658 h
:= e_Textures
[ID
].tx
.height
;
665 glTexCoord2f(0, v
); glVertex2i(X
, Y
);
666 glTexCoord2f(u
, v
); glVertex2i(X
+w
, Y
);
667 glTexCoord2f(u
, 0); glVertex2i(X
+w
, Y
+h
);
668 glTexCoord2f(0, 0); glVertex2i(X
, Y
+h
);
682 procedure e_DrawAdv(ID
: DWORD
; X
, Y
: Integer; Alpha
: Byte; AlphaChannel
: Boolean;
683 Blending
: Boolean; Angle
: Single; RC
: PDFPoint
; Mirror
: TMirrorType
= M_NONE
);
685 if e_NoGraphics
then Exit
;
687 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
689 if (Alpha
> 0) or (AlphaChannel
) or (Blending
) then
694 if (AlphaChannel
) or (Alpha
> 0) then
695 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
698 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255-Alpha
);
701 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
703 if (Angle
<> 0) and (RC
<> nil) then
706 glTranslatef(X
+RC
.X
, Y
+RC
.Y
, 0);
707 glRotatef(Angle
, 0, 0, 1);
708 glTranslatef(-(X
+RC
.X
), -(Y
+RC
.Y
), 0);
711 glEnable(GL_TEXTURE_2D
);
712 glBindTexture(GL_TEXTURE_2D
, e_Textures
[id
].tx
.id
);
713 glBegin(GL_QUADS
); //0-1 1-1
715 drawTxQuad(X
, Y
, e_Textures
[id
].tx
.width
, e_Textures
[id
].tx
.height
, e_Textures
[ID
].tx
.u
, e_Textures
[ID
].tx
.v
, Mirror
);
724 procedure e_DrawPoint(Size
: Byte; X
, Y
: Integer; Red
, Green
, Blue
: Byte);
726 if e_NoGraphics
then Exit
;
727 glDisable(GL_TEXTURE_2D
);
728 glColor3ub(Red
, Green
, Blue
);
731 if (Size
= 2) or (Size
= 4) then
735 glVertex2f(X
+0.3, Y
+1.0);
738 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
741 procedure e_LineCorrection(var X1
, Y1
, X2
, Y2
: Integer);
743 // Make lines only top-left/bottom-right and top-right/bottom-left
755 // Pixel-perfect hack
763 procedure e_DrawQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
765 nX1
, nY1
, nX2
, nY2
: Integer;
767 if e_NoGraphics
then Exit
;
768 // Only top-left/bottom-right quad
785 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
789 glDisable(GL_TEXTURE_2D
);
790 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
794 nX1
:= X1
; nY1
:= Y1
;
795 nX2
:= X2
; nY2
:= Y1
;
796 e_LineCorrection(nX1
, nY1
, nX2
, nY2
); // Pixel-perfect lines
797 glVertex2i(nX1
, nY1
);
798 glVertex2i(nX2
, nY2
);
800 nX1
:= X2
; nY1
:= Y1
;
801 nX2
:= X2
; nY2
:= Y2
;
802 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
803 glVertex2i(nX1
, nY1
);
804 glVertex2i(nX2
, nY2
);
806 nX1
:= X2
; nY1
:= Y2
;
807 nX2
:= X1
; nY2
:= Y2
;
808 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
809 glVertex2i(nX1
, nY1
);
810 glVertex2i(nX2
, nY2
);
812 nX1
:= X1
; nY1
:= Y2
;
813 nX2
:= X1
; nY2
:= Y1
;
814 e_LineCorrection(nX1
, nY1
, nX2
, nY2
);
815 glVertex2i(nX1
, nY1
);
816 glVertex2i(nX2
, nY2
);
819 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
824 procedure e_DrawFillQuad(X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
, Alpha
: Byte;
825 Blending
: TBlending
= B_NONE
);
827 if e_NoGraphics
then Exit
;
828 if (Alpha
> 0) or (Blending
<> B_NONE
) then
833 if Blending
= B_BLEND
then
834 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
)
836 if Blending
= B_FILTER
then
837 glBlendFunc(GL_DST_COLOR
, GL_SRC_COLOR
)
839 if Blending
= B_INVERT
then
840 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
)
843 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
845 glDisable(GL_TEXTURE_2D
);
846 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
858 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
864 // ////////////////////////////////////////////////////////////////////////// //
865 procedure e_DarkenQuad (x0
, y0
, x1
, y1
: Integer; a
: Integer);
867 if (a
< 0) then a
:= 0;
868 if (a
> 255) then a
:= 255;
870 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
871 glDisable(GL_TEXTURE_2D
);
872 glColor4ub(0, 0, 0, Byte(255-a
));
879 //glRect(x, y, x+w, y+h);
880 glColor4ub(1, 1, 1, 1);
882 //glBlendEquation(GL_FUNC_ADD);
885 procedure e_DarkenQuadWH (x
, y
, w
, h
: Integer; a
: Integer);
887 if (w
> 0) and (h
> 0) then e_DarkenQuad(x
, y
, x
+w
, y
+h
, a
);
891 procedure e_DrawLine(Width
: Byte; X1
, Y1
, X2
, Y2
: Integer; Red
, Green
, Blue
: Byte; Alpha
: Byte = 0);
893 if e_NoGraphics
then Exit
;
894 // Pixel-perfect lines
896 e_LineCorrection(X1
, Y1
, X2
, Y2
);
901 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
905 glDisable(GL_TEXTURE_2D
);
906 glColor4ub(Red
, Green
, Blue
, 255-Alpha
);
914 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
919 //------------------------------------------------------------------
920 // Óäàëÿåò òåêñòóðó èç ìàññèâà
921 //------------------------------------------------------------------
922 procedure e_DeleteTexture(ID
: DWORD
);
924 if not e_NoGraphics
then
925 glDeleteTextures(1, @e_Textures
[ID
].tx
.id
);
926 e_Textures
[ID
].tx
.id
:= 0;
927 e_Textures
[ID
].tx
.Width
:= 0;
928 e_Textures
[ID
].tx
.Height
:= 0;
931 //------------------------------------------------------------------
932 // Óäàëÿåò âñå òåêñòóðû
933 //------------------------------------------------------------------
934 procedure e_RemoveAllTextures();
938 if e_Textures
= nil then Exit
;
940 for i
:= 0 to High(e_Textures
) do
941 if e_Textures
[i
].tx
.Width
<> 0 then e_DeleteTexture(i
);
945 //------------------------------------------------------------------
947 //------------------------------------------------------------------
948 procedure e_ReleaseEngine();
951 e_RemoveAllTextureFont
;
954 procedure e_BeginRender();
956 if e_NoGraphics
then Exit
;
957 glEnable(GL_ALPHA_TEST
);
958 glAlphaFunc(GL_GREATER
, 0.0);
961 procedure e_Clear(Mask
: TGLbitfield
; Red
, Green
, Blue
: Single); overload
;
963 if e_NoGraphics
then Exit
;
964 glClearColor(Red
, Green
, Blue
, 0);
968 procedure e_Clear(); overload
;
970 if e_NoGraphics
then Exit
;
971 glClearColor(0, 0, 0, 0);
972 glClear(GL_COLOR_BUFFER_BIT
);
975 procedure e_EndRender();
977 if e_NoGraphics
then Exit
;
981 function e_GetGamma(win
: PSDL_Window
): Byte;
983 ramp
: array [0..256*3-1] of Word;
984 rgb
: array [0..2] of Double;
993 if e_NoGraphics
then Exit
;
998 SDL_GetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1007 for j
:= min
to max
- 1 do
1010 B
:= (j
mod 256)/256;
1012 sum
:= sum
+ ln(A
)/ln(B
);
1015 rgb
[i
] := sum
/ count
;
1018 Result
:= 100 - Trunc(((rgb
[0] + rgb
[1] + rgb
[2])/3 - 0.23) * 100/(2.7 - 0.23));
1021 procedure e_SetGamma(win
: PSDL_Window
; Gamma
: Byte);
1023 ramp
: array [0..256*3-1] of Word;
1028 if e_NoGraphics
then Exit
;
1029 g
:= (100 - Gamma
)*(2.7 - 0.23)/100 + 0.23;
1031 for i
:= 0 to 255 do
1033 r
:= Exp(g
* ln(i
/256))*65536;
1034 if r
< 0 then r
:= 0
1035 else if r
> 65535 then r
:= 65535;
1036 ramp
[i
] := trunc(r
);
1037 ramp
[i
+ 256] := trunc(r
);
1038 ramp
[i
+ 512] := trunc(r
);
1041 SDL_SetWindowGammaRamp(win
, @ramp
[0], @ramp
[256], @ramp
[512]);
1044 function e_CharFont_Create(sp
: ShortInt=0): DWORD
;
1048 e_WriteLog('Creating CharFont...', MSG_NOTIFY
);
1052 if e_CharFonts
<> nil then
1053 for i
:= 0 to High(e_CharFonts
) do
1054 if not e_CharFonts
[i
].alive
then
1060 if id
= DWORD(-1) then
1062 SetLength(e_CharFonts
, Length(e_CharFonts
) + 1);
1063 id
:= High(e_CharFonts
);
1066 with e_CharFonts
[id
] do
1068 for i
:= 0 to High(Chars
) do
1082 procedure e_CharFont_AddChar(FontID
: DWORD
; Texture
: Integer; c
: Char; w
: Byte);
1084 with e_CharFonts
[FontID
].Chars
[Ord(c
)] do
1086 TextureID
:= Texture
;
1091 procedure e_CharFont_Print(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1095 if e_NoGraphics
then Exit
;
1096 if Text = '' then Exit
;
1097 if e_CharFonts
= nil then Exit
;
1098 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1100 with e_CharFonts
[FontID
] do
1102 for a
:= 1 to Length(Text) do
1103 with Chars
[Ord(Text[a
])] do
1104 if TextureID
<> -1 then
1106 e_Draw(TextureID
, X
, Y
, 0, True, False);
1107 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1112 procedure e_CharFont_PrintEx(FontID
: DWORD
; X
, Y
: Integer; Text: string;
1113 Color
: TRGB
; Scale
: Single = 1.0);
1118 if e_NoGraphics
then Exit
;
1119 if Text = '' then Exit
;
1120 if e_CharFonts
= nil then Exit
;
1121 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1123 with e_CharFonts
[FontID
] do
1125 for a
:= 1 to Length(Text) do
1126 with Chars
[Ord(Text[a
])] do
1127 if TextureID
<> -1 then
1129 if Scale
<> 1.0 then
1132 glScalef(Scale
, Scale
, 0);
1137 e_Draw(TextureID
, X
, Y
, 0, True, False);
1140 if Scale
<> 1.0 then glPopMatrix
;
1142 X
:= X
+Width
+IfThen(a
= Length(Text), 0, Space
);
1147 procedure e_CharFont_PrintFmt(FontID
: DWORD
; X
, Y
: Integer; Text: string);
1149 a
, TX
, TY
, len
: Integer;
1153 if e_NoGraphics
then Exit
;
1154 if Text = '' then Exit
;
1155 if e_CharFonts
= nil then Exit
;
1156 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1164 len
:= Length(Text);
1166 e_CharFont_GetSize(FontID
, 'A', w
, h
);
1168 with e_CharFonts
[FontID
] do
1170 for a
:= 1 to len
do
1181 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1186 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1191 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1196 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1201 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1206 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1211 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1216 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1221 with Chars
[Ord(Text[a
])] do
1222 if TextureID
<> -1 then
1226 e_Draw(TextureID
, TX
, TY
, 0, True, False);
1229 TX
:= TX
+Width
+IfThen(a
= Length(Text), 0, Space
);
1235 procedure e_CharFont_GetSize(FontID
: DWORD
; Text: string; var w
, h
: Word);
1243 if Text = '' then Exit
;
1244 if e_CharFonts
= nil then Exit
;
1245 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1247 with e_CharFonts
[FontID
] do
1249 for a
:= 1 to Length(Text) do
1250 with Chars
[Ord(Text[a
])] do
1251 if TextureID
<> -1 then
1253 w
:= w
+Width
+IfThen(a
= Length(Text), 0, Space
);
1254 e_GetTextureSize(TextureID
, nil, @h2
);
1255 if h2
> h
then h
:= h2
;
1260 procedure e_CharFont_GetSizeFmt(FontID
: DWORD
; Text: string; var w
, h
: Word);
1262 a
, lines
, len
: Integer;
1269 if Text = '' then Exit
;
1270 if e_CharFonts
= nil then Exit
;
1271 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1274 len
:= Length(Text);
1276 with e_CharFonts
[FontID
] do
1278 for a
:= 1 to len
do
1280 if Text[a
] = #10 then
1290 else if Text[a
] in [#1, #2, #3, #4, #18, #19, #20, #21] then
1293 with Chars
[Ord(Text[a
])] do
1294 if TextureID
<> -1 then
1296 w2
:= w2
+ Width
+ IfThen(a
= len
, 0, Space
);
1297 e_GetTextureSize(TextureID
, nil, @h2
);
1298 if h2
> h
then h
:= h2
;
1308 function e_CharFont_GetMaxWidth(FontID
: DWORD
): Word;
1314 if e_CharFonts
= nil then Exit
;
1315 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1317 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1318 Result
:= Max(Result
, e_CharFonts
[FontID
].Chars
[a
].Width
);
1321 function e_CharFont_GetMaxHeight(FontID
: DWORD
): Word;
1328 if e_CharFonts
= nil then Exit
;
1329 if Integer(FontID
) > High(e_CharFonts
) then Exit
;
1331 for a
:= 0 to High(e_CharFonts
[FontID
].Chars
) do
1333 if e_CharFonts
[FontID
].Chars
[a
].TextureID
<> -1 then
1334 e_GetTextureSize(e_CharFonts
[FontID
].Chars
[a
].TextureID
, nil, @h2
)
1336 if h2
> Result
then Result
:= h2
;
1340 procedure e_CharFont_Remove(FontID
: DWORD
);
1344 with e_CharFonts
[FontID
] do
1345 for a
:= 0 to High(Chars
) do
1346 if Chars
[a
].TextureID
<> -1 then e_DeleteTexture(Chars
[a
].TextureID
);
1348 e_CharFonts
[FontID
].alive
:= False;
1351 procedure e_CharFont_RemoveAll();
1355 if e_CharFonts
= nil then Exit
;
1357 for a
:= 0 to High(e_CharFonts
) do
1358 e_CharFont_Remove(a
);
1363 procedure e_TextureFontBuild(Tex
: DWORD
; var FontID
: DWORD
; XCount
, YCount
: Word;
1370 if e_NoGraphics
then Exit
;
1371 e_WriteLog('Creating texture font...', MSG_NOTIFY
);
1375 if e_TextureFonts
<> nil then
1376 for i
:= 0 to High(e_TextureFonts
) do
1377 if e_TextureFonts
[i
].Base
= 0 then
1383 if id
= DWORD(-1) then
1385 SetLength(e_TextureFonts
, Length(e_TextureFonts
) + 1);
1386 id
:= High(e_TextureFonts
);
1389 with e_TextureFonts
[id
] do
1391 Base
:= glGenLists(XCount
*YCount
);
1392 TextureID
:= e_Textures
[Tex
].tx
.id
;
1393 CharWidth
:= (e_Textures
[Tex
].tx
.Width
div XCount
)+Space
;
1394 CharHeight
:= e_Textures
[Tex
].tx
.Height
div YCount
;
1401 glBindTexture(GL_TEXTURE_2D
, e_Textures
[Tex
].tx
.id
);
1402 for loop1
:= 0 to XCount
*YCount
-1 do
1404 cx
:= (loop1
mod XCount
)/XCount
;
1405 cy
:= (loop1
div YCount
)/YCount
;
1407 glNewList(e_TextureFonts
[id
].Base
+loop1
, GL_COMPILE
);
1409 glTexCoord2f(cx
, 1.0-cy
-1/YCount
);
1410 glVertex2i(0, e_Textures
[Tex
].tx
.Height
div YCount
);
1412 glTexCoord2f(cx
+1/XCount
, 1.0-cy
-1/YCount
);
1413 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, e_Textures
[Tex
].tx
.Height
div YCount
);
1415 glTexCoord2f(cx
+1/XCount
, 1.0-cy
);
1416 glVertex2i(e_Textures
[Tex
].tx
.Width
div XCount
, 0);
1418 glTexCoord2f(cx
, 1.0-cy
);
1421 glTranslated((e_Textures
[Tex
].tx
.Width
div XCount
)+Space
, 0, 0);
1428 procedure e_TextureFontKill(FontID
: DWORD
);
1430 if e_NoGraphics
then Exit
;
1431 glDeleteLists(e_TextureFonts
[FontID
].Base
, 256);
1432 e_TextureFonts
[FontID
].Base
:= 0;
1435 procedure e_TextureFontPrint(X
, Y
: GLint
; Text: string; FontID
: DWORD
);
1437 if e_NoGraphics
then Exit
;
1438 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1439 if Text = '' then Exit
;
1441 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1444 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1447 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1448 glEnable(GL_TEXTURE_2D
);
1449 glTranslated(x
, y
, 0);
1450 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1451 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1452 glDisable(GL_TEXTURE_2D
);
1455 glDisable(GL_BLEND
);
1458 // god forgive me for this, but i cannot figure out how to do it without lists
1459 procedure e_TextureFontPrintChar(X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1461 if e_NoGraphics
then Exit
;
1466 glColor4ub(0, 0, 0, 128);
1467 glTranslated(X
+1, Y
+1, 0);
1468 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1473 glColor4ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
, 255);
1474 glTranslated(X
, Y
, 0);
1475 glCallLists(1, GL_UNSIGNED_BYTE
, @Ch
);
1480 procedure e_TextureFontPrintCharEx (X
, Y
: Integer; Ch
: Char; FontID
: DWORD
; Shadow
: Boolean = False);
1482 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1483 glEnable(GL_TEXTURE_2D
);
1484 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1486 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1488 e_TextureFontPrintChar(X
, Y
, Ch
, FontID
, Shadow
);
1489 glDisable(GL_TEXTURE_2D
);
1490 glDisable(GL_BLEND
);
1493 function e_TextureFontCharWidth (ch
: Char; FontID
: DWORD
): Integer;
1495 result
:= e_TextureFonts
[FontID
].CharWidth
;
1498 procedure e_TextureFontPrintFmt(X
, Y
: Integer; Text: string; FontID
: DWORD
; Shadow
: Boolean = False);
1500 a
, TX
, TY
, len
: Integer;
1504 if e_NoGraphics
then Exit
;
1505 if Text = '' then Exit
;
1506 if e_TextureFonts
= nil then Exit
;
1507 if Integer(FontID
) > High(e_TextureFonts
) then Exit
;
1515 len
:= Length(Text);
1517 w
:= e_TextureFonts
[FontID
].CharWidth
;
1519 with e_TextureFonts
[FontID
] do
1521 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1522 glEnable(GL_TEXTURE_2D
);
1523 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1525 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1528 for a
:= 1 to len
do
1539 c
.R
:= 0; c
.G
:= 0; c
.B
:= 0;
1544 c
.R
:= 255; c
.G
:= 255; c
.B
:= 255;
1549 c
.R
:= c
.R
div 2; c
.G
:= c
.G
div 2; c
.B
:= c
.B
div 2;
1554 c
.R
:= Min(c
.R
* 2, 255); c
.G
:= Min(c
.G
* 2, 255); c
.B
:= Min(c
.B
* 2, 255);
1559 c
.R
:= 255; c
.G
:= 0; c
.B
:= 0;
1564 c
.R
:= 0; c
.G
:= 255; c
.B
:= 0;
1569 c
.R
:= 0; c
.G
:= 0; c
.B
:= 255;
1574 c
.R
:= 255; c
.G
:= 255; c
.B
:= 0;
1581 e_TextureFontPrintChar(TX
, TY
, Text[a
], FontID
, Shadow
);
1586 glDisable(GL_TEXTURE_2D
);
1587 glDisable(GL_BLEND
);
1591 procedure e_TextureFontPrintEx(X
, Y
: GLint
; Text: string; FontID
: DWORD
; Red
, Green
,
1592 Blue
: Byte; Scale
: Single; Shadow
: Boolean = False);
1594 if e_NoGraphics
then Exit
;
1595 if Text = '' then Exit
;
1598 glBindTexture(GL_TEXTURE_2D
, e_TextureFonts
[FontID
].TextureID
);
1599 glEnable(GL_TEXTURE_2D
);
1600 glListBase(DWORD(Integer(e_TextureFonts
[FontID
].Base
)-32));
1602 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1607 glColor4ub(0, 0, 0, 128);
1608 glTranslated(x
+1, y
+1, 0);
1609 glScalef(Scale
, Scale
, 0);
1610 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1615 glColor4ub(Red
, Green
, Blue
, 255);
1616 glTranslated(x
, y
, 0);
1617 glScalef(Scale
, Scale
, 0);
1618 glCallLists(Length(Text), GL_UNSIGNED_BYTE
, PChar(Text));
1620 glDisable(GL_TEXTURE_2D
);
1622 glColor3ub(e_Colors
.R
, e_Colors
.G
, e_Colors
.B
);
1623 glDisable(GL_BLEND
);
1626 procedure e_TextureFontGetSize(ID
: DWORD
; out CharWidth
, CharHeight
: Byte);
1630 if e_NoGraphics
then Exit
;
1631 if Integer(ID
) > High(e_TextureFonts
) then
1633 CharWidth
:= e_TextureFonts
[ID
].CharWidth
;
1634 CharHeight
:= e_TextureFonts
[ID
].CharHeight
;
1637 procedure e_RemoveAllTextureFont();
1641 if e_NoGraphics
then Exit
;
1642 if e_TextureFonts
= nil then Exit
;
1644 for i
:= 0 to High(e_TextureFonts
) do
1645 if e_TextureFonts
[i
].Base
<> 0 then
1647 glDeleteLists(e_TextureFonts
[i
].Base
, 256);
1648 e_TextureFonts
[i
].Base
:= 0;
1651 e_TextureFonts
:= nil;
1654 function _RGB(Red
, Green
, Blue
: Byte): TRGB
;
1661 function _Point(X
, Y
: Integer): TPoint2i
;
1667 function _Rect(X
, Y
: Integer; Width
, Height
: Word): TRectWH
;
1671 Result
.Width
:= Width
;
1672 Result
.Height
:= Height
;
1675 function _TRect(L
, T
, R
, B
: LongInt): TRect
;
1684 procedure e_MakeScreenshot (st
: TStream
; Width
, Height
: Word);
1686 pixels
, obuf
, scln
, ps
, pd
: PByte;
1689 i
, x
, y
, res
: Integer;
1690 sign
: array [0..7] of Byte;
1691 hbuf
: array [0..12] of Byte;
1696 if e_NoGraphics
then Exit
;
1699 // first, extract and pack graphics data
1700 if (Width
mod 4) > 0 then Width
:= Width
+4-(Width
mod 4);
1702 GetMem(pixels
, Width
*Height
*3);
1704 FillChar(pixels
^, Width
*Height
*3, 0);
1705 glReadPixels(0, 0, Width
, Height
, GL_RGB
, GL_UNSIGNED_BYTE
, pixels
);
1706 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1708 if e_FastScreenshots
then
1711 GetMem(scln
, (Width
*3+1)*Height
);
1715 Inc(ps
, (Width
*3)*(Height
-1));
1716 for i
:= 0 to Height
-1 do
1720 Move(ps
^, pd
^, Width
*3);
1732 obufsize
:= (Width
*3+1)*Height
*2;
1733 GetMem(obuf
, obufsize
);
1738 res
:= compress2(Pointer(obuf
), dlen
, Pointer(pixels
), (Width
*3+1)*Height
, 9);
1739 if res
= Z_OK
then break
;
1740 if res
<> Z_BUF_ERROR
then raise Exception
.Create('can''t pack data for PNG');
1741 obufsize
:= obufsize
*2;
1744 GetMem(obuf
, obufsize
);
1746 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1759 st
.writeBuffer(sign
, 8);
1760 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1763 writeIntBE(st
, LongWord(13));
1768 st
.writeBuffer(sign
, 4);
1769 crc
:= crc32(0, @sign
[0], 4);
1772 hbuf
[2] := (Width
shr 8) and $ff;
1773 hbuf
[3] := Width
and $ff;
1776 hbuf
[6] := (Height
shr 8) and $ff;
1777 hbuf
[7] := Height
and $ff;
1778 hbuf
[8] := 8; // bit depth
1779 hbuf
[9] := 2; // RGB
1780 hbuf
[10] := 0; // compression method
1781 hbuf
[11] := 0; // filter method
1782 hbuf
[12] := 0; // no interlace
1783 crc
:= crc32(crc
, @hbuf
[0], 13);
1784 st
.writeBuffer(hbuf
, 13);
1785 writeIntBE(st
, crc
);
1786 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1789 writeIntBE(st
, LongWord(dlen
));
1794 st
.writeBuffer(sign
, 4);
1795 crc
:= crc32(0, @sign
[0], 4);
1796 crc
:= crc32(crc
, obuf
, dlen
);
1797 st
.writeBuffer(obuf
^, dlen
);
1798 writeIntBE(st
, crc
);
1799 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
1802 writeIntBE(st
, LongWord(0));
1807 st
.writeBuffer(sign
, 4);
1808 crc
:= crc32(0, @sign
[0], 4);
1809 writeIntBE(st
, crc
);
1810 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
1812 if obuf
<> nil then FreeMem(obuf
);
1817 Imaging
.SetOption(ImagingPNGCompressLevel
, 9);
1818 Imaging
.SetOption(ImagingPNGPreFilter
, 6);
1821 NewImage(Width
, Height
, TImageFormat
.ifR8G8B8
, img
);
1823 //writeln(stderr, 'moving pixels...');
1824 for y
:= Height
-1 downto 0 do
1826 for x
:= 0 to Width
-1 do
1828 clr
.r
:= ps
^; Inc(ps
);
1829 clr
.g
:= ps
^; Inc(ps
);
1830 clr
.b
:= ps
^; Inc(ps
);
1832 SetPixel32(img
, x
, y
, clr
);
1835 GlobalMetadata
.ClearMetaItems();
1836 GlobalMetadata
.ClearMetaItemsForSaving();
1837 //writeln(stderr, 'compressing image...');
1838 if not SaveImageToStream('png', st
, img
) then raise Exception
.Create('screenshot writing error');
1839 //writeln(stderr, 'done!');