DEADSOFTWARE

utils: made `wadExtensions` array public, because we may need it elsewhere
[d2df-sdl.git] / src / engine / e_graphics.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit e_graphics;
18 interface
20 uses
21 {$INCLUDE ../nogl/noGLuses.inc}
22 {$IFDEF USE_SDL2}
23 SDL2,
24 {$ENDIF}
25 SysUtils, Classes, Math, e_log, e_texture,
26 MAPDEF, ImagingTypes, Imaging, ImagingUtility;
28 type
29 TMirrorType=(None, Horizontal, Vertical);
30 TBlending=(None, Blend, Filter, Invert);
32 TPoint2i = record
33 X, Y: Integer;
34 end;
36 TPoint2f = record
37 X, Y: Double;
38 end;
40 TRect = record
41 Left, Top, Right, Bottom: Integer;
42 end;
44 TRectWH = record
45 X, Y: Integer;
46 Width, Height: Word;
47 end;
49 TRGB = packed record
50 R, G, B: Byte;
51 end;
53 PDFPoint = ^TDFPoint;
54 PPoint2f = ^TPoint2f;
55 PRect = ^TRect;
56 PRectWH = ^TRectWH;
59 //------------------------------------------------------------------
60 // ïðîòîòèïû ôóíêöèé
61 //------------------------------------------------------------------
62 procedure e_InitGL();
63 procedure e_SetViewPort(X, Y, Width, Height: Word);
64 procedure e_ResizeWindow(Width, Height: Integer);
66 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
67 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
68 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
69 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
70 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
71 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
72 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
73 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
75 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
76 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
78 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
79 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
81 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
83 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
84 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
85 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
86 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
87 Blending: TBlending = TBlending.None);
88 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
89 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
91 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
92 function e_CreateTexture(FileName: string; var ID: DWORD): Boolean;
93 function e_CreateTextureEx(FileName: string; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
94 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
95 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
96 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
97 procedure e_DeleteTexture(ID: DWORD);
98 procedure e_RemoveAllTextures();
100 // CharFont
101 function e_CharFont_Create(sp: ShortInt=0): DWORD;
102 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
103 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
104 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
105 Color: TRGB; Scale: Single = 1.0);
106 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
107 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
108 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
109 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
110 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
111 procedure e_CharFont_Remove(FontID: DWORD);
112 procedure e_CharFont_RemoveAll();
114 // TextureFont
115 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
116 Space: ShortInt=0);
117 procedure e_TextureFontKill(FontID: DWORD);
118 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
119 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
120 Blue: Byte; Scale: Single; Shadow: Boolean = False);
121 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD;
122 Shadow: Boolean = False; Newlines: Boolean = False);
123 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
124 procedure e_RemoveAllTextureFont();
126 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
127 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
129 procedure e_ReleaseEngine();
130 procedure e_BeginRender();
131 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
132 procedure e_Clear(); overload;
133 procedure e_EndRender();
135 {$IFDEF USE_SDL2}
136 function e_GetGamma(win: PSDL_Window): Byte;
137 procedure e_SetGamma(win: PSDL_Window;Gamma: Byte);
138 {$ENDIF}
140 procedure e_MakeScreenshot(st: TStream; Width, Height: Word);
142 function _RGB(Red, Green, Blue: Byte): TRGB;
143 function _Point(X, Y: Integer): TPoint2i;
144 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
145 function _TRect(L, T, R, B: LongInt): TRect;
147 //function e_getTextGLId (ID: DWORD): GLuint;
149 var
150 e_Colors: TRGB;
151 e_NoGraphics: Boolean = False;
152 e_FastScreenshots: Boolean = true; // it's REALLY SLOW with `false`
153 g_dbg_scale: Single = 1.0;
156 implementation
158 uses
159 paszlib, crc, utils;
162 type
163 TTexture = record
164 tx: GLTexture;
165 end;
167 TTextureFont = record
168 Texture: DWORD;
169 TextureID: DWORD;
170 Base: Uint32;
171 CharWidth: Byte;
172 CharHeight: Byte;
173 XC, YC: WORD;
174 SPC: ShortInt;
175 end;
177 TCharFont = record
178 Chars: array[0..255] of
179 record
180 TextureID: Integer;
181 Width: Byte;
182 end;
183 Space: ShortInt;
184 Height: ShortInt;
185 alive: Boolean;
186 end;
188 TSavedTexture = record
189 TexID: DWORD;
190 OldID: DWORD;
191 Pixels: Pointer;
192 end;
194 var
195 e_Textures: array of TTexture = nil;
196 e_TextureFonts: array of TTextureFont = nil;
197 e_CharFonts: array of TCharFont;
198 //e_SavedTextures: array of TSavedTexture;
200 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
202 //------------------------------------------------------------------
203 // Èíèöèàëèçèðóåò OpenGL
204 //------------------------------------------------------------------
205 procedure e_InitGL();
206 begin
207 if e_NoGraphics then
208 begin
209 e_DummyTextures := True;
210 Exit;
211 end;
212 e_Colors.R := 255;
213 e_Colors.G := 255;
214 e_Colors.B := 255;
215 glDisable(GL_DEPTH_TEST);
216 glEnable(GL_SCISSOR_TEST);
217 glClearColor(0, 0, 0, 0);
218 end;
220 procedure e_SetViewPort(X, Y, Width, Height: Word);
221 var
222 mat: Array [0..15] of GLDouble;
224 begin
225 if e_NoGraphics then Exit;
226 glLoadIdentity();
227 glScissor(X, Y, Width, Height);
228 glViewport(X, Y, Width, Height);
229 //gluOrtho2D(0, Width, Height, 0);
231 glMatrixMode(GL_PROJECTION);
233 mat[ 0] := 2.0 / Width;
234 mat[ 1] := 0.0;
235 mat[ 2] := 0.0;
236 mat[ 3] := 0.0;
238 mat[ 4] := 0.0;
239 mat[ 5] := -2.0 / Height;
240 mat[ 6] := 0.0;
241 mat[ 7] := 0.0;
243 mat[ 8] := 0.0;
244 mat[ 9] := 0.0;
245 mat[10] := 1.0;
246 mat[11] := 0.0;
248 mat[12] := -1.0;
249 mat[13] := 1.0;
250 mat[14] := 0.0;
251 mat[15] := 1.0;
253 glLoadMatrixd(@mat[0]);
255 glMatrixMode(GL_MODELVIEW);
256 glLoadIdentity();
257 end;
259 //------------------------------------------------------------------
260 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
261 //------------------------------------------------------------------
262 function FindTexture(): DWORD;
263 var
264 i: integer;
265 begin
266 if e_Textures <> nil then
267 for i := 0 to High(e_Textures) do
268 if e_Textures[i].tx.Width = 0 then
269 begin
270 Result := i;
271 Exit;
272 end;
274 if e_Textures = nil then
275 begin
276 SetLength(e_Textures, 32);
277 Result := 0;
278 end
279 else
280 begin
281 Result := High(e_Textures) + 1;
282 SetLength(e_Textures, Length(e_Textures) + 32);
283 end;
284 end;
286 //------------------------------------------------------------------
287 // Ñîçäàåò òåêñòóðó
288 //------------------------------------------------------------------
289 function e_CreateTexture(FileName: String; var ID: DWORD): Boolean;
290 var
291 find_id: DWORD;
292 fmt: Word;
293 begin
294 Result := False;
296 e_WriteLog('Loading texture from '+FileName, TMsgType.Notify);
298 find_id := FindTexture();
300 if not LoadTexture(FileName, e_Textures[find_id].tx, e_Textures[find_id].tx.Width,
301 e_Textures[find_id].tx.Height, @fmt) then Exit;
303 ID := find_id;
305 Result := True;
306 end;
308 function e_CreateTextureEx(FileName: String; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
309 var
310 find_id: DWORD;
311 fmt: Word;
312 begin
313 Result := False;
315 find_id := FindTexture();
317 if not LoadTextureEx(FileName, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
319 ID := find_id;
321 Result := True;
322 end;
324 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
325 var
326 find_id: DWORD;
327 fmt: Word;
328 begin
329 Result := False;
331 find_id := FindTexture;
333 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;
335 id := find_id;
337 Result := True;
338 end;
340 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
341 var
342 find_id: DWORD;
343 fmt: Word;
344 begin
345 Result := False;
347 find_id := FindTexture();
349 if not LoadTextureMemEx(pData, dataSize, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
351 ID := find_id;
353 Result := True;
354 end;
356 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
357 var
358 find_id: DWORD;
359 fmt, tw, th: Word;
360 begin
361 result := false;
362 find_id := FindTexture();
363 if not LoadTextureImg(img, e_Textures[find_id].tx, tw, th, @fmt) then exit;
364 ID := find_id;
365 result := True;
366 end;
368 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
369 begin
370 if Width <> nil then Width^ := e_Textures[ID].tx.Width;
371 if Height <> nil then Height^ := e_Textures[ID].tx.Height;
372 end;
374 procedure e_ResizeWindow(Width, Height: Integer);
375 begin
376 if Height = 0 then
377 Height := 1;
378 e_SetViewPort(0, 0, Width, Height);
379 end;
381 procedure drawTxQuad (x0, y0, w, h, tw, th: Integer; u, v: single; Mirror: TMirrorType);
382 var
383 x1, y1, tmp: Integer;
384 begin
385 if (w < 1) or (h < 1) then exit;
386 x1 := x0+w;
387 y1 := y0+h;
388 if Mirror = TMirrorType.Horizontal then begin tmp := x1; x1 := x0; x0 := tmp; end
389 else if Mirror = TMirrorType.Vertical then begin tmp := y1; y1 := y0; y0 := tmp; end;
390 //HACK: make texture one pixel shorter, so it won't wrap
391 if (g_dbg_scale <> 1.0) then
392 begin
393 u := u*tw/(tw+1);
394 v := v*th/(th+1);
395 end;
396 glTexCoord2f(0, v); glVertex2i(x0, y0);
397 glTexCoord2f(0, 0); glVertex2i(x0, y1);
398 glTexCoord2f(u, 0); glVertex2i(x1, y1);
399 glTexCoord2f(u, v); glVertex2i(x1, y0);
400 end;
402 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
403 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
404 begin
405 if e_NoGraphics then Exit;
406 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
408 if (Alpha > 0) or (AlphaChannel) or (Blending) then
409 glEnable(GL_BLEND)
410 else
411 glDisable(GL_BLEND);
413 if (AlphaChannel) or (Alpha > 0) then
414 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
416 if Alpha > 0 then
417 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
419 if Blending then
420 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
422 glEnable(GL_TEXTURE_2D);
423 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
424 glBegin(GL_QUADS);
426 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);
428 //u := e_Textures[ID].tx.u;
429 //v := e_Textures[ID].tx.v;
432 if Mirror = M_NONE then
433 begin
434 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
435 glTexCoord2f(0, 0); glVertex2i(X, Y);
436 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
437 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
438 end
439 else
440 if Mirror = M_HORIZONTAL then
441 begin
442 glTexCoord2f(u, 0); glVertex2i(X, Y);
443 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
444 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
445 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
446 end
447 else
448 if Mirror = M_VERTICAL then
449 begin
450 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
451 glTexCoord2f(0, -v); glVertex2i(X, Y);
452 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
453 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
454 end;
457 glEnd();
459 glDisable(GL_BLEND);
460 end;
462 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
463 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
464 var
465 u, v: Single;
466 begin
467 if e_NoGraphics then Exit;
468 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
470 if (Alpha > 0) or (AlphaChannel) or (Blending) then
471 glEnable(GL_BLEND)
472 else
473 glDisable(GL_BLEND);
475 if (AlphaChannel) or (Alpha > 0) then
476 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
478 if Alpha > 0 then
479 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
481 if Blending then
482 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
484 glEnable(GL_TEXTURE_2D);
485 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
487 u := e_Textures[ID].tx.u;
488 v := e_Textures[ID].tx.v;
490 glBegin(GL_QUADS);
491 glTexCoord2f(0, v); glVertex2i(X, Y);
492 glTexCoord2f(u, v); glVertex2i(X + Width, Y);
493 glTexCoord2f(u, 0); glVertex2i(X + Width, Y + Height);
494 glTexCoord2f(0, 0); glVertex2i(X, Y + Height);
495 glEnd();
497 glDisable(GL_BLEND);
498 end;
500 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
501 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
502 begin
503 if e_NoGraphics then Exit;
504 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
506 if (Alpha > 0) or (AlphaChannel) or (Blending) then
507 glEnable(GL_BLEND)
508 else
509 glDisable(GL_BLEND);
511 if (AlphaChannel) or (Alpha > 0) then
512 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
514 if Alpha > 0 then
515 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
517 if Blending then
518 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
520 glEnable(GL_TEXTURE_2D);
521 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
522 glBegin(GL_QUADS);
523 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);
524 glEnd();
526 glDisable(GL_BLEND);
527 end;
529 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
530 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
531 var
532 X2, Y2, dx, w, h: Integer;
533 u, v: Single;
534 begin
535 if e_NoGraphics then Exit;
536 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
537 ambientBlendMode := false;
539 if (Alpha > 0) or AlphaChannel or Blending then
540 begin
541 glEnable(GL_BLEND);
542 end
543 else
544 begin
545 if not ambientBlendMode then glDisable(GL_BLEND);
546 end;
547 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
548 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
549 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
551 if (XCount = 0) then XCount := 1;
552 if (YCount = 0) then YCount := 1;
554 glEnable(GL_TEXTURE_2D);
555 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
557 X2 := X+e_Textures[ID].tx.width*XCount;
558 Y2 := Y+e_Textures[ID].tx.height*YCount;
560 //k8: this SHOULD work... i hope
561 if (e_Textures[ID].tx.width = e_Textures[ID].tx.glwidth) and (e_Textures[ID].tx.height = e_Textures[ID].tx.glheight) then
562 begin
563 glBegin(GL_QUADS);
564 glTexCoord2i(0, YCount); glVertex2i(X, Y);
565 glTexCoord2i(XCount, YCount); glVertex2i(X2, Y);
566 glTexCoord2i(XCount, 0); glVertex2i(X2, Y2);
567 glTexCoord2i(0, 0); glVertex2i(X, Y2);
568 glEnd();
569 end
570 else
571 begin
572 glBegin(GL_QUADS);
573 // hard day's night
574 u := e_Textures[ID].tx.u;
575 v := e_Textures[ID].tx.v;
576 w := e_Textures[ID].tx.width;
577 h := e_Textures[ID].tx.height;
578 while YCount > 0 do
579 begin
580 dx := XCount;
581 x2 := X;
582 while dx > 0 do
583 begin
584 glTexCoord2f(0, v); glVertex2i(X, Y);
585 glTexCoord2f(u, v); glVertex2i(X+w, Y);
586 glTexCoord2f(u, 0); glVertex2i(X+w, Y+h);
587 glTexCoord2f(0, 0); glVertex2i(X, Y+h);
588 Inc(X, w);
589 Dec(dx);
590 end;
591 X := x2;
592 Inc(Y, h);
593 Dec(YCount);
594 end;
595 glEnd();
596 end;
598 glDisable(GL_BLEND);
599 end;
602 //TODO: overflow checks
603 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
604 var
605 ex0, ey0: Integer;
606 begin
607 result := false;
608 if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit;
609 // check for intersection
610 if (x0+w0 <= x1) or (y0+h0 <= y1) or (x1+w1 <= x0) or (y1+h1 <= y0) then exit;
611 if (x0 >= x1+w1) or (y0 >= y1+h1) or (x1 >= x0+h0) or (y1 >= y0+h0) then exit;
612 // ok, intersects
613 ex0 := x0+w0;
614 ey0 := y0+h0;
615 if (x0 < x1) then x0 := x1;
616 if (y0 < y1) then y0 := y1;
617 if (ex0 > x1+w1) then ex0 := x1+w1;
618 if (ey0 > y1+h1) then ey0 := y1+h1;
619 w0 := ex0-x0;
620 h0 := ey0-y0;
621 result := (w0 > 0) and (h0 > 0);
622 end;
625 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
626 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
627 var
628 x2, y2: Integer;
630 wassc: Boolean;
631 scxywh: array[0..3] of GLint;
632 vpxywh: array[0..3] of GLint;
634 w, h, dw, cw, ch, yofs: Integer;
635 u, v, cu, cv: Single;
636 onlyOneY: Boolean;
639 procedure setScissorGLInternal (x, y, w, h: Integer);
640 begin
641 //if not scallowed then exit;
642 x := trunc(x*scale);
643 y := trunc(y*scale);
644 w := trunc(w*scale);
645 h := trunc(h*scale);
646 y := vpxywh[3]-(y+h);
647 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
648 begin
649 glScissor(0, 0, 0, 0);
650 end
651 else
652 begin
653 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
654 glScissor(x, y, w, h);
655 end;
656 end;
659 begin
660 if e_NoGraphics then exit;
661 ambientBlendMode := false;
663 if (wdt < 1) or (hgt < 1) then exit;
665 if (wdt mod e_Textures[ID].tx.width = 0) and (hgt mod e_Textures[ID].tx.height = 0) then
666 begin
667 e_DrawFill(id, x, y, wdt div e_Textures[ID].tx.width, hgt div e_Textures[ID].tx.height, alpha, alphachannel, blending, ambientBlendMode);
668 exit;
669 end;
671 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
673 if (Alpha > 0) or AlphaChannel or Blending then
674 begin
675 glEnable(GL_BLEND);
676 end
677 else
678 begin
679 if not ambientBlendMode then glDisable(GL_BLEND);
680 end;
681 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
682 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
683 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
685 glEnable(GL_TEXTURE_2D);
686 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
688 x2 := x+wdt;
689 y2 := y+hgt;
691 //k8: this SHOULD work... i hope
692 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
693 begin
694 glBegin(GL_QUADS);
695 glTexCoord2f(0, hgt/e_Textures[ID].tx.height); glVertex2i(x, y);
696 glTexCoord2f(wdt/e_Textures[ID].tx.width, hgt/e_Textures[ID].tx.height); glVertex2i(x2, y);
697 glTexCoord2f(wdt/e_Textures[ID].tx.width, 0); glVertex2i(x2, y2);
698 glTexCoord2f(0, 0); glVertex2i(x, y2);
699 glEnd();
700 end
701 else
702 begin
703 // hard day's night; setup scissor
705 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
706 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
707 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
708 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
709 //glEnable(GL_SCISSOR_TEST);
710 setScissorGLInternal(x, y, wdt, hgt);
712 // draw quads
713 u := e_Textures[ID].tx.u;
714 v := e_Textures[ID].tx.v;
715 w := e_Textures[ID].tx.width;
716 h := e_Textures[ID].tx.height;
717 x2 := x;
718 if (hgt > h) then begin y += hgt-h; onlyOneY := false; end else onlyOneY := true;
719 glBegin(GL_QUADS);
720 while (hgt > 0) do
721 begin
722 if (hgt >= h) then begin ch := h; cv := v; yofs := 0; end else begin ch := hgt; cv := v/(h/hgt); yofs := h-hgt; end;
723 if onlyOneY then yofs := 0;
724 Dec(hgt, h);
725 dw := wdt;
726 x := x2;
727 while (dw > 0) do
728 begin
729 if (dw >= w) then begin cw := w; cu := u; end else begin cw := dw; cu := u/(w/dw); end;
730 Dec(dw, w);
731 glTexCoord2f(0, cv); glVertex2i(X, Y+yofs);
732 glTexCoord2f(cu, cv); glVertex2i(X+cw, Y+yofs);
733 glTexCoord2f(cu, 0); glVertex2i(X+cw, Y+ch+yofs);
734 glTexCoord2f(0, 0); glVertex2i(X, Y+ch+yofs);
735 Inc(X, w);
736 end;
737 Dec(Y, h);
738 end;
739 glEnd();
740 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
741 end;
743 glDisable(GL_BLEND);
744 end;
747 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
748 begin
749 if e_NoGraphics then exit;
750 if (w < 1) or (h < 1) then exit;
751 if (a <> 255) or ((r or g or b) <> 0) then
752 begin
753 glEnable(GL_BLEND);
754 glDisable(GL_TEXTURE_2D);
755 glColor4ub(r, g, b, a);
756 if ((r or g or b) <> 0) then
757 begin
758 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
759 glBegin(GL_QUADS);
760 glVertex2i(x, y);
761 glVertex2i(x+w, y);
762 glVertex2i(x+w, y+h);
763 glVertex2i(x, y+h);
764 glEnd();
765 end;
766 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
767 glBegin(GL_QUADS);
768 glVertex2i(x, y);
769 glVertex2i(x+w, y);
770 glVertex2i(x+w, y+h);
771 glVertex2i(x, y+h);
772 glEnd();
773 glDisable(GL_BLEND);
774 end;
775 end;
778 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
779 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
780 begin
781 if e_NoGraphics then Exit;
783 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
785 if (Alpha > 0) or (AlphaChannel) or (Blending) then
786 glEnable(GL_BLEND)
787 else
788 glDisable(GL_BLEND);
790 if (AlphaChannel) or (Alpha > 0) then
791 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
793 if Alpha > 0 then
794 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
796 if Blending then
797 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
799 if (Angle <> 0) and (RC <> nil) then
800 begin
801 glPushMatrix();
802 glTranslatef(X+RC.X, Y+RC.Y, 0);
803 glRotatef(Angle, 0, 0, 1);
804 glTranslatef(-(X+RC.X), -(Y+RC.Y), 0);
805 end;
807 glEnable(GL_TEXTURE_2D);
808 glBindTexture(GL_TEXTURE_2D, e_Textures[id].tx.id);
809 glBegin(GL_QUADS); //0-1 1-1
810 //00 10
811 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);
812 glEnd();
814 if Angle <> 0 then
815 glPopMatrix();
817 glDisable(GL_BLEND);
818 end;
820 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
821 begin
822 if e_NoGraphics then Exit;
823 glDisable(GL_TEXTURE_2D);
824 glColor3ub(Red, Green, Blue);
825 glPointSize(Size);
827 if (Size = 2) or (Size = 4) then
828 X := X + 1;
830 glBegin(GL_POINTS);
831 glVertex2f(X+0.3, Y+1.0);
832 glEnd();
834 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
835 end;
837 procedure e_LineCorrection(var X1, Y1, X2, Y2: Integer);
838 begin
839 // Make lines only top-left/bottom-right and top-right/bottom-left
840 if Y2 < Y1 then
841 begin
842 X1 := X1 xor X2;
843 X2 := X1 xor X2;
844 X1 := X1 xor X2;
846 Y1 := Y1 xor Y2;
847 Y2 := Y1 xor Y2;
848 Y1 := Y1 xor Y2;
849 end;
851 // Pixel-perfect hack
852 if X1 < X2 then
853 Inc(X2)
854 else
855 Inc(X1);
856 Inc(Y2);
857 end;
859 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
860 var
861 nX1, nY1, nX2, nY2: Integer;
862 begin
863 if e_NoGraphics then Exit;
864 // Only top-left/bottom-right quad
865 if X1 > X2 then
866 begin
867 X1 := X1 xor X2;
868 X2 := X1 xor X2;
869 X1 := X1 xor X2;
870 end;
871 if Y1 > Y2 then
872 begin
873 Y1 := Y1 xor Y2;
874 Y2 := Y1 xor Y2;
875 Y1 := Y1 xor Y2;
876 end;
878 if Alpha > 0 then
879 begin
880 glEnable(GL_BLEND);
881 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
882 end
883 else
884 glDisable(GL_BLEND);
886 glDisable(GL_TEXTURE_2D);
887 glColor4ub(Red, Green, Blue, 255-Alpha);
888 glLineWidth(1);
889 glBegin(GL_LINES);
890 nX1 := X1; nY1 := Y1;
891 nX2 := X2; nY2 := Y1;
892 e_LineCorrection(nX1, nY1, nX2, nY2); // Pixel-perfect lines
893 glVertex2i(nX1, nY1);
894 glVertex2i(nX2, nY2);
896 nX1 := X2; nY1 := Y1;
897 nX2 := X2; nY2 := Y2;
898 e_LineCorrection(nX1, nY1, nX2, nY2);
899 glVertex2i(nX1, nY1);
900 glVertex2i(nX2, nY2);
902 nX1 := X2; nY1 := Y2;
903 nX2 := X1; nY2 := Y2;
904 e_LineCorrection(nX1, nY1, nX2, nY2);
905 glVertex2i(nX1, nY1);
906 glVertex2i(nX2, nY2);
908 nX1 := X1; nY1 := Y2;
909 nX2 := X1; nY2 := Y1;
910 e_LineCorrection(nX1, nY1, nX2, nY2);
911 glVertex2i(nX1, nY1);
912 glVertex2i(nX2, nY2);
913 glEnd();
914 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
915 glDisable(GL_BLEND);
916 end;
918 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
919 Blending: TBlending = TBlending.None);
920 begin
921 if e_NoGraphics then Exit;
922 if (Alpha > 0) or (Blending <> TBlending.None) then
923 glEnable(GL_BLEND)
924 else
925 glDisable(GL_BLEND);
927 if Blending = TBlending.Blend then
928 glBlendFunc(GL_SRC_ALPHA, GL_ONE)
929 else
930 if Blending = TBlending.Filter then
931 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR)
932 else
933 if Blending = TBlending.Invert then
934 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO)
935 else
936 if Alpha > 0 then
937 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
939 glDisable(GL_TEXTURE_2D);
940 glColor4ub(Red, Green, Blue, 255-Alpha);
942 X2 := X2 + 1;
943 Y2 := Y2 + 1;
945 glBegin(GL_QUADS);
946 glVertex2i(X1, Y1);
947 glVertex2i(X2, Y1);
948 glVertex2i(X2, Y2);
949 glVertex2i(X1, Y2);
950 glEnd();
952 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
954 glDisable(GL_BLEND);
955 end;
958 // ////////////////////////////////////////////////////////////////////////// //
959 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
960 begin
961 if (a < 0) then a := 0;
962 if (a > 255) then a := 255;
963 glEnable(GL_BLEND);
964 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
965 glDisable(GL_TEXTURE_2D);
966 glColor4ub(0, 0, 0, Byte(255-a));
967 glBegin(GL_QUADS);
968 glVertex2i(x0, y0);
969 glVertex2i(x1, y0);
970 glVertex2i(x1, y1);
971 glVertex2i(x0, y1);
972 glEnd();
973 //glRect(x, y, x+w, y+h);
974 glColor4ub(1, 1, 1, 1);
975 glDisable(GL_BLEND);
976 //glBlendEquation(GL_FUNC_ADD);
977 end;
979 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
980 begin
981 if (w > 0) and (h > 0) then e_DarkenQuad(x, y, x+w, y+h, a);
982 end;
985 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
986 begin
987 if e_NoGraphics then Exit;
988 // Pixel-perfect lines
989 if Width = 1 then
990 e_LineCorrection(X1, Y1, X2, Y2);
992 if Alpha > 0 then
993 begin
994 glEnable(GL_BLEND);
995 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
996 end else
997 glDisable(GL_BLEND);
999 glDisable(GL_TEXTURE_2D);
1000 glColor4ub(Red, Green, Blue, 255-Alpha);
1001 glLineWidth(Width);
1002 glBegin(GL_LINES);
1003 glVertex2i(X1, Y1);
1004 glVertex2i(X2, Y2);
1005 glEnd();
1006 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1008 glDisable(GL_BLEND);
1009 end;
1011 //------------------------------------------------------------------
1012 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1013 //------------------------------------------------------------------
1014 procedure e_DeleteTexture(ID: DWORD);
1015 begin
1016 if not e_NoGraphics then
1017 glDeleteTextures(1, @e_Textures[ID].tx.id);
1018 e_Textures[ID].tx.id := 0;
1019 e_Textures[ID].tx.Width := 0;
1020 e_Textures[ID].tx.Height := 0;
1021 end;
1023 //------------------------------------------------------------------
1024 // Óäàëÿåò âñå òåêñòóðû
1025 //------------------------------------------------------------------
1026 procedure e_RemoveAllTextures();
1027 var
1028 i: integer;
1029 begin
1030 if e_Textures = nil then Exit;
1032 for i := 0 to High(e_Textures) do
1033 if e_Textures[i].tx.Width <> 0 then e_DeleteTexture(i);
1034 e_Textures := nil;
1035 end;
1037 //------------------------------------------------------------------
1038 // Óäàëÿåò äâèæîê
1039 //------------------------------------------------------------------
1040 procedure e_ReleaseEngine();
1041 begin
1042 e_RemoveAllTextures;
1043 e_RemoveAllTextureFont;
1044 end;
1046 procedure e_BeginRender();
1047 begin
1048 if e_NoGraphics then Exit;
1049 glEnable(GL_ALPHA_TEST);
1050 glAlphaFunc(GL_GREATER, 0.0);
1051 end;
1053 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
1054 begin
1055 if e_NoGraphics then Exit;
1056 glClearColor(Red, Green, Blue, 0);
1057 glClear(Mask);
1058 end;
1060 procedure e_Clear(); overload;
1061 begin
1062 if e_NoGraphics then Exit;
1063 glClearColor(0, 0, 0, 0);
1064 glClear(GL_COLOR_BUFFER_BIT);
1065 end;
1067 procedure e_EndRender();
1068 begin
1069 if e_NoGraphics then Exit;
1070 glPopMatrix();
1071 end;
1073 {$IFDEF USE_SDL2}
1074 function e_GetGamma(win: PSDL_Window): Byte;
1075 var
1076 ramp: array [0..256*3-1] of Word;
1077 rgb: array [0..2] of Double;
1078 sum: double;
1079 count: integer;
1080 min: integer;
1081 max: integer;
1082 A, B: double;
1083 i, j: integer;
1084 begin
1085 Result := 0;
1086 if e_NoGraphics then Exit;
1087 rgb[0] := 1.0;
1088 rgb[1] := 1.0;
1089 rgb[2] := 1.0;
1091 SDL_GetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1093 for i := 0 to 2 do
1094 begin
1095 sum := 0;
1096 count := 0;
1097 min := 256 * i;
1098 max := min + 256;
1100 for j := min to max - 1 do
1101 if ramp[j] > 0 then
1102 begin
1103 B := (j mod 256)/256;
1104 A := ramp[j]/65536;
1105 sum := sum + ln(A)/ln(B);
1106 inc(count);
1107 end;
1108 rgb[i] := sum / count;
1109 end;
1111 Result := 100 - Trunc(((rgb[0] + rgb[1] + rgb[2])/3 - 0.23) * 100/(2.7 - 0.23));
1112 end;
1114 procedure e_SetGamma(win: PSDL_Window; Gamma: Byte);
1115 var
1116 ramp: array [0..256*3-1] of Word;
1117 i: integer;
1118 r: double;
1119 g: double;
1120 begin
1121 if e_NoGraphics then Exit;
1122 g := (100 - Gamma)*(2.7 - 0.23)/100 + 0.23;
1124 for i := 0 to 255 do
1125 begin
1126 r := Exp(g * ln(i/256))*65536;
1127 if r < 0 then r := 0
1128 else if r > 65535 then r := 65535;
1129 ramp[i] := trunc(r);
1130 ramp[i + 256] := trunc(r);
1131 ramp[i + 512] := trunc(r);
1132 end;
1134 SDL_SetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1135 end;
1136 {$ENDIF}
1138 function e_CharFont_Create(sp: ShortInt=0): DWORD;
1139 var
1140 i, id: DWORD;
1141 begin
1142 e_WriteLog('Creating CharFont...', TMsgType.Notify);
1144 id := DWORD(-1);
1146 if e_CharFonts <> nil then
1147 for i := 0 to High(e_CharFonts) do
1148 if not e_CharFonts[i].alive then
1149 begin
1150 id := i;
1151 Break;
1152 end;
1154 if id = DWORD(-1) then
1155 begin
1156 SetLength(e_CharFonts, Length(e_CharFonts) + 1);
1157 id := High(e_CharFonts);
1158 end;
1160 with e_CharFonts[id] do
1161 begin
1162 for i := 0 to High(Chars) do
1163 with Chars[i] do
1164 begin
1165 TextureID := -1;
1166 Width := 0;
1167 end;
1169 Space := sp;
1170 alive := True;
1171 end;
1173 Result := id;
1174 end;
1176 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
1177 begin
1178 with e_CharFonts[FontID].Chars[Ord(c)] do
1179 begin
1180 TextureID := Texture;
1181 Width := w;
1182 end;
1183 end;
1185 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
1186 var
1187 a: Integer;
1188 begin
1189 if e_NoGraphics then Exit;
1190 if Text = '' then Exit;
1191 if e_CharFonts = nil then Exit;
1192 if Integer(FontID) > High(e_CharFonts) then Exit;
1194 with e_CharFonts[FontID] do
1195 begin
1196 for a := 1 to Length(Text) do
1197 with Chars[Ord(Text[a])] do
1198 if TextureID <> -1 then
1199 begin
1200 e_Draw(TextureID, X, Y, 0, True, False);
1201 X := X+Width+IfThen(a = Length(Text), 0, Space);
1202 end;
1203 end;
1204 end;
1206 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
1207 Color: TRGB; Scale: Single = 1.0);
1208 var
1209 a: Integer;
1210 c: TRGB;
1211 begin
1212 if e_NoGraphics then Exit;
1213 if Text = '' then Exit;
1214 if e_CharFonts = nil then Exit;
1215 if Integer(FontID) > High(e_CharFonts) then Exit;
1217 with e_CharFonts[FontID] do
1218 begin
1219 for a := 1 to Length(Text) do
1220 with Chars[Ord(Text[a])] do
1221 if TextureID <> -1 then
1222 begin
1223 if Scale <> 1.0 then
1224 begin
1225 glPushMatrix;
1226 glScalef(Scale, Scale, 0);
1227 end;
1229 c := e_Colors;
1230 e_Colors := Color;
1231 e_Draw(TextureID, X, Y, 0, True, False);
1232 e_Colors := c;
1234 if Scale <> 1.0 then glPopMatrix;
1236 X := X+Width+IfThen(a = Length(Text), 0, Space);
1237 end;
1238 end;
1239 end;
1241 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
1242 var
1243 a, TX, TY, len: Integer;
1244 tc, c: TRGB;
1245 w, h: Word;
1246 begin
1247 if e_NoGraphics then Exit;
1248 if Text = '' then Exit;
1249 if e_CharFonts = nil then Exit;
1250 if Integer(FontID) > High(e_CharFonts) then Exit;
1252 c.R := 255;
1253 c.G := 255;
1254 c.B := 255;
1256 TX := X;
1257 TY := Y;
1258 len := Length(Text);
1260 e_CharFont_GetSize(FontID, 'A', w, h);
1262 with e_CharFonts[FontID] do
1263 begin
1264 for a := 1 to len do
1265 begin
1266 case Text[a] of
1267 #10: // line feed
1268 begin
1269 TX := X;
1270 TY := TY + h;
1271 continue;
1272 end;
1273 #1: // black
1274 begin
1275 c.R := 0; c.G := 0; c.B := 0;
1276 continue;
1277 end;
1278 #2: // white
1279 begin
1280 c.R := 255; c.G := 255; c.B := 255;
1281 continue;
1282 end;
1283 #3: // darker
1284 begin
1285 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1286 continue;
1287 end;
1288 #4: // lighter
1289 begin
1290 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1291 continue;
1292 end;
1293 #18: // red
1294 begin
1295 c.R := 255; c.G := 0; c.B := 0;
1296 continue;
1297 end;
1298 #19: // green
1299 begin
1300 c.R := 0; c.G := 255; c.B := 0;
1301 continue;
1302 end;
1303 #20: // blue
1304 begin
1305 c.R := 0; c.G := 0; c.B := 255;
1306 continue;
1307 end;
1308 #21: // yellow
1309 begin
1310 c.R := 255; c.G := 255; c.B := 0;
1311 continue;
1312 end;
1313 end;
1315 with Chars[Ord(Text[a])] do
1316 if TextureID <> -1 then
1317 begin
1318 tc := e_Colors;
1319 e_Colors := c;
1320 e_Draw(TextureID, TX, TY, 0, True, False);
1321 e_Colors := tc;
1323 TX := TX+Width+IfThen(a = Length(Text), 0, Space);
1324 end;
1325 end;
1326 end;
1327 end;
1329 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
1330 var
1331 a: Integer;
1332 h2: Word;
1333 begin
1334 w := 0;
1335 h := 0;
1337 if Text = '' then Exit;
1338 if e_CharFonts = nil then Exit;
1339 if Integer(FontID) > High(e_CharFonts) then Exit;
1341 with e_CharFonts[FontID] do
1342 begin
1343 for a := 1 to Length(Text) do
1344 with Chars[Ord(Text[a])] do
1345 if TextureID <> -1 then
1346 begin
1347 w := w+Width+IfThen(a = Length(Text), 0, Space);
1348 e_GetTextureSize(TextureID, nil, @h2);
1349 if h2 > h then h := h2;
1350 end;
1351 end;
1352 end;
1354 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
1355 var
1356 a, lines, len: Integer;
1357 h2, w2, tw, th: Word;
1358 begin
1359 w2 := 0;
1360 h2 := 0;
1361 tw := 0;
1362 th := 0;
1364 if Text = '' then Exit;
1365 if e_CharFonts = nil then Exit;
1366 if Integer(FontID) > High(e_CharFonts) then Exit;
1368 lines := 1;
1369 len := Length(Text);
1371 with e_CharFonts[FontID] do
1372 begin
1373 for a := 1 to len do
1374 begin
1375 if Text[a] = #10 then
1376 begin
1377 Inc(lines);
1378 if w2 > tw then tw := w2;
1379 w2 := 0;
1380 continue;
1381 end;
1383 with Chars[Ord(Text[a])] do
1384 if TextureID <> -1 then
1385 begin
1386 w2 := w2 + Width + IfThen(a = len, 0, Space);
1387 e_GetTextureSize(TextureID, nil, @h2);
1388 if h2 > th then th := h2;
1389 end;
1390 end;
1391 end;
1393 if w2 > tw then
1394 tw := w2;
1396 w := tw;
1397 h := th * lines;
1398 end;
1400 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
1401 var
1402 a: Integer;
1403 begin
1404 Result := 0;
1406 if e_CharFonts = nil then Exit;
1407 if Integer(FontID) > High(e_CharFonts) then Exit;
1409 for a := 0 to High(e_CharFonts[FontID].Chars) do
1410 Result := Max(Result, e_CharFonts[FontID].Chars[a].Width);
1411 end;
1413 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
1414 var
1415 a: Integer;
1416 h2: Word;
1417 begin
1418 Result := 0;
1420 if e_CharFonts = nil then Exit;
1421 if Integer(FontID) > High(e_CharFonts) then Exit;
1423 for a := 0 to High(e_CharFonts[FontID].Chars) do
1424 begin
1425 if e_CharFonts[FontID].Chars[a].TextureID <> -1 then
1426 e_GetTextureSize(e_CharFonts[FontID].Chars[a].TextureID, nil, @h2)
1427 else h2 := 0;
1428 if h2 > Result then Result := h2;
1429 end;
1430 end;
1432 procedure e_CharFont_Remove(FontID: DWORD);
1433 var
1434 a: Integer;
1435 begin
1436 with e_CharFonts[FontID] do
1437 for a := 0 to High(Chars) do
1438 if Chars[a].TextureID <> -1 then e_DeleteTexture(Chars[a].TextureID);
1440 e_CharFonts[FontID].alive := False;
1441 end;
1443 procedure e_CharFont_RemoveAll();
1444 var
1445 a: Integer;
1446 begin
1447 if e_CharFonts = nil then Exit;
1449 for a := 0 to High(e_CharFonts) do
1450 e_CharFont_Remove(a);
1452 e_CharFonts := nil;
1453 end;
1455 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
1456 Space: ShortInt=0);
1457 var
1458 {$IFDEF NOGL_LISTS}
1459 loop1 : GLuint;
1460 cx, cy : real;
1461 {$ENDIF}
1462 i, id: DWORD;
1463 begin
1464 if e_NoGraphics then Exit;
1465 e_WriteLog('Creating texture font...', TMsgType.Notify);
1467 id := DWORD(-1);
1469 if e_TextureFonts <> nil then
1470 for i := 0 to High(e_TextureFonts) do
1471 if e_TextureFonts[i].Base = 0 then
1472 begin
1473 id := i;
1474 Break;
1475 end;
1477 if id = DWORD(-1) then
1478 begin
1479 SetLength(e_TextureFonts, Length(e_TextureFonts) + 1);
1480 id := High(e_TextureFonts);
1481 end;
1483 with e_TextureFonts[id] do
1484 begin
1485 {$IFDEF NOGL_LISTS}
1486 Base := glGenLists(XCount*YCount);
1487 {$ENDIF}
1488 TextureID := e_Textures[Tex].tx.id;
1489 CharWidth := (e_Textures[Tex].tx.Width div XCount)+Space;
1490 CharHeight := e_Textures[Tex].tx.Height div YCount;
1491 XC := XCount;
1492 YC := YCount;
1493 Texture := Tex;
1494 SPC := Space;
1495 end;
1497 {$IFDEF NOGL_LISTS}
1498 glBindTexture(GL_TEXTURE_2D, e_Textures[Tex].tx.id);
1499 for loop1 := 0 to XCount*YCount-1 do
1500 begin
1501 cx := (loop1 mod XCount)/XCount;
1502 cy := (loop1 div YCount)/YCount;
1504 glNewList(e_TextureFonts[id].Base+loop1, GL_COMPILE);
1505 glBegin(GL_QUADS);
1506 glTexCoord2f(cx, 1.0-cy-1/YCount);
1507 glVertex2i(0, e_Textures[Tex].tx.Height div YCount);
1509 glTexCoord2f(cx+1/XCount, 1.0-cy-1/YCount);
1510 glVertex2i(e_Textures[Tex].tx.Width div XCount, e_Textures[Tex].tx.Height div YCount);
1512 glTexCoord2f(cx+1/XCount, 1.0-cy);
1513 glVertex2i(e_Textures[Tex].tx.Width div XCount, 0);
1515 glTexCoord2f(cx, 1.0-cy);
1516 glVertex2i(0, 0);
1517 glEnd();
1518 glTranslated((e_Textures[Tex].tx.Width div XCount)+Space, 0, 0);
1519 glEndList();
1520 end;
1521 {$ENDIF}
1523 FontID := id;
1524 end;
1526 procedure e_TextureFontKill(FontID: DWORD);
1527 begin
1528 if e_NoGraphics then Exit;
1529 {$IFDEF NOGL_LISTS}
1530 glDeleteLists(e_TextureFonts[FontID].Base, 256);
1531 {$ENDIF}
1532 e_TextureFonts[FontID].Base := 0;
1533 end;
1535 {$IFNDEF NOGL_LISTS}
1536 procedure e_TextureFontDrawChar(ch: Char; FontID: DWORD);
1537 var
1538 index: Integer;
1539 cx, cy: GLfloat;
1540 Tex: Integer;
1541 Width, Height: Integer;
1542 XCount, YCount: Integer;
1543 begin
1544 index := Ord(ch) - 32;
1545 Tex := e_TextureFonts[FontID].Texture;
1546 Width := e_Textures[Tex].tx.Width;
1547 Height := e_Textures[Tex].tx.Height;
1548 XCount := e_TextureFonts[FontID].XC;
1549 YCount := e_TextureFonts[FontID].YC;
1550 cx := (index mod XCount)/XCount;
1551 cy := (index div YCount)/YCount;
1552 glBegin(GL_QUADS);
1553 glTexCoord2f(cx, 1 - cy - 1/YCount);
1554 glVertex2i(0, Height div YCount);
1555 glTexCoord2f(cx + 1/XCount, 1 - cy - 1/YCount);
1556 glVertex2i(Width div XCount, Height div YCount);
1557 glTexCoord2f(cx + 1/XCount, 1 - cy);
1558 glVertex2i(Width div XCount, 0);
1559 glTexCoord2f(cx, 1 - cy);
1560 glVertex2i(0, 0);
1561 glEnd();
1562 glTranslatef((e_Textures[Tex].tx.Width div XCount) + e_TextureFonts[FontID].SPC, 0, 0);
1563 end;
1565 procedure e_TextureFontDrawString(Text: String; FontID: DWORD);
1566 var
1567 i: Integer;
1568 begin
1569 for i := 1 to High(Text) do
1570 e_TextureFontDrawChar(Text[i], FontID);
1571 end;
1572 {$ENDIF}
1574 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
1575 begin
1576 if e_NoGraphics then Exit;
1577 if Integer(FontID) > High(e_TextureFonts) then Exit;
1578 if Text = '' then Exit;
1580 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1581 glEnable(GL_BLEND);
1583 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1585 glPushMatrix;
1586 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1587 glEnable(GL_TEXTURE_2D);
1588 glTranslatef(x, y, 0);
1589 {$IFDEF NOGL_LISTS}
1590 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1591 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1592 {$ELSE}
1593 e_TextureFontDrawString(Text, FontID);
1594 {$ENDIF}
1595 glDisable(GL_TEXTURE_2D);
1596 glPopMatrix;
1598 glDisable(GL_BLEND);
1599 end;
1601 // god forgive me for this, but i cannot figure out how to do it without lists
1602 procedure e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1603 begin
1604 if e_NoGraphics then Exit;
1605 glPushMatrix;
1607 if Shadow then
1608 begin
1609 glColor4ub(0, 0, 0, 128);
1610 glTranslatef(X+1, Y+1, 0);
1611 {$IFDEF NOGL_LISTS}
1612 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1613 {$ELSE}
1614 e_TextureFontDrawChar(Ch, FontID);
1615 {$ENDIF}
1616 glPopMatrix;
1617 glPushMatrix;
1618 end;
1620 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1621 glTranslatef(X, Y, 0);
1622 {$IFDEF NOGL_LISTS}
1623 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1624 {$ELSE}
1625 e_TextureFontDrawChar(Ch, FontID);
1626 {$ENDIF}
1628 glPopMatrix;
1629 end;
1631 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1632 begin
1633 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1634 glEnable(GL_TEXTURE_2D);
1635 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1637 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1638 glEnable(GL_BLEND);
1639 e_TextureFontPrintChar(X, Y, Ch, FontID, Shadow);
1640 glDisable(GL_TEXTURE_2D);
1641 glDisable(GL_BLEND);
1642 end;
1644 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
1645 begin
1646 result := e_TextureFonts[FontID].CharWidth;
1647 end;
1649 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD;
1650 Shadow: Boolean = False; Newlines: Boolean = False);
1651 var
1652 a, TX, TY, len: Integer;
1653 tc, c: TRGB;
1654 w, h: Word;
1655 begin
1656 if e_NoGraphics then Exit;
1657 if Text = '' then Exit;
1658 if e_TextureFonts = nil then Exit;
1659 if Integer(FontID) > High(e_TextureFonts) then Exit;
1661 c.R := 255;
1662 c.G := 255;
1663 c.B := 255;
1665 TX := X;
1666 TY := Y;
1667 len := Length(Text);
1669 w := e_TextureFonts[FontID].CharWidth;
1670 h := e_TextureFonts[FontID].CharHeight;
1672 with e_TextureFonts[FontID] do
1673 begin
1674 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1675 glEnable(GL_TEXTURE_2D);
1677 {$IFDEF NOGL_LISTS}
1678 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1679 {$ENDIF}
1681 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1682 glEnable(GL_BLEND);
1684 for a := 1 to len do
1685 begin
1686 case Text[a] of
1687 #10: // line feed
1688 begin
1689 if Newlines then
1690 begin
1691 TX := X;
1692 TY := TY + h;
1693 continue;
1694 end;
1695 end;
1696 #1: // black
1697 begin
1698 c.R := 0; c.G := 0; c.B := 0;
1699 continue;
1700 end;
1701 #2: // white
1702 begin
1703 c.R := 255; c.G := 255; c.B := 255;
1704 continue;
1705 end;
1706 #3: // darker
1707 begin
1708 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1709 continue;
1710 end;
1711 #4: // lighter
1712 begin
1713 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1714 continue;
1715 end;
1716 #18: // red
1717 begin
1718 c.R := 255; c.G := 0; c.B := 0;
1719 continue;
1720 end;
1721 #19: // green
1722 begin
1723 c.R := 0; c.G := 255; c.B := 0;
1724 continue;
1725 end;
1726 #20: // blue
1727 begin
1728 c.R := 0; c.G := 0; c.B := 255;
1729 continue;
1730 end;
1731 #21: // yellow
1732 begin
1733 c.R := 255; c.G := 255; c.B := 0;
1734 continue;
1735 end;
1736 end;
1738 tc := e_Colors;
1739 e_Colors := c;
1740 e_TextureFontPrintChar(TX, TY, Text[a], FontID, Shadow);
1741 e_Colors := tc;
1743 TX := TX+w;
1744 end;
1745 glDisable(GL_TEXTURE_2D);
1746 glDisable(GL_BLEND);
1747 end;
1748 end;
1750 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
1751 Blue: Byte; Scale: Single; Shadow: Boolean = False);
1752 begin
1753 if e_NoGraphics then Exit;
1754 if Text = '' then Exit;
1756 glPushMatrix;
1757 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1758 glEnable(GL_TEXTURE_2D);
1760 {$IFDEF NOGL_LISTS}
1761 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1762 {$ENDIF}
1764 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1765 glEnable(GL_BLEND);
1767 if Shadow then
1768 begin
1769 glColor4ub(0, 0, 0, 128);
1770 glTranslatef(x+1, y+1, 0);
1771 glScalef(Scale, Scale, 0);
1772 {$IFDEF NOGL_LISTS}
1773 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1774 {$ELSE}
1775 e_TextureFontDrawString(Text, FontID);
1776 {$ENDIF}
1777 glPopMatrix;
1778 glPushMatrix;
1779 end;
1781 glColor4ub(Red, Green, Blue, 255);
1782 glTranslatef(x, y, 0);
1783 glScalef(Scale, Scale, 0);
1784 {$IFDEF NOGL_LISTS}
1785 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1786 {$ELSE}
1787 e_TextureFontDrawString(Text, FontID);
1788 {$ENDIF}
1790 glDisable(GL_TEXTURE_2D);
1791 glPopMatrix;
1792 glColor3ub(e_Colors.R, e_Colors.G, e_Colors.B);
1793 glDisable(GL_BLEND);
1794 end;
1796 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
1797 begin
1798 CharWidth := 16;
1799 CharHeight := 16;
1800 if e_NoGraphics then Exit;
1801 if Integer(ID) > High(e_TextureFonts) then
1802 Exit;
1803 CharWidth := e_TextureFonts[ID].CharWidth;
1804 CharHeight := e_TextureFonts[ID].CharHeight;
1805 end;
1807 procedure e_RemoveAllTextureFont();
1808 var
1809 i: integer;
1810 begin
1811 if e_NoGraphics then Exit;
1812 if e_TextureFonts = nil then Exit;
1814 for i := 0 to High(e_TextureFonts) do
1815 if e_TextureFonts[i].Base <> 0 then
1816 begin
1817 {$IFDEF NOGL_LISTS}
1818 glDeleteLists(e_TextureFonts[i].Base, 256);
1819 {$ENDIF}
1820 e_TextureFonts[i].Base := 0;
1821 end;
1823 e_TextureFonts := nil;
1824 end;
1826 function _RGB(Red, Green, Blue: Byte): TRGB;
1827 begin
1828 Result.R := Red;
1829 Result.G := Green;
1830 Result.B := Blue;
1831 end;
1833 function _Point(X, Y: Integer): TPoint2i;
1834 begin
1835 Result.X := X;
1836 Result.Y := Y;
1837 end;
1839 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
1840 begin
1841 Result.X := X;
1842 Result.Y := Y;
1843 Result.Width := Width;
1844 Result.Height := Height;
1845 end;
1847 function _TRect(L, T, R, B: LongInt): TRect;
1848 begin
1849 Result.Top := T;
1850 Result.Left := L;
1851 Result.Right := R;
1852 Result.Bottom := B;
1853 end;
1856 procedure e_MakeScreenshot (st: TStream; Width, Height: Word);
1857 var
1858 pixels, obuf, scln, ps, pd: PByte;
1859 obufsize: Integer;
1860 dlen: Cardinal;
1861 i, x, y, res: Integer;
1862 sign: array [0..7] of Byte;
1863 hbuf: array [0..12] of Byte;
1864 crc: LongWord;
1865 img: TImageData;
1866 clr: TColor32Rec;
1867 begin
1868 if e_NoGraphics then Exit;
1869 obuf := nil;
1871 // first, extract and pack graphics data
1872 if (Width mod 4) > 0 then Width := Width+4-(Width mod 4);
1874 GetMem(pixels, Width*Height*3);
1875 try
1876 FillChar(pixels^, Width*Height*3, 0);
1877 glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
1878 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1880 if e_FastScreenshots then
1881 begin
1882 // create scanlines
1883 GetMem(scln, (Width*3+1)*Height);
1884 try
1885 ps := pixels;
1886 pd := scln;
1887 Inc(ps, (Width*3)*(Height-1));
1888 for i := 0 to Height-1 do
1889 begin
1890 pd^ := 0; // filter
1891 Inc(pd);
1892 Move(ps^, pd^, Width*3);
1893 Dec(ps, Width*3);
1894 Inc(pd, Width*3);
1895 end;
1896 except
1897 FreeMem(scln);
1898 raise;
1899 end;
1900 FreeMem(pixels);
1901 pixels := scln;
1903 // pack it
1904 obufsize := (Width*3+1)*Height*2;
1905 GetMem(obuf, obufsize);
1906 try
1907 while true do
1908 begin
1909 dlen := obufsize;
1910 res := compress2(Pointer(obuf), dlen, Pointer(pixels), (Width*3+1)*Height, 9);
1911 if res = Z_OK then break;
1912 if res <> Z_BUF_ERROR then raise Exception.Create('can''t pack data for PNG');
1913 obufsize := obufsize*2;
1914 FreeMem(obuf);
1915 obuf := nil;
1916 GetMem(obuf, obufsize);
1917 end;
1918 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1920 // now write PNG
1922 // signature
1923 sign[0] := 137;
1924 sign[1] := 80;
1925 sign[2] := 78;
1926 sign[3] := 71;
1927 sign[4] := 13;
1928 sign[5] := 10;
1929 sign[6] := 26;
1930 sign[7] := 10;
1931 st.writeBuffer(sign, 8);
1932 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1934 // header
1935 writeIntBE(st, LongWord(13));
1936 sign[0] := 73;
1937 sign[1] := 72;
1938 sign[2] := 68;
1939 sign[3] := 82;
1940 st.writeBuffer(sign, 4);
1941 crc := crc32(0, @sign[0], 4);
1942 hbuf[0] := 0;
1943 hbuf[1] := 0;
1944 hbuf[2] := (Width shr 8) and $ff;
1945 hbuf[3] := Width and $ff;
1946 hbuf[4] := 0;
1947 hbuf[5] := 0;
1948 hbuf[6] := (Height shr 8) and $ff;
1949 hbuf[7] := Height and $ff;
1950 hbuf[8] := 8; // bit depth
1951 hbuf[9] := 2; // RGB
1952 hbuf[10] := 0; // compression method
1953 hbuf[11] := 0; // filter method
1954 hbuf[12] := 0; // no interlace
1955 crc := crc32(crc, @hbuf[0], 13);
1956 st.writeBuffer(hbuf, 13);
1957 writeIntBE(st, crc);
1958 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1960 // image data
1961 writeIntBE(st, LongWord(dlen));
1962 sign[0] := 73;
1963 sign[1] := 68;
1964 sign[2] := 65;
1965 sign[3] := 84;
1966 st.writeBuffer(sign, 4);
1967 crc := crc32(0, @sign[0], 4);
1968 crc := crc32(crc, obuf, dlen);
1969 st.writeBuffer(obuf^, dlen);
1970 writeIntBE(st, crc);
1971 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
1973 // image data end
1974 writeIntBE(st, LongWord(0));
1975 sign[0] := 73;
1976 sign[1] := 69;
1977 sign[2] := 78;
1978 sign[3] := 68;
1979 st.writeBuffer(sign, 4);
1980 crc := crc32(0, @sign[0], 4);
1981 writeIntBE(st, crc);
1982 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
1983 finally
1984 if obuf <> nil then FreeMem(obuf);
1985 end;
1986 end
1987 else
1988 begin
1989 Imaging.SetOption(ImagingPNGCompressLevel, 9);
1990 Imaging.SetOption(ImagingPNGPreFilter, 6);
1991 InitImage(img);
1992 try
1993 NewImage(Width, Height, TImageFormat.ifR8G8B8, img);
1994 ps := pixels;
1995 //writeln(stderr, 'moving pixels...');
1996 for y := Height-1 downto 0 do
1997 begin
1998 for x := 0 to Width-1 do
1999 begin
2000 clr.r := ps^; Inc(ps);
2001 clr.g := ps^; Inc(ps);
2002 clr.b := ps^; Inc(ps);
2003 clr.a := 255;
2004 SetPixel32(img, x, y, clr);
2005 end;
2006 end;
2007 GlobalMetadata.ClearMetaItems();
2008 GlobalMetadata.ClearMetaItemsForSaving();
2009 //writeln(stderr, 'compressing image...');
2010 if not SaveImageToStream('png', st, img) then raise Exception.Create('screenshot writing error');
2011 //writeln(stderr, 'done!');
2012 finally
2013 FreeImage(img);
2014 end;
2015 end;
2016 finally
2017 FreeMem(pixels);
2018 end;
2019 end;
2022 end.