DEADSOFTWARE

c5ec8f36e94baae90f47ca21bbd20d32416a36d9
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit e_graphics;
19 interface
21 uses
22 {$IFDEF USE_NANOGL}
23 nanoGL,
24 {$ELSE}
25 GL, GLExt,
26 {$ENDIF}
27 SysUtils, Classes, Math, e_log, e_texture, SDL2, MAPDEF, ImagingTypes, Imaging, ImagingUtility;
29 type
30 TMirrorType=(None, Horizontal, Vertical);
31 TBlending=(None, Blend, Filter, Invert);
33 TPoint2i = record
34 X, Y: Integer;
35 end;
37 TPoint2f = record
38 X, Y: Double;
39 end;
41 TRect = record
42 Left, Top, Right, Bottom: Integer;
43 end;
45 TRectWH = record
46 X, Y: Integer;
47 Width, Height: Word;
48 end;
50 TRGB = packed record
51 R, G, B: Byte;
52 end;
54 PDFPoint = ^TDFPoint;
55 PPoint2f = ^TPoint2f;
56 PRect = ^TRect;
57 PRectWH = ^TRectWH;
60 //------------------------------------------------------------------
61 // ïðîòîòèïû ôóíêöèé
62 //------------------------------------------------------------------
63 procedure e_InitGL();
64 procedure e_SetViewPort(X, Y, Width, Height: Word);
65 procedure e_ResizeWindow(Width, Height: Integer);
67 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
68 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
69 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
70 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
71 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
72 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
73 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
74 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
76 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
77 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
79 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
80 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
82 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
84 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
85 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
86 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
87 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
88 Blending: TBlending = TBlending.None);
89 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
90 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
92 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
93 function e_CreateTexture(FileName: string; var ID: DWORD): Boolean;
94 function e_CreateTextureEx(FileName: string; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
95 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
96 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
97 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
98 procedure e_DeleteTexture(ID: DWORD);
99 procedure e_RemoveAllTextures();
101 // CharFont
102 function e_CharFont_Create(sp: ShortInt=0): DWORD;
103 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
104 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
105 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
106 Color: TRGB; Scale: Single = 1.0);
107 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
108 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
109 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
110 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
111 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
112 procedure e_CharFont_Remove(FontID: DWORD);
113 procedure e_CharFont_RemoveAll();
115 // TextureFont
116 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
117 Space: ShortInt=0);
118 procedure e_TextureFontKill(FontID: DWORD);
119 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
120 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
121 Blue: Byte; Scale: Single; Shadow: Boolean = False);
122 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD; Shadow: 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 function e_GetGamma(win: PSDL_Window): Byte;
136 procedure e_SetGamma(win: PSDL_Window;Gamma: Byte);
138 procedure e_MakeScreenshot(st: TStream; Width, Height: Word);
140 function _RGB(Red, Green, Blue: Byte): TRGB;
141 function _Point(X, Y: Integer): TPoint2i;
142 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
143 function _TRect(L, T, R, B: LongInt): TRect;
145 //function e_getTextGLId (ID: DWORD): GLuint;
147 var
148 e_Colors: TRGB;
149 e_NoGraphics: Boolean = False;
150 e_FastScreenshots: Boolean = true; // it's REALLY SLOW with `false`
151 g_dbg_scale: Single = 1.0;
154 implementation
156 uses
157 paszlib, crc, utils;
160 type
161 TTexture = record
162 tx: GLTexture;
163 end;
165 TTextureFont = record
166 Texture: DWORD;
167 TextureID: DWORD;
168 Base: Uint32;
169 CharWidth: Byte;
170 CharHeight: Byte;
171 XC, YC: WORD;
172 SPC: ShortInt;
173 end;
175 TCharFont = record
176 Chars: array[0..255] of
177 record
178 TextureID: Integer;
179 Width: Byte;
180 end;
181 Space: ShortInt;
182 Height: ShortInt;
183 alive: Boolean;
184 end;
186 TSavedTexture = record
187 TexID: DWORD;
188 OldID: DWORD;
189 Pixels: Pointer;
190 end;
192 var
193 e_Textures: array of TTexture = nil;
194 e_TextureFonts: array of TTextureFont = nil;
195 e_CharFonts: array of TCharFont;
196 //e_SavedTextures: array of TSavedTexture;
198 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
200 //------------------------------------------------------------------
201 // Èíèöèàëèçèðóåò OpenGL
202 //------------------------------------------------------------------
203 procedure e_InitGL();
204 begin
205 if e_NoGraphics then
206 begin
207 e_DummyTextures := True;
208 Exit;
209 end;
210 e_Colors.R := 255;
211 e_Colors.G := 255;
212 e_Colors.B := 255;
213 glDisable(GL_DEPTH_TEST);
214 glEnable(GL_SCISSOR_TEST);
215 glClearColor(0, 0, 0, 0);
216 end;
218 procedure e_SetViewPort(X, Y, Width, Height: Word);
219 var
220 mat: Array [0..15] of GLDouble;
222 begin
223 if e_NoGraphics then Exit;
224 glLoadIdentity();
225 glScissor(X, Y, Width, Height);
226 glViewport(X, Y, Width, Height);
227 //gluOrtho2D(0, Width, Height, 0);
229 glMatrixMode(GL_PROJECTION);
231 mat[ 0] := 2.0 / Width;
232 mat[ 1] := 0.0;
233 mat[ 2] := 0.0;
234 mat[ 3] := 0.0;
236 mat[ 4] := 0.0;
237 mat[ 5] := -2.0 / Height;
238 mat[ 6] := 0.0;
239 mat[ 7] := 0.0;
241 mat[ 8] := 0.0;
242 mat[ 9] := 0.0;
243 mat[10] := 1.0;
244 mat[11] := 0.0;
246 mat[12] := -1.0;
247 mat[13] := 1.0;
248 mat[14] := 0.0;
249 mat[15] := 1.0;
251 glLoadMatrixd(@mat[0]);
253 glMatrixMode(GL_MODELVIEW);
254 glLoadIdentity();
255 end;
257 //------------------------------------------------------------------
258 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
259 //------------------------------------------------------------------
260 function FindTexture(): DWORD;
261 var
262 i: integer;
263 begin
264 if e_Textures <> nil then
265 for i := 0 to High(e_Textures) do
266 if e_Textures[i].tx.Width = 0 then
267 begin
268 Result := i;
269 Exit;
270 end;
272 if e_Textures = nil then
273 begin
274 SetLength(e_Textures, 32);
275 Result := 0;
276 end
277 else
278 begin
279 Result := High(e_Textures) + 1;
280 SetLength(e_Textures, Length(e_Textures) + 32);
281 end;
282 end;
284 //------------------------------------------------------------------
285 // Ñîçäàåò òåêñòóðó
286 //------------------------------------------------------------------
287 function e_CreateTexture(FileName: String; var ID: DWORD): Boolean;
288 var
289 find_id: DWORD;
290 fmt: Word;
291 begin
292 Result := False;
294 e_WriteLog('Loading texture from '+FileName, TMsgType.Notify);
296 find_id := FindTexture();
298 if not LoadTexture(FileName, e_Textures[find_id].tx, e_Textures[find_id].tx.Width,
299 e_Textures[find_id].tx.Height, @fmt) then Exit;
301 ID := find_id;
303 Result := True;
304 end;
306 function e_CreateTextureEx(FileName: String; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
307 var
308 find_id: DWORD;
309 fmt: Word;
310 begin
311 Result := False;
313 find_id := FindTexture();
315 if not LoadTextureEx(FileName, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
317 ID := find_id;
319 Result := True;
320 end;
322 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
323 var
324 find_id: DWORD;
325 fmt: Word;
326 begin
327 Result := False;
329 find_id := FindTexture;
331 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;
333 id := find_id;
335 Result := True;
336 end;
338 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
339 var
340 find_id: DWORD;
341 fmt: Word;
342 begin
343 Result := False;
345 find_id := FindTexture();
347 if not LoadTextureMemEx(pData, dataSize, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
349 ID := find_id;
351 Result := True;
352 end;
354 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
355 var
356 find_id: DWORD;
357 fmt, tw, th: Word;
358 begin
359 result := false;
360 find_id := FindTexture();
361 if not LoadTextureImg(img, e_Textures[find_id].tx, tw, th, @fmt) then exit;
362 ID := find_id;
363 result := True;
364 end;
366 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
367 begin
368 if Width <> nil then Width^ := e_Textures[ID].tx.Width;
369 if Height <> nil then Height^ := e_Textures[ID].tx.Height;
370 end;
372 procedure e_ResizeWindow(Width, Height: Integer);
373 begin
374 if Height = 0 then
375 Height := 1;
376 e_SetViewPort(0, 0, Width, Height);
377 end;
379 procedure drawTxQuad (x0, y0, w, h, tw, th: Integer; u, v: single; Mirror: TMirrorType);
380 var
381 x1, y1, tmp: Integer;
382 begin
383 if (w < 1) or (h < 1) then exit;
384 x1 := x0+w;
385 y1 := y0+h;
386 if Mirror = TMirrorType.Horizontal then begin tmp := x1; x1 := x0; x0 := tmp; end
387 else if Mirror = TMirrorType.Vertical then begin tmp := y1; y1 := y0; y0 := tmp; end;
388 //HACK: make texture one pixel shorter, so it won't wrap
389 if (g_dbg_scale <> 1.0) then
390 begin
391 u := u*tw/(tw+1);
392 v := v*th/(th+1);
393 end;
394 glTexCoord2f(0, v); glVertex2i(x0, y0);
395 glTexCoord2f(0, 0); glVertex2i(x0, y1);
396 glTexCoord2f(u, 0); glVertex2i(x1, y1);
397 glTexCoord2f(u, v); glVertex2i(x1, y0);
398 end;
400 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
401 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
402 begin
403 if e_NoGraphics then Exit;
404 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
406 if (Alpha > 0) or (AlphaChannel) or (Blending) then
407 glEnable(GL_BLEND)
408 else
409 glDisable(GL_BLEND);
411 if (AlphaChannel) or (Alpha > 0) then
412 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
414 if Alpha > 0 then
415 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
417 if Blending then
418 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
420 glEnable(GL_TEXTURE_2D);
421 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
422 glBegin(GL_QUADS);
424 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);
426 //u := e_Textures[ID].tx.u;
427 //v := e_Textures[ID].tx.v;
430 if Mirror = M_NONE then
431 begin
432 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
433 glTexCoord2f(0, 0); glVertex2i(X, Y);
434 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
435 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
436 end
437 else
438 if Mirror = M_HORIZONTAL then
439 begin
440 glTexCoord2f(u, 0); glVertex2i(X, Y);
441 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
442 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
443 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
444 end
445 else
446 if Mirror = M_VERTICAL then
447 begin
448 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
449 glTexCoord2f(0, -v); glVertex2i(X, Y);
450 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
451 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
452 end;
455 glEnd();
457 glDisable(GL_BLEND);
458 end;
460 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
461 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
462 var
463 u, v: Single;
464 begin
465 if e_NoGraphics then Exit;
466 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
468 if (Alpha > 0) or (AlphaChannel) or (Blending) then
469 glEnable(GL_BLEND)
470 else
471 glDisable(GL_BLEND);
473 if (AlphaChannel) or (Alpha > 0) then
474 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
476 if Alpha > 0 then
477 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
479 if Blending then
480 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
482 glEnable(GL_TEXTURE_2D);
483 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
485 u := e_Textures[ID].tx.u;
486 v := e_Textures[ID].tx.v;
488 glBegin(GL_QUADS);
489 glTexCoord2f(0, v); glVertex2i(X, Y);
490 glTexCoord2f(u, v); glVertex2i(X + Width, Y);
491 glTexCoord2f(u, 0); glVertex2i(X + Width, Y + Height);
492 glTexCoord2f(0, 0); glVertex2i(X, Y + Height);
493 glEnd();
495 glDisable(GL_BLEND);
496 end;
498 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
499 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
500 begin
501 if e_NoGraphics then Exit;
502 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
504 if (Alpha > 0) or (AlphaChannel) or (Blending) then
505 glEnable(GL_BLEND)
506 else
507 glDisable(GL_BLEND);
509 if (AlphaChannel) or (Alpha > 0) then
510 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
512 if Alpha > 0 then
513 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
515 if Blending then
516 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
518 glEnable(GL_TEXTURE_2D);
519 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
520 glBegin(GL_QUADS);
521 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);
522 glEnd();
524 glDisable(GL_BLEND);
525 end;
527 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
528 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
529 var
530 X2, Y2, dx, w, h: Integer;
531 u, v: Single;
532 begin
533 if e_NoGraphics then Exit;
534 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
535 ambientBlendMode := false;
537 if (Alpha > 0) or AlphaChannel or Blending then
538 begin
539 glEnable(GL_BLEND);
540 end
541 else
542 begin
543 if not ambientBlendMode then glDisable(GL_BLEND);
544 end;
545 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
546 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
547 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
549 if (XCount = 0) then XCount := 1;
550 if (YCount = 0) then YCount := 1;
552 glEnable(GL_TEXTURE_2D);
553 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
555 X2 := X+e_Textures[ID].tx.width*XCount;
556 Y2 := Y+e_Textures[ID].tx.height*YCount;
558 //k8: this SHOULD work... i hope
559 if (e_Textures[ID].tx.width = e_Textures[ID].tx.glwidth) and (e_Textures[ID].tx.height = e_Textures[ID].tx.glheight) then
560 begin
561 glBegin(GL_QUADS);
562 glTexCoord2i(0, YCount); glVertex2i(X, Y);
563 glTexCoord2i(XCount, YCount); glVertex2i(X2, Y);
564 glTexCoord2i(XCount, 0); glVertex2i(X2, Y2);
565 glTexCoord2i(0, 0); glVertex2i(X, Y2);
566 glEnd();
567 end
568 else
569 begin
570 glBegin(GL_QUADS);
571 // hard day's night
572 u := e_Textures[ID].tx.u;
573 v := e_Textures[ID].tx.v;
574 w := e_Textures[ID].tx.width;
575 h := e_Textures[ID].tx.height;
576 while YCount > 0 do
577 begin
578 dx := XCount;
579 x2 := X;
580 while dx > 0 do
581 begin
582 glTexCoord2f(0, v); glVertex2i(X, Y);
583 glTexCoord2f(u, v); glVertex2i(X+w, Y);
584 glTexCoord2f(u, 0); glVertex2i(X+w, Y+h);
585 glTexCoord2f(0, 0); glVertex2i(X, Y+h);
586 Inc(X, w);
587 Dec(dx);
588 end;
589 X := x2;
590 Inc(Y, h);
591 Dec(YCount);
592 end;
593 glEnd();
594 end;
596 glDisable(GL_BLEND);
597 end;
600 //TODO: overflow checks
601 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
602 var
603 ex0, ey0: Integer;
604 begin
605 result := false;
606 if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit;
607 // check for intersection
608 if (x0+w0 <= x1) or (y0+h0 <= y1) or (x1+w1 <= x0) or (y1+h1 <= y0) then exit;
609 if (x0 >= x1+w1) or (y0 >= y1+h1) or (x1 >= x0+h0) or (y1 >= y0+h0) then exit;
610 // ok, intersects
611 ex0 := x0+w0;
612 ey0 := y0+h0;
613 if (x0 < x1) then x0 := x1;
614 if (y0 < y1) then y0 := y1;
615 if (ex0 > x1+w1) then ex0 := x1+w1;
616 if (ey0 > y1+h1) then ey0 := y1+h1;
617 w0 := ex0-x0;
618 h0 := ey0-y0;
619 result := (w0 > 0) and (h0 > 0);
620 end;
623 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
624 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
625 var
626 x2, y2: Integer;
628 wassc: Boolean;
629 scxywh: array[0..3] of GLint;
630 vpxywh: array[0..3] of GLint;
632 w, h, dw, cw, ch, yofs: Integer;
633 u, v, cu, cv: Single;
634 onlyOneY: Boolean;
637 procedure setScissorGLInternal (x, y, w, h: Integer);
638 begin
639 //if not scallowed then exit;
640 x := trunc(x*scale);
641 y := trunc(y*scale);
642 w := trunc(w*scale);
643 h := trunc(h*scale);
644 y := vpxywh[3]-(y+h);
645 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
646 begin
647 glScissor(0, 0, 0, 0);
648 end
649 else
650 begin
651 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
652 glScissor(x, y, w, h);
653 end;
654 end;
657 begin
658 if e_NoGraphics then exit;
659 ambientBlendMode := false;
661 if (wdt < 1) or (hgt < 1) then exit;
663 if (wdt mod e_Textures[ID].tx.width = 0) and (hgt mod e_Textures[ID].tx.height = 0) then
664 begin
665 e_DrawFill(id, x, y, wdt div e_Textures[ID].tx.width, hgt div e_Textures[ID].tx.height, alpha, alphachannel, blending, ambientBlendMode);
666 exit;
667 end;
669 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
671 if (Alpha > 0) or AlphaChannel or Blending then
672 begin
673 glEnable(GL_BLEND);
674 end
675 else
676 begin
677 if not ambientBlendMode then glDisable(GL_BLEND);
678 end;
679 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
680 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
681 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
683 glEnable(GL_TEXTURE_2D);
684 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
686 x2 := x+wdt;
687 y2 := y+hgt;
689 //k8: this SHOULD work... i hope
690 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
691 begin
692 glBegin(GL_QUADS);
693 glTexCoord2f(0, hgt/e_Textures[ID].tx.height); glVertex2i(x, y);
694 glTexCoord2f(wdt/e_Textures[ID].tx.width, hgt/e_Textures[ID].tx.height); glVertex2i(x2, y);
695 glTexCoord2f(wdt/e_Textures[ID].tx.width, 0); glVertex2i(x2, y2);
696 glTexCoord2f(0, 0); glVertex2i(x, y2);
697 glEnd();
698 end
699 else
700 begin
701 // hard day's night; setup scissor
703 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
704 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
705 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
706 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
707 //glEnable(GL_SCISSOR_TEST);
708 setScissorGLInternal(x, y, wdt, hgt);
710 // draw quads
711 u := e_Textures[ID].tx.u;
712 v := e_Textures[ID].tx.v;
713 w := e_Textures[ID].tx.width;
714 h := e_Textures[ID].tx.height;
715 x2 := x;
716 if (hgt > h) then begin y += hgt-h; onlyOneY := false; end else onlyOneY := true;
717 glBegin(GL_QUADS);
718 while (hgt > 0) do
719 begin
720 if (hgt >= h) then begin ch := h; cv := v; yofs := 0; end else begin ch := hgt; cv := v/(h/hgt); yofs := h-hgt; end;
721 if onlyOneY then yofs := 0;
722 Dec(hgt, h);
723 dw := wdt;
724 x := x2;
725 while (dw > 0) do
726 begin
727 if (dw >= w) then begin cw := w; cu := u; end else begin cw := dw; cu := u/(w/dw); end;
728 Dec(dw, w);
729 glTexCoord2f(0, cv); glVertex2i(X, Y+yofs);
730 glTexCoord2f(cu, cv); glVertex2i(X+cw, Y+yofs);
731 glTexCoord2f(cu, 0); glVertex2i(X+cw, Y+ch+yofs);
732 glTexCoord2f(0, 0); glVertex2i(X, Y+ch+yofs);
733 Inc(X, w);
734 end;
735 Dec(Y, h);
736 end;
737 glEnd();
738 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
739 end;
741 glDisable(GL_BLEND);
742 end;
745 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
746 begin
747 if e_NoGraphics then exit;
748 if (w < 1) or (h < 1) then exit;
749 if (a <> 255) or ((r or g or b) <> 0) then
750 begin
751 glEnable(GL_BLEND);
752 glDisable(GL_TEXTURE_2D);
753 glColor4ub(r, g, b, a);
754 if ((r or g or b) <> 0) then
755 begin
756 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
757 glBegin(GL_QUADS);
758 glVertex2i(x, y);
759 glVertex2i(x+w, y);
760 glVertex2i(x+w, y+h);
761 glVertex2i(x, y+h);
762 glEnd();
763 end;
764 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
765 glBegin(GL_QUADS);
766 glVertex2i(x, y);
767 glVertex2i(x+w, y);
768 glVertex2i(x+w, y+h);
769 glVertex2i(x, y+h);
770 glEnd();
771 glDisable(GL_BLEND);
772 end;
773 end;
776 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
777 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
778 begin
779 if e_NoGraphics then Exit;
781 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
783 if (Alpha > 0) or (AlphaChannel) or (Blending) then
784 glEnable(GL_BLEND)
785 else
786 glDisable(GL_BLEND);
788 if (AlphaChannel) or (Alpha > 0) then
789 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
791 if Alpha > 0 then
792 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
794 if Blending then
795 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
797 if (Angle <> 0) and (RC <> nil) then
798 begin
799 glPushMatrix();
800 glTranslatef(X+RC.X, Y+RC.Y, 0);
801 glRotatef(Angle, 0, 0, 1);
802 glTranslatef(-(X+RC.X), -(Y+RC.Y), 0);
803 end;
805 glEnable(GL_TEXTURE_2D);
806 glBindTexture(GL_TEXTURE_2D, e_Textures[id].tx.id);
807 glBegin(GL_QUADS); //0-1 1-1
808 //00 10
809 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);
810 glEnd();
812 if Angle <> 0 then
813 glPopMatrix();
815 glDisable(GL_BLEND);
816 end;
818 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
819 begin
820 if e_NoGraphics then Exit;
821 glDisable(GL_TEXTURE_2D);
822 glColor3ub(Red, Green, Blue);
823 glPointSize(Size);
825 if (Size = 2) or (Size = 4) then
826 X := X + 1;
828 glBegin(GL_POINTS);
829 glVertex2f(X+0.3, Y+1.0);
830 glEnd();
832 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
833 end;
835 procedure e_LineCorrection(var X1, Y1, X2, Y2: Integer);
836 begin
837 // Make lines only top-left/bottom-right and top-right/bottom-left
838 if Y2 < Y1 then
839 begin
840 X1 := X1 xor X2;
841 X2 := X1 xor X2;
842 X1 := X1 xor X2;
844 Y1 := Y1 xor Y2;
845 Y2 := Y1 xor Y2;
846 Y1 := Y1 xor Y2;
847 end;
849 // Pixel-perfect hack
850 if X1 < X2 then
851 Inc(X2)
852 else
853 Inc(X1);
854 Inc(Y2);
855 end;
857 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
858 var
859 nX1, nY1, nX2, nY2: Integer;
860 {$IFDEF USE_NANOGL}
861 v: array [0..15] of GLfloat;
862 {$ENDIF}
863 begin
864 if e_NoGraphics then Exit;
865 // Only top-left/bottom-right quad
866 if X1 > X2 then
867 begin
868 X1 := X1 xor X2;
869 X2 := X1 xor X2;
870 X1 := X1 xor X2;
871 end;
872 if Y1 > Y2 then
873 begin
874 Y1 := Y1 xor Y2;
875 Y2 := Y1 xor Y2;
876 Y1 := Y1 xor Y2;
877 end;
879 if Alpha > 0 then
880 begin
881 glEnable(GL_BLEND);
882 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
883 end else
884 glDisable(GL_BLEND);
886 glDisable(GL_TEXTURE_2D);
887 glColor4ub(Red, Green, Blue, 255-Alpha);
888 glLineWidth(1);
889 {$IFDEF USE_NANOGL}
890 nX1 := X1; nY1 := Y1;
891 nX2 := X2; nY2 := Y1;
892 e_LineCorrection(nX1, nY1, nX2, nY2);
893 v[0] := nX1; v[1] := nY1; v[2] := nX2; v[3] := nY2;
895 nX1 := X2; nY1 := Y1;
896 nX2 := X2; nY2 := Y2;
897 e_LineCorrection(nX1, nY1, nX2, nY2);
898 v[4] := nX1; v[5] := nY1; v[6] := nX2; v[7] := nY2;
900 nX1 := X2; nY1 := Y2;
901 nX2 := X1; nY2 := Y2;
902 e_LineCorrection(nX1, nY1, nX2, nY2);
903 v[8] := nX1; v[9] := nY1; v[10] := nX2; v[11] := nY2;
905 nX1 := X1; nY1 := Y2;
906 nX2 := X1; nY2 := Y1;
907 e_LineCorrection(nX1, nY1, nX2, nY2);
908 v[12] := nX1; v[13] := nY1; v[14] := nX2; v[15] := nY2;
910 glVertexPointer(2, GL_FLOAT, 0, @v[0]);
911 glEnableClientState(GL_VERTEX_ARRAY);
912 glDisableClientState(GL_COLOR_ARRAY);
913 glDisableClientState(GL_NORMAL_ARRAY);
914 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
915 glDrawArrays(GL_LINES, 0, 16);
916 {$ELSE}
917 glBegin(GL_LINES);
918 nX1 := X1; nY1 := Y1;
919 nX2 := X2; nY2 := Y1;
920 e_LineCorrection(nX1, nY1, nX2, nY2); // Pixel-perfect lines
921 glVertex2i(nX1, nY1);
922 glVertex2i(nX2, nY2);
924 nX1 := X2; nY1 := Y1;
925 nX2 := X2; nY2 := Y2;
926 e_LineCorrection(nX1, nY1, nX2, nY2);
927 glVertex2i(nX1, nY1);
928 glVertex2i(nX2, nY2);
930 nX1 := X2; nY1 := Y2;
931 nX2 := X1; nY2 := Y2;
932 e_LineCorrection(nX1, nY1, nX2, nY2);
933 glVertex2i(nX1, nY1);
934 glVertex2i(nX2, nY2);
936 nX1 := X1; nY1 := Y2;
937 nX2 := X1; nY2 := Y1;
938 e_LineCorrection(nX1, nY1, nX2, nY2);
939 glVertex2i(nX1, nY1);
940 glVertex2i(nX2, nY2);
941 glEnd();
942 {$ENDIF}
944 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
946 glDisable(GL_BLEND);
947 end;
949 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
950 Blending: TBlending = TBlending.None);
951 begin
952 if e_NoGraphics then Exit;
953 if (Alpha > 0) or (Blending <> TBlending.None) then
954 glEnable(GL_BLEND)
955 else
956 glDisable(GL_BLEND);
958 if Blending = TBlending.Blend then
959 glBlendFunc(GL_SRC_ALPHA, GL_ONE)
960 else
961 if Blending = TBlending.Filter then
962 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR)
963 else
964 if Blending = TBlending.Invert then
965 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO)
966 else
967 if Alpha > 0 then
968 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
970 glDisable(GL_TEXTURE_2D);
971 glColor4ub(Red, Green, Blue, 255-Alpha);
973 X2 := X2 + 1;
974 Y2 := Y2 + 1;
976 glBegin(GL_QUADS);
977 glVertex2i(X1, Y1);
978 glVertex2i(X2, Y1);
979 glVertex2i(X2, Y2);
980 glVertex2i(X1, Y2);
981 glEnd();
983 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
985 glDisable(GL_BLEND);
986 end;
989 // ////////////////////////////////////////////////////////////////////////// //
990 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
991 begin
992 if (a < 0) then a := 0;
993 if (a > 255) then a := 255;
994 glEnable(GL_BLEND);
995 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
996 glDisable(GL_TEXTURE_2D);
997 glColor4ub(0, 0, 0, Byte(255-a));
998 glBegin(GL_QUADS);
999 glVertex2i(x0, y0);
1000 glVertex2i(x1, y0);
1001 glVertex2i(x1, y1);
1002 glVertex2i(x0, y1);
1003 glEnd();
1004 //glRect(x, y, x+w, y+h);
1005 glColor4ub(1, 1, 1, 1);
1006 glDisable(GL_BLEND);
1007 //glBlendEquation(GL_FUNC_ADD);
1008 end;
1010 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
1011 begin
1012 if (w > 0) and (h > 0) then e_DarkenQuad(x, y, x+w, y+h, a);
1013 end;
1016 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
1017 {$IFDEF USE_NANOGL}
1018 var
1019 v: array [0..3] of GLfloat;
1020 {$ENDIF}
1021 begin
1022 if e_NoGraphics then Exit;
1023 // Pixel-perfect lines
1024 if Width = 1 then
1025 e_LineCorrection(X1, Y1, X2, Y2);
1027 if Alpha > 0 then
1028 begin
1029 glEnable(GL_BLEND);
1030 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1031 end else
1032 glDisable(GL_BLEND);
1034 glDisable(GL_TEXTURE_2D);
1035 glColor4ub(Red, Green, Blue, 255-Alpha);
1036 glLineWidth(Width);
1038 {$IFDEF USE_NANOGL}
1039 v[0] := X1; v[1] := Y1; v[2] := X2; v[3] := Y2;
1040 glVertexPointer(2, GL_FLOAT, 0, @v[0]);
1041 glEnableClientState(GL_VERTEX_ARRAY);
1042 glDisableClientState(GL_COLOR_ARRAY);
1043 glDisableClientState(GL_NORMAL_ARRAY);
1044 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1045 glDrawArrays(GL_LINES, 0, 4);
1046 {$ELSE}
1047 glBegin(GL_LINES);
1048 glVertex2i(X1, Y1);
1049 glVertex2i(X2, Y2);
1050 glEnd();
1051 {$ENDIF}
1053 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1055 glDisable(GL_BLEND);
1056 end;
1058 //------------------------------------------------------------------
1059 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1060 //------------------------------------------------------------------
1061 procedure e_DeleteTexture(ID: DWORD);
1062 begin
1063 if not e_NoGraphics then
1064 glDeleteTextures(1, @e_Textures[ID].tx.id);
1065 e_Textures[ID].tx.id := 0;
1066 e_Textures[ID].tx.Width := 0;
1067 e_Textures[ID].tx.Height := 0;
1068 end;
1070 //------------------------------------------------------------------
1071 // Óäàëÿåò âñå òåêñòóðû
1072 //------------------------------------------------------------------
1073 procedure e_RemoveAllTextures();
1074 var
1075 i: integer;
1076 begin
1077 if e_Textures = nil then Exit;
1079 for i := 0 to High(e_Textures) do
1080 if e_Textures[i].tx.Width <> 0 then e_DeleteTexture(i);
1081 e_Textures := nil;
1082 end;
1084 //------------------------------------------------------------------
1085 // Óäàëÿåò äâèæîê
1086 //------------------------------------------------------------------
1087 procedure e_ReleaseEngine();
1088 begin
1089 e_RemoveAllTextures;
1090 e_RemoveAllTextureFont;
1091 end;
1093 procedure e_BeginRender();
1094 begin
1095 if e_NoGraphics then Exit;
1096 glEnable(GL_ALPHA_TEST);
1097 glAlphaFunc(GL_GREATER, 0.0);
1098 end;
1100 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
1101 begin
1102 if e_NoGraphics then Exit;
1103 glClearColor(Red, Green, Blue, 0);
1104 glClear(Mask);
1105 end;
1107 procedure e_Clear(); overload;
1108 begin
1109 if e_NoGraphics then Exit;
1110 glClearColor(0, 0, 0, 0);
1111 glClear(GL_COLOR_BUFFER_BIT);
1112 end;
1114 procedure e_EndRender();
1115 begin
1116 if e_NoGraphics then Exit;
1117 glPopMatrix();
1118 end;
1120 function e_GetGamma(win: PSDL_Window): Byte;
1121 var
1122 ramp: array [0..256*3-1] of Word;
1123 rgb: array [0..2] of Double;
1124 sum: double;
1125 count: integer;
1126 min: integer;
1127 max: integer;
1128 A, B: double;
1129 i, j: integer;
1130 begin
1131 Result := 0;
1132 if e_NoGraphics then Exit;
1133 rgb[0] := 1.0;
1134 rgb[1] := 1.0;
1135 rgb[2] := 1.0;
1137 SDL_GetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1139 for i := 0 to 2 do
1140 begin
1141 sum := 0;
1142 count := 0;
1143 min := 256 * i;
1144 max := min + 256;
1146 for j := min to max - 1 do
1147 if ramp[j] > 0 then
1148 begin
1149 B := (j mod 256)/256;
1150 A := ramp[j]/65536;
1151 sum := sum + ln(A)/ln(B);
1152 inc(count);
1153 end;
1154 rgb[i] := sum / count;
1155 end;
1157 Result := 100 - Trunc(((rgb[0] + rgb[1] + rgb[2])/3 - 0.23) * 100/(2.7 - 0.23));
1158 end;
1160 procedure e_SetGamma(win: PSDL_Window; Gamma: Byte);
1161 var
1162 ramp: array [0..256*3-1] of Word;
1163 i: integer;
1164 r: double;
1165 g: double;
1166 begin
1167 if e_NoGraphics then Exit;
1168 g := (100 - Gamma)*(2.7 - 0.23)/100 + 0.23;
1170 for i := 0 to 255 do
1171 begin
1172 r := Exp(g * ln(i/256))*65536;
1173 if r < 0 then r := 0
1174 else if r > 65535 then r := 65535;
1175 ramp[i] := trunc(r);
1176 ramp[i + 256] := trunc(r);
1177 ramp[i + 512] := trunc(r);
1178 end;
1180 SDL_SetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1181 end;
1183 function e_CharFont_Create(sp: ShortInt=0): DWORD;
1184 var
1185 i, id: DWORD;
1186 begin
1187 e_WriteLog('Creating CharFont...', TMsgType.Notify);
1189 id := DWORD(-1);
1191 if e_CharFonts <> nil then
1192 for i := 0 to High(e_CharFonts) do
1193 if not e_CharFonts[i].alive then
1194 begin
1195 id := i;
1196 Break;
1197 end;
1199 if id = DWORD(-1) then
1200 begin
1201 SetLength(e_CharFonts, Length(e_CharFonts) + 1);
1202 id := High(e_CharFonts);
1203 end;
1205 with e_CharFonts[id] do
1206 begin
1207 for i := 0 to High(Chars) do
1208 with Chars[i] do
1209 begin
1210 TextureID := -1;
1211 Width := 0;
1212 end;
1214 Space := sp;
1215 alive := True;
1216 end;
1218 Result := id;
1219 end;
1221 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
1222 begin
1223 with e_CharFonts[FontID].Chars[Ord(c)] do
1224 begin
1225 TextureID := Texture;
1226 Width := w;
1227 end;
1228 end;
1230 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
1231 var
1232 a: Integer;
1233 begin
1234 if e_NoGraphics then Exit;
1235 if Text = '' then Exit;
1236 if e_CharFonts = nil then Exit;
1237 if Integer(FontID) > High(e_CharFonts) then Exit;
1239 with e_CharFonts[FontID] do
1240 begin
1241 for a := 1 to Length(Text) do
1242 with Chars[Ord(Text[a])] do
1243 if TextureID <> -1 then
1244 begin
1245 e_Draw(TextureID, X, Y, 0, True, False);
1246 X := X+Width+IfThen(a = Length(Text), 0, Space);
1247 end;
1248 end;
1249 end;
1251 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
1252 Color: TRGB; Scale: Single = 1.0);
1253 var
1254 a: Integer;
1255 c: TRGB;
1256 begin
1257 if e_NoGraphics then Exit;
1258 if Text = '' then Exit;
1259 if e_CharFonts = nil then Exit;
1260 if Integer(FontID) > High(e_CharFonts) then Exit;
1262 with e_CharFonts[FontID] do
1263 begin
1264 for a := 1 to Length(Text) do
1265 with Chars[Ord(Text[a])] do
1266 if TextureID <> -1 then
1267 begin
1268 if Scale <> 1.0 then
1269 begin
1270 glPushMatrix;
1271 glScalef(Scale, Scale, 0);
1272 end;
1274 c := e_Colors;
1275 e_Colors := Color;
1276 e_Draw(TextureID, X, Y, 0, True, False);
1277 e_Colors := c;
1279 if Scale <> 1.0 then glPopMatrix;
1281 X := X+Width+IfThen(a = Length(Text), 0, Space);
1282 end;
1283 end;
1284 end;
1286 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
1287 var
1288 a, TX, TY, len: Integer;
1289 tc, c: TRGB;
1290 w, h: Word;
1291 begin
1292 if e_NoGraphics then Exit;
1293 if Text = '' then Exit;
1294 if e_CharFonts = nil then Exit;
1295 if Integer(FontID) > High(e_CharFonts) then Exit;
1297 c.R := 255;
1298 c.G := 255;
1299 c.B := 255;
1301 TX := X;
1302 TY := Y;
1303 len := Length(Text);
1305 e_CharFont_GetSize(FontID, 'A', w, h);
1307 with e_CharFonts[FontID] do
1308 begin
1309 for a := 1 to len do
1310 begin
1311 case Text[a] of
1312 #10: // line feed
1313 begin
1314 TX := X;
1315 TY := TY + h;
1316 continue;
1317 end;
1318 #1: // black
1319 begin
1320 c.R := 0; c.G := 0; c.B := 0;
1321 continue;
1322 end;
1323 #2: // white
1324 begin
1325 c.R := 255; c.G := 255; c.B := 255;
1326 continue;
1327 end;
1328 #3: // darker
1329 begin
1330 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1331 continue;
1332 end;
1333 #4: // lighter
1334 begin
1335 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1336 continue;
1337 end;
1338 #18: // red
1339 begin
1340 c.R := 255; c.G := 0; c.B := 0;
1341 continue;
1342 end;
1343 #19: // green
1344 begin
1345 c.R := 0; c.G := 255; c.B := 0;
1346 continue;
1347 end;
1348 #20: // blue
1349 begin
1350 c.R := 0; c.G := 0; c.B := 255;
1351 continue;
1352 end;
1353 #21: // yellow
1354 begin
1355 c.R := 255; c.G := 255; c.B := 0;
1356 continue;
1357 end;
1358 end;
1360 with Chars[Ord(Text[a])] do
1361 if TextureID <> -1 then
1362 begin
1363 tc := e_Colors;
1364 e_Colors := c;
1365 e_Draw(TextureID, TX, TY, 0, True, False);
1366 e_Colors := tc;
1368 TX := TX+Width+IfThen(a = Length(Text), 0, Space);
1369 end;
1370 end;
1371 end;
1372 end;
1374 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
1375 var
1376 a: Integer;
1377 h2: Word;
1378 begin
1379 w := 0;
1380 h := 0;
1382 if Text = '' then Exit;
1383 if e_CharFonts = nil then Exit;
1384 if Integer(FontID) > High(e_CharFonts) then Exit;
1386 with e_CharFonts[FontID] do
1387 begin
1388 for a := 1 to Length(Text) do
1389 with Chars[Ord(Text[a])] do
1390 if TextureID <> -1 then
1391 begin
1392 w := w+Width+IfThen(a = Length(Text), 0, Space);
1393 e_GetTextureSize(TextureID, nil, @h2);
1394 if h2 > h then h := h2;
1395 end;
1396 end;
1397 end;
1399 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
1400 var
1401 a, lines, len: Integer;
1402 h2, w2, tw, th: Word;
1403 begin
1404 w2 := 0;
1405 h2 := 0;
1406 tw := 0;
1407 th := 0;
1409 if Text = '' then Exit;
1410 if e_CharFonts = nil then Exit;
1411 if Integer(FontID) > High(e_CharFonts) then Exit;
1413 lines := 1;
1414 len := Length(Text);
1416 with e_CharFonts[FontID] do
1417 begin
1418 for a := 1 to len do
1419 begin
1420 if Text[a] = #10 then
1421 begin
1422 Inc(lines);
1423 if w2 > tw then tw := w2;
1424 w2 := 0;
1425 continue;
1426 end;
1428 with Chars[Ord(Text[a])] do
1429 if TextureID <> -1 then
1430 begin
1431 w2 := w2 + Width + IfThen(a = len, 0, Space);
1432 e_GetTextureSize(TextureID, nil, @h2);
1433 if h2 > th then th := h2;
1434 end;
1435 end;
1436 end;
1438 if w2 > tw then
1439 tw := w2;
1441 w := tw;
1442 h := th * lines;
1443 end;
1445 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
1446 var
1447 a: Integer;
1448 begin
1449 Result := 0;
1451 if e_CharFonts = nil then Exit;
1452 if Integer(FontID) > High(e_CharFonts) then Exit;
1454 for a := 0 to High(e_CharFonts[FontID].Chars) do
1455 Result := Max(Result, e_CharFonts[FontID].Chars[a].Width);
1456 end;
1458 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
1459 var
1460 a: Integer;
1461 h2: Word;
1462 begin
1463 Result := 0;
1465 if e_CharFonts = nil then Exit;
1466 if Integer(FontID) > High(e_CharFonts) then Exit;
1468 for a := 0 to High(e_CharFonts[FontID].Chars) do
1469 begin
1470 if e_CharFonts[FontID].Chars[a].TextureID <> -1 then
1471 e_GetTextureSize(e_CharFonts[FontID].Chars[a].TextureID, nil, @h2)
1472 else h2 := 0;
1473 if h2 > Result then Result := h2;
1474 end;
1475 end;
1477 procedure e_CharFont_Remove(FontID: DWORD);
1478 var
1479 a: Integer;
1480 begin
1481 with e_CharFonts[FontID] do
1482 for a := 0 to High(Chars) do
1483 if Chars[a].TextureID <> -1 then e_DeleteTexture(Chars[a].TextureID);
1485 e_CharFonts[FontID].alive := False;
1486 end;
1488 procedure e_CharFont_RemoveAll();
1489 var
1490 a: Integer;
1491 begin
1492 if e_CharFonts = nil then Exit;
1494 for a := 0 to High(e_CharFonts) do
1495 e_CharFont_Remove(a);
1497 e_CharFonts := nil;
1498 end;
1500 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
1501 Space: ShortInt=0);
1502 var
1503 loop1 : GLuint;
1504 cx, cy : real;
1505 i, id: DWORD;
1506 begin
1507 if e_NoGraphics then Exit;
1508 e_WriteLog('Creating texture font...', TMsgType.Notify);
1510 id := DWORD(-1);
1512 if e_TextureFonts <> nil then
1513 for i := 0 to High(e_TextureFonts) do
1514 if e_TextureFonts[i].Base = 0 then
1515 begin
1516 id := i;
1517 Break;
1518 end;
1520 if id = DWORD(-1) then
1521 begin
1522 SetLength(e_TextureFonts, Length(e_TextureFonts) + 1);
1523 id := High(e_TextureFonts);
1524 end;
1526 with e_TextureFonts[id] do
1527 begin
1528 {$IF not DEFINED(USE_NANOGL)}
1529 Base := glGenLists(XCount*YCount);
1530 {$ENDIF}
1531 TextureID := e_Textures[Tex].tx.id;
1532 CharWidth := (e_Textures[Tex].tx.Width div XCount)+Space;
1533 CharHeight := e_Textures[Tex].tx.Height div YCount;
1534 XC := XCount;
1535 YC := YCount;
1536 Texture := Tex;
1537 SPC := Space;
1538 end;
1540 {$IF not DEFINED(USE_NANOGL)}
1541 glBindTexture(GL_TEXTURE_2D, e_Textures[Tex].tx.id);
1542 for loop1 := 0 to XCount*YCount-1 do
1543 begin
1544 cx := (loop1 mod XCount)/XCount;
1545 cy := (loop1 div YCount)/YCount;
1547 glNewList(e_TextureFonts[id].Base+loop1, GL_COMPILE);
1548 glBegin(GL_QUADS);
1549 glTexCoord2f(cx, 1.0-cy-1/YCount);
1550 glVertex2i(0, e_Textures[Tex].tx.Height div YCount);
1552 glTexCoord2f(cx+1/XCount, 1.0-cy-1/YCount);
1553 glVertex2i(e_Textures[Tex].tx.Width div XCount, e_Textures[Tex].tx.Height div YCount);
1555 glTexCoord2f(cx+1/XCount, 1.0-cy);
1556 glVertex2i(e_Textures[Tex].tx.Width div XCount, 0);
1558 glTexCoord2f(cx, 1.0-cy);
1559 glVertex2i(0, 0);
1560 glEnd();
1561 glTranslated((e_Textures[Tex].tx.Width div XCount)+Space, 0, 0);
1562 glEndList();
1563 end;
1564 {$ENDIF}
1566 FontID := id;
1567 end;
1569 procedure e_TextureFontKill(FontID: DWORD);
1570 begin
1571 if e_NoGraphics then Exit;
1572 {$IF not DEFINED(USE_NANOGL)}
1573 glDeleteLists(e_TextureFonts[FontID].Base, 256);
1574 {$ENDIF}
1575 e_TextureFonts[FontID].Base := 0;
1576 end;
1578 {$IFDEF USE_NANOGL}
1579 procedure e_TextureFontDrawChar(ch: Char; FontID: DWORD);
1580 var
1581 index: Integer;
1582 cx, cy: GLfloat;
1583 Tex: Integer;
1584 Width, Height: Integer;
1585 XCount, YCount: Integer;
1586 begin
1587 index := Ord(ch) - 32;
1588 Tex := e_TextureFonts[FontID].Texture;
1589 Width := e_Textures[Tex].tx.Width;
1590 Height := e_Textures[Tex].tx.Height;
1591 XCount := e_TextureFonts[FontID].XC;
1592 YCount := e_TextureFonts[FontID].YC;
1593 cx := (index mod XCount)/XCount;
1594 cy := (index div YCount)/YCount;
1595 glBegin(GL_QUADS);
1596 glTexCoord2f(cx, 1 - cy - 1/YCount);
1597 glVertex2i(0, Height div YCount);
1598 glTexCoord2f(cx + 1/XCount, 1 - cy - 1/YCount);
1599 glVertex2i(Width div XCount, Height div YCount);
1600 glTexCoord2f(cx + 1/XCount, 1 - cy);
1601 glVertex2i(Width div XCount, 0);
1602 glTexCoord2f(cx, 1 - cy);
1603 glVertex2i(0, 0);
1604 glEnd();
1605 glTranslatef((e_Textures[Tex].tx.Width div XCount) + e_TextureFonts[FontID].SPC, 0, 0);
1606 end;
1608 procedure e_TextureFontDrawString(Text: String; FontID: DWORD);
1609 var
1610 i: Integer;
1611 begin
1612 for i := 1 to High(Text) do
1613 e_TextureFontDrawChar(Text[i], FontID);
1614 end;
1615 {$ENDIF}
1617 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
1618 begin
1619 if e_NoGraphics then Exit;
1620 if Integer(FontID) > High(e_TextureFonts) then Exit;
1621 if Text = '' then Exit;
1623 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1624 glEnable(GL_BLEND);
1626 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1628 glPushMatrix;
1629 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1630 glEnable(GL_TEXTURE_2D);
1631 glTranslatef(x, y, 0);
1632 {$IFDEF USE_NANOGL}
1633 e_TextureFontDrawString(Text, FontID);
1634 {$ELSE}
1635 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1636 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1637 {$ENDIF}
1638 glDisable(GL_TEXTURE_2D);
1639 glPopMatrix;
1641 glDisable(GL_BLEND);
1642 end;
1644 // god forgive me for this, but i cannot figure out how to do it without lists
1645 procedure e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1646 begin
1647 if e_NoGraphics then Exit;
1648 glPushMatrix;
1650 if Shadow then
1651 begin
1652 glColor4ub(0, 0, 0, 128);
1653 glTranslatef(X+1, Y+1, 0);
1654 {$IFDEF USE_NANOGL}
1655 e_TextureFontDrawChar(Ch, FontID);
1656 {$ELSE}
1657 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1658 {$ENDIF}
1659 glPopMatrix;
1660 glPushMatrix;
1661 end;
1663 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1664 glTranslatef(X, Y, 0);
1665 {$IFDEF USE_NANOGL}
1666 e_TextureFontDrawChar(Ch, FontID);
1667 {$ELSE}
1668 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1669 {$ENDIF}
1671 glPopMatrix;
1672 end;
1674 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1675 begin
1676 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1677 glEnable(GL_TEXTURE_2D);
1678 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1680 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1681 glEnable(GL_BLEND);
1682 e_TextureFontPrintChar(X, Y, Ch, FontID, Shadow);
1683 glDisable(GL_TEXTURE_2D);
1684 glDisable(GL_BLEND);
1685 end;
1687 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
1688 begin
1689 result := e_TextureFonts[FontID].CharWidth;
1690 end;
1692 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD; Shadow: Boolean = False);
1693 var
1694 a, TX, TY, len: Integer;
1695 tc, c: TRGB;
1696 w: Word;
1697 begin
1698 if e_NoGraphics then Exit;
1699 if Text = '' then Exit;
1700 if e_TextureFonts = nil then Exit;
1701 if Integer(FontID) > High(e_TextureFonts) then Exit;
1703 c.R := 255;
1704 c.G := 255;
1705 c.B := 255;
1707 TX := X;
1708 TY := Y;
1709 len := Length(Text);
1711 w := e_TextureFonts[FontID].CharWidth;
1713 with e_TextureFonts[FontID] do
1714 begin
1715 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1716 glEnable(GL_TEXTURE_2D);
1718 {$IF not DEFINED(USE_NANOGL)}
1719 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1720 {$ENDIF}
1722 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1723 glEnable(GL_BLEND);
1725 for a := 1 to len do
1726 begin
1727 case Text[a] of
1728 {#10: // line feed
1729 begin
1730 TX := X;
1731 TY := TY + h;
1732 continue;
1733 end;}
1734 #1: // black
1735 begin
1736 c.R := 0; c.G := 0; c.B := 0;
1737 continue;
1738 end;
1739 #2: // white
1740 begin
1741 c.R := 255; c.G := 255; c.B := 255;
1742 continue;
1743 end;
1744 #3: // darker
1745 begin
1746 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1747 continue;
1748 end;
1749 #4: // lighter
1750 begin
1751 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1752 continue;
1753 end;
1754 #18: // red
1755 begin
1756 c.R := 255; c.G := 0; c.B := 0;
1757 continue;
1758 end;
1759 #19: // green
1760 begin
1761 c.R := 0; c.G := 255; c.B := 0;
1762 continue;
1763 end;
1764 #20: // blue
1765 begin
1766 c.R := 0; c.G := 0; c.B := 255;
1767 continue;
1768 end;
1769 #21: // yellow
1770 begin
1771 c.R := 255; c.G := 255; c.B := 0;
1772 continue;
1773 end;
1774 end;
1776 tc := e_Colors;
1777 e_Colors := c;
1778 e_TextureFontPrintChar(TX, TY, Text[a], FontID, Shadow);
1779 e_Colors := tc;
1781 TX := TX+w;
1782 end;
1783 glDisable(GL_TEXTURE_2D);
1784 glDisable(GL_BLEND);
1785 end;
1786 end;
1788 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
1789 Blue: Byte; Scale: Single; Shadow: Boolean = False);
1790 begin
1791 if e_NoGraphics then Exit;
1792 if Text = '' then Exit;
1794 glPushMatrix;
1795 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1796 glEnable(GL_TEXTURE_2D);
1798 {$IF not DEFINED(USE_NANOGL)}
1799 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1800 {$ENDIF}
1802 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1803 glEnable(GL_BLEND);
1805 if Shadow then
1806 begin
1807 glColor4ub(0, 0, 0, 128);
1808 glTranslatef(x+1, y+1, 0);
1809 glScalef(Scale, Scale, 0);
1810 {$IFDEF USE_NANOGL}
1811 e_TextureFontDrawString(Text, FontID);
1812 {$ELSE}
1813 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1814 {$ENDIF}
1815 glPopMatrix;
1816 glPushMatrix;
1817 end;
1819 glColor4ub(Red, Green, Blue, 255);
1820 glTranslatef(x, y, 0);
1821 glScalef(Scale, Scale, 0);
1822 {$IFDEF USE_NANOGL}
1823 e_TextureFontDrawString(Text, FontID);
1824 {$ELSE}
1825 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1826 {$ENDIF}
1828 glDisable(GL_TEXTURE_2D);
1829 glPopMatrix;
1830 glColor3ub(e_Colors.R, e_Colors.G, e_Colors.B);
1831 glDisable(GL_BLEND);
1832 end;
1834 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
1835 begin
1836 CharWidth := 16;
1837 CharHeight := 16;
1838 if e_NoGraphics then Exit;
1839 if Integer(ID) > High(e_TextureFonts) then
1840 Exit;
1841 CharWidth := e_TextureFonts[ID].CharWidth;
1842 CharHeight := e_TextureFonts[ID].CharHeight;
1843 end;
1845 procedure e_RemoveAllTextureFont();
1846 var
1847 i: integer;
1848 begin
1849 if e_NoGraphics then Exit;
1850 if e_TextureFonts = nil then Exit;
1852 for i := 0 to High(e_TextureFonts) do
1853 if e_TextureFonts[i].Base <> 0 then
1854 begin
1855 {$IFNDEF USE_NANOGL}
1856 glDeleteLists(e_TextureFonts[i].Base, 256);
1857 {$ENDIF}
1858 e_TextureFonts[i].Base := 0;
1859 end;
1861 e_TextureFonts := nil;
1862 end;
1864 function _RGB(Red, Green, Blue: Byte): TRGB;
1865 begin
1866 Result.R := Red;
1867 Result.G := Green;
1868 Result.B := Blue;
1869 end;
1871 function _Point(X, Y: Integer): TPoint2i;
1872 begin
1873 Result.X := X;
1874 Result.Y := Y;
1875 end;
1877 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
1878 begin
1879 Result.X := X;
1880 Result.Y := Y;
1881 Result.Width := Width;
1882 Result.Height := Height;
1883 end;
1885 function _TRect(L, T, R, B: LongInt): TRect;
1886 begin
1887 Result.Top := T;
1888 Result.Left := L;
1889 Result.Right := R;
1890 Result.Bottom := B;
1891 end;
1894 procedure e_MakeScreenshot (st: TStream; Width, Height: Word);
1895 var
1896 pixels, obuf, scln, ps, pd: PByte;
1897 obufsize: Integer;
1898 dlen: Cardinal;
1899 i, x, y, res: Integer;
1900 sign: array [0..7] of Byte;
1901 hbuf: array [0..12] of Byte;
1902 crc: LongWord;
1903 img: TImageData;
1904 clr: TColor32Rec;
1905 begin
1906 if e_NoGraphics then Exit;
1907 obuf := nil;
1909 // first, extract and pack graphics data
1910 if (Width mod 4) > 0 then Width := Width+4-(Width mod 4);
1912 GetMem(pixels, Width*Height*3);
1913 try
1914 FillChar(pixels^, Width*Height*3, 0);
1915 glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
1916 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1918 if e_FastScreenshots then
1919 begin
1920 // create scanlines
1921 GetMem(scln, (Width*3+1)*Height);
1922 try
1923 ps := pixels;
1924 pd := scln;
1925 Inc(ps, (Width*3)*(Height-1));
1926 for i := 0 to Height-1 do
1927 begin
1928 pd^ := 0; // filter
1929 Inc(pd);
1930 Move(ps^, pd^, Width*3);
1931 Dec(ps, Width*3);
1932 Inc(pd, Width*3);
1933 end;
1934 except
1935 FreeMem(scln);
1936 raise;
1937 end;
1938 FreeMem(pixels);
1939 pixels := scln;
1941 // pack it
1942 obufsize := (Width*3+1)*Height*2;
1943 GetMem(obuf, obufsize);
1944 try
1945 while true do
1946 begin
1947 dlen := obufsize;
1948 res := compress2(Pointer(obuf), dlen, Pointer(pixels), (Width*3+1)*Height, 9);
1949 if res = Z_OK then break;
1950 if res <> Z_BUF_ERROR then raise Exception.Create('can''t pack data for PNG');
1951 obufsize := obufsize*2;
1952 FreeMem(obuf);
1953 obuf := nil;
1954 GetMem(obuf, obufsize);
1955 end;
1956 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1958 // now write PNG
1960 // signature
1961 sign[0] := 137;
1962 sign[1] := 80;
1963 sign[2] := 78;
1964 sign[3] := 71;
1965 sign[4] := 13;
1966 sign[5] := 10;
1967 sign[6] := 26;
1968 sign[7] := 10;
1969 st.writeBuffer(sign, 8);
1970 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1972 // header
1973 writeIntBE(st, LongWord(13));
1974 sign[0] := 73;
1975 sign[1] := 72;
1976 sign[2] := 68;
1977 sign[3] := 82;
1978 st.writeBuffer(sign, 4);
1979 crc := crc32(0, @sign[0], 4);
1980 hbuf[0] := 0;
1981 hbuf[1] := 0;
1982 hbuf[2] := (Width shr 8) and $ff;
1983 hbuf[3] := Width and $ff;
1984 hbuf[4] := 0;
1985 hbuf[5] := 0;
1986 hbuf[6] := (Height shr 8) and $ff;
1987 hbuf[7] := Height and $ff;
1988 hbuf[8] := 8; // bit depth
1989 hbuf[9] := 2; // RGB
1990 hbuf[10] := 0; // compression method
1991 hbuf[11] := 0; // filter method
1992 hbuf[12] := 0; // no interlace
1993 crc := crc32(crc, @hbuf[0], 13);
1994 st.writeBuffer(hbuf, 13);
1995 writeIntBE(st, crc);
1996 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1998 // image data
1999 writeIntBE(st, LongWord(dlen));
2000 sign[0] := 73;
2001 sign[1] := 68;
2002 sign[2] := 65;
2003 sign[3] := 84;
2004 st.writeBuffer(sign, 4);
2005 crc := crc32(0, @sign[0], 4);
2006 crc := crc32(crc, obuf, dlen);
2007 st.writeBuffer(obuf^, dlen);
2008 writeIntBE(st, crc);
2009 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2011 // image data end
2012 writeIntBE(st, LongWord(0));
2013 sign[0] := 73;
2014 sign[1] := 69;
2015 sign[2] := 78;
2016 sign[3] := 68;
2017 st.writeBuffer(sign, 4);
2018 crc := crc32(0, @sign[0], 4);
2019 writeIntBE(st, crc);
2020 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2021 finally
2022 if obuf <> nil then FreeMem(obuf);
2023 end;
2024 end
2025 else
2026 begin
2027 Imaging.SetOption(ImagingPNGCompressLevel, 9);
2028 Imaging.SetOption(ImagingPNGPreFilter, 6);
2029 InitImage(img);
2030 try
2031 NewImage(Width, Height, TImageFormat.ifR8G8B8, img);
2032 ps := pixels;
2033 //writeln(stderr, 'moving pixels...');
2034 for y := Height-1 downto 0 do
2035 begin
2036 for x := 0 to Width-1 do
2037 begin
2038 clr.r := ps^; Inc(ps);
2039 clr.g := ps^; Inc(ps);
2040 clr.b := ps^; Inc(ps);
2041 clr.a := 255;
2042 SetPixel32(img, x, y, clr);
2043 end;
2044 end;
2045 GlobalMetadata.ClearMetaItems();
2046 GlobalMetadata.ClearMetaItemsForSaving();
2047 //writeln(stderr, 'compressing image...');
2048 if not SaveImageToStream('png', st, img) then raise Exception.Create('screenshot writing error');
2049 //writeln(stderr, 'done!');
2050 finally
2051 FreeImage(img);
2052 end;
2053 end;
2054 finally
2055 FreeMem(pixels);
2056 end;
2057 end;
2060 end.