DEADSOFTWARE

fix stub system driver and some warnings
[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 {$IFDEF USE_NANOGL}
863 v: array [0..15] of GLfloat;
864 {$ENDIF}
865 begin
866 if e_NoGraphics then Exit;
867 // Only top-left/bottom-right quad
868 if X1 > X2 then
869 begin
870 X1 := X1 xor X2;
871 X2 := X1 xor X2;
872 X1 := X1 xor X2;
873 end;
874 if Y1 > Y2 then
875 begin
876 Y1 := Y1 xor Y2;
877 Y2 := Y1 xor Y2;
878 Y1 := Y1 xor Y2;
879 end;
881 if Alpha > 0 then
882 begin
883 glEnable(GL_BLEND);
884 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
885 end else
886 glDisable(GL_BLEND);
888 glDisable(GL_TEXTURE_2D);
889 glColor4ub(Red, Green, Blue, 255-Alpha);
890 glLineWidth(1);
891 {$IFDEF USE_NANOGL}
892 nX1 := X1; nY1 := Y1;
893 nX2 := X2; nY2 := Y1;
894 e_LineCorrection(nX1, nY1, nX2, nY2);
895 v[0] := nX1; v[1] := nY1; v[2] := nX2; v[3] := nY2;
897 nX1 := X2; nY1 := Y1;
898 nX2 := X2; nY2 := Y2;
899 e_LineCorrection(nX1, nY1, nX2, nY2);
900 v[4] := nX1; v[5] := nY1; v[6] := nX2; v[7] := nY2;
902 nX1 := X2; nY1 := Y2;
903 nX2 := X1; nY2 := Y2;
904 e_LineCorrection(nX1, nY1, nX2, nY2);
905 v[8] := nX1; v[9] := nY1; v[10] := nX2; v[11] := nY2;
907 nX1 := X1; nY1 := Y2;
908 nX2 := X1; nY2 := Y1;
909 e_LineCorrection(nX1, nY1, nX2, nY2);
910 v[12] := nX1; v[13] := nY1; v[14] := nX2; v[15] := nY2;
912 glVertexPointer(2, GL_FLOAT, 0, @v[0]);
913 glEnableClientState(GL_VERTEX_ARRAY);
914 glDisableClientState(GL_COLOR_ARRAY);
915 glDisableClientState(GL_NORMAL_ARRAY);
916 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
917 glDrawArrays(GL_LINES, 0, 16);
918 {$ELSE}
919 glBegin(GL_LINES);
920 nX1 := X1; nY1 := Y1;
921 nX2 := X2; nY2 := Y1;
922 e_LineCorrection(nX1, nY1, nX2, nY2); // Pixel-perfect lines
923 glVertex2i(nX1, nY1);
924 glVertex2i(nX2, nY2);
926 nX1 := X2; nY1 := Y1;
927 nX2 := X2; nY2 := Y2;
928 e_LineCorrection(nX1, nY1, nX2, nY2);
929 glVertex2i(nX1, nY1);
930 glVertex2i(nX2, nY2);
932 nX1 := X2; nY1 := Y2;
933 nX2 := X1; nY2 := Y2;
934 e_LineCorrection(nX1, nY1, nX2, nY2);
935 glVertex2i(nX1, nY1);
936 glVertex2i(nX2, nY2);
938 nX1 := X1; nY1 := Y2;
939 nX2 := X1; nY2 := Y1;
940 e_LineCorrection(nX1, nY1, nX2, nY2);
941 glVertex2i(nX1, nY1);
942 glVertex2i(nX2, nY2);
943 glEnd();
944 {$ENDIF}
946 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
948 glDisable(GL_BLEND);
949 end;
951 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
952 Blending: TBlending = TBlending.None);
953 begin
954 if e_NoGraphics then Exit;
955 if (Alpha > 0) or (Blending <> TBlending.None) then
956 glEnable(GL_BLEND)
957 else
958 glDisable(GL_BLEND);
960 if Blending = TBlending.Blend then
961 glBlendFunc(GL_SRC_ALPHA, GL_ONE)
962 else
963 if Blending = TBlending.Filter then
964 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR)
965 else
966 if Blending = TBlending.Invert then
967 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO)
968 else
969 if Alpha > 0 then
970 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
972 glDisable(GL_TEXTURE_2D);
973 glColor4ub(Red, Green, Blue, 255-Alpha);
975 X2 := X2 + 1;
976 Y2 := Y2 + 1;
978 glBegin(GL_QUADS);
979 glVertex2i(X1, Y1);
980 glVertex2i(X2, Y1);
981 glVertex2i(X2, Y2);
982 glVertex2i(X1, Y2);
983 glEnd();
985 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
987 glDisable(GL_BLEND);
988 end;
991 // ////////////////////////////////////////////////////////////////////////// //
992 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
993 begin
994 if (a < 0) then a := 0;
995 if (a > 255) then a := 255;
996 glEnable(GL_BLEND);
997 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
998 glDisable(GL_TEXTURE_2D);
999 glColor4ub(0, 0, 0, Byte(255-a));
1000 glBegin(GL_QUADS);
1001 glVertex2i(x0, y0);
1002 glVertex2i(x1, y0);
1003 glVertex2i(x1, y1);
1004 glVertex2i(x0, y1);
1005 glEnd();
1006 //glRect(x, y, x+w, y+h);
1007 glColor4ub(1, 1, 1, 1);
1008 glDisable(GL_BLEND);
1009 //glBlendEquation(GL_FUNC_ADD);
1010 end;
1012 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
1013 begin
1014 if (w > 0) and (h > 0) then e_DarkenQuad(x, y, x+w, y+h, a);
1015 end;
1018 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
1019 {$IFDEF USE_NANOGL}
1020 var
1021 v: array [0..3] of GLfloat;
1022 {$ENDIF}
1023 begin
1024 if e_NoGraphics then Exit;
1025 // Pixel-perfect lines
1026 if Width = 1 then
1027 e_LineCorrection(X1, Y1, X2, Y2);
1029 if Alpha > 0 then
1030 begin
1031 glEnable(GL_BLEND);
1032 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1033 end else
1034 glDisable(GL_BLEND);
1036 glDisable(GL_TEXTURE_2D);
1037 glColor4ub(Red, Green, Blue, 255-Alpha);
1038 glLineWidth(Width);
1040 {$IFDEF USE_NANOGL}
1041 v[0] := X1; v[1] := Y1; v[2] := X2; v[3] := Y2;
1042 glVertexPointer(2, GL_FLOAT, 0, @v[0]);
1043 glEnableClientState(GL_VERTEX_ARRAY);
1044 glDisableClientState(GL_COLOR_ARRAY);
1045 glDisableClientState(GL_NORMAL_ARRAY);
1046 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1047 glDrawArrays(GL_LINES, 0, 4);
1048 {$ELSE}
1049 glBegin(GL_LINES);
1050 glVertex2i(X1, Y1);
1051 glVertex2i(X2, Y2);
1052 glEnd();
1053 {$ENDIF}
1055 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1057 glDisable(GL_BLEND);
1058 end;
1060 //------------------------------------------------------------------
1061 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1062 //------------------------------------------------------------------
1063 procedure e_DeleteTexture(ID: DWORD);
1064 begin
1065 if not e_NoGraphics then
1066 glDeleteTextures(1, @e_Textures[ID].tx.id);
1067 e_Textures[ID].tx.id := 0;
1068 e_Textures[ID].tx.Width := 0;
1069 e_Textures[ID].tx.Height := 0;
1070 end;
1072 //------------------------------------------------------------------
1073 // Óäàëÿåò âñå òåêñòóðû
1074 //------------------------------------------------------------------
1075 procedure e_RemoveAllTextures();
1076 var
1077 i: integer;
1078 begin
1079 if e_Textures = nil then Exit;
1081 for i := 0 to High(e_Textures) do
1082 if e_Textures[i].tx.Width <> 0 then e_DeleteTexture(i);
1083 e_Textures := nil;
1084 end;
1086 //------------------------------------------------------------------
1087 // Óäàëÿåò äâèæîê
1088 //------------------------------------------------------------------
1089 procedure e_ReleaseEngine();
1090 begin
1091 e_RemoveAllTextures;
1092 e_RemoveAllTextureFont;
1093 end;
1095 procedure e_BeginRender();
1096 begin
1097 if e_NoGraphics then Exit;
1098 glEnable(GL_ALPHA_TEST);
1099 glAlphaFunc(GL_GREATER, 0.0);
1100 end;
1102 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
1103 begin
1104 if e_NoGraphics then Exit;
1105 glClearColor(Red, Green, Blue, 0);
1106 glClear(Mask);
1107 end;
1109 procedure e_Clear(); overload;
1110 begin
1111 if e_NoGraphics then Exit;
1112 glClearColor(0, 0, 0, 0);
1113 glClear(GL_COLOR_BUFFER_BIT);
1114 end;
1116 procedure e_EndRender();
1117 begin
1118 if e_NoGraphics then Exit;
1119 glPopMatrix();
1120 end;
1122 {$IFDEF USE_SDL2}
1123 function e_GetGamma(win: PSDL_Window): Byte;
1124 var
1125 ramp: array [0..256*3-1] of Word;
1126 rgb: array [0..2] of Double;
1127 sum: double;
1128 count: integer;
1129 min: integer;
1130 max: integer;
1131 A, B: double;
1132 i, j: integer;
1133 begin
1134 Result := 0;
1135 if e_NoGraphics then Exit;
1136 rgb[0] := 1.0;
1137 rgb[1] := 1.0;
1138 rgb[2] := 1.0;
1140 SDL_GetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1142 for i := 0 to 2 do
1143 begin
1144 sum := 0;
1145 count := 0;
1146 min := 256 * i;
1147 max := min + 256;
1149 for j := min to max - 1 do
1150 if ramp[j] > 0 then
1151 begin
1152 B := (j mod 256)/256;
1153 A := ramp[j]/65536;
1154 sum := sum + ln(A)/ln(B);
1155 inc(count);
1156 end;
1157 rgb[i] := sum / count;
1158 end;
1160 Result := 100 - Trunc(((rgb[0] + rgb[1] + rgb[2])/3 - 0.23) * 100/(2.7 - 0.23));
1161 end;
1163 procedure e_SetGamma(win: PSDL_Window; Gamma: Byte);
1164 var
1165 ramp: array [0..256*3-1] of Word;
1166 i: integer;
1167 r: double;
1168 g: double;
1169 begin
1170 if e_NoGraphics then Exit;
1171 g := (100 - Gamma)*(2.7 - 0.23)/100 + 0.23;
1173 for i := 0 to 255 do
1174 begin
1175 r := Exp(g * ln(i/256))*65536;
1176 if r < 0 then r := 0
1177 else if r > 65535 then r := 65535;
1178 ramp[i] := trunc(r);
1179 ramp[i + 256] := trunc(r);
1180 ramp[i + 512] := trunc(r);
1181 end;
1183 SDL_SetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1184 end;
1185 {$ENDIF}
1187 function e_CharFont_Create(sp: ShortInt=0): DWORD;
1188 var
1189 i, id: DWORD;
1190 begin
1191 e_WriteLog('Creating CharFont...', TMsgType.Notify);
1193 id := DWORD(-1);
1195 if e_CharFonts <> nil then
1196 for i := 0 to High(e_CharFonts) do
1197 if not e_CharFonts[i].alive then
1198 begin
1199 id := i;
1200 Break;
1201 end;
1203 if id = DWORD(-1) then
1204 begin
1205 SetLength(e_CharFonts, Length(e_CharFonts) + 1);
1206 id := High(e_CharFonts);
1207 end;
1209 with e_CharFonts[id] do
1210 begin
1211 for i := 0 to High(Chars) do
1212 with Chars[i] do
1213 begin
1214 TextureID := -1;
1215 Width := 0;
1216 end;
1218 Space := sp;
1219 alive := True;
1220 end;
1222 Result := id;
1223 end;
1225 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
1226 begin
1227 with e_CharFonts[FontID].Chars[Ord(c)] do
1228 begin
1229 TextureID := Texture;
1230 Width := w;
1231 end;
1232 end;
1234 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
1235 var
1236 a: Integer;
1237 begin
1238 if e_NoGraphics then Exit;
1239 if Text = '' then Exit;
1240 if e_CharFonts = nil then Exit;
1241 if Integer(FontID) > High(e_CharFonts) then Exit;
1243 with e_CharFonts[FontID] do
1244 begin
1245 for a := 1 to Length(Text) do
1246 with Chars[Ord(Text[a])] do
1247 if TextureID <> -1 then
1248 begin
1249 e_Draw(TextureID, X, Y, 0, True, False);
1250 X := X+Width+IfThen(a = Length(Text), 0, Space);
1251 end;
1252 end;
1253 end;
1255 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
1256 Color: TRGB; Scale: Single = 1.0);
1257 var
1258 a: Integer;
1259 c: TRGB;
1260 begin
1261 if e_NoGraphics then Exit;
1262 if Text = '' then Exit;
1263 if e_CharFonts = nil then Exit;
1264 if Integer(FontID) > High(e_CharFonts) then Exit;
1266 with e_CharFonts[FontID] do
1267 begin
1268 for a := 1 to Length(Text) do
1269 with Chars[Ord(Text[a])] do
1270 if TextureID <> -1 then
1271 begin
1272 if Scale <> 1.0 then
1273 begin
1274 glPushMatrix;
1275 glScalef(Scale, Scale, 0);
1276 end;
1278 c := e_Colors;
1279 e_Colors := Color;
1280 e_Draw(TextureID, X, Y, 0, True, False);
1281 e_Colors := c;
1283 if Scale <> 1.0 then glPopMatrix;
1285 X := X+Width+IfThen(a = Length(Text), 0, Space);
1286 end;
1287 end;
1288 end;
1290 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
1291 var
1292 a, TX, TY, len: Integer;
1293 tc, c: TRGB;
1294 w, h: Word;
1295 begin
1296 if e_NoGraphics then Exit;
1297 if Text = '' then Exit;
1298 if e_CharFonts = nil then Exit;
1299 if Integer(FontID) > High(e_CharFonts) then Exit;
1301 c.R := 255;
1302 c.G := 255;
1303 c.B := 255;
1305 TX := X;
1306 TY := Y;
1307 len := Length(Text);
1309 e_CharFont_GetSize(FontID, 'A', w, h);
1311 with e_CharFonts[FontID] do
1312 begin
1313 for a := 1 to len do
1314 begin
1315 case Text[a] of
1316 #10: // line feed
1317 begin
1318 TX := X;
1319 TY := TY + h;
1320 continue;
1321 end;
1322 #1: // black
1323 begin
1324 c.R := 0; c.G := 0; c.B := 0;
1325 continue;
1326 end;
1327 #2: // white
1328 begin
1329 c.R := 255; c.G := 255; c.B := 255;
1330 continue;
1331 end;
1332 #3: // darker
1333 begin
1334 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1335 continue;
1336 end;
1337 #4: // lighter
1338 begin
1339 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1340 continue;
1341 end;
1342 #18: // red
1343 begin
1344 c.R := 255; c.G := 0; c.B := 0;
1345 continue;
1346 end;
1347 #19: // green
1348 begin
1349 c.R := 0; c.G := 255; c.B := 0;
1350 continue;
1351 end;
1352 #20: // blue
1353 begin
1354 c.R := 0; c.G := 0; c.B := 255;
1355 continue;
1356 end;
1357 #21: // yellow
1358 begin
1359 c.R := 255; c.G := 255; c.B := 0;
1360 continue;
1361 end;
1362 end;
1364 with Chars[Ord(Text[a])] do
1365 if TextureID <> -1 then
1366 begin
1367 tc := e_Colors;
1368 e_Colors := c;
1369 e_Draw(TextureID, TX, TY, 0, True, False);
1370 e_Colors := tc;
1372 TX := TX+Width+IfThen(a = Length(Text), 0, Space);
1373 end;
1374 end;
1375 end;
1376 end;
1378 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
1379 var
1380 a: Integer;
1381 h2: Word;
1382 begin
1383 w := 0;
1384 h := 0;
1386 if Text = '' then Exit;
1387 if e_CharFonts = nil then Exit;
1388 if Integer(FontID) > High(e_CharFonts) then Exit;
1390 with e_CharFonts[FontID] do
1391 begin
1392 for a := 1 to Length(Text) do
1393 with Chars[Ord(Text[a])] do
1394 if TextureID <> -1 then
1395 begin
1396 w := w+Width+IfThen(a = Length(Text), 0, Space);
1397 e_GetTextureSize(TextureID, nil, @h2);
1398 if h2 > h then h := h2;
1399 end;
1400 end;
1401 end;
1403 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
1404 var
1405 a, lines, len: Integer;
1406 h2, w2, tw, th: Word;
1407 begin
1408 w2 := 0;
1409 h2 := 0;
1410 tw := 0;
1411 th := 0;
1413 if Text = '' then Exit;
1414 if e_CharFonts = nil then Exit;
1415 if Integer(FontID) > High(e_CharFonts) then Exit;
1417 lines := 1;
1418 len := Length(Text);
1420 with e_CharFonts[FontID] do
1421 begin
1422 for a := 1 to len do
1423 begin
1424 if Text[a] = #10 then
1425 begin
1426 Inc(lines);
1427 if w2 > tw then tw := w2;
1428 w2 := 0;
1429 continue;
1430 end;
1432 with Chars[Ord(Text[a])] do
1433 if TextureID <> -1 then
1434 begin
1435 w2 := w2 + Width + IfThen(a = len, 0, Space);
1436 e_GetTextureSize(TextureID, nil, @h2);
1437 if h2 > th then th := h2;
1438 end;
1439 end;
1440 end;
1442 if w2 > tw then
1443 tw := w2;
1445 w := tw;
1446 h := th * lines;
1447 end;
1449 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
1450 var
1451 a: Integer;
1452 begin
1453 Result := 0;
1455 if e_CharFonts = nil then Exit;
1456 if Integer(FontID) > High(e_CharFonts) then Exit;
1458 for a := 0 to High(e_CharFonts[FontID].Chars) do
1459 Result := Max(Result, e_CharFonts[FontID].Chars[a].Width);
1460 end;
1462 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
1463 var
1464 a: Integer;
1465 h2: Word;
1466 begin
1467 Result := 0;
1469 if e_CharFonts = nil then Exit;
1470 if Integer(FontID) > High(e_CharFonts) then Exit;
1472 for a := 0 to High(e_CharFonts[FontID].Chars) do
1473 begin
1474 if e_CharFonts[FontID].Chars[a].TextureID <> -1 then
1475 e_GetTextureSize(e_CharFonts[FontID].Chars[a].TextureID, nil, @h2)
1476 else h2 := 0;
1477 if h2 > Result then Result := h2;
1478 end;
1479 end;
1481 procedure e_CharFont_Remove(FontID: DWORD);
1482 var
1483 a: Integer;
1484 begin
1485 with e_CharFonts[FontID] do
1486 for a := 0 to High(Chars) do
1487 if Chars[a].TextureID <> -1 then e_DeleteTexture(Chars[a].TextureID);
1489 e_CharFonts[FontID].alive := False;
1490 end;
1492 procedure e_CharFont_RemoveAll();
1493 var
1494 a: Integer;
1495 begin
1496 if e_CharFonts = nil then Exit;
1498 for a := 0 to High(e_CharFonts) do
1499 e_CharFont_Remove(a);
1501 e_CharFonts := nil;
1502 end;
1504 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
1505 Space: ShortInt=0);
1506 var
1507 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1508 loop1 : GLuint;
1509 cx, cy : real;
1510 {$ENDIF}
1511 i, id: DWORD;
1512 begin
1513 if e_NoGraphics then Exit;
1514 e_WriteLog('Creating texture font...', TMsgType.Notify);
1516 id := DWORD(-1);
1518 if e_TextureFonts <> nil then
1519 for i := 0 to High(e_TextureFonts) do
1520 if e_TextureFonts[i].Base = 0 then
1521 begin
1522 id := i;
1523 Break;
1524 end;
1526 if id = DWORD(-1) then
1527 begin
1528 SetLength(e_TextureFonts, Length(e_TextureFonts) + 1);
1529 id := High(e_TextureFonts);
1530 end;
1532 with e_TextureFonts[id] do
1533 begin
1534 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1535 Base := glGenLists(XCount*YCount);
1536 {$ENDIF}
1537 TextureID := e_Textures[Tex].tx.id;
1538 CharWidth := (e_Textures[Tex].tx.Width div XCount)+Space;
1539 CharHeight := e_Textures[Tex].tx.Height div YCount;
1540 XC := XCount;
1541 YC := YCount;
1542 Texture := Tex;
1543 SPC := Space;
1544 end;
1546 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1547 glBindTexture(GL_TEXTURE_2D, e_Textures[Tex].tx.id);
1548 for loop1 := 0 to XCount*YCount-1 do
1549 begin
1550 cx := (loop1 mod XCount)/XCount;
1551 cy := (loop1 div YCount)/YCount;
1553 glNewList(e_TextureFonts[id].Base+loop1, GL_COMPILE);
1554 glBegin(GL_QUADS);
1555 glTexCoord2f(cx, 1.0-cy-1/YCount);
1556 glVertex2i(0, e_Textures[Tex].tx.Height div YCount);
1558 glTexCoord2f(cx+1/XCount, 1.0-cy-1/YCount);
1559 glVertex2i(e_Textures[Tex].tx.Width div XCount, e_Textures[Tex].tx.Height div YCount);
1561 glTexCoord2f(cx+1/XCount, 1.0-cy);
1562 glVertex2i(e_Textures[Tex].tx.Width div XCount, 0);
1564 glTexCoord2f(cx, 1.0-cy);
1565 glVertex2i(0, 0);
1566 glEnd();
1567 glTranslated((e_Textures[Tex].tx.Width div XCount)+Space, 0, 0);
1568 glEndList();
1569 end;
1570 {$ENDIF}
1572 FontID := id;
1573 end;
1575 procedure e_TextureFontKill(FontID: DWORD);
1576 begin
1577 if e_NoGraphics then Exit;
1578 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1579 glDeleteLists(e_TextureFonts[FontID].Base, 256);
1580 {$ENDIF}
1581 e_TextureFonts[FontID].Base := 0;
1582 end;
1584 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1585 procedure e_TextureFontDrawChar(ch: Char; FontID: DWORD);
1586 var
1587 index: Integer;
1588 cx, cy: GLfloat;
1589 Tex: Integer;
1590 Width, Height: Integer;
1591 XCount, YCount: Integer;
1592 begin
1593 index := Ord(ch) - 32;
1594 Tex := e_TextureFonts[FontID].Texture;
1595 Width := e_Textures[Tex].tx.Width;
1596 Height := e_Textures[Tex].tx.Height;
1597 XCount := e_TextureFonts[FontID].XC;
1598 YCount := e_TextureFonts[FontID].YC;
1599 cx := (index mod XCount)/XCount;
1600 cy := (index div YCount)/YCount;
1601 glBegin(GL_QUADS);
1602 glTexCoord2f(cx, 1 - cy - 1/YCount);
1603 glVertex2i(0, Height div YCount);
1604 glTexCoord2f(cx + 1/XCount, 1 - cy - 1/YCount);
1605 glVertex2i(Width div XCount, Height div YCount);
1606 glTexCoord2f(cx + 1/XCount, 1 - cy);
1607 glVertex2i(Width div XCount, 0);
1608 glTexCoord2f(cx, 1 - cy);
1609 glVertex2i(0, 0);
1610 glEnd();
1611 glTranslatef((e_Textures[Tex].tx.Width div XCount) + e_TextureFonts[FontID].SPC, 0, 0);
1612 end;
1614 procedure e_TextureFontDrawString(Text: String; FontID: DWORD);
1615 var
1616 i: Integer;
1617 begin
1618 for i := 1 to High(Text) do
1619 e_TextureFontDrawChar(Text[i], FontID);
1620 end;
1621 {$ENDIF}
1623 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
1624 begin
1625 if e_NoGraphics then Exit;
1626 if Integer(FontID) > High(e_TextureFonts) then Exit;
1627 if Text = '' then Exit;
1629 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1630 glEnable(GL_BLEND);
1632 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1634 glPushMatrix;
1635 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1636 glEnable(GL_TEXTURE_2D);
1637 glTranslatef(x, y, 0);
1638 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1639 e_TextureFontDrawString(Text, FontID);
1640 {$ELSE}
1641 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1642 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1643 {$ENDIF}
1644 glDisable(GL_TEXTURE_2D);
1645 glPopMatrix;
1647 glDisable(GL_BLEND);
1648 end;
1650 // god forgive me for this, but i cannot figure out how to do it without lists
1651 procedure e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1652 begin
1653 if e_NoGraphics then Exit;
1654 glPushMatrix;
1656 if Shadow then
1657 begin
1658 glColor4ub(0, 0, 0, 128);
1659 glTranslatef(X+1, Y+1, 0);
1660 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1661 e_TextureFontDrawChar(Ch, FontID);
1662 {$ELSE}
1663 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1664 {$ENDIF}
1665 glPopMatrix;
1666 glPushMatrix;
1667 end;
1669 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1670 glTranslatef(X, Y, 0);
1671 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1672 e_TextureFontDrawChar(Ch, FontID);
1673 {$ELSE}
1674 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1675 {$ENDIF}
1677 glPopMatrix;
1678 end;
1680 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1681 begin
1682 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1683 glEnable(GL_TEXTURE_2D);
1684 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1686 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1687 glEnable(GL_BLEND);
1688 e_TextureFontPrintChar(X, Y, Ch, FontID, Shadow);
1689 glDisable(GL_TEXTURE_2D);
1690 glDisable(GL_BLEND);
1691 end;
1693 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
1694 begin
1695 result := e_TextureFonts[FontID].CharWidth;
1696 end;
1698 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD;
1699 Shadow: Boolean = False; Newlines: Boolean = False);
1700 var
1701 a, TX, TY, len: Integer;
1702 tc, c: TRGB;
1703 w, h: Word;
1704 begin
1705 if e_NoGraphics then Exit;
1706 if Text = '' then Exit;
1707 if e_TextureFonts = nil then Exit;
1708 if Integer(FontID) > High(e_TextureFonts) then Exit;
1710 c.R := 255;
1711 c.G := 255;
1712 c.B := 255;
1714 TX := X;
1715 TY := Y;
1716 len := Length(Text);
1718 w := e_TextureFonts[FontID].CharWidth;
1719 h := e_TextureFonts[FontID].CharHeight;
1721 with e_TextureFonts[FontID] do
1722 begin
1723 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1724 glEnable(GL_TEXTURE_2D);
1726 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1727 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1728 {$ENDIF}
1730 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1731 glEnable(GL_BLEND);
1733 for a := 1 to len do
1734 begin
1735 case Text[a] of
1736 #10: // line feed
1737 begin
1738 if Newlines then
1739 begin
1740 TX := X;
1741 TY := TY + h;
1742 continue;
1743 end;
1744 end;
1745 #1: // black
1746 begin
1747 c.R := 0; c.G := 0; c.B := 0;
1748 continue;
1749 end;
1750 #2: // white
1751 begin
1752 c.R := 255; c.G := 255; c.B := 255;
1753 continue;
1754 end;
1755 #3: // darker
1756 begin
1757 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1758 continue;
1759 end;
1760 #4: // lighter
1761 begin
1762 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1763 continue;
1764 end;
1765 #18: // red
1766 begin
1767 c.R := 255; c.G := 0; c.B := 0;
1768 continue;
1769 end;
1770 #19: // green
1771 begin
1772 c.R := 0; c.G := 255; c.B := 0;
1773 continue;
1774 end;
1775 #20: // blue
1776 begin
1777 c.R := 0; c.G := 0; c.B := 255;
1778 continue;
1779 end;
1780 #21: // yellow
1781 begin
1782 c.R := 255; c.G := 255; c.B := 0;
1783 continue;
1784 end;
1785 end;
1787 tc := e_Colors;
1788 e_Colors := c;
1789 e_TextureFontPrintChar(TX, TY, Text[a], FontID, Shadow);
1790 e_Colors := tc;
1792 TX := TX+w;
1793 end;
1794 glDisable(GL_TEXTURE_2D);
1795 glDisable(GL_BLEND);
1796 end;
1797 end;
1799 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
1800 Blue: Byte; Scale: Single; Shadow: Boolean = False);
1801 begin
1802 if e_NoGraphics then Exit;
1803 if Text = '' then Exit;
1805 glPushMatrix;
1806 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1807 glEnable(GL_TEXTURE_2D);
1809 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1810 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1811 {$ENDIF}
1813 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1814 glEnable(GL_BLEND);
1816 if Shadow then
1817 begin
1818 glColor4ub(0, 0, 0, 128);
1819 glTranslatef(x+1, y+1, 0);
1820 glScalef(Scale, Scale, 0);
1821 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1822 e_TextureFontDrawString(Text, FontID);
1823 {$ELSE}
1824 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1825 {$ENDIF}
1826 glPopMatrix;
1827 glPushMatrix;
1828 end;
1830 glColor4ub(Red, Green, Blue, 255);
1831 glTranslatef(x, y, 0);
1832 glScalef(Scale, Scale, 0);
1833 {$IF DEFINED(USE_NANOGL) or DEFINED(USE_NOGL)}
1834 e_TextureFontDrawString(Text, FontID);
1835 {$ELSE}
1836 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1837 {$ENDIF}
1839 glDisable(GL_TEXTURE_2D);
1840 glPopMatrix;
1841 glColor3ub(e_Colors.R, e_Colors.G, e_Colors.B);
1842 glDisable(GL_BLEND);
1843 end;
1845 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
1846 begin
1847 CharWidth := 16;
1848 CharHeight := 16;
1849 if e_NoGraphics then Exit;
1850 if Integer(ID) > High(e_TextureFonts) then
1851 Exit;
1852 CharWidth := e_TextureFonts[ID].CharWidth;
1853 CharHeight := e_TextureFonts[ID].CharHeight;
1854 end;
1856 procedure e_RemoveAllTextureFont();
1857 var
1858 i: integer;
1859 begin
1860 if e_NoGraphics then Exit;
1861 if e_TextureFonts = nil then Exit;
1863 for i := 0 to High(e_TextureFonts) do
1864 if e_TextureFonts[i].Base <> 0 then
1865 begin
1866 {$IF not DEFINED(USE_NANOGL) and not DEFINED(USE_NOGL)}
1867 glDeleteLists(e_TextureFonts[i].Base, 256);
1868 {$ENDIF}
1869 e_TextureFonts[i].Base := 0;
1870 end;
1872 e_TextureFonts := nil;
1873 end;
1875 function _RGB(Red, Green, Blue: Byte): TRGB;
1876 begin
1877 Result.R := Red;
1878 Result.G := Green;
1879 Result.B := Blue;
1880 end;
1882 function _Point(X, Y: Integer): TPoint2i;
1883 begin
1884 Result.X := X;
1885 Result.Y := Y;
1886 end;
1888 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
1889 begin
1890 Result.X := X;
1891 Result.Y := Y;
1892 Result.Width := Width;
1893 Result.Height := Height;
1894 end;
1896 function _TRect(L, T, R, B: LongInt): TRect;
1897 begin
1898 Result.Top := T;
1899 Result.Left := L;
1900 Result.Right := R;
1901 Result.Bottom := B;
1902 end;
1905 procedure e_MakeScreenshot (st: TStream; Width, Height: Word);
1906 var
1907 pixels, obuf, scln, ps, pd: PByte;
1908 obufsize: Integer;
1909 dlen: Cardinal;
1910 i, x, y, res: Integer;
1911 sign: array [0..7] of Byte;
1912 hbuf: array [0..12] of Byte;
1913 crc: LongWord;
1914 img: TImageData;
1915 clr: TColor32Rec;
1916 begin
1917 if e_NoGraphics then Exit;
1918 obuf := nil;
1920 // first, extract and pack graphics data
1921 if (Width mod 4) > 0 then Width := Width+4-(Width mod 4);
1923 GetMem(pixels, Width*Height*3);
1924 try
1925 FillChar(pixels^, Width*Height*3, 0);
1926 glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
1927 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1929 if e_FastScreenshots then
1930 begin
1931 // create scanlines
1932 GetMem(scln, (Width*3+1)*Height);
1933 try
1934 ps := pixels;
1935 pd := scln;
1936 Inc(ps, (Width*3)*(Height-1));
1937 for i := 0 to Height-1 do
1938 begin
1939 pd^ := 0; // filter
1940 Inc(pd);
1941 Move(ps^, pd^, Width*3);
1942 Dec(ps, Width*3);
1943 Inc(pd, Width*3);
1944 end;
1945 except
1946 FreeMem(scln);
1947 raise;
1948 end;
1949 FreeMem(pixels);
1950 pixels := scln;
1952 // pack it
1953 obufsize := (Width*3+1)*Height*2;
1954 GetMem(obuf, obufsize);
1955 try
1956 while true do
1957 begin
1958 dlen := obufsize;
1959 res := compress2(Pointer(obuf), dlen, Pointer(pixels), (Width*3+1)*Height, 9);
1960 if res = Z_OK then break;
1961 if res <> Z_BUF_ERROR then raise Exception.Create('can''t pack data for PNG');
1962 obufsize := obufsize*2;
1963 FreeMem(obuf);
1964 obuf := nil;
1965 GetMem(obuf, obufsize);
1966 end;
1967 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1969 // now write PNG
1971 // signature
1972 sign[0] := 137;
1973 sign[1] := 80;
1974 sign[2] := 78;
1975 sign[3] := 71;
1976 sign[4] := 13;
1977 sign[5] := 10;
1978 sign[6] := 26;
1979 sign[7] := 10;
1980 st.writeBuffer(sign, 8);
1981 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1983 // header
1984 writeIntBE(st, LongWord(13));
1985 sign[0] := 73;
1986 sign[1] := 72;
1987 sign[2] := 68;
1988 sign[3] := 82;
1989 st.writeBuffer(sign, 4);
1990 crc := crc32(0, @sign[0], 4);
1991 hbuf[0] := 0;
1992 hbuf[1] := 0;
1993 hbuf[2] := (Width shr 8) and $ff;
1994 hbuf[3] := Width and $ff;
1995 hbuf[4] := 0;
1996 hbuf[5] := 0;
1997 hbuf[6] := (Height shr 8) and $ff;
1998 hbuf[7] := Height and $ff;
1999 hbuf[8] := 8; // bit depth
2000 hbuf[9] := 2; // RGB
2001 hbuf[10] := 0; // compression method
2002 hbuf[11] := 0; // filter method
2003 hbuf[12] := 0; // no interlace
2004 crc := crc32(crc, @hbuf[0], 13);
2005 st.writeBuffer(hbuf, 13);
2006 writeIntBE(st, crc);
2007 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2009 // image data
2010 writeIntBE(st, LongWord(dlen));
2011 sign[0] := 73;
2012 sign[1] := 68;
2013 sign[2] := 65;
2014 sign[3] := 84;
2015 st.writeBuffer(sign, 4);
2016 crc := crc32(0, @sign[0], 4);
2017 crc := crc32(crc, obuf, dlen);
2018 st.writeBuffer(obuf^, dlen);
2019 writeIntBE(st, crc);
2020 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2022 // image data end
2023 writeIntBE(st, LongWord(0));
2024 sign[0] := 73;
2025 sign[1] := 69;
2026 sign[2] := 78;
2027 sign[3] := 68;
2028 st.writeBuffer(sign, 4);
2029 crc := crc32(0, @sign[0], 4);
2030 writeIntBE(st, crc);
2031 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2032 finally
2033 if obuf <> nil then FreeMem(obuf);
2034 end;
2035 end
2036 else
2037 begin
2038 Imaging.SetOption(ImagingPNGCompressLevel, 9);
2039 Imaging.SetOption(ImagingPNGPreFilter, 6);
2040 InitImage(img);
2041 try
2042 NewImage(Width, Height, TImageFormat.ifR8G8B8, img);
2043 ps := pixels;
2044 //writeln(stderr, 'moving pixels...');
2045 for y := Height-1 downto 0 do
2046 begin
2047 for x := 0 to Width-1 do
2048 begin
2049 clr.r := ps^; Inc(ps);
2050 clr.g := ps^; Inc(ps);
2051 clr.b := ps^; Inc(ps);
2052 clr.a := 255;
2053 SetPixel32(img, x, y, clr);
2054 end;
2055 end;
2056 GlobalMetadata.ClearMetaItems();
2057 GlobalMetadata.ClearMetaItemsForSaving();
2058 //writeln(stderr, 'compressing image...');
2059 if not SaveImageToStream('png', st, img) then raise Exception.Create('screenshot writing error');
2060 //writeln(stderr, 'done!');
2061 finally
2062 FreeImage(img);
2063 end;
2064 end;
2065 finally
2066 FreeMem(pixels);
2067 end;
2068 end;
2071 end.