DEADSOFTWARE

BlendMode.Filter is now a proper filter
[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);
65 procedure e_ResizeFramebuffer(Width, Height: Integer);
66 procedure e_BlitFramebuffer(WinWidth, WinHeight: Integer);
68 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
69 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
70 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
71 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
72 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
73 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
74 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
75 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
77 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
78 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
80 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
81 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
83 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
85 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
86 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
87 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
88 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
89 Blending: TBlending = TBlending.None);
90 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
91 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
93 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
94 function e_CreateTexture(FileName: string; var ID: DWORD): Boolean;
95 function e_CreateTextureEx(FileName: string; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
96 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
97 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
98 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
99 procedure e_DeleteTexture(ID: DWORD);
100 procedure e_RemoveAllTextures();
102 // CharFont
103 function e_CharFont_Create(sp: ShortInt=0): DWORD;
104 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
105 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
106 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
107 Color: TRGB; Scale: Single = 1.0);
108 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
109 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
110 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
111 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
112 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
113 procedure e_CharFont_Remove(FontID: DWORD);
114 procedure e_CharFont_RemoveAll();
116 // TextureFont
117 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
118 Space: ShortInt=0);
119 procedure e_TextureFontKill(FontID: DWORD);
120 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
121 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
122 Blue: Byte; Scale: Single; Shadow: Boolean = False);
123 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD;
124 Shadow: Boolean = False; Newlines: Boolean = False);
125 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
126 procedure e_RemoveAllTextureFont();
128 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
129 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
131 procedure e_ReleaseEngine();
132 procedure e_BeginRender();
133 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
134 procedure e_Clear(); overload;
135 procedure e_EndRender();
137 {$IFDEF USE_SDL2}
138 function e_GetGamma(win: PSDL_Window): Byte;
139 procedure e_SetGamma(win: PSDL_Window;Gamma: Byte);
140 {$ENDIF}
142 procedure e_MakeScreenshot(st: TStream; Width, Height: Word);
144 function _RGB(Red, Green, Blue: Byte): TRGB;
145 function _Point(X, Y: Integer): TPoint2i;
146 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
147 function _TRect(L, T, R, B: LongInt): TRect;
149 //function e_getTextGLId (ID: DWORD): GLuint;
151 var
152 e_Colors: TRGB;
153 e_NoGraphics: Boolean = False;
154 e_FastScreenshots: Boolean = true; // it's REALLY SLOW with `false`
155 g_dbg_scale: Single = 1.0;
156 r_pixel_scale: Single = 1.0;
159 implementation
161 uses
162 paszlib, crc, utils;
165 type
166 TTexture = record
167 tx: GLTexture;
168 end;
170 TTextureFont = record
171 Texture: DWORD;
172 TextureID: DWORD;
173 Base: Uint32;
174 CharWidth: Byte;
175 CharHeight: Byte;
176 XC, YC: WORD;
177 SPC: ShortInt;
178 end;
180 TCharFont = record
181 Chars: array[0..255] of
182 record
183 TextureID: Integer;
184 Width: Byte;
185 end;
186 Space: ShortInt;
187 Height: ShortInt;
188 alive: Boolean;
189 end;
191 TSavedTexture = record
192 TexID: DWORD;
193 OldID: DWORD;
194 Pixels: Pointer;
195 end;
197 var
198 e_Textures: array of TTexture = nil;
199 e_TextureFonts: array of TTextureFont = nil;
200 e_CharFonts: array of TCharFont;
201 //e_SavedTextures: array of TSavedTexture;
202 e_FBO: GLuint = 0;
203 e_RBO: GLuint = 0;
204 e_Frame: GLuint = 0;
205 e_FrameW: Integer = -1;
206 e_FrameH: Integer = -1;
208 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
210 //------------------------------------------------------------------
211 // Èíèöèàëèçèðóåò OpenGL
212 //------------------------------------------------------------------
213 procedure e_InitGL();
214 begin
215 if e_NoGraphics then
216 begin
217 e_DummyTextures := True;
218 Exit;
219 end;
220 e_Colors.R := 255;
221 e_Colors.G := 255;
222 e_Colors.B := 255;
223 glDisable(GL_DEPTH_TEST);
224 glEnable(GL_SCISSOR_TEST);
225 glClearColor(0, 0, 0, 0);
226 end;
228 procedure e_SetViewPort(X, Y, Width, Height: Word);
229 var
230 mat: Array [0..15] of GLDouble;
232 begin
233 if e_NoGraphics then Exit;
234 glLoadIdentity();
235 glScissor(X, Y, Width, Height);
236 glViewport(X, Y, Width, Height);
237 //gluOrtho2D(0, Width, Height, 0);
239 glMatrixMode(GL_PROJECTION);
241 mat[ 0] := 2.0 / Width;
242 mat[ 1] := 0.0;
243 mat[ 2] := 0.0;
244 mat[ 3] := 0.0;
246 mat[ 4] := 0.0;
247 mat[ 5] := -2.0 / Height;
248 mat[ 6] := 0.0;
249 mat[ 7] := 0.0;
251 mat[ 8] := 0.0;
252 mat[ 9] := 0.0;
253 mat[10] := 1.0;
254 mat[11] := 0.0;
256 mat[12] := -1.0;
257 mat[13] := 1.0;
258 mat[14] := 0.0;
259 mat[15] := 1.0;
261 glLoadMatrixd(@mat[0]);
263 glMatrixMode(GL_MODELVIEW);
264 glLoadIdentity();
265 end;
267 //------------------------------------------------------------------
268 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
269 //------------------------------------------------------------------
270 function FindTexture(): DWORD;
271 var
272 i: integer;
273 begin
274 if e_Textures <> nil then
275 for i := 0 to High(e_Textures) do
276 if e_Textures[i].tx.Width = 0 then
277 begin
278 Result := i;
279 Exit;
280 end;
282 if e_Textures = nil then
283 begin
284 SetLength(e_Textures, 32);
285 Result := 0;
286 end
287 else
288 begin
289 Result := High(e_Textures) + 1;
290 SetLength(e_Textures, Length(e_Textures) + 32);
291 end;
292 end;
294 //------------------------------------------------------------------
295 // Ñîçäàåò òåêñòóðó
296 //------------------------------------------------------------------
297 function e_CreateTexture(FileName: String; var ID: DWORD): Boolean;
298 var
299 find_id: DWORD;
300 fmt: Word;
301 begin
302 Result := False;
304 e_WriteLog('Loading texture from '+FileName, TMsgType.Notify);
306 find_id := FindTexture();
308 if not LoadTexture(FileName, e_Textures[find_id].tx, e_Textures[find_id].tx.Width,
309 e_Textures[find_id].tx.Height, @fmt) then Exit;
311 ID := find_id;
313 Result := True;
314 end;
316 function e_CreateTextureEx(FileName: String; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
317 var
318 find_id: DWORD;
319 fmt: Word;
320 begin
321 Result := False;
323 find_id := FindTexture();
325 if not LoadTextureEx(FileName, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
327 ID := find_id;
329 Result := True;
330 end;
332 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
333 var
334 find_id: DWORD;
335 fmt: Word;
336 begin
337 Result := False;
339 find_id := FindTexture;
341 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;
343 id := find_id;
345 Result := True;
346 end;
348 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
349 var
350 find_id: DWORD;
351 fmt: Word;
352 begin
353 Result := False;
355 find_id := FindTexture();
357 if not LoadTextureMemEx(pData, dataSize, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
359 ID := find_id;
361 Result := True;
362 end;
364 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
365 var
366 find_id: DWORD;
367 fmt, tw, th: Word;
368 begin
369 result := false;
370 find_id := FindTexture();
371 if not LoadTextureImg(img, e_Textures[find_id].tx, tw, th, @fmt) then exit;
372 ID := find_id;
373 result := True;
374 end;
376 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
377 begin
378 if Width <> nil then Width^ := e_Textures[ID].tx.Width;
379 if Height <> nil then Height^ := e_Textures[ID].tx.Height;
380 end;
382 procedure e_ResizeFramebuffer(Width, Height: Integer);
383 begin
384 if e_NoGraphics then Exit;
386 glBindTexture(GL_TEXTURE_2D, 0);
387 glBindRenderbuffer(GL_RENDERBUFFER, 0);
388 glBindFramebuffer(GL_FRAMEBUFFER, 0);
390 if e_Frame > 0 then
391 begin
392 glDeleteTextures(1, @e_Frame);
393 e_Frame := 0;
394 end;
396 if e_RBO > 0 then
397 begin
398 glDeleteRenderbuffers(1, @e_RBO);
399 e_RBO := 0;
400 end;
402 if e_FBO > 0 then
403 begin
404 glDeleteFramebuffers(1, @e_FBO);
405 e_FBO := 0;
406 end;
408 e_FrameW := Width;
409 e_FrameH := Height;
411 glGenFramebuffers(1, @e_FBO);
413 glGenTextures(1, @e_Frame);
414 glBindTexture(GL_TEXTURE_2D, e_Frame);
415 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, nil);
416 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
417 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
419 glGenRenderbuffers(1, @e_RBO);
420 glBindRenderbuffer(GL_RENDERBUFFER, e_RBO);
421 glRenderbufferStorage(GL_RENDERBUFFER, {$IFNDEF USE_GLES1}GL_DEPTH24_STENCIL8{$ELSE}GL_DEPTH_COMPONENT16{$ENDIF}, Width, Height);
423 glBindFramebuffer(GL_FRAMEBUFFER, e_FBO);
424 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, e_Frame, 0);
425 glFramebufferRenderbuffer(GL_FRAMEBUFFER, {$IFNDEF USE_GLES1}GL_DEPTH_STENCIL_ATTACHMENT{$ELSE}GL_DEPTH_ATTACHMENT{$ENDIF}, GL_RENDERBUFFER, e_RBO);
426 end;
428 procedure e_ResizeWindow(Width, Height: Integer);
429 begin
430 if Height = 0 then
431 Height := 1;
432 e_SetViewPort(0, 0, Width, Height);
433 end;
435 procedure drawTxQuad (x0, y0, w, h, tw, th: Integer; u, v: single; Mirror: TMirrorType);
436 var
437 x1, y1, tmp: Integer;
438 begin
439 if (w < 1) or (h < 1) then exit;
440 x1 := x0+w;
441 y1 := y0+h;
442 if Mirror = TMirrorType.Horizontal then begin tmp := x1; x1 := x0; x0 := tmp; end
443 else if Mirror = TMirrorType.Vertical then begin tmp := y1; y1 := y0; y0 := tmp; end;
444 glTexCoord2f(0, v); glVertex2i(x0, y0);
445 glTexCoord2f(0, 0); glVertex2i(x0, y1);
446 glTexCoord2f(u, 0); glVertex2i(x1, y1);
447 glTexCoord2f(u, v); glVertex2i(x1, y0);
448 end;
450 procedure e_BlitFramebuffer(WinWidth, WinHeight: Integer);
451 begin
452 if (e_FBO = 0) or (e_Frame = 0) or e_NoGraphics then exit;
454 glDisable(GL_BLEND);
455 glEnable(GL_TEXTURE_2D);
456 glBindFramebuffer(GL_FRAMEBUFFER, 0);
457 glBindTexture(GL_TEXTURE_2D, e_Frame);
458 e_SetViewPort(0, 0, WinWidth, WinHeight);
459 glColor4ub(255, 255, 255, 255);
461 glBegin(GL_QUADS);
462 glTexCoord2f(0, 1); glVertex2i( 0, 0);
463 glTexCoord2f(0, 0); glVertex2i( 0, WinHeight);
464 glTexCoord2f(1, 0); glVertex2i(WinWidth, WinHeight);
465 glTexCoord2f(1, 1); glVertex2i(WinWidth, 0);
466 glEnd();
468 glBindFramebuffer(GL_FRAMEBUFFER, e_FBO);
469 e_SetViewPort(0, 0, e_FrameW, e_FrameH);
470 end;
472 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
473 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
474 begin
475 if e_NoGraphics then Exit;
476 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
478 if (Alpha > 0) or (AlphaChannel) or (Blending) then
479 glEnable(GL_BLEND)
480 else
481 glDisable(GL_BLEND);
483 if (AlphaChannel) or (Alpha > 0) then
484 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
486 if Alpha > 0 then
487 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
489 if Blending then
490 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
492 glEnable(GL_TEXTURE_2D);
493 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
494 glBegin(GL_QUADS);
496 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);
498 //u := e_Textures[ID].tx.u;
499 //v := e_Textures[ID].tx.v;
502 if Mirror = M_NONE then
503 begin
504 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
505 glTexCoord2f(0, 0); glVertex2i(X, Y);
506 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
507 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
508 end
509 else
510 if Mirror = M_HORIZONTAL then
511 begin
512 glTexCoord2f(u, 0); glVertex2i(X, Y);
513 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
514 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
515 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
516 end
517 else
518 if Mirror = M_VERTICAL then
519 begin
520 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
521 glTexCoord2f(0, -v); glVertex2i(X, Y);
522 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
523 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
524 end;
527 glEnd();
529 glDisable(GL_BLEND);
530 end;
532 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
533 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
534 var
535 u, v: Single;
536 begin
537 if e_NoGraphics then Exit;
538 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
540 if (Alpha > 0) or (AlphaChannel) or (Blending) then
541 glEnable(GL_BLEND)
542 else
543 glDisable(GL_BLEND);
545 if (AlphaChannel) or (Alpha > 0) then
546 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
548 if Alpha > 0 then
549 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
551 if Blending then
552 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
554 glEnable(GL_TEXTURE_2D);
555 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
557 u := e_Textures[ID].tx.u;
558 v := e_Textures[ID].tx.v;
560 glBegin(GL_QUADS);
561 glTexCoord2f(0, v); glVertex2i(X, Y);
562 glTexCoord2f(u, v); glVertex2i(X + Width, Y);
563 glTexCoord2f(u, 0); glVertex2i(X + Width, Y + Height);
564 glTexCoord2f(0, 0); glVertex2i(X, Y + Height);
565 glEnd();
567 glDisable(GL_BLEND);
568 end;
570 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
571 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
572 begin
573 if e_NoGraphics then Exit;
574 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
576 if (Alpha > 0) or (AlphaChannel) or (Blending) then
577 glEnable(GL_BLEND)
578 else
579 glDisable(GL_BLEND);
581 if (AlphaChannel) or (Alpha > 0) then
582 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
584 if Alpha > 0 then
585 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
587 if Blending then
588 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
590 glEnable(GL_TEXTURE_2D);
591 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
592 glBegin(GL_QUADS);
593 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);
594 glEnd();
596 glDisable(GL_BLEND);
597 end;
599 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
600 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
601 var
602 X2, Y2, dx, w, h: Integer;
603 u, v: Single;
604 begin
605 if e_NoGraphics then Exit;
606 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
607 ambientBlendMode := false;
609 if (Alpha > 0) or AlphaChannel or Blending then
610 begin
611 glEnable(GL_BLEND);
612 end
613 else
614 begin
615 if not ambientBlendMode then glDisable(GL_BLEND);
616 end;
617 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
618 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
619 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
621 if (XCount = 0) then XCount := 1;
622 if (YCount = 0) then YCount := 1;
624 glEnable(GL_TEXTURE_2D);
625 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
627 X2 := X+e_Textures[ID].tx.width*XCount;
628 Y2 := Y+e_Textures[ID].tx.height*YCount;
630 //k8: this SHOULD work... i hope
631 if (e_Textures[ID].tx.width = e_Textures[ID].tx.glwidth) and (e_Textures[ID].tx.height = e_Textures[ID].tx.glheight) then
632 begin
633 glBegin(GL_QUADS);
634 glTexCoord2i(0, YCount); glVertex2i(X, Y);
635 glTexCoord2i(XCount, YCount); glVertex2i(X2, Y);
636 glTexCoord2i(XCount, 0); glVertex2i(X2, Y2);
637 glTexCoord2i(0, 0); glVertex2i(X, Y2);
638 glEnd();
639 end
640 else
641 begin
642 glBegin(GL_QUADS);
643 // hard day's night
644 u := e_Textures[ID].tx.u;
645 v := e_Textures[ID].tx.v;
646 w := e_Textures[ID].tx.width;
647 h := e_Textures[ID].tx.height;
648 while YCount > 0 do
649 begin
650 dx := XCount;
651 x2 := X;
652 while dx > 0 do
653 begin
654 glTexCoord2f(0, v); glVertex2i(X, Y);
655 glTexCoord2f(u, v); glVertex2i(X+w, Y);
656 glTexCoord2f(u, 0); glVertex2i(X+w, Y+h);
657 glTexCoord2f(0, 0); glVertex2i(X, Y+h);
658 Inc(X, w);
659 Dec(dx);
660 end;
661 X := x2;
662 Inc(Y, h);
663 Dec(YCount);
664 end;
665 glEnd();
666 end;
668 glDisable(GL_BLEND);
669 end;
672 //TODO: overflow checks
673 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
674 var
675 ex0, ey0: Integer;
676 begin
677 result := false;
678 if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit;
679 // check for intersection
680 if (x0+w0 <= x1) or (y0+h0 <= y1) or (x1+w1 <= x0) or (y1+h1 <= y0) then exit;
681 if (x0 >= x1+w1) or (y0 >= y1+h1) or (x1 >= x0+h0) or (y1 >= y0+h0) then exit;
682 // ok, intersects
683 ex0 := x0+w0;
684 ey0 := y0+h0;
685 if (x0 < x1) then x0 := x1;
686 if (y0 < y1) then y0 := y1;
687 if (ex0 > x1+w1) then ex0 := x1+w1;
688 if (ey0 > y1+h1) then ey0 := y1+h1;
689 w0 := ex0-x0;
690 h0 := ey0-y0;
691 result := (w0 > 0) and (h0 > 0);
692 end;
695 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
696 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
697 var
698 x2, y2: Integer;
700 wassc: Boolean;
701 scxywh: array[0..3] of GLint;
702 vpxywh: array[0..3] of GLint;
704 w, h, dw, cw, ch, yofs: Integer;
705 u, v, cu, cv: Single;
706 onlyOneY: Boolean;
709 procedure setScissorGLInternal (x, y, w, h: Integer);
710 begin
711 //if not scallowed then exit;
712 x := trunc(x*scale);
713 y := trunc(y*scale);
714 w := trunc(w*scale);
715 h := trunc(h*scale);
716 y := vpxywh[3]-(y+h);
717 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
718 begin
719 glScissor(0, 0, 0, 0);
720 end
721 else
722 begin
723 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
724 glScissor(x, y, w, h);
725 end;
726 end;
729 begin
730 if e_NoGraphics then exit;
731 ambientBlendMode := false;
733 if (wdt < 1) or (hgt < 1) then exit;
735 if (wdt mod e_Textures[ID].tx.width = 0) and (hgt mod e_Textures[ID].tx.height = 0) then
736 begin
737 e_DrawFill(id, x, y, wdt div e_Textures[ID].tx.width, hgt div e_Textures[ID].tx.height, alpha, alphachannel, blending, ambientBlendMode);
738 exit;
739 end;
741 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
743 if (Alpha > 0) or AlphaChannel or Blending then
744 begin
745 glEnable(GL_BLEND);
746 end
747 else
748 begin
749 if not ambientBlendMode then glDisable(GL_BLEND);
750 end;
751 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
752 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
753 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
755 glEnable(GL_TEXTURE_2D);
756 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
758 x2 := x+wdt;
759 y2 := y+hgt;
761 //k8: this SHOULD work... i hope
762 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
763 begin
764 glBegin(GL_QUADS);
765 glTexCoord2f(0, hgt/e_Textures[ID].tx.height); glVertex2i(x, y);
766 glTexCoord2f(wdt/e_Textures[ID].tx.width, hgt/e_Textures[ID].tx.height); glVertex2i(x2, y);
767 glTexCoord2f(wdt/e_Textures[ID].tx.width, 0); glVertex2i(x2, y2);
768 glTexCoord2f(0, 0); glVertex2i(x, y2);
769 glEnd();
770 end
771 else
772 begin
773 // hard day's night; setup scissor
775 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
776 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
777 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
778 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
779 //glEnable(GL_SCISSOR_TEST);
780 setScissorGLInternal(x, y, wdt, hgt);
782 // draw quads
783 u := e_Textures[ID].tx.u;
784 v := e_Textures[ID].tx.v;
785 w := e_Textures[ID].tx.width;
786 h := e_Textures[ID].tx.height;
787 x2 := x;
788 if (hgt > h) then begin y += hgt-h; onlyOneY := false; end else onlyOneY := true;
789 glBegin(GL_QUADS);
790 while (hgt > 0) do
791 begin
792 if (hgt >= h) then begin ch := h; cv := v; yofs := 0; end else begin ch := hgt; cv := v/(h/hgt); yofs := h-hgt; end;
793 if onlyOneY then yofs := 0;
794 Dec(hgt, h);
795 dw := wdt;
796 x := x2;
797 while (dw > 0) do
798 begin
799 if (dw >= w) then begin cw := w; cu := u; end else begin cw := dw; cu := u/(w/dw); end;
800 Dec(dw, w);
801 glTexCoord2f(0, cv); glVertex2i(X, Y+yofs);
802 glTexCoord2f(cu, cv); glVertex2i(X+cw, Y+yofs);
803 glTexCoord2f(cu, 0); glVertex2i(X+cw, Y+ch+yofs);
804 glTexCoord2f(0, 0); glVertex2i(X, Y+ch+yofs);
805 Inc(X, w);
806 end;
807 Dec(Y, h);
808 end;
809 glEnd();
810 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
811 end;
813 glDisable(GL_BLEND);
814 end;
817 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
818 begin
819 if e_NoGraphics then exit;
820 if (w < 1) or (h < 1) then exit;
821 if (a <> 255) or ((r or g or b) <> 0) then
822 begin
823 glEnable(GL_BLEND);
824 glDisable(GL_TEXTURE_2D);
825 glColor4ub(r, g, b, a);
826 if ((r or g or b) <> 0) then
827 begin
828 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
829 glBegin(GL_QUADS);
830 glVertex2i(x, y);
831 glVertex2i(x+w, y);
832 glVertex2i(x+w, y+h);
833 glVertex2i(x, y+h);
834 glEnd();
835 end;
836 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
837 glBegin(GL_QUADS);
838 glVertex2i(x, y);
839 glVertex2i(x+w, y);
840 glVertex2i(x+w, y+h);
841 glVertex2i(x, y+h);
842 glEnd();
843 glDisable(GL_BLEND);
844 end;
845 end;
848 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
849 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
850 begin
851 if e_NoGraphics then Exit;
853 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
855 if (Alpha > 0) or (AlphaChannel) or (Blending) then
856 glEnable(GL_BLEND)
857 else
858 glDisable(GL_BLEND);
860 if (AlphaChannel) or (Alpha > 0) then
861 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
863 if Alpha > 0 then
864 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
866 if Blending then
867 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
869 if (Angle <> 0) and (RC <> nil) then
870 begin
871 glPushMatrix();
872 glTranslatef(X+RC.X, Y+RC.Y, 0);
873 glRotatef(Angle, 0, 0, 1);
874 glTranslatef(-(X+RC.X), -(Y+RC.Y), 0);
875 end;
877 glEnable(GL_TEXTURE_2D);
878 glBindTexture(GL_TEXTURE_2D, e_Textures[id].tx.id);
879 glBegin(GL_QUADS); //0-1 1-1
880 //00 10
881 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);
882 glEnd();
884 if Angle <> 0 then
885 glPopMatrix();
887 glDisable(GL_BLEND);
888 end;
890 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
891 begin
892 if e_NoGraphics then Exit;
893 glDisable(GL_TEXTURE_2D);
894 glColor3ub(Red, Green, Blue);
895 glPointSize(Size);
897 if (Size = 2) or (Size = 4) then
898 X := X + 1;
900 glBegin(GL_POINTS);
901 glVertex2f(X+0.3, Y+1.0);
902 glEnd();
904 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
905 end;
907 procedure e_LineCorrection(var X1, Y1, X2, Y2: Integer);
908 begin
909 // Make lines only top-left/bottom-right and top-right/bottom-left
910 if Y2 < Y1 then
911 begin
912 X1 := X1 xor X2;
913 X2 := X1 xor X2;
914 X1 := X1 xor X2;
916 Y1 := Y1 xor Y2;
917 Y2 := Y1 xor Y2;
918 Y1 := Y1 xor Y2;
919 end;
921 // Pixel-perfect hack
922 if X1 < X2 then
923 Inc(X2)
924 else
925 Inc(X1);
926 Inc(Y2);
927 end;
929 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
930 var
931 nX1, nY1, nX2, nY2: Integer;
932 begin
933 if e_NoGraphics then Exit;
934 // Only top-left/bottom-right quad
935 if X1 > X2 then
936 begin
937 X1 := X1 xor X2;
938 X2 := X1 xor X2;
939 X1 := X1 xor X2;
940 end;
941 if Y1 > Y2 then
942 begin
943 Y1 := Y1 xor Y2;
944 Y2 := Y1 xor Y2;
945 Y1 := Y1 xor Y2;
946 end;
948 if Alpha > 0 then
949 begin
950 glEnable(GL_BLEND);
951 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
952 end
953 else
954 glDisable(GL_BLEND);
956 glDisable(GL_TEXTURE_2D);
957 glColor4ub(Red, Green, Blue, 255-Alpha);
958 glLineWidth(1);
959 glBegin(GL_LINES);
960 nX1 := X1; nY1 := Y1;
961 nX2 := X2; nY2 := Y1;
962 e_LineCorrection(nX1, nY1, nX2, nY2); // Pixel-perfect lines
963 glVertex2i(nX1, nY1);
964 glVertex2i(nX2, nY2);
966 nX1 := X2; nY1 := Y1;
967 nX2 := X2; nY2 := Y2;
968 e_LineCorrection(nX1, nY1, nX2, nY2);
969 glVertex2i(nX1, nY1);
970 glVertex2i(nX2, nY2);
972 nX1 := X2; nY1 := Y2;
973 nX2 := X1; nY2 := Y2;
974 e_LineCorrection(nX1, nY1, nX2, nY2);
975 glVertex2i(nX1, nY1);
976 glVertex2i(nX2, nY2);
978 nX1 := X1; nY1 := Y2;
979 nX2 := X1; nY2 := Y1;
980 e_LineCorrection(nX1, nY1, nX2, nY2);
981 glVertex2i(nX1, nY1);
982 glVertex2i(nX2, nY2);
983 glEnd();
984 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
985 glDisable(GL_BLEND);
986 end;
988 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
989 Blending: TBlending = TBlending.None);
990 begin
991 if e_NoGraphics then Exit;
992 if (Alpha > 0) or (Blending <> TBlending.None) then
993 glEnable(GL_BLEND)
994 else
995 glDisable(GL_BLEND);
997 case Blending of
998 TBlending.None: if Alpha > 0 then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
999 TBlending.Blend: glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1000 TBlending.Invert: glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1001 TBlending.Filter: glBlendFunc(GL_ZERO, GL_SRC_COLOR);
1002 end;
1004 glDisable(GL_TEXTURE_2D);
1005 glColor4ub(Red, Green, Blue, 255-Alpha);
1007 X2 := X2 + 1;
1008 Y2 := Y2 + 1;
1010 glBegin(GL_QUADS);
1011 glVertex2i(X1, Y1);
1012 glVertex2i(X2, Y1);
1013 glVertex2i(X2, Y2);
1014 glVertex2i(X1, Y2);
1015 glEnd();
1017 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1018 glDisable(GL_BLEND);
1019 end;
1022 // ////////////////////////////////////////////////////////////////////////// //
1023 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
1024 begin
1025 if (a < 0) then a := 0;
1026 if (a > 255) then a := 255;
1027 glEnable(GL_BLEND);
1028 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
1029 glDisable(GL_TEXTURE_2D);
1030 glColor4ub(0, 0, 0, Byte(255-a));
1031 glBegin(GL_QUADS);
1032 glVertex2i(x0, y0);
1033 glVertex2i(x1, y0);
1034 glVertex2i(x1, y1);
1035 glVertex2i(x0, y1);
1036 glEnd();
1037 //glRect(x, y, x+w, y+h);
1038 glColor4ub(1, 1, 1, 1);
1039 glDisable(GL_BLEND);
1040 //glBlendEquation(GL_FUNC_ADD);
1041 end;
1043 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
1044 begin
1045 if (w > 0) and (h > 0) then e_DarkenQuad(x, y, x+w, y+h, a);
1046 end;
1049 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
1050 begin
1051 if e_NoGraphics then Exit;
1052 // Pixel-perfect lines
1053 if Width = 1 then
1054 e_LineCorrection(X1, Y1, X2, Y2);
1056 if Alpha > 0 then
1057 begin
1058 glEnable(GL_BLEND);
1059 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1060 end else
1061 glDisable(GL_BLEND);
1063 glDisable(GL_TEXTURE_2D);
1064 glColor4ub(Red, Green, Blue, 255-Alpha);
1065 glLineWidth(Width);
1066 glBegin(GL_LINES);
1067 glVertex2i(X1, Y1);
1068 glVertex2i(X2, Y2);
1069 glEnd();
1070 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1072 glDisable(GL_BLEND);
1073 end;
1075 //------------------------------------------------------------------
1076 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1077 //------------------------------------------------------------------
1078 procedure e_DeleteTexture(ID: DWORD);
1079 begin
1080 if not e_NoGraphics then
1081 glDeleteTextures(1, @e_Textures[ID].tx.id);
1082 e_Textures[ID].tx.id := 0;
1083 e_Textures[ID].tx.Width := 0;
1084 e_Textures[ID].tx.Height := 0;
1085 end;
1087 //------------------------------------------------------------------
1088 // Óäàëÿåò âñå òåêñòóðû
1089 //------------------------------------------------------------------
1090 procedure e_RemoveAllTextures();
1091 var
1092 i: integer;
1093 begin
1094 if e_Textures = nil then Exit;
1096 for i := 0 to High(e_Textures) do
1097 if e_Textures[i].tx.Width <> 0 then e_DeleteTexture(i);
1098 e_Textures := nil;
1099 end;
1101 //------------------------------------------------------------------
1102 // Óäàëÿåò äâèæîê
1103 //------------------------------------------------------------------
1104 procedure e_ReleaseEngine();
1105 begin
1106 e_RemoveAllTextures;
1107 e_RemoveAllTextureFont;
1108 end;
1110 procedure e_BeginRender();
1111 begin
1112 if e_NoGraphics then Exit;
1113 glEnable(GL_ALPHA_TEST);
1114 glAlphaFunc(GL_GREATER, 0.0);
1115 end;
1117 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
1118 begin
1119 if e_NoGraphics then Exit;
1120 glClearColor(Red, Green, Blue, 0);
1121 glClear(Mask);
1122 end;
1124 procedure e_Clear(); overload;
1125 begin
1126 if e_NoGraphics then Exit;
1127 glClearColor(0, 0, 0, 0);
1128 glClear(GL_COLOR_BUFFER_BIT);
1129 end;
1131 procedure e_EndRender();
1132 begin
1133 if e_NoGraphics then Exit;
1134 glPopMatrix();
1135 end;
1137 {$IFDEF USE_SDL2}
1138 function e_GetGamma(win: PSDL_Window): Byte;
1139 var
1140 ramp: array [0..256*3-1] of Word;
1141 rgb: array [0..2] of Double;
1142 sum: double;
1143 count: integer;
1144 min: integer;
1145 max: integer;
1146 A, B: double;
1147 i, j: integer;
1148 begin
1149 Result := 0;
1150 if e_NoGraphics then Exit;
1151 rgb[0] := 1.0;
1152 rgb[1] := 1.0;
1153 rgb[2] := 1.0;
1155 SDL_GetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1157 for i := 0 to 2 do
1158 begin
1159 sum := 0;
1160 count := 0;
1161 min := 256 * i;
1162 max := min + 256;
1164 for j := min to max - 1 do
1165 if ramp[j] > 0 then
1166 begin
1167 B := (j mod 256)/256;
1168 A := ramp[j]/65536;
1169 sum := sum + ln(A)/ln(B);
1170 inc(count);
1171 end;
1172 rgb[i] := sum / count;
1173 end;
1175 Result := 100 - Trunc(((rgb[0] + rgb[1] + rgb[2])/3 - 0.23) * 100/(2.7 - 0.23));
1176 end;
1178 procedure e_SetGamma(win: PSDL_Window; Gamma: Byte);
1179 var
1180 ramp: array [0..256*3-1] of Word;
1181 i: integer;
1182 r: double;
1183 g: double;
1184 begin
1185 if e_NoGraphics then Exit;
1186 g := (100 - Gamma)*(2.7 - 0.23)/100 + 0.23;
1188 for i := 0 to 255 do
1189 begin
1190 r := Exp(g * ln(i/256))*65536;
1191 if r < 0 then r := 0
1192 else if r > 65535 then r := 65535;
1193 ramp[i] := trunc(r);
1194 ramp[i + 256] := trunc(r);
1195 ramp[i + 512] := trunc(r);
1196 end;
1198 SDL_SetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1199 end;
1200 {$ENDIF}
1202 function e_CharFont_Create(sp: ShortInt=0): DWORD;
1203 var
1204 i, id: DWORD;
1205 begin
1206 e_WriteLog('Creating CharFont...', TMsgType.Notify);
1208 id := DWORD(-1);
1210 if e_CharFonts <> nil then
1211 for i := 0 to High(e_CharFonts) do
1212 if not e_CharFonts[i].alive then
1213 begin
1214 id := i;
1215 Break;
1216 end;
1218 if id = DWORD(-1) then
1219 begin
1220 SetLength(e_CharFonts, Length(e_CharFonts) + 1);
1221 id := High(e_CharFonts);
1222 end;
1224 with e_CharFonts[id] do
1225 begin
1226 for i := 0 to High(Chars) do
1227 with Chars[i] do
1228 begin
1229 TextureID := -1;
1230 Width := 0;
1231 end;
1233 Space := sp;
1234 alive := True;
1235 end;
1237 Result := id;
1238 end;
1240 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
1241 begin
1242 with e_CharFonts[FontID].Chars[Ord(c)] do
1243 begin
1244 TextureID := Texture;
1245 Width := w;
1246 end;
1247 end;
1249 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
1250 var
1251 a: Integer;
1252 begin
1253 if e_NoGraphics then Exit;
1254 if Text = '' then Exit;
1255 if e_CharFonts = nil then Exit;
1256 if Integer(FontID) > High(e_CharFonts) then Exit;
1258 with e_CharFonts[FontID] do
1259 begin
1260 for a := 1 to Length(Text) do
1261 with Chars[Ord(Text[a])] do
1262 if TextureID <> -1 then
1263 begin
1264 e_Draw(TextureID, X, Y, 0, True, False);
1265 X := X+Width+IfThen(a = Length(Text), 0, Space);
1266 end;
1267 end;
1268 end;
1270 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
1271 Color: TRGB; Scale: Single = 1.0);
1272 var
1273 a: Integer;
1274 c: TRGB;
1275 begin
1276 if e_NoGraphics then Exit;
1277 if Text = '' then Exit;
1278 if e_CharFonts = nil then Exit;
1279 if Integer(FontID) > High(e_CharFonts) then Exit;
1281 with e_CharFonts[FontID] do
1282 begin
1283 for a := 1 to Length(Text) do
1284 with Chars[Ord(Text[a])] do
1285 if TextureID <> -1 then
1286 begin
1287 if Scale <> 1.0 then
1288 begin
1289 glPushMatrix;
1290 glScalef(Scale, Scale, 0);
1291 end;
1293 c := e_Colors;
1294 e_Colors := Color;
1295 e_Draw(TextureID, X, Y, 0, True, False);
1296 e_Colors := c;
1298 if Scale <> 1.0 then glPopMatrix;
1300 X := X+Width+IfThen(a = Length(Text), 0, Space);
1301 end;
1302 end;
1303 end;
1305 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
1306 var
1307 a, TX, TY, len: Integer;
1308 tc, c: TRGB;
1309 w, h: Word;
1310 begin
1311 if e_NoGraphics then Exit;
1312 if Text = '' then Exit;
1313 if e_CharFonts = nil then Exit;
1314 if Integer(FontID) > High(e_CharFonts) then Exit;
1316 c.R := 255;
1317 c.G := 255;
1318 c.B := 255;
1320 TX := X;
1321 TY := Y;
1322 len := Length(Text);
1324 e_CharFont_GetSize(FontID, 'A', w, h);
1326 with e_CharFonts[FontID] do
1327 begin
1328 for a := 1 to len do
1329 begin
1330 case Text[a] of
1331 #10: // line feed
1332 begin
1333 TX := X;
1334 TY := TY + h;
1335 continue;
1336 end;
1337 #1: // black
1338 begin
1339 c.R := 0; c.G := 0; c.B := 0;
1340 continue;
1341 end;
1342 #2: // white
1343 begin
1344 c.R := 255; c.G := 255; c.B := 255;
1345 continue;
1346 end;
1347 #3: // darker
1348 begin
1349 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1350 continue;
1351 end;
1352 #4: // lighter
1353 begin
1354 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1355 continue;
1356 end;
1357 #18: // red
1358 begin
1359 c.R := 255; c.G := 0; c.B := 0;
1360 continue;
1361 end;
1362 #19: // green
1363 begin
1364 c.R := 0; c.G := 255; c.B := 0;
1365 continue;
1366 end;
1367 #20: // blue
1368 begin
1369 c.R := 0; c.G := 0; c.B := 255;
1370 continue;
1371 end;
1372 #21: // yellow
1373 begin
1374 c.R := 255; c.G := 255; c.B := 0;
1375 continue;
1376 end;
1377 end;
1379 with Chars[Ord(Text[a])] do
1380 if TextureID <> -1 then
1381 begin
1382 tc := e_Colors;
1383 e_Colors := c;
1384 e_Draw(TextureID, TX, TY, 0, True, False);
1385 e_Colors := tc;
1387 TX := TX+Width+IfThen(a = Length(Text), 0, Space);
1388 end;
1389 end;
1390 end;
1391 end;
1393 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
1394 var
1395 a: Integer;
1396 h2: Word;
1397 begin
1398 w := 0;
1399 h := 0;
1401 if Text = '' then Exit;
1402 if e_CharFonts = nil then Exit;
1403 if Integer(FontID) > High(e_CharFonts) then Exit;
1405 with e_CharFonts[FontID] do
1406 begin
1407 for a := 1 to Length(Text) do
1408 with Chars[Ord(Text[a])] do
1409 if TextureID <> -1 then
1410 begin
1411 w := w+Width+IfThen(a = Length(Text), 0, Space);
1412 e_GetTextureSize(TextureID, nil, @h2);
1413 if h2 > h then h := h2;
1414 end;
1415 end;
1416 end;
1418 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
1419 var
1420 a, lines, len: Integer;
1421 h2, w2, tw, th: Word;
1422 begin
1423 w2 := 0;
1424 h2 := 0;
1425 tw := 0;
1426 th := 0;
1428 if Text = '' then Exit;
1429 if e_CharFonts = nil then Exit;
1430 if Integer(FontID) > High(e_CharFonts) then Exit;
1432 lines := 1;
1433 len := Length(Text);
1435 with e_CharFonts[FontID] do
1436 begin
1437 for a := 1 to len do
1438 begin
1439 if Text[a] = #10 then
1440 begin
1441 Inc(lines);
1442 if w2 > tw then tw := w2;
1443 w2 := 0;
1444 continue;
1445 end;
1447 with Chars[Ord(Text[a])] do
1448 if TextureID <> -1 then
1449 begin
1450 w2 := w2 + Width + IfThen(a = len, 0, Space);
1451 e_GetTextureSize(TextureID, nil, @h2);
1452 if h2 > th then th := h2;
1453 end;
1454 end;
1455 end;
1457 if w2 > tw then
1458 tw := w2;
1460 w := tw;
1461 h := th * lines;
1462 end;
1464 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
1465 var
1466 a: Integer;
1467 begin
1468 Result := 0;
1470 if e_CharFonts = nil then Exit;
1471 if Integer(FontID) > High(e_CharFonts) then Exit;
1473 for a := 0 to High(e_CharFonts[FontID].Chars) do
1474 Result := Max(Result, e_CharFonts[FontID].Chars[a].Width);
1475 end;
1477 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
1478 var
1479 a: Integer;
1480 h2: Word;
1481 begin
1482 Result := 0;
1484 if e_CharFonts = nil then Exit;
1485 if Integer(FontID) > High(e_CharFonts) then Exit;
1487 for a := 0 to High(e_CharFonts[FontID].Chars) do
1488 begin
1489 if e_CharFonts[FontID].Chars[a].TextureID <> -1 then
1490 e_GetTextureSize(e_CharFonts[FontID].Chars[a].TextureID, nil, @h2)
1491 else h2 := 0;
1492 if h2 > Result then Result := h2;
1493 end;
1494 end;
1496 procedure e_CharFont_Remove(FontID: DWORD);
1497 var
1498 a: Integer;
1499 begin
1500 with e_CharFonts[FontID] do
1501 for a := 0 to High(Chars) do
1502 if Chars[a].TextureID <> -1 then e_DeleteTexture(Chars[a].TextureID);
1504 e_CharFonts[FontID].alive := False;
1505 end;
1507 procedure e_CharFont_RemoveAll();
1508 var
1509 a: Integer;
1510 begin
1511 if e_CharFonts = nil then Exit;
1513 for a := 0 to High(e_CharFonts) do
1514 e_CharFont_Remove(a);
1516 e_CharFonts := nil;
1517 end;
1519 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
1520 Space: ShortInt=0);
1521 var
1522 {$IFDEF NOGL_LISTS}
1523 loop1 : GLuint;
1524 cx, cy : real;
1525 {$ENDIF}
1526 i, id: DWORD;
1527 begin
1528 if e_NoGraphics then Exit;
1529 e_WriteLog('Creating texture font...', TMsgType.Notify);
1531 id := DWORD(-1);
1533 if e_TextureFonts <> nil then
1534 for i := 0 to High(e_TextureFonts) do
1535 if e_TextureFonts[i].Base = 0 then
1536 begin
1537 id := i;
1538 Break;
1539 end;
1541 if id = DWORD(-1) then
1542 begin
1543 SetLength(e_TextureFonts, Length(e_TextureFonts) + 1);
1544 id := High(e_TextureFonts);
1545 end;
1547 with e_TextureFonts[id] do
1548 begin
1549 {$IFDEF NOGL_LISTS}
1550 Base := glGenLists(XCount*YCount);
1551 {$ENDIF}
1552 TextureID := e_Textures[Tex].tx.id;
1553 CharWidth := (e_Textures[Tex].tx.Width div XCount)+Space;
1554 CharHeight := e_Textures[Tex].tx.Height div YCount;
1555 XC := XCount;
1556 YC := YCount;
1557 Texture := Tex;
1558 SPC := Space;
1559 end;
1561 {$IFDEF NOGL_LISTS}
1562 glBindTexture(GL_TEXTURE_2D, e_Textures[Tex].tx.id);
1563 for loop1 := 0 to XCount*YCount-1 do
1564 begin
1565 cx := (loop1 mod XCount)/XCount;
1566 cy := (loop1 div YCount)/YCount;
1568 glNewList(e_TextureFonts[id].Base+loop1, GL_COMPILE);
1569 glBegin(GL_QUADS);
1570 glTexCoord2f(cx, 1.0-cy-1/YCount);
1571 glVertex2i(0, e_Textures[Tex].tx.Height div YCount);
1573 glTexCoord2f(cx+1/XCount, 1.0-cy-1/YCount);
1574 glVertex2i(e_Textures[Tex].tx.Width div XCount, e_Textures[Tex].tx.Height div YCount);
1576 glTexCoord2f(cx+1/XCount, 1.0-cy);
1577 glVertex2i(e_Textures[Tex].tx.Width div XCount, 0);
1579 glTexCoord2f(cx, 1.0-cy);
1580 glVertex2i(0, 0);
1581 glEnd();
1582 glTranslated((e_Textures[Tex].tx.Width div XCount)+Space, 0, 0);
1583 glEndList();
1584 end;
1585 {$ENDIF}
1587 FontID := id;
1588 end;
1590 procedure e_TextureFontKill(FontID: DWORD);
1591 begin
1592 if e_NoGraphics then Exit;
1593 {$IFDEF NOGL_LISTS}
1594 glDeleteLists(e_TextureFonts[FontID].Base, 256);
1595 {$ENDIF}
1596 e_TextureFonts[FontID].Base := 0;
1597 end;
1599 {$IFNDEF NOGL_LISTS}
1600 procedure e_TextureFontDrawChar(ch: Char; FontID: DWORD);
1601 var
1602 index: Integer;
1603 cx, cy: GLfloat;
1604 Tex: Integer;
1605 Width, Height: Integer;
1606 XCount, YCount: Integer;
1607 begin
1608 index := Ord(ch) - 32;
1609 Tex := e_TextureFonts[FontID].Texture;
1610 Width := e_Textures[Tex].tx.Width;
1611 Height := e_Textures[Tex].tx.Height;
1612 XCount := e_TextureFonts[FontID].XC;
1613 YCount := e_TextureFonts[FontID].YC;
1614 cx := (index mod XCount)/XCount;
1615 cy := (index div YCount)/YCount;
1616 glBegin(GL_QUADS);
1617 glTexCoord2f(cx, 1 - cy - 1/YCount);
1618 glVertex2i(0, Height div YCount);
1619 glTexCoord2f(cx + 1/XCount, 1 - cy - 1/YCount);
1620 glVertex2i(Width div XCount, Height div YCount);
1621 glTexCoord2f(cx + 1/XCount, 1 - cy);
1622 glVertex2i(Width div XCount, 0);
1623 glTexCoord2f(cx, 1 - cy);
1624 glVertex2i(0, 0);
1625 glEnd();
1626 glTranslatef((e_Textures[Tex].tx.Width div XCount) + e_TextureFonts[FontID].SPC, 0, 0);
1627 end;
1629 procedure e_TextureFontDrawString(Text: String; FontID: DWORD);
1630 var
1631 i: Integer;
1632 begin
1633 for i := 1 to High(Text) do
1634 e_TextureFontDrawChar(Text[i], FontID);
1635 end;
1636 {$ENDIF}
1638 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
1639 begin
1640 if e_NoGraphics then Exit;
1641 if Integer(FontID) > High(e_TextureFonts) then Exit;
1642 if Text = '' then Exit;
1644 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1645 glEnable(GL_BLEND);
1647 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1649 glPushMatrix;
1650 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1651 glEnable(GL_TEXTURE_2D);
1652 glTranslatef(x, y, 0);
1653 {$IFDEF NOGL_LISTS}
1654 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1655 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1656 {$ELSE}
1657 e_TextureFontDrawString(Text, FontID);
1658 {$ENDIF}
1659 glDisable(GL_TEXTURE_2D);
1660 glPopMatrix;
1662 glDisable(GL_BLEND);
1663 end;
1665 // god forgive me for this, but i cannot figure out how to do it without lists
1666 procedure e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1667 begin
1668 if e_NoGraphics then Exit;
1669 glPushMatrix;
1671 if Shadow then
1672 begin
1673 glColor4ub(0, 0, 0, 128);
1674 glTranslatef(X+1, Y+1, 0);
1675 {$IFDEF NOGL_LISTS}
1676 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1677 {$ELSE}
1678 e_TextureFontDrawChar(Ch, FontID);
1679 {$ENDIF}
1680 glPopMatrix;
1681 glPushMatrix;
1682 end;
1684 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1685 glTranslatef(X, Y, 0);
1686 {$IFDEF NOGL_LISTS}
1687 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1688 {$ELSE}
1689 e_TextureFontDrawChar(Ch, FontID);
1690 {$ENDIF}
1692 glPopMatrix;
1693 end;
1695 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1696 begin
1697 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1698 glEnable(GL_TEXTURE_2D);
1699 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1701 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1702 glEnable(GL_BLEND);
1703 e_TextureFontPrintChar(X, Y, Ch, FontID, Shadow);
1704 glDisable(GL_TEXTURE_2D);
1705 glDisable(GL_BLEND);
1706 end;
1708 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
1709 begin
1710 result := e_TextureFonts[FontID].CharWidth;
1711 end;
1713 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD;
1714 Shadow: Boolean = False; Newlines: Boolean = False);
1715 var
1716 a, TX, TY, len: Integer;
1717 tc, c: TRGB;
1718 w, h: Word;
1719 begin
1720 if e_NoGraphics then Exit;
1721 if Text = '' then Exit;
1722 if e_TextureFonts = nil then Exit;
1723 if Integer(FontID) > High(e_TextureFonts) then Exit;
1725 c.R := 255;
1726 c.G := 255;
1727 c.B := 255;
1729 TX := X;
1730 TY := Y;
1731 len := Length(Text);
1733 w := e_TextureFonts[FontID].CharWidth;
1734 h := e_TextureFonts[FontID].CharHeight;
1736 with e_TextureFonts[FontID] do
1737 begin
1738 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1739 glEnable(GL_TEXTURE_2D);
1741 {$IFDEF NOGL_LISTS}
1742 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1743 {$ENDIF}
1745 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1746 glEnable(GL_BLEND);
1748 for a := 1 to len do
1749 begin
1750 case Text[a] of
1751 #10: // line feed
1752 begin
1753 if Newlines then
1754 begin
1755 TX := X;
1756 TY := TY + h;
1757 continue;
1758 end;
1759 end;
1760 #1: // black
1761 begin
1762 c.R := 0; c.G := 0; c.B := 0;
1763 continue;
1764 end;
1765 #2: // white
1766 begin
1767 c.R := 255; c.G := 255; c.B := 255;
1768 continue;
1769 end;
1770 #3: // darker
1771 begin
1772 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1773 continue;
1774 end;
1775 #4: // lighter
1776 begin
1777 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1778 continue;
1779 end;
1780 #18: // red
1781 begin
1782 c.R := 255; c.G := 0; c.B := 0;
1783 continue;
1784 end;
1785 #19: // green
1786 begin
1787 c.R := 0; c.G := 255; c.B := 0;
1788 continue;
1789 end;
1790 #20: // blue
1791 begin
1792 c.R := 0; c.G := 0; c.B := 255;
1793 continue;
1794 end;
1795 #21: // yellow
1796 begin
1797 c.R := 255; c.G := 255; c.B := 0;
1798 continue;
1799 end;
1800 end;
1802 tc := e_Colors;
1803 e_Colors := c;
1804 e_TextureFontPrintChar(TX, TY, Text[a], FontID, Shadow);
1805 e_Colors := tc;
1807 TX := TX+w;
1808 end;
1809 glDisable(GL_TEXTURE_2D);
1810 glDisable(GL_BLEND);
1811 end;
1812 end;
1814 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
1815 Blue: Byte; Scale: Single; Shadow: Boolean = False);
1816 begin
1817 if e_NoGraphics then Exit;
1818 if Text = '' then Exit;
1820 glPushMatrix;
1821 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1822 glEnable(GL_TEXTURE_2D);
1824 {$IFDEF NOGL_LISTS}
1825 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1826 {$ENDIF}
1828 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1829 glEnable(GL_BLEND);
1831 if Shadow then
1832 begin
1833 glColor4ub(0, 0, 0, 128);
1834 glTranslatef(x+1, y+1, 0);
1835 glScalef(Scale, Scale, 0);
1836 {$IFDEF NOGL_LISTS}
1837 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1838 {$ELSE}
1839 e_TextureFontDrawString(Text, FontID);
1840 {$ENDIF}
1841 glPopMatrix;
1842 glPushMatrix;
1843 end;
1845 glColor4ub(Red, Green, Blue, 255);
1846 glTranslatef(x, y, 0);
1847 glScalef(Scale, Scale, 0);
1848 {$IFDEF NOGL_LISTS}
1849 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1850 {$ELSE}
1851 e_TextureFontDrawString(Text, FontID);
1852 {$ENDIF}
1854 glDisable(GL_TEXTURE_2D);
1855 glPopMatrix;
1856 glColor3ub(e_Colors.R, e_Colors.G, e_Colors.B);
1857 glDisable(GL_BLEND);
1858 end;
1860 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
1861 begin
1862 CharWidth := 16;
1863 CharHeight := 16;
1864 if e_NoGraphics then Exit;
1865 if Integer(ID) > High(e_TextureFonts) then
1866 Exit;
1867 CharWidth := e_TextureFonts[ID].CharWidth;
1868 CharHeight := e_TextureFonts[ID].CharHeight;
1869 end;
1871 procedure e_RemoveAllTextureFont();
1872 var
1873 i: integer;
1874 begin
1875 if e_NoGraphics then Exit;
1876 if e_TextureFonts = nil then Exit;
1878 for i := 0 to High(e_TextureFonts) do
1879 if e_TextureFonts[i].Base <> 0 then
1880 begin
1881 {$IFDEF NOGL_LISTS}
1882 glDeleteLists(e_TextureFonts[i].Base, 256);
1883 {$ENDIF}
1884 e_TextureFonts[i].Base := 0;
1885 end;
1887 e_TextureFonts := nil;
1888 end;
1890 function _RGB(Red, Green, Blue: Byte): TRGB;
1891 begin
1892 Result.R := Red;
1893 Result.G := Green;
1894 Result.B := Blue;
1895 end;
1897 function _Point(X, Y: Integer): TPoint2i;
1898 begin
1899 Result.X := X;
1900 Result.Y := Y;
1901 end;
1903 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
1904 begin
1905 Result.X := X;
1906 Result.Y := Y;
1907 Result.Width := Width;
1908 Result.Height := Height;
1909 end;
1911 function _TRect(L, T, R, B: LongInt): TRect;
1912 begin
1913 Result.Top := T;
1914 Result.Left := L;
1915 Result.Right := R;
1916 Result.Bottom := B;
1917 end;
1920 procedure e_MakeScreenshot (st: TStream; Width, Height: Word);
1921 var
1922 pixels, obuf, scln, ps, pd: PByte;
1923 obufsize: Integer;
1924 dlen: Cardinal;
1925 i, x, y, res: Integer;
1926 sign: array [0..7] of Byte;
1927 hbuf: array [0..12] of Byte;
1928 crc: LongWord;
1929 img: TImageData;
1930 clr: TColor32Rec;
1931 begin
1932 if e_NoGraphics then Exit;
1933 obuf := nil;
1935 // first, extract and pack graphics data
1936 if (Width mod 4) > 0 then Width := Width+4-(Width mod 4);
1938 GetMem(pixels, Width*Height*3);
1939 try
1940 FillChar(pixels^, Width*Height*3, 0);
1941 glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
1942 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1944 if e_FastScreenshots then
1945 begin
1946 // create scanlines
1947 GetMem(scln, (Width*3+1)*Height);
1948 try
1949 ps := pixels;
1950 pd := scln;
1951 Inc(ps, (Width*3)*(Height-1));
1952 for i := 0 to Height-1 do
1953 begin
1954 pd^ := 0; // filter
1955 Inc(pd);
1956 Move(ps^, pd^, Width*3);
1957 Dec(ps, Width*3);
1958 Inc(pd, Width*3);
1959 end;
1960 except
1961 FreeMem(scln);
1962 raise;
1963 end;
1964 FreeMem(pixels);
1965 pixels := scln;
1967 // pack it
1968 obufsize := (Width*3+1)*Height*2;
1969 GetMem(obuf, obufsize);
1970 try
1971 while true do
1972 begin
1973 dlen := obufsize;
1974 res := compress2(Pointer(obuf), dlen, Pointer(pixels), (Width*3+1)*Height, 9);
1975 if res = Z_OK then break;
1976 if res <> Z_BUF_ERROR then raise Exception.Create('can''t pack data for PNG');
1977 obufsize := obufsize*2;
1978 FreeMem(obuf);
1979 obuf := nil;
1980 GetMem(obuf, obufsize);
1981 end;
1982 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1984 // now write PNG
1986 // signature
1987 sign[0] := 137;
1988 sign[1] := 80;
1989 sign[2] := 78;
1990 sign[3] := 71;
1991 sign[4] := 13;
1992 sign[5] := 10;
1993 sign[6] := 26;
1994 sign[7] := 10;
1995 st.writeBuffer(sign, 8);
1996 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1998 // header
1999 writeIntBE(st, LongWord(13));
2000 sign[0] := 73;
2001 sign[1] := 72;
2002 sign[2] := 68;
2003 sign[3] := 82;
2004 st.writeBuffer(sign, 4);
2005 crc := crc32(0, @sign[0], 4);
2006 hbuf[0] := 0;
2007 hbuf[1] := 0;
2008 hbuf[2] := (Width shr 8) and $ff;
2009 hbuf[3] := Width and $ff;
2010 hbuf[4] := 0;
2011 hbuf[5] := 0;
2012 hbuf[6] := (Height shr 8) and $ff;
2013 hbuf[7] := Height and $ff;
2014 hbuf[8] := 8; // bit depth
2015 hbuf[9] := 2; // RGB
2016 hbuf[10] := 0; // compression method
2017 hbuf[11] := 0; // filter method
2018 hbuf[12] := 0; // no interlace
2019 crc := crc32(crc, @hbuf[0], 13);
2020 st.writeBuffer(hbuf, 13);
2021 writeIntBE(st, crc);
2022 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2024 // image data
2025 writeIntBE(st, LongWord(dlen));
2026 sign[0] := 73;
2027 sign[1] := 68;
2028 sign[2] := 65;
2029 sign[3] := 84;
2030 st.writeBuffer(sign, 4);
2031 crc := crc32(0, @sign[0], 4);
2032 crc := crc32(crc, obuf, dlen);
2033 st.writeBuffer(obuf^, dlen);
2034 writeIntBE(st, crc);
2035 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2037 // image data end
2038 writeIntBE(st, LongWord(0));
2039 sign[0] := 73;
2040 sign[1] := 69;
2041 sign[2] := 78;
2042 sign[3] := 68;
2043 st.writeBuffer(sign, 4);
2044 crc := crc32(0, @sign[0], 4);
2045 writeIntBE(st, crc);
2046 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2047 finally
2048 if obuf <> nil then FreeMem(obuf);
2049 end;
2050 end
2051 else
2052 begin
2053 Imaging.SetOption(ImagingPNGCompressLevel, 9);
2054 Imaging.SetOption(ImagingPNGPreFilter, 6);
2055 InitImage(img);
2056 try
2057 NewImage(Width, Height, TImageFormat.ifR8G8B8, img);
2058 ps := pixels;
2059 //writeln(stderr, 'moving pixels...');
2060 for y := Height-1 downto 0 do
2061 begin
2062 for x := 0 to Width-1 do
2063 begin
2064 clr.r := ps^; Inc(ps);
2065 clr.g := ps^; Inc(ps);
2066 clr.b := ps^; Inc(ps);
2067 clr.a := 255;
2068 SetPixel32(img, x, y, clr);
2069 end;
2070 end;
2071 GlobalMetadata.ClearMetaItems();
2072 GlobalMetadata.ClearMetaItemsForSaving();
2073 //writeln(stderr, 'compressing image...');
2074 if not SaveImageToStream('png', st, img) then raise Exception.Create('screenshot writing error');
2075 //writeln(stderr, 'done!');
2076 finally
2077 FreeImage(img);
2078 end;
2079 end;
2080 finally
2081 FreeMem(pixels);
2082 end;
2083 end;
2086 end.