DEADSOFTWARE

Options: add third masterserver (by TerminalHash) in config.
[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 function e_ResizeFramebuffer(Width, Height: Integer): Boolean;
66 procedure e_BlitFramebuffer(WinWidth, WinHeight: Integer);
67 procedure e_SetRenderTarget(Framebuffer: Boolean);
69 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
70 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
71 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
72 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
73 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
74 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
75 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
76 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
78 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
79 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
81 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
82 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
84 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
86 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
87 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
88 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
89 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
90 Blending: TBlending = TBlending.None);
91 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
92 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
94 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
95 function e_CreateTexture(FileName: string; var ID: DWORD): Boolean;
96 function e_CreateTextureEx(FileName: string; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
97 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
98 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
99 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
100 procedure e_DeleteTexture(ID: DWORD);
101 procedure e_RemoveAllTextures();
103 // CharFont
104 function e_CharFont_Create(sp: ShortInt=0): DWORD;
105 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
106 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
107 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
108 Color: TRGB; Scale: Single = 1.0);
109 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
110 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
111 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
112 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
113 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
114 procedure e_CharFont_Remove(FontID: DWORD);
115 procedure e_CharFont_RemoveAll();
117 // TextureFont
118 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
119 Space: ShortInt=0);
120 procedure e_TextureFontKill(FontID: DWORD);
121 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
122 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
123 Blue: Byte; Scale: Single; Shadow: Boolean = False);
124 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD;
125 Shadow: Boolean = False; Newlines: Boolean = False);
126 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
127 procedure e_RemoveAllTextureFont();
129 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
130 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
132 procedure e_ReleaseEngine();
133 procedure e_BeginRender();
134 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
135 procedure e_Clear(); overload;
136 procedure e_EndRender();
138 {$IFDEF USE_SDL2}
139 function e_GetGamma(win: PSDL_Window): Byte;
140 procedure e_SetGamma(win: PSDL_Window;Gamma: Byte);
141 {$ENDIF}
143 procedure e_MakeScreenshot(st: TStream; Width, Height: Word);
145 function _RGB(Red, Green, Blue: Byte): TRGB;
146 function _Point(X, Y: Integer): TPoint2i;
147 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
148 function _TRect(L, T, R, B: LongInt): TRect;
150 //function e_getTextGLId (ID: DWORD): GLuint;
152 var
153 e_Colors: TRGB;
154 e_NoGraphics: Boolean = False;
155 e_FastScreenshots: Boolean = true; // it's REALLY SLOW with `false`
156 g_dbg_scale: Single = 1.0;
157 r_pixel_scale: Single = 1.0;
160 implementation
162 uses
163 paszlib, crc, utils;
166 type
167 TTexture = record
168 tx: GLTexture;
169 end;
171 TTextureFont = record
172 Texture: DWORD;
173 TextureID: DWORD;
174 Base: Uint32;
175 CharWidth: Byte;
176 CharHeight: Byte;
177 XC, YC: WORD;
178 SPC: ShortInt;
179 end;
181 TCharFont = record
182 Chars: array[0..255] of
183 record
184 TextureID: Integer;
185 Width: Byte;
186 end;
187 Space: ShortInt;
188 Height: ShortInt;
189 alive: Boolean;
190 end;
192 TSavedTexture = record
193 TexID: DWORD;
194 OldID: DWORD;
195 Pixels: Pointer;
196 end;
198 var
199 e_Textures: array of TTexture = nil;
200 e_TextureFonts: array of TTextureFont = nil;
201 e_CharFonts: array of TCharFont;
202 //e_SavedTextures: array of TSavedTexture;
203 e_FBO: GLuint = 0;
204 e_RBO: GLuint = 0;
205 e_RBOSupported: Boolean = True;
206 e_Frame: GLuint = 0;
207 e_FrameW: Integer = -1;
208 e_FrameH: Integer = -1;
210 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
212 //------------------------------------------------------------------
213 // Èíèöèàëèçèðóåò OpenGL
214 //------------------------------------------------------------------
215 procedure e_InitGL();
216 begin
217 if e_NoGraphics then
218 begin
219 e_DummyTextures := True;
220 Exit;
221 end;
222 e_Colors.R := 255;
223 e_Colors.G := 255;
224 e_Colors.B := 255;
225 glDisable(GL_DEPTH_TEST);
226 glEnable(GL_SCISSOR_TEST);
227 glClearColor(0, 0, 0, 0);
228 end;
230 procedure e_SetViewPort(X, Y, Width, Height: Word);
231 var
232 mat: Array [0..15] of GLDouble;
234 begin
235 if e_NoGraphics then Exit;
236 glLoadIdentity();
237 glScissor(X, Y, Width, Height);
238 glViewport(X, Y, Width, Height);
239 //gluOrtho2D(0, Width, Height, 0);
241 glMatrixMode(GL_PROJECTION);
243 mat[ 0] := 2.0 / Width;
244 mat[ 1] := 0.0;
245 mat[ 2] := 0.0;
246 mat[ 3] := 0.0;
248 mat[ 4] := 0.0;
249 mat[ 5] := -2.0 / Height;
250 mat[ 6] := 0.0;
251 mat[ 7] := 0.0;
253 mat[ 8] := 0.0;
254 mat[ 9] := 0.0;
255 mat[10] := 1.0;
256 mat[11] := 0.0;
258 mat[12] := -1.0;
259 mat[13] := 1.0;
260 mat[14] := 0.0;
261 mat[15] := 1.0;
263 glLoadMatrixd(@mat[0]);
265 glMatrixMode(GL_MODELVIEW);
266 glLoadIdentity();
267 end;
269 //------------------------------------------------------------------
270 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
271 //------------------------------------------------------------------
272 function FindTexture(): DWORD;
273 var
274 i: integer;
275 begin
276 if e_Textures <> nil then
277 for i := 0 to High(e_Textures) do
278 if e_Textures[i].tx.Width = 0 then
279 begin
280 Result := i;
281 Exit;
282 end;
284 if e_Textures = nil then
285 begin
286 SetLength(e_Textures, 32);
287 Result := 0;
288 end
289 else
290 begin
291 Result := High(e_Textures) + 1;
292 SetLength(e_Textures, Length(e_Textures) + 32);
293 end;
294 end;
296 //------------------------------------------------------------------
297 // Ñîçäàåò òåêñòóðó
298 //------------------------------------------------------------------
299 function e_CreateTexture(FileName: String; var ID: DWORD): Boolean;
300 var
301 find_id: DWORD;
302 fmt: Word;
303 begin
304 Result := False;
306 e_WriteLog('Loading texture from '+FileName, TMsgType.Notify);
308 find_id := FindTexture();
310 if not LoadTexture(FileName, e_Textures[find_id].tx, e_Textures[find_id].tx.Width,
311 e_Textures[find_id].tx.Height, @fmt) then Exit;
313 ID := find_id;
315 Result := True;
316 end;
318 function e_CreateTextureEx(FileName: String; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
319 var
320 find_id: DWORD;
321 fmt: Word;
322 begin
323 Result := False;
325 find_id := FindTexture();
327 if not LoadTextureEx(FileName, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
329 ID := find_id;
331 Result := True;
332 end;
334 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
335 var
336 find_id: DWORD;
337 fmt: Word;
338 begin
339 Result := False;
341 find_id := FindTexture;
343 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;
345 id := find_id;
347 Result := True;
348 end;
350 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
351 var
352 find_id: DWORD;
353 fmt: Word;
354 begin
355 Result := False;
357 find_id := FindTexture();
359 if not LoadTextureMemEx(pData, dataSize, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
361 ID := find_id;
363 Result := True;
364 end;
366 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
367 var
368 find_id: DWORD;
369 fmt, tw, th: Word;
370 begin
371 result := false;
372 find_id := FindTexture();
373 if not LoadTextureImg(img, e_Textures[find_id].tx, tw, th, @fmt) then exit;
374 ID := find_id;
375 result := True;
376 end;
378 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
379 begin
380 if Width <> nil then Width^ := e_Textures[ID].tx.Width;
381 if Height <> nil then Height^ := e_Textures[ID].tx.Height;
382 end;
384 procedure DestroyFramebuffer;
385 begin
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;
407 end;
409 function e_ResizeFramebuffer(Width, Height: Integer): Boolean;
410 begin
411 Result := False;
413 if e_NoGraphics then Exit;
415 DestroyFramebuffer;
417 e_FrameW := Width;
418 e_FrameH := Height;
420 glGetError();
422 glGenFramebuffers(1, @e_FBO);
424 if glGetError() <> GL_NO_ERROR then
425 begin
426 e_LogWriteln('GL: glGenFramebuffers failed');
427 Exit;
428 end;
430 glGenTextures(1, @e_Frame);
431 glBindTexture(GL_TEXTURE_2D, e_Frame);
432 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, nil);
433 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
434 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
436 if glGetError() <> GL_NO_ERROR then
437 begin
438 e_LogWriteln('GL: can''t create FBO color buffer');
439 DestroyFramebuffer;
440 Exit;
441 end;
443 glBindFramebuffer(GL_FRAMEBUFFER, e_FBO);
444 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, e_Frame, 0);
445 if glCheckFramebufferStatus(GL_FRAMEBUFFER) <> GL_FRAMEBUFFER_COMPLETE then
446 begin
447 e_LogWriteln('GL: can''t construct framebuffer with color attachment');
448 DestroyFramebuffer;
449 Exit;
450 end;
452 {$IFNDEF USE_GLES1}
453 if e_RBOSupported then
454 begin
455 glGenRenderbuffers(1, @e_RBO);
456 glBindRenderbuffer(GL_RENDERBUFFER, e_RBO);
457 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, Width, Height);
458 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, e_RBO);
459 if glCheckFramebufferStatus(GL_FRAMEBUFFER) <> GL_FRAMEBUFFER_COMPLETE then
460 begin
461 e_LogWriteln('GL: can''t construct framebuffer with depth+stencil attachment, trying without');
462 e_RBOSupported := False;
463 Result := e_ResizeFramebuffer(Width, Height);
464 Exit;
465 end;
466 end;
467 {$ENDIF}
469 Result := True;
470 end;
472 procedure e_ResizeWindow(Width, Height: Integer);
473 begin
474 if Height = 0 then
475 Height := 1;
476 e_SetViewPort(0, 0, Width, Height);
477 end;
479 procedure drawTxQuad (x0, y0, w, h, tw, th: Integer; u, v: single; Mirror: TMirrorType);
480 var
481 x1, y1, tmp: Integer;
482 begin
483 if (w < 1) or (h < 1) then exit;
484 x1 := x0+w;
485 y1 := y0+h;
486 if Mirror = TMirrorType.Horizontal then begin tmp := x1; x1 := x0; x0 := tmp; end
487 else if Mirror = TMirrorType.Vertical then begin tmp := y1; y1 := y0; y0 := tmp; end;
488 glTexCoord2f(0, v); glVertex2i(x0, y0);
489 glTexCoord2f(0, 0); glVertex2i(x0, y1);
490 glTexCoord2f(u, 0); glVertex2i(x1, y1);
491 glTexCoord2f(u, v); glVertex2i(x1, y0);
492 end;
494 procedure e_SetRenderTarget(Framebuffer: Boolean);
495 begin
496 if (e_FBO = 0) or e_NoGraphics then exit;
497 if Framebuffer then
498 glBindFramebuffer(GL_FRAMEBUFFER, e_FBO)
499 else
500 glBindFramebuffer(GL_FRAMEBUFFER, 0);
501 end;
503 procedure e_BlitFramebuffer(WinWidth, WinHeight: Integer);
504 begin
505 if (e_FBO = 0) or (e_Frame = 0) or e_NoGraphics then exit;
507 glDisable(GL_BLEND);
508 glEnable(GL_TEXTURE_2D);
509 glBindTexture(GL_TEXTURE_2D, e_Frame);
510 glColor4ub(255, 255, 255, 255);
512 glBegin(GL_QUADS);
513 glTexCoord2f(0, 1); glVertex2i( 0, 0);
514 glTexCoord2f(0, 0); glVertex2i( 0, WinHeight);
515 glTexCoord2f(1, 0); glVertex2i(WinWidth, WinHeight);
516 glTexCoord2f(1, 1); glVertex2i(WinWidth, 0);
517 glEnd();
518 end;
520 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
521 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
522 begin
523 if e_NoGraphics then Exit;
524 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
526 if (Alpha > 0) or (AlphaChannel) or (Blending) then
527 glEnable(GL_BLEND)
528 else
529 glDisable(GL_BLEND);
531 if (AlphaChannel) or (Alpha > 0) then
532 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
534 if Alpha > 0 then
535 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
537 if Blending then
538 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
540 glEnable(GL_TEXTURE_2D);
541 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
542 glBegin(GL_QUADS);
544 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);
546 //u := e_Textures[ID].tx.u;
547 //v := e_Textures[ID].tx.v;
550 if Mirror = M_NONE then
551 begin
552 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
553 glTexCoord2f(0, 0); glVertex2i(X, Y);
554 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
555 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
556 end
557 else
558 if Mirror = M_HORIZONTAL then
559 begin
560 glTexCoord2f(u, 0); glVertex2i(X, Y);
561 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
562 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
563 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
564 end
565 else
566 if Mirror = M_VERTICAL then
567 begin
568 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
569 glTexCoord2f(0, -v); glVertex2i(X, Y);
570 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
571 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
572 end;
575 glEnd();
577 glDisable(GL_BLEND);
578 end;
580 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
581 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
582 var
583 u, v: Single;
584 begin
585 if e_NoGraphics then Exit;
586 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
588 if (Alpha > 0) or (AlphaChannel) or (Blending) then
589 glEnable(GL_BLEND)
590 else
591 glDisable(GL_BLEND);
593 if (AlphaChannel) or (Alpha > 0) then
594 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
596 if Alpha > 0 then
597 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
599 if Blending then
600 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
602 glEnable(GL_TEXTURE_2D);
603 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
605 u := e_Textures[ID].tx.u;
606 v := e_Textures[ID].tx.v;
608 glBegin(GL_QUADS);
609 glTexCoord2f(0, v); glVertex2i(X, Y);
610 glTexCoord2f(u, v); glVertex2i(X + Width, Y);
611 glTexCoord2f(u, 0); glVertex2i(X + Width, Y + Height);
612 glTexCoord2f(0, 0); glVertex2i(X, Y + Height);
613 glEnd();
615 glDisable(GL_BLEND);
616 end;
618 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
619 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
620 begin
621 if e_NoGraphics then Exit;
622 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
624 if (Alpha > 0) or (AlphaChannel) or (Blending) then
625 glEnable(GL_BLEND)
626 else
627 glDisable(GL_BLEND);
629 if (AlphaChannel) or (Alpha > 0) then
630 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
632 if Alpha > 0 then
633 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
635 if Blending then
636 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
638 glEnable(GL_TEXTURE_2D);
639 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
640 glBegin(GL_QUADS);
641 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);
642 glEnd();
644 glDisable(GL_BLEND);
645 end;
647 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
648 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
649 var
650 X2, Y2, dx, w, h: Integer;
651 u, v: Single;
652 begin
653 if e_NoGraphics then Exit;
654 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
655 ambientBlendMode := false;
657 if (Alpha > 0) or AlphaChannel or Blending then
658 begin
659 glEnable(GL_BLEND);
660 end
661 else
662 begin
663 if not ambientBlendMode then glDisable(GL_BLEND);
664 end;
665 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
666 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
667 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
669 if (XCount = 0) then XCount := 1;
670 if (YCount = 0) then YCount := 1;
672 glEnable(GL_TEXTURE_2D);
673 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
675 X2 := X+e_Textures[ID].tx.width*XCount;
676 Y2 := Y+e_Textures[ID].tx.height*YCount;
678 //k8: this SHOULD work... i hope
679 if (e_Textures[ID].tx.width = e_Textures[ID].tx.glwidth) and (e_Textures[ID].tx.height = e_Textures[ID].tx.glheight) then
680 begin
681 glBegin(GL_QUADS);
682 glTexCoord2i(0, YCount); glVertex2i(X, Y);
683 glTexCoord2i(XCount, YCount); glVertex2i(X2, Y);
684 glTexCoord2i(XCount, 0); glVertex2i(X2, Y2);
685 glTexCoord2i(0, 0); glVertex2i(X, Y2);
686 glEnd();
687 end
688 else
689 begin
690 glBegin(GL_QUADS);
691 // hard day's night
692 u := e_Textures[ID].tx.u;
693 v := e_Textures[ID].tx.v;
694 w := e_Textures[ID].tx.width;
695 h := e_Textures[ID].tx.height;
696 while YCount > 0 do
697 begin
698 dx := XCount;
699 x2 := X;
700 while dx > 0 do
701 begin
702 glTexCoord2f(0, v); glVertex2i(X, Y);
703 glTexCoord2f(u, v); glVertex2i(X+w, Y);
704 glTexCoord2f(u, 0); glVertex2i(X+w, Y+h);
705 glTexCoord2f(0, 0); glVertex2i(X, Y+h);
706 Inc(X, w);
707 Dec(dx);
708 end;
709 X := x2;
710 Inc(Y, h);
711 Dec(YCount);
712 end;
713 glEnd();
714 end;
716 glDisable(GL_BLEND);
717 end;
720 //TODO: overflow checks
721 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
722 var
723 ex0, ey0: Integer;
724 begin
725 result := false;
726 if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit;
727 // check for intersection
728 if (x0+w0 <= x1) or (y0+h0 <= y1) or (x1+w1 <= x0) or (y1+h1 <= y0) then exit;
729 if (x0 >= x1+w1) or (y0 >= y1+h1) or (x1 >= x0+h0) or (y1 >= y0+h0) then exit;
730 // ok, intersects
731 ex0 := x0+w0;
732 ey0 := y0+h0;
733 if (x0 < x1) then x0 := x1;
734 if (y0 < y1) then y0 := y1;
735 if (ex0 > x1+w1) then ex0 := x1+w1;
736 if (ey0 > y1+h1) then ey0 := y1+h1;
737 w0 := ex0-x0;
738 h0 := ey0-y0;
739 result := (w0 > 0) and (h0 > 0);
740 end;
743 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
744 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
745 var
746 x2, y2: Integer;
748 wassc: Boolean;
749 scxywh: array[0..3] of GLint;
750 vpxywh: array[0..3] of GLint;
752 w, h, dw, cw, ch, yofs: Integer;
753 u, v, cu, cv: Single;
754 onlyOneY: Boolean;
757 procedure setScissorGLInternal (x, y, w, h: Integer);
758 begin
759 //if not scallowed then exit;
760 x := trunc(x*scale);
761 y := trunc(y*scale);
762 w := trunc(w*scale);
763 h := trunc(h*scale);
764 y := vpxywh[3]-(y+h);
765 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
766 begin
767 glScissor(0, 0, 0, 0);
768 end
769 else
770 begin
771 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
772 glScissor(x, y, w, h);
773 end;
774 end;
777 begin
778 if e_NoGraphics then exit;
779 ambientBlendMode := false;
781 if (wdt < 1) or (hgt < 1) then exit;
783 if (wdt mod e_Textures[ID].tx.width = 0) and (hgt mod e_Textures[ID].tx.height = 0) then
784 begin
785 e_DrawFill(id, x, y, wdt div e_Textures[ID].tx.width, hgt div e_Textures[ID].tx.height, alpha, alphachannel, blending, ambientBlendMode);
786 exit;
787 end;
789 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
791 if (Alpha > 0) or AlphaChannel or Blending then
792 begin
793 glEnable(GL_BLEND);
794 end
795 else
796 begin
797 if not ambientBlendMode then glDisable(GL_BLEND);
798 end;
799 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
800 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
801 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
803 glEnable(GL_TEXTURE_2D);
804 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
806 x2 := x+wdt;
807 y2 := y+hgt;
809 //k8: this SHOULD work... i hope
810 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
811 begin
812 glBegin(GL_QUADS);
813 glTexCoord2f(0, hgt/e_Textures[ID].tx.height); glVertex2i(x, y);
814 glTexCoord2f(wdt/e_Textures[ID].tx.width, hgt/e_Textures[ID].tx.height); glVertex2i(x2, y);
815 glTexCoord2f(wdt/e_Textures[ID].tx.width, 0); glVertex2i(x2, y2);
816 glTexCoord2f(0, 0); glVertex2i(x, y2);
817 glEnd();
818 end
819 else
820 begin
821 // hard day's night; setup scissor
823 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
824 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
825 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
826 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
827 //glEnable(GL_SCISSOR_TEST);
828 setScissorGLInternal(x, y, wdt, hgt);
830 // draw quads
831 u := e_Textures[ID].tx.u;
832 v := e_Textures[ID].tx.v;
833 w := e_Textures[ID].tx.width;
834 h := e_Textures[ID].tx.height;
835 x2 := x;
836 if (hgt > h) then begin y += hgt-h; onlyOneY := false; end else onlyOneY := true;
837 glBegin(GL_QUADS);
838 while (hgt > 0) do
839 begin
840 if (hgt >= h) then begin ch := h; cv := v; yofs := 0; end else begin ch := hgt; cv := v/(h/hgt); yofs := h-hgt; end;
841 if onlyOneY then yofs := 0;
842 Dec(hgt, h);
843 dw := wdt;
844 x := x2;
845 while (dw > 0) do
846 begin
847 if (dw >= w) then begin cw := w; cu := u; end else begin cw := dw; cu := u/(w/dw); end;
848 Dec(dw, w);
849 glTexCoord2f(0, cv); glVertex2i(X, Y+yofs);
850 glTexCoord2f(cu, cv); glVertex2i(X+cw, Y+yofs);
851 glTexCoord2f(cu, 0); glVertex2i(X+cw, Y+ch+yofs);
852 glTexCoord2f(0, 0); glVertex2i(X, Y+ch+yofs);
853 Inc(X, w);
854 end;
855 Dec(Y, h);
856 end;
857 glEnd();
858 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
859 end;
861 glDisable(GL_BLEND);
862 end;
865 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
866 begin
867 if e_NoGraphics then exit;
868 if (w < 1) or (h < 1) then exit;
869 if (a <> 255) or ((r or g or b) <> 0) then
870 begin
871 glEnable(GL_BLEND);
872 glDisable(GL_TEXTURE_2D);
873 glColor4ub(r, g, b, a);
874 if ((r or g or b) <> 0) then
875 begin
876 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
877 glBegin(GL_QUADS);
878 glVertex2i(x, y);
879 glVertex2i(x+w, y);
880 glVertex2i(x+w, y+h);
881 glVertex2i(x, y+h);
882 glEnd();
883 end;
884 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
885 glBegin(GL_QUADS);
886 glVertex2i(x, y);
887 glVertex2i(x+w, y);
888 glVertex2i(x+w, y+h);
889 glVertex2i(x, y+h);
890 glEnd();
891 glDisable(GL_BLEND);
892 end;
893 end;
896 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
897 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
898 begin
899 if e_NoGraphics then Exit;
901 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
903 if (Alpha > 0) or (AlphaChannel) or (Blending) then
904 glEnable(GL_BLEND)
905 else
906 glDisable(GL_BLEND);
908 if (AlphaChannel) or (Alpha > 0) then
909 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
911 if Alpha > 0 then
912 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
914 if Blending then
915 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
917 if (Angle <> 0) and (RC <> nil) then
918 begin
919 glPushMatrix();
920 glTranslatef(X+RC.X, Y+RC.Y, 0);
921 glRotatef(Angle, 0, 0, 1);
922 glTranslatef(-(X+RC.X), -(Y+RC.Y), 0);
923 end;
925 glEnable(GL_TEXTURE_2D);
926 glBindTexture(GL_TEXTURE_2D, e_Textures[id].tx.id);
927 glBegin(GL_QUADS); //0-1 1-1
928 //00 10
929 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);
930 glEnd();
932 if Angle <> 0 then
933 glPopMatrix();
935 glDisable(GL_BLEND);
936 end;
938 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
939 begin
940 if e_NoGraphics then Exit;
941 glDisable(GL_TEXTURE_2D);
942 glColor3ub(Red, Green, Blue);
943 glPointSize(Size);
945 if (Size = 2) or (Size = 4) then
946 X := X + 1;
948 glBegin(GL_POINTS);
949 glVertex2f(X+0.3, Y+1.0);
950 glEnd();
952 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
953 end;
955 procedure e_LineCorrection(var X1, Y1, X2, Y2: Integer);
956 begin
957 // Make lines only top-left/bottom-right and top-right/bottom-left
958 if Y2 < Y1 then
959 begin
960 X1 := X1 xor X2;
961 X2 := X1 xor X2;
962 X1 := X1 xor X2;
964 Y1 := Y1 xor Y2;
965 Y2 := Y1 xor Y2;
966 Y1 := Y1 xor Y2;
967 end;
969 // Pixel-perfect hack
970 if X1 < X2 then
971 Inc(X2)
972 else
973 Inc(X1);
974 Inc(Y2);
975 end;
977 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
978 var
979 nX1, nY1, nX2, nY2: Integer;
980 begin
981 if e_NoGraphics then Exit;
982 // Only top-left/bottom-right quad
983 if X1 > X2 then
984 begin
985 X1 := X1 xor X2;
986 X2 := X1 xor X2;
987 X1 := X1 xor X2;
988 end;
989 if Y1 > Y2 then
990 begin
991 Y1 := Y1 xor Y2;
992 Y2 := Y1 xor Y2;
993 Y1 := Y1 xor Y2;
994 end;
996 if Alpha > 0 then
997 begin
998 glEnable(GL_BLEND);
999 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1000 end
1001 else
1002 glDisable(GL_BLEND);
1004 glDisable(GL_TEXTURE_2D);
1005 glColor4ub(Red, Green, Blue, 255-Alpha);
1006 glLineWidth(1);
1007 glBegin(GL_LINES);
1008 nX1 := X1; nY1 := Y1;
1009 nX2 := X2; nY2 := Y1;
1010 e_LineCorrection(nX1, nY1, nX2, nY2); // Pixel-perfect lines
1011 glVertex2i(nX1, nY1);
1012 glVertex2i(nX2, nY2);
1014 nX1 := X2; nY1 := Y1;
1015 nX2 := X2; nY2 := Y2;
1016 e_LineCorrection(nX1, nY1, nX2, nY2);
1017 glVertex2i(nX1, nY1);
1018 glVertex2i(nX2, nY2);
1020 nX1 := X2; nY1 := Y2;
1021 nX2 := X1; nY2 := Y2;
1022 e_LineCorrection(nX1, nY1, nX2, nY2);
1023 glVertex2i(nX1, nY1);
1024 glVertex2i(nX2, nY2);
1026 nX1 := X1; nY1 := Y2;
1027 nX2 := X1; nY2 := Y1;
1028 e_LineCorrection(nX1, nY1, nX2, nY2);
1029 glVertex2i(nX1, nY1);
1030 glVertex2i(nX2, nY2);
1031 glEnd();
1032 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1033 glDisable(GL_BLEND);
1034 end;
1036 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
1037 Blending: TBlending = TBlending.None);
1038 begin
1039 if e_NoGraphics then Exit;
1040 if (Alpha > 0) or (Blending <> TBlending.None) then
1041 glEnable(GL_BLEND)
1042 else
1043 glDisable(GL_BLEND);
1045 case Blending of
1046 TBlending.None: if Alpha > 0 then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1047 TBlending.Blend: glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1048 TBlending.Invert: glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1049 TBlending.Filter: glBlendFunc(GL_ZERO, GL_SRC_COLOR);
1050 end;
1052 glDisable(GL_TEXTURE_2D);
1053 glColor4ub(Red, Green, Blue, 255-Alpha);
1055 X2 := X2 + 1;
1056 Y2 := Y2 + 1;
1058 glBegin(GL_QUADS);
1059 glVertex2i(X1, Y1);
1060 glVertex2i(X2, Y1);
1061 glVertex2i(X2, Y2);
1062 glVertex2i(X1, Y2);
1063 glEnd();
1065 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1066 glDisable(GL_BLEND);
1067 end;
1070 // ////////////////////////////////////////////////////////////////////////// //
1071 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
1072 begin
1073 if (a < 0) then a := 0;
1074 if (a > 255) then a := 255;
1075 glEnable(GL_BLEND);
1076 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
1077 glDisable(GL_TEXTURE_2D);
1078 glColor4ub(0, 0, 0, Byte(255-a));
1079 glBegin(GL_QUADS);
1080 glVertex2i(x0, y0);
1081 glVertex2i(x1, y0);
1082 glVertex2i(x1, y1);
1083 glVertex2i(x0, y1);
1084 glEnd();
1085 //glRect(x, y, x+w, y+h);
1086 glColor4ub(1, 1, 1, 1);
1087 glDisable(GL_BLEND);
1088 //glBlendEquation(GL_FUNC_ADD);
1089 end;
1091 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
1092 begin
1093 if (w > 0) and (h > 0) then e_DarkenQuad(x, y, x+w, y+h, a);
1094 end;
1097 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
1098 begin
1099 if e_NoGraphics then Exit;
1100 // Pixel-perfect lines
1101 if Width = 1 then
1102 e_LineCorrection(X1, Y1, X2, Y2);
1104 if Alpha > 0 then
1105 begin
1106 glEnable(GL_BLEND);
1107 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1108 end else
1109 glDisable(GL_BLEND);
1111 glDisable(GL_TEXTURE_2D);
1112 glColor4ub(Red, Green, Blue, 255-Alpha);
1113 glLineWidth(Width);
1114 glBegin(GL_LINES);
1115 glVertex2i(X1, Y1);
1116 glVertex2i(X2, Y2);
1117 glEnd();
1118 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1120 glDisable(GL_BLEND);
1121 end;
1123 //------------------------------------------------------------------
1124 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1125 //------------------------------------------------------------------
1126 procedure e_DeleteTexture(ID: DWORD);
1127 begin
1128 if not e_NoGraphics then
1129 glDeleteTextures(1, @e_Textures[ID].tx.id);
1130 e_Textures[ID].tx.id := 0;
1131 e_Textures[ID].tx.Width := 0;
1132 e_Textures[ID].tx.Height := 0;
1133 end;
1135 //------------------------------------------------------------------
1136 // Óäàëÿåò âñå òåêñòóðû
1137 //------------------------------------------------------------------
1138 procedure e_RemoveAllTextures();
1139 var
1140 i: integer;
1141 begin
1142 if e_Textures = nil then Exit;
1144 for i := 0 to High(e_Textures) do
1145 if e_Textures[i].tx.Width <> 0 then e_DeleteTexture(i);
1146 e_Textures := nil;
1147 end;
1149 //------------------------------------------------------------------
1150 // Óäàëÿåò äâèæîê
1151 //------------------------------------------------------------------
1152 procedure e_ReleaseEngine();
1153 begin
1154 e_RemoveAllTextures;
1155 e_RemoveAllTextureFont;
1156 end;
1158 procedure e_BeginRender();
1159 begin
1160 if e_NoGraphics then Exit;
1161 glEnable(GL_ALPHA_TEST);
1162 glAlphaFunc(GL_GREATER, 0.0);
1163 end;
1165 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
1166 begin
1167 if e_NoGraphics then Exit;
1168 glClearColor(Red, Green, Blue, 0);
1169 glClear(Mask);
1170 end;
1172 procedure e_Clear(); overload;
1173 begin
1174 if e_NoGraphics then Exit;
1175 glClearColor(0, 0, 0, 0);
1176 glClear(GL_COLOR_BUFFER_BIT);
1177 end;
1179 procedure e_EndRender();
1180 begin
1181 if e_NoGraphics then Exit;
1182 glPopMatrix();
1183 end;
1185 {$IFDEF USE_SDL2}
1186 function e_GetGamma(win: PSDL_Window): Byte;
1187 var
1188 ramp: array [0..256*3-1] of Word;
1189 rgb: array [0..2] of Double;
1190 sum: double;
1191 count: integer;
1192 min: integer;
1193 max: integer;
1194 A, B: double;
1195 i, j: integer;
1196 begin
1197 Result := 0;
1198 if e_NoGraphics then Exit;
1199 rgb[0] := 1.0;
1200 rgb[1] := 1.0;
1201 rgb[2] := 1.0;
1203 SDL_GetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1205 for i := 0 to 2 do
1206 begin
1207 sum := 0;
1208 count := 0;
1209 min := 256 * i;
1210 max := min + 256;
1212 for j := min to max - 1 do
1213 if ramp[j] > 0 then
1214 begin
1215 B := (j mod 256)/256;
1216 A := ramp[j]/65536;
1217 sum := sum + ln(A)/ln(B);
1218 inc(count);
1219 end;
1220 rgb[i] := sum / count;
1221 end;
1223 Result := 100 - Trunc(((rgb[0] + rgb[1] + rgb[2])/3 - 0.23) * 100/(2.7 - 0.23));
1224 end;
1226 procedure e_SetGamma(win: PSDL_Window; Gamma: Byte);
1227 var
1228 ramp: array [0..256*3-1] of Word;
1229 i: integer;
1230 r: double;
1231 g: double;
1232 begin
1233 if e_NoGraphics then Exit;
1234 g := (100 - Gamma)*(2.7 - 0.23)/100 + 0.23;
1236 for i := 0 to 255 do
1237 begin
1238 r := Exp(g * ln(i/256))*65536;
1239 if r < 0 then r := 0
1240 else if r > 65535 then r := 65535;
1241 ramp[i] := trunc(r);
1242 ramp[i + 256] := trunc(r);
1243 ramp[i + 512] := trunc(r);
1244 end;
1246 SDL_SetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1247 end;
1248 {$ENDIF}
1250 function e_CharFont_Create(sp: ShortInt=0): DWORD;
1251 var
1252 i, id: DWORD;
1253 begin
1254 e_WriteLog('Creating CharFont...', TMsgType.Notify);
1256 id := DWORD(-1);
1258 if e_CharFonts <> nil then
1259 for i := 0 to High(e_CharFonts) do
1260 if not e_CharFonts[i].alive then
1261 begin
1262 id := i;
1263 Break;
1264 end;
1266 if id = DWORD(-1) then
1267 begin
1268 SetLength(e_CharFonts, Length(e_CharFonts) + 1);
1269 id := High(e_CharFonts);
1270 end;
1272 with e_CharFonts[id] do
1273 begin
1274 for i := 0 to High(Chars) do
1275 with Chars[i] do
1276 begin
1277 TextureID := -1;
1278 Width := 0;
1279 end;
1281 Space := sp;
1282 alive := True;
1283 end;
1285 Result := id;
1286 end;
1288 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
1289 begin
1290 with e_CharFonts[FontID].Chars[Ord(c)] do
1291 begin
1292 TextureID := Texture;
1293 Width := w;
1294 end;
1295 end;
1297 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
1298 var
1299 a: Integer;
1300 begin
1301 if e_NoGraphics then Exit;
1302 if Text = '' then Exit;
1303 if e_CharFonts = nil then Exit;
1304 if Integer(FontID) > High(e_CharFonts) then Exit;
1306 with e_CharFonts[FontID] do
1307 begin
1308 for a := 1 to Length(Text) do
1309 with Chars[Ord(Text[a])] do
1310 if TextureID <> -1 then
1311 begin
1312 e_Draw(TextureID, X, Y, 0, True, False);
1313 X := X+Width+IfThen(a = Length(Text), 0, Space);
1314 end;
1315 end;
1316 end;
1318 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
1319 Color: TRGB; Scale: Single = 1.0);
1320 var
1321 a: Integer;
1322 c: TRGB;
1323 begin
1324 if e_NoGraphics then Exit;
1325 if Text = '' then Exit;
1326 if e_CharFonts = nil then Exit;
1327 if Integer(FontID) > High(e_CharFonts) then Exit;
1329 with e_CharFonts[FontID] do
1330 begin
1331 for a := 1 to Length(Text) do
1332 with Chars[Ord(Text[a])] do
1333 if TextureID <> -1 then
1334 begin
1335 if Scale <> 1.0 then
1336 begin
1337 glPushMatrix;
1338 glScalef(Scale, Scale, 0);
1339 end;
1341 c := e_Colors;
1342 e_Colors := Color;
1343 e_Draw(TextureID, X, Y, 0, True, False);
1344 e_Colors := c;
1346 if Scale <> 1.0 then glPopMatrix;
1348 X := X+Width+IfThen(a = Length(Text), 0, Space);
1349 end;
1350 end;
1351 end;
1353 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
1354 var
1355 a, TX, TY, len: Integer;
1356 tc, c: TRGB;
1357 w, h: Word;
1358 begin
1359 if e_NoGraphics then Exit;
1360 if Text = '' then Exit;
1361 if e_CharFonts = nil then Exit;
1362 if Integer(FontID) > High(e_CharFonts) then Exit;
1364 c.R := 255;
1365 c.G := 255;
1366 c.B := 255;
1368 TX := X;
1369 TY := Y;
1370 len := Length(Text);
1372 e_CharFont_GetSize(FontID, 'A', w, h);
1374 with e_CharFonts[FontID] do
1375 begin
1376 for a := 1 to len do
1377 begin
1378 case Text[a] of
1379 #10: // line feed
1380 begin
1381 TX := X;
1382 TY := TY + h;
1383 continue;
1384 end;
1385 #1: // black
1386 begin
1387 c.R := 0; c.G := 0; c.B := 0;
1388 continue;
1389 end;
1390 #2: // white
1391 begin
1392 c.R := 255; c.G := 255; c.B := 255;
1393 continue;
1394 end;
1395 #3: // darker
1396 begin
1397 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1398 continue;
1399 end;
1400 #4: // lighter
1401 begin
1402 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1403 continue;
1404 end;
1405 #18: // red
1406 begin
1407 c.R := 255; c.G := 0; c.B := 0;
1408 continue;
1409 end;
1410 #19: // green
1411 begin
1412 c.R := 0; c.G := 255; c.B := 0;
1413 continue;
1414 end;
1415 #20: // blue
1416 begin
1417 c.R := 0; c.G := 0; c.B := 255;
1418 continue;
1419 end;
1420 #21: // yellow
1421 begin
1422 c.R := 255; c.G := 255; c.B := 0;
1423 continue;
1424 end;
1425 end;
1427 with Chars[Ord(Text[a])] do
1428 if TextureID <> -1 then
1429 begin
1430 tc := e_Colors;
1431 e_Colors := c;
1432 e_Draw(TextureID, TX, TY, 0, True, False);
1433 e_Colors := tc;
1435 TX := TX+Width+IfThen(a = Length(Text), 0, Space);
1436 end;
1437 end;
1438 end;
1439 end;
1441 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
1442 var
1443 a: Integer;
1444 h2: Word;
1445 begin
1446 w := 0;
1447 h := 0;
1449 if Text = '' then Exit;
1450 if e_CharFonts = nil then Exit;
1451 if Integer(FontID) > High(e_CharFonts) then Exit;
1453 with e_CharFonts[FontID] do
1454 begin
1455 for a := 1 to Length(Text) do
1456 with Chars[Ord(Text[a])] do
1457 if TextureID <> -1 then
1458 begin
1459 w := w+Width+IfThen(a = Length(Text), 0, Space);
1460 e_GetTextureSize(TextureID, nil, @h2);
1461 if h2 > h then h := h2;
1462 end;
1463 end;
1464 end;
1466 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
1467 var
1468 a, lines, len: Integer;
1469 h2, w2, tw, th: Word;
1470 begin
1471 w2 := 0;
1472 h2 := 0;
1473 tw := 0;
1474 th := 0;
1476 if Text = '' then Exit;
1477 if e_CharFonts = nil then Exit;
1478 if Integer(FontID) > High(e_CharFonts) then Exit;
1480 lines := 1;
1481 len := Length(Text);
1483 with e_CharFonts[FontID] do
1484 begin
1485 for a := 1 to len do
1486 begin
1487 if Text[a] = #10 then
1488 begin
1489 Inc(lines);
1490 if w2 > tw then tw := w2;
1491 w2 := 0;
1492 continue;
1493 end;
1495 with Chars[Ord(Text[a])] do
1496 if TextureID <> -1 then
1497 begin
1498 w2 := w2 + Width + IfThen(a = len, 0, Space);
1499 e_GetTextureSize(TextureID, nil, @h2);
1500 if h2 > th then th := h2;
1501 end;
1502 end;
1503 end;
1505 if w2 > tw then
1506 tw := w2;
1508 w := tw;
1509 h := th * lines;
1510 end;
1512 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
1513 var
1514 a: Integer;
1515 begin
1516 Result := 0;
1518 if e_CharFonts = nil then Exit;
1519 if Integer(FontID) > High(e_CharFonts) then Exit;
1521 for a := 0 to High(e_CharFonts[FontID].Chars) do
1522 Result := Max(Result, e_CharFonts[FontID].Chars[a].Width);
1523 end;
1525 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
1526 var
1527 a: Integer;
1528 h2: Word;
1529 begin
1530 Result := 0;
1532 if e_CharFonts = nil then Exit;
1533 if Integer(FontID) > High(e_CharFonts) then Exit;
1535 for a := 0 to High(e_CharFonts[FontID].Chars) do
1536 begin
1537 if e_CharFonts[FontID].Chars[a].TextureID <> -1 then
1538 e_GetTextureSize(e_CharFonts[FontID].Chars[a].TextureID, nil, @h2)
1539 else h2 := 0;
1540 if h2 > Result then Result := h2;
1541 end;
1542 end;
1544 procedure e_CharFont_Remove(FontID: DWORD);
1545 var
1546 a: Integer;
1547 begin
1548 with e_CharFonts[FontID] do
1549 for a := 0 to High(Chars) do
1550 if Chars[a].TextureID <> -1 then e_DeleteTexture(Chars[a].TextureID);
1552 e_CharFonts[FontID].alive := False;
1553 end;
1555 procedure e_CharFont_RemoveAll();
1556 var
1557 a: Integer;
1558 begin
1559 if e_CharFonts = nil then Exit;
1561 for a := 0 to High(e_CharFonts) do
1562 e_CharFont_Remove(a);
1564 e_CharFonts := nil;
1565 end;
1567 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
1568 Space: ShortInt=0);
1569 var
1570 {$IFDEF NOGL_LISTS}
1571 loop1 : GLuint;
1572 cx, cy : real;
1573 {$ENDIF}
1574 i, id: DWORD;
1575 begin
1576 if e_NoGraphics then Exit;
1577 e_WriteLog('Creating texture font...', TMsgType.Notify);
1579 id := DWORD(-1);
1581 if e_TextureFonts <> nil then
1582 for i := 0 to High(e_TextureFonts) do
1583 if e_TextureFonts[i].Base = 0 then
1584 begin
1585 id := i;
1586 Break;
1587 end;
1589 if id = DWORD(-1) then
1590 begin
1591 SetLength(e_TextureFonts, Length(e_TextureFonts) + 1);
1592 id := High(e_TextureFonts);
1593 end;
1595 with e_TextureFonts[id] do
1596 begin
1597 {$IFDEF NOGL_LISTS}
1598 Base := glGenLists(XCount*YCount);
1599 {$ENDIF}
1600 TextureID := e_Textures[Tex].tx.id;
1601 CharWidth := (e_Textures[Tex].tx.Width div XCount)+Space;
1602 CharHeight := e_Textures[Tex].tx.Height div YCount;
1603 XC := XCount;
1604 YC := YCount;
1605 Texture := Tex;
1606 SPC := Space;
1607 end;
1609 {$IFDEF NOGL_LISTS}
1610 glBindTexture(GL_TEXTURE_2D, e_Textures[Tex].tx.id);
1611 for loop1 := 0 to XCount*YCount-1 do
1612 begin
1613 cx := (loop1 mod XCount)/XCount;
1614 cy := (loop1 div YCount)/YCount;
1616 glNewList(e_TextureFonts[id].Base+loop1, GL_COMPILE);
1617 glBegin(GL_QUADS);
1618 glTexCoord2f(cx, 1.0-cy-1/YCount);
1619 glVertex2i(0, e_Textures[Tex].tx.Height div YCount);
1621 glTexCoord2f(cx+1/XCount, 1.0-cy-1/YCount);
1622 glVertex2i(e_Textures[Tex].tx.Width div XCount, e_Textures[Tex].tx.Height div YCount);
1624 glTexCoord2f(cx+1/XCount, 1.0-cy);
1625 glVertex2i(e_Textures[Tex].tx.Width div XCount, 0);
1627 glTexCoord2f(cx, 1.0-cy);
1628 glVertex2i(0, 0);
1629 glEnd();
1630 glTranslated((e_Textures[Tex].tx.Width div XCount)+Space, 0, 0);
1631 glEndList();
1632 end;
1633 {$ENDIF}
1635 FontID := id;
1636 end;
1638 procedure e_TextureFontKill(FontID: DWORD);
1639 begin
1640 if e_NoGraphics then Exit;
1641 {$IFDEF NOGL_LISTS}
1642 glDeleteLists(e_TextureFonts[FontID].Base, 256);
1643 {$ENDIF}
1644 e_TextureFonts[FontID].Base := 0;
1645 end;
1647 {$IFNDEF NOGL_LISTS}
1648 procedure e_TextureFontDrawChar(ch: Char; FontID: DWORD);
1649 var
1650 index: Integer;
1651 cx, cy: GLfloat;
1652 Tex: Integer;
1653 Width, Height: Integer;
1654 XCount, YCount: Integer;
1655 begin
1656 index := Ord(ch) - 32;
1657 Tex := e_TextureFonts[FontID].Texture;
1658 Width := e_Textures[Tex].tx.Width;
1659 Height := e_Textures[Tex].tx.Height;
1660 XCount := e_TextureFonts[FontID].XC;
1661 YCount := e_TextureFonts[FontID].YC;
1662 cx := (index mod XCount)/XCount;
1663 cy := (index div YCount)/YCount;
1664 glBegin(GL_QUADS);
1665 glTexCoord2f(cx, 1 - cy - 1/YCount);
1666 glVertex2i(0, Height div YCount);
1667 glTexCoord2f(cx + 1/XCount, 1 - cy - 1/YCount);
1668 glVertex2i(Width div XCount, Height div YCount);
1669 glTexCoord2f(cx + 1/XCount, 1 - cy);
1670 glVertex2i(Width div XCount, 0);
1671 glTexCoord2f(cx, 1 - cy);
1672 glVertex2i(0, 0);
1673 glEnd();
1674 glTranslatef((e_Textures[Tex].tx.Width div XCount) + e_TextureFonts[FontID].SPC, 0, 0);
1675 end;
1677 procedure e_TextureFontDrawString(Text: String; FontID: DWORD);
1678 var
1679 i: Integer;
1680 begin
1681 for i := 1 to High(Text) do
1682 e_TextureFontDrawChar(Text[i], FontID);
1683 end;
1684 {$ENDIF}
1686 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
1687 begin
1688 if e_NoGraphics then Exit;
1689 if Integer(FontID) > High(e_TextureFonts) then Exit;
1690 if Text = '' then Exit;
1692 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1693 glEnable(GL_BLEND);
1695 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1697 glPushMatrix;
1698 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1699 glEnable(GL_TEXTURE_2D);
1700 glTranslatef(x, y, 0);
1701 {$IFDEF NOGL_LISTS}
1702 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1703 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1704 {$ELSE}
1705 e_TextureFontDrawString(Text, FontID);
1706 {$ENDIF}
1707 glDisable(GL_TEXTURE_2D);
1708 glPopMatrix;
1710 glDisable(GL_BLEND);
1711 end;
1713 // god forgive me for this, but i cannot figure out how to do it without lists
1714 procedure e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1715 begin
1716 if e_NoGraphics then Exit;
1717 glPushMatrix;
1719 if Shadow then
1720 begin
1721 glColor4ub(0, 0, 0, 128);
1722 glTranslatef(X+1, Y+1, 0);
1723 {$IFDEF NOGL_LISTS}
1724 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1725 {$ELSE}
1726 e_TextureFontDrawChar(Ch, FontID);
1727 {$ENDIF}
1728 glPopMatrix;
1729 glPushMatrix;
1730 end;
1732 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1733 glTranslatef(X, Y, 0);
1734 {$IFDEF NOGL_LISTS}
1735 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1736 {$ELSE}
1737 e_TextureFontDrawChar(Ch, FontID);
1738 {$ENDIF}
1740 glPopMatrix;
1741 end;
1743 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1744 begin
1745 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1746 glEnable(GL_TEXTURE_2D);
1747 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1749 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1750 glEnable(GL_BLEND);
1751 e_TextureFontPrintChar(X, Y, Ch, FontID, Shadow);
1752 glDisable(GL_TEXTURE_2D);
1753 glDisable(GL_BLEND);
1754 end;
1756 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
1757 begin
1758 result := e_TextureFonts[FontID].CharWidth;
1759 end;
1761 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD;
1762 Shadow: Boolean = False; Newlines: Boolean = False);
1763 var
1764 a, TX, TY, len: Integer;
1765 tc, c: TRGB;
1766 w, h: Word;
1767 begin
1768 if e_NoGraphics then Exit;
1769 if Text = '' then Exit;
1770 if e_TextureFonts = nil then Exit;
1771 if Integer(FontID) > High(e_TextureFonts) then Exit;
1773 c.R := 255;
1774 c.G := 255;
1775 c.B := 255;
1777 TX := X;
1778 TY := Y;
1779 len := Length(Text);
1781 w := e_TextureFonts[FontID].CharWidth;
1782 h := e_TextureFonts[FontID].CharHeight;
1784 with e_TextureFonts[FontID] do
1785 begin
1786 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1787 glEnable(GL_TEXTURE_2D);
1789 {$IFDEF NOGL_LISTS}
1790 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1791 {$ENDIF}
1793 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1794 glEnable(GL_BLEND);
1796 for a := 1 to len do
1797 begin
1798 case Text[a] of
1799 #10: // line feed
1800 begin
1801 if Newlines then
1802 begin
1803 TX := X;
1804 TY := TY + h;
1805 continue;
1806 end;
1807 end;
1808 #1: // black
1809 begin
1810 c.R := 0; c.G := 0; c.B := 0;
1811 continue;
1812 end;
1813 #2: // white
1814 begin
1815 c.R := 255; c.G := 255; c.B := 255;
1816 continue;
1817 end;
1818 #3: // darker
1819 begin
1820 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1821 continue;
1822 end;
1823 #4: // lighter
1824 begin
1825 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1826 continue;
1827 end;
1828 #18: // red
1829 begin
1830 c.R := 255; c.G := 0; c.B := 0;
1831 continue;
1832 end;
1833 #19: // green
1834 begin
1835 c.R := 0; c.G := 255; c.B := 0;
1836 continue;
1837 end;
1838 #20: // blue
1839 begin
1840 c.R := 0; c.G := 0; c.B := 255;
1841 continue;
1842 end;
1843 #21: // yellow
1844 begin
1845 c.R := 255; c.G := 255; c.B := 0;
1846 continue;
1847 end;
1848 end;
1850 tc := e_Colors;
1851 e_Colors := c;
1852 e_TextureFontPrintChar(TX, TY, Text[a], FontID, Shadow);
1853 e_Colors := tc;
1855 TX := TX+w;
1856 end;
1857 glDisable(GL_TEXTURE_2D);
1858 glDisable(GL_BLEND);
1859 end;
1860 end;
1862 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
1863 Blue: Byte; Scale: Single; Shadow: Boolean = False);
1864 begin
1865 if e_NoGraphics then Exit;
1866 if Text = '' then Exit;
1868 glPushMatrix;
1869 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1870 glEnable(GL_TEXTURE_2D);
1872 {$IFDEF NOGL_LISTS}
1873 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1874 {$ENDIF}
1876 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1877 glEnable(GL_BLEND);
1879 if Shadow then
1880 begin
1881 glColor4ub(0, 0, 0, 128);
1882 glTranslatef(x+1, y+1, 0);
1883 glScalef(Scale, Scale, 0);
1884 {$IFDEF NOGL_LISTS}
1885 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1886 {$ELSE}
1887 e_TextureFontDrawString(Text, FontID);
1888 {$ENDIF}
1889 glPopMatrix;
1890 glPushMatrix;
1891 end;
1893 glColor4ub(Red, Green, Blue, 255);
1894 glTranslatef(x, y, 0);
1895 glScalef(Scale, Scale, 0);
1896 {$IFDEF NOGL_LISTS}
1897 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1898 {$ELSE}
1899 e_TextureFontDrawString(Text, FontID);
1900 {$ENDIF}
1902 glDisable(GL_TEXTURE_2D);
1903 glPopMatrix;
1904 glColor3ub(e_Colors.R, e_Colors.G, e_Colors.B);
1905 glDisable(GL_BLEND);
1906 end;
1908 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
1909 begin
1910 CharWidth := 16;
1911 CharHeight := 16;
1912 if e_NoGraphics then Exit;
1913 if Integer(ID) > High(e_TextureFonts) then
1914 Exit;
1915 CharWidth := e_TextureFonts[ID].CharWidth;
1916 CharHeight := e_TextureFonts[ID].CharHeight;
1917 end;
1919 procedure e_RemoveAllTextureFont();
1920 var
1921 i: integer;
1922 begin
1923 if e_NoGraphics then Exit;
1924 if e_TextureFonts = nil then Exit;
1926 for i := 0 to High(e_TextureFonts) do
1927 if e_TextureFonts[i].Base <> 0 then
1928 begin
1929 {$IFDEF NOGL_LISTS}
1930 glDeleteLists(e_TextureFonts[i].Base, 256);
1931 {$ENDIF}
1932 e_TextureFonts[i].Base := 0;
1933 end;
1935 e_TextureFonts := nil;
1936 end;
1938 function _RGB(Red, Green, Blue: Byte): TRGB;
1939 begin
1940 Result.R := Red;
1941 Result.G := Green;
1942 Result.B := Blue;
1943 end;
1945 function _Point(X, Y: Integer): TPoint2i;
1946 begin
1947 Result.X := X;
1948 Result.Y := Y;
1949 end;
1951 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
1952 begin
1953 Result.X := X;
1954 Result.Y := Y;
1955 Result.Width := Width;
1956 Result.Height := Height;
1957 end;
1959 function _TRect(L, T, R, B: LongInt): TRect;
1960 begin
1961 Result.Top := T;
1962 Result.Left := L;
1963 Result.Right := R;
1964 Result.Bottom := B;
1965 end;
1968 procedure e_MakeScreenshot (st: TStream; Width, Height: Word);
1969 var
1970 pixels, obuf, scln, ps, pd: PByte;
1971 obufsize: Integer;
1972 dlen: Cardinal;
1973 i, x, y, res: Integer;
1974 sign: array [0..7] of Byte;
1975 hbuf: array [0..12] of Byte;
1976 crc: LongWord;
1977 img: TImageData;
1978 clr: TColor32Rec;
1979 begin
1980 if e_NoGraphics then Exit;
1981 obuf := nil;
1983 // first, extract and pack graphics data
1984 if (Width mod 4) > 0 then Width := Width+4-(Width mod 4);
1986 GetMem(pixels, Width*Height*3);
1987 try
1988 FillChar(pixels^, Width*Height*3, 0);
1989 glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
1990 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1992 if e_FastScreenshots then
1993 begin
1994 // create scanlines
1995 GetMem(scln, (Width*3+1)*Height);
1996 try
1997 ps := pixels;
1998 pd := scln;
1999 Inc(ps, (Width*3)*(Height-1));
2000 for i := 0 to Height-1 do
2001 begin
2002 pd^ := 0; // filter
2003 Inc(pd);
2004 Move(ps^, pd^, Width*3);
2005 Dec(ps, Width*3);
2006 Inc(pd, Width*3);
2007 end;
2008 except
2009 FreeMem(scln);
2010 raise;
2011 end;
2012 FreeMem(pixels);
2013 pixels := scln;
2015 // pack it
2016 obufsize := (Width*3+1)*Height*2;
2017 GetMem(obuf, obufsize);
2018 try
2019 while true do
2020 begin
2021 dlen := obufsize;
2022 res := compress2(Pointer(obuf), dlen, Pointer(pixels), (Width*3+1)*Height, 9);
2023 if res = Z_OK then break;
2024 if res <> Z_BUF_ERROR then raise Exception.Create('can''t pack data for PNG');
2025 obufsize := obufsize*2;
2026 FreeMem(obuf);
2027 obuf := nil;
2028 GetMem(obuf, obufsize);
2029 end;
2030 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
2032 // now write PNG
2034 // signature
2035 sign[0] := 137;
2036 sign[1] := 80;
2037 sign[2] := 78;
2038 sign[3] := 71;
2039 sign[4] := 13;
2040 sign[5] := 10;
2041 sign[6] := 26;
2042 sign[7] := 10;
2043 st.writeBuffer(sign, 8);
2044 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2046 // header
2047 writeIntBE(st, LongWord(13));
2048 sign[0] := 73;
2049 sign[1] := 72;
2050 sign[2] := 68;
2051 sign[3] := 82;
2052 st.writeBuffer(sign, 4);
2053 crc := crc32(0, @sign[0], 4);
2054 hbuf[0] := 0;
2055 hbuf[1] := 0;
2056 hbuf[2] := (Width shr 8) and $ff;
2057 hbuf[3] := Width and $ff;
2058 hbuf[4] := 0;
2059 hbuf[5] := 0;
2060 hbuf[6] := (Height shr 8) and $ff;
2061 hbuf[7] := Height and $ff;
2062 hbuf[8] := 8; // bit depth
2063 hbuf[9] := 2; // RGB
2064 hbuf[10] := 0; // compression method
2065 hbuf[11] := 0; // filter method
2066 hbuf[12] := 0; // no interlace
2067 crc := crc32(crc, @hbuf[0], 13);
2068 st.writeBuffer(hbuf, 13);
2069 writeIntBE(st, crc);
2070 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2072 // image data
2073 writeIntBE(st, LongWord(dlen));
2074 sign[0] := 73;
2075 sign[1] := 68;
2076 sign[2] := 65;
2077 sign[3] := 84;
2078 st.writeBuffer(sign, 4);
2079 crc := crc32(0, @sign[0], 4);
2080 crc := crc32(crc, obuf, dlen);
2081 st.writeBuffer(obuf^, dlen);
2082 writeIntBE(st, crc);
2083 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2085 // image data end
2086 writeIntBE(st, LongWord(0));
2087 sign[0] := 73;
2088 sign[1] := 69;
2089 sign[2] := 78;
2090 sign[3] := 68;
2091 st.writeBuffer(sign, 4);
2092 crc := crc32(0, @sign[0], 4);
2093 writeIntBE(st, crc);
2094 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2095 finally
2096 if obuf <> nil then FreeMem(obuf);
2097 end;
2098 end
2099 else
2100 begin
2101 Imaging.SetOption(ImagingPNGCompressLevel, 9);
2102 Imaging.SetOption(ImagingPNGPreFilter, 6);
2103 InitImage(img);
2104 try
2105 NewImage(Width, Height, TImageFormat.ifR8G8B8, img);
2106 ps := pixels;
2107 //writeln(stderr, 'moving pixels...');
2108 for y := Height-1 downto 0 do
2109 begin
2110 for x := 0 to Width-1 do
2111 begin
2112 clr.r := ps^; Inc(ps);
2113 clr.g := ps^; Inc(ps);
2114 clr.b := ps^; Inc(ps);
2115 clr.a := 255;
2116 SetPixel32(img, x, y, clr);
2117 end;
2118 end;
2119 GlobalMetadata.ClearMetaItems();
2120 GlobalMetadata.ClearMetaItemsForSaving();
2121 //writeln(stderr, 'compressing image...');
2122 if not SaveImageToStream('png', st, img) then raise Exception.Create('screenshot writing error');
2123 //writeln(stderr, 'done!');
2124 finally
2125 FreeImage(img);
2126 end;
2127 end;
2128 finally
2129 FreeMem(pixels);
2130 end;
2131 end;
2134 end.