DEADSOFTWARE

rebuild the FBO from scratch when RBO is not supported
[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);
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_RBOSupported: Boolean = True;
205 e_Frame: GLuint = 0;
206 e_FrameW: Integer = -1;
207 e_FrameH: Integer = -1;
209 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
211 //------------------------------------------------------------------
212 // Èíèöèàëèçèðóåò OpenGL
213 //------------------------------------------------------------------
214 procedure e_InitGL();
215 begin
216 if e_NoGraphics then
217 begin
218 e_DummyTextures := True;
219 Exit;
220 end;
221 e_Colors.R := 255;
222 e_Colors.G := 255;
223 e_Colors.B := 255;
224 glDisable(GL_DEPTH_TEST);
225 glEnable(GL_SCISSOR_TEST);
226 glClearColor(0, 0, 0, 0);
227 end;
229 procedure e_SetViewPort(X, Y, Width, Height: Word);
230 var
231 mat: Array [0..15] of GLDouble;
233 begin
234 if e_NoGraphics then Exit;
235 glLoadIdentity();
236 glScissor(X, Y, Width, Height);
237 glViewport(X, Y, Width, Height);
238 //gluOrtho2D(0, Width, Height, 0);
240 glMatrixMode(GL_PROJECTION);
242 mat[ 0] := 2.0 / Width;
243 mat[ 1] := 0.0;
244 mat[ 2] := 0.0;
245 mat[ 3] := 0.0;
247 mat[ 4] := 0.0;
248 mat[ 5] := -2.0 / Height;
249 mat[ 6] := 0.0;
250 mat[ 7] := 0.0;
252 mat[ 8] := 0.0;
253 mat[ 9] := 0.0;
254 mat[10] := 1.0;
255 mat[11] := 0.0;
257 mat[12] := -1.0;
258 mat[13] := 1.0;
259 mat[14] := 0.0;
260 mat[15] := 1.0;
262 glLoadMatrixd(@mat[0]);
264 glMatrixMode(GL_MODELVIEW);
265 glLoadIdentity();
266 end;
268 //------------------------------------------------------------------
269 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
270 //------------------------------------------------------------------
271 function FindTexture(): DWORD;
272 var
273 i: integer;
274 begin
275 if e_Textures <> nil then
276 for i := 0 to High(e_Textures) do
277 if e_Textures[i].tx.Width = 0 then
278 begin
279 Result := i;
280 Exit;
281 end;
283 if e_Textures = nil then
284 begin
285 SetLength(e_Textures, 32);
286 Result := 0;
287 end
288 else
289 begin
290 Result := High(e_Textures) + 1;
291 SetLength(e_Textures, Length(e_Textures) + 32);
292 end;
293 end;
295 //------------------------------------------------------------------
296 // Ñîçäàåò òåêñòóðó
297 //------------------------------------------------------------------
298 function e_CreateTexture(FileName: String; var ID: DWORD): Boolean;
299 var
300 find_id: DWORD;
301 fmt: Word;
302 begin
303 Result := False;
305 e_WriteLog('Loading texture from '+FileName, TMsgType.Notify);
307 find_id := FindTexture();
309 if not LoadTexture(FileName, e_Textures[find_id].tx, e_Textures[find_id].tx.Width,
310 e_Textures[find_id].tx.Height, @fmt) then Exit;
312 ID := find_id;
314 Result := True;
315 end;
317 function e_CreateTextureEx(FileName: String; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
318 var
319 find_id: DWORD;
320 fmt: Word;
321 begin
322 Result := False;
324 find_id := FindTexture();
326 if not LoadTextureEx(FileName, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
328 ID := find_id;
330 Result := True;
331 end;
333 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
334 var
335 find_id: DWORD;
336 fmt: Word;
337 begin
338 Result := False;
340 find_id := FindTexture;
342 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;
344 id := find_id;
346 Result := True;
347 end;
349 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
350 var
351 find_id: DWORD;
352 fmt: Word;
353 begin
354 Result := False;
356 find_id := FindTexture();
358 if not LoadTextureMemEx(pData, dataSize, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
360 ID := find_id;
362 Result := True;
363 end;
365 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
366 var
367 find_id: DWORD;
368 fmt, tw, th: Word;
369 begin
370 result := false;
371 find_id := FindTexture();
372 if not LoadTextureImg(img, e_Textures[find_id].tx, tw, th, @fmt) then exit;
373 ID := find_id;
374 result := True;
375 end;
377 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
378 begin
379 if Width <> nil then Width^ := e_Textures[ID].tx.Width;
380 if Height <> nil then Height^ := e_Textures[ID].tx.Height;
381 end;
383 procedure DestroyFramebuffer;
384 begin
385 glBindTexture(GL_TEXTURE_2D, 0);
386 glBindRenderbuffer(GL_RENDERBUFFER, 0);
387 glBindFramebuffer(GL_FRAMEBUFFER, 0);
389 if e_Frame > 0 then
390 begin
391 glDeleteTextures(1, @e_Frame);
392 e_Frame := 0;
393 end;
395 if e_RBO > 0 then
396 begin
397 glDeleteRenderbuffers(1, @e_RBO);
398 e_RBO := 0;
399 end;
401 if e_FBO > 0 then
402 begin
403 glDeleteFramebuffers(1, @e_FBO);
404 e_FBO := 0;
405 end;
406 end;
408 function e_ResizeFramebuffer(Width, Height: Integer): Boolean;
409 begin
410 Result := False;
412 if e_NoGraphics then Exit;
414 DestroyFramebuffer;
416 e_FrameW := Width;
417 e_FrameH := Height;
419 glGetError();
421 glGenFramebuffers(1, @e_FBO);
423 if glGetError() <> GL_NO_ERROR then
424 begin
425 e_LogWriteln('GL: glGenFramebuffers failed');
426 Exit;
427 end;
429 glGenTextures(1, @e_Frame);
430 glBindTexture(GL_TEXTURE_2D, e_Frame);
431 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, nil);
432 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
433 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
435 if glGetError() <> GL_NO_ERROR then
436 begin
437 e_LogWriteln('GL: can''t create FBO color buffer');
438 DestroyFramebuffer;
439 Exit;
440 end;
442 glBindFramebuffer(GL_FRAMEBUFFER, e_FBO);
443 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, e_Frame, 0);
444 if glCheckFramebufferStatus(GL_FRAMEBUFFER) <> GL_FRAMEBUFFER_COMPLETE then
445 begin
446 e_LogWriteln('GL: can''t construct framebuffer with color attachment');
447 DestroyFramebuffer;
448 Exit;
449 end;
451 {$IFNDEF USE_GLES1}
452 if e_RBOSupported then
453 begin
454 glGenRenderbuffers(1, @e_RBO);
455 glBindRenderbuffer(GL_RENDERBUFFER, e_RBO);
456 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, Width, Height);
457 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, e_RBO);
458 if glCheckFramebufferStatus(GL_FRAMEBUFFER) <> GL_FRAMEBUFFER_COMPLETE then
459 begin
460 e_LogWriteln('GL: can''t construct framebuffer with depth+stencil attachment, trying without');
461 e_RBOSupported := False;
462 Result := e_ResizeFramebuffer(Width, Height);
463 Exit;
464 end;
465 end;
466 {$ENDIF}
468 Result := True;
469 end;
471 procedure e_ResizeWindow(Width, Height: Integer);
472 begin
473 if Height = 0 then
474 Height := 1;
475 e_SetViewPort(0, 0, Width, Height);
476 end;
478 procedure drawTxQuad (x0, y0, w, h, tw, th: Integer; u, v: single; Mirror: TMirrorType);
479 var
480 x1, y1, tmp: Integer;
481 begin
482 if (w < 1) or (h < 1) then exit;
483 x1 := x0+w;
484 y1 := y0+h;
485 if Mirror = TMirrorType.Horizontal then begin tmp := x1; x1 := x0; x0 := tmp; end
486 else if Mirror = TMirrorType.Vertical then begin tmp := y1; y1 := y0; y0 := tmp; end;
487 glTexCoord2f(0, v); glVertex2i(x0, y0);
488 glTexCoord2f(0, 0); glVertex2i(x0, y1);
489 glTexCoord2f(u, 0); glVertex2i(x1, y1);
490 glTexCoord2f(u, v); glVertex2i(x1, y0);
491 end;
493 procedure e_BlitFramebuffer(WinWidth, WinHeight: Integer);
494 begin
495 if (e_FBO = 0) or (e_Frame = 0) or e_NoGraphics then exit;
497 glDisable(GL_BLEND);
498 glEnable(GL_TEXTURE_2D);
499 glBindFramebuffer(GL_FRAMEBUFFER, 0);
500 glBindTexture(GL_TEXTURE_2D, e_Frame);
501 e_SetViewPort(0, 0, WinWidth, WinHeight);
502 glColor4ub(255, 255, 255, 255);
504 glBegin(GL_QUADS);
505 glTexCoord2f(0, 1); glVertex2i( 0, 0);
506 glTexCoord2f(0, 0); glVertex2i( 0, WinHeight);
507 glTexCoord2f(1, 0); glVertex2i(WinWidth, WinHeight);
508 glTexCoord2f(1, 1); glVertex2i(WinWidth, 0);
509 glEnd();
511 glBindFramebuffer(GL_FRAMEBUFFER, e_FBO);
512 e_SetViewPort(0, 0, e_FrameW, e_FrameH);
513 end;
515 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
516 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
517 begin
518 if e_NoGraphics then Exit;
519 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
521 if (Alpha > 0) or (AlphaChannel) or (Blending) then
522 glEnable(GL_BLEND)
523 else
524 glDisable(GL_BLEND);
526 if (AlphaChannel) or (Alpha > 0) then
527 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
529 if Alpha > 0 then
530 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
532 if Blending then
533 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
535 glEnable(GL_TEXTURE_2D);
536 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
537 glBegin(GL_QUADS);
539 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);
541 //u := e_Textures[ID].tx.u;
542 //v := e_Textures[ID].tx.v;
545 if Mirror = M_NONE then
546 begin
547 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
548 glTexCoord2f(0, 0); glVertex2i(X, Y);
549 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
550 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
551 end
552 else
553 if Mirror = M_HORIZONTAL then
554 begin
555 glTexCoord2f(u, 0); glVertex2i(X, Y);
556 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
557 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
558 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
559 end
560 else
561 if Mirror = M_VERTICAL then
562 begin
563 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
564 glTexCoord2f(0, -v); glVertex2i(X, Y);
565 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
566 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
567 end;
570 glEnd();
572 glDisable(GL_BLEND);
573 end;
575 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
576 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
577 var
578 u, v: Single;
579 begin
580 if e_NoGraphics then Exit;
581 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
583 if (Alpha > 0) or (AlphaChannel) or (Blending) then
584 glEnable(GL_BLEND)
585 else
586 glDisable(GL_BLEND);
588 if (AlphaChannel) or (Alpha > 0) then
589 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
591 if Alpha > 0 then
592 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
594 if Blending then
595 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
597 glEnable(GL_TEXTURE_2D);
598 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
600 u := e_Textures[ID].tx.u;
601 v := e_Textures[ID].tx.v;
603 glBegin(GL_QUADS);
604 glTexCoord2f(0, v); glVertex2i(X, Y);
605 glTexCoord2f(u, v); glVertex2i(X + Width, Y);
606 glTexCoord2f(u, 0); glVertex2i(X + Width, Y + Height);
607 glTexCoord2f(0, 0); glVertex2i(X, Y + Height);
608 glEnd();
610 glDisable(GL_BLEND);
611 end;
613 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
614 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
615 begin
616 if e_NoGraphics then Exit;
617 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
619 if (Alpha > 0) or (AlphaChannel) or (Blending) then
620 glEnable(GL_BLEND)
621 else
622 glDisable(GL_BLEND);
624 if (AlphaChannel) or (Alpha > 0) then
625 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
627 if Alpha > 0 then
628 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
630 if Blending then
631 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
633 glEnable(GL_TEXTURE_2D);
634 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
635 glBegin(GL_QUADS);
636 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);
637 glEnd();
639 glDisable(GL_BLEND);
640 end;
642 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
643 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
644 var
645 X2, Y2, dx, w, h: Integer;
646 u, v: Single;
647 begin
648 if e_NoGraphics then Exit;
649 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
650 ambientBlendMode := false;
652 if (Alpha > 0) or AlphaChannel or Blending then
653 begin
654 glEnable(GL_BLEND);
655 end
656 else
657 begin
658 if not ambientBlendMode then glDisable(GL_BLEND);
659 end;
660 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
661 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
662 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
664 if (XCount = 0) then XCount := 1;
665 if (YCount = 0) then YCount := 1;
667 glEnable(GL_TEXTURE_2D);
668 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
670 X2 := X+e_Textures[ID].tx.width*XCount;
671 Y2 := Y+e_Textures[ID].tx.height*YCount;
673 //k8: this SHOULD work... i hope
674 if (e_Textures[ID].tx.width = e_Textures[ID].tx.glwidth) and (e_Textures[ID].tx.height = e_Textures[ID].tx.glheight) then
675 begin
676 glBegin(GL_QUADS);
677 glTexCoord2i(0, YCount); glVertex2i(X, Y);
678 glTexCoord2i(XCount, YCount); glVertex2i(X2, Y);
679 glTexCoord2i(XCount, 0); glVertex2i(X2, Y2);
680 glTexCoord2i(0, 0); glVertex2i(X, Y2);
681 glEnd();
682 end
683 else
684 begin
685 glBegin(GL_QUADS);
686 // hard day's night
687 u := e_Textures[ID].tx.u;
688 v := e_Textures[ID].tx.v;
689 w := e_Textures[ID].tx.width;
690 h := e_Textures[ID].tx.height;
691 while YCount > 0 do
692 begin
693 dx := XCount;
694 x2 := X;
695 while dx > 0 do
696 begin
697 glTexCoord2f(0, v); glVertex2i(X, Y);
698 glTexCoord2f(u, v); glVertex2i(X+w, Y);
699 glTexCoord2f(u, 0); glVertex2i(X+w, Y+h);
700 glTexCoord2f(0, 0); glVertex2i(X, Y+h);
701 Inc(X, w);
702 Dec(dx);
703 end;
704 X := x2;
705 Inc(Y, h);
706 Dec(YCount);
707 end;
708 glEnd();
709 end;
711 glDisable(GL_BLEND);
712 end;
715 //TODO: overflow checks
716 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
717 var
718 ex0, ey0: Integer;
719 begin
720 result := false;
721 if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit;
722 // check for intersection
723 if (x0+w0 <= x1) or (y0+h0 <= y1) or (x1+w1 <= x0) or (y1+h1 <= y0) then exit;
724 if (x0 >= x1+w1) or (y0 >= y1+h1) or (x1 >= x0+h0) or (y1 >= y0+h0) then exit;
725 // ok, intersects
726 ex0 := x0+w0;
727 ey0 := y0+h0;
728 if (x0 < x1) then x0 := x1;
729 if (y0 < y1) then y0 := y1;
730 if (ex0 > x1+w1) then ex0 := x1+w1;
731 if (ey0 > y1+h1) then ey0 := y1+h1;
732 w0 := ex0-x0;
733 h0 := ey0-y0;
734 result := (w0 > 0) and (h0 > 0);
735 end;
738 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
739 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
740 var
741 x2, y2: Integer;
743 wassc: Boolean;
744 scxywh: array[0..3] of GLint;
745 vpxywh: array[0..3] of GLint;
747 w, h, dw, cw, ch, yofs: Integer;
748 u, v, cu, cv: Single;
749 onlyOneY: Boolean;
752 procedure setScissorGLInternal (x, y, w, h: Integer);
753 begin
754 //if not scallowed then exit;
755 x := trunc(x*scale);
756 y := trunc(y*scale);
757 w := trunc(w*scale);
758 h := trunc(h*scale);
759 y := vpxywh[3]-(y+h);
760 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
761 begin
762 glScissor(0, 0, 0, 0);
763 end
764 else
765 begin
766 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
767 glScissor(x, y, w, h);
768 end;
769 end;
772 begin
773 if e_NoGraphics then exit;
774 ambientBlendMode := false;
776 if (wdt < 1) or (hgt < 1) then exit;
778 if (wdt mod e_Textures[ID].tx.width = 0) and (hgt mod e_Textures[ID].tx.height = 0) then
779 begin
780 e_DrawFill(id, x, y, wdt div e_Textures[ID].tx.width, hgt div e_Textures[ID].tx.height, alpha, alphachannel, blending, ambientBlendMode);
781 exit;
782 end;
784 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
786 if (Alpha > 0) or AlphaChannel or Blending then
787 begin
788 glEnable(GL_BLEND);
789 end
790 else
791 begin
792 if not ambientBlendMode then glDisable(GL_BLEND);
793 end;
794 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
795 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
796 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
798 glEnable(GL_TEXTURE_2D);
799 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
801 x2 := x+wdt;
802 y2 := y+hgt;
804 //k8: this SHOULD work... i hope
805 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
806 begin
807 glBegin(GL_QUADS);
808 glTexCoord2f(0, hgt/e_Textures[ID].tx.height); glVertex2i(x, y);
809 glTexCoord2f(wdt/e_Textures[ID].tx.width, hgt/e_Textures[ID].tx.height); glVertex2i(x2, y);
810 glTexCoord2f(wdt/e_Textures[ID].tx.width, 0); glVertex2i(x2, y2);
811 glTexCoord2f(0, 0); glVertex2i(x, y2);
812 glEnd();
813 end
814 else
815 begin
816 // hard day's night; setup scissor
818 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
819 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
820 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
821 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
822 //glEnable(GL_SCISSOR_TEST);
823 setScissorGLInternal(x, y, wdt, hgt);
825 // draw quads
826 u := e_Textures[ID].tx.u;
827 v := e_Textures[ID].tx.v;
828 w := e_Textures[ID].tx.width;
829 h := e_Textures[ID].tx.height;
830 x2 := x;
831 if (hgt > h) then begin y += hgt-h; onlyOneY := false; end else onlyOneY := true;
832 glBegin(GL_QUADS);
833 while (hgt > 0) do
834 begin
835 if (hgt >= h) then begin ch := h; cv := v; yofs := 0; end else begin ch := hgt; cv := v/(h/hgt); yofs := h-hgt; end;
836 if onlyOneY then yofs := 0;
837 Dec(hgt, h);
838 dw := wdt;
839 x := x2;
840 while (dw > 0) do
841 begin
842 if (dw >= w) then begin cw := w; cu := u; end else begin cw := dw; cu := u/(w/dw); end;
843 Dec(dw, w);
844 glTexCoord2f(0, cv); glVertex2i(X, Y+yofs);
845 glTexCoord2f(cu, cv); glVertex2i(X+cw, Y+yofs);
846 glTexCoord2f(cu, 0); glVertex2i(X+cw, Y+ch+yofs);
847 glTexCoord2f(0, 0); glVertex2i(X, Y+ch+yofs);
848 Inc(X, w);
849 end;
850 Dec(Y, h);
851 end;
852 glEnd();
853 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
854 end;
856 glDisable(GL_BLEND);
857 end;
860 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
861 begin
862 if e_NoGraphics then exit;
863 if (w < 1) or (h < 1) then exit;
864 if (a <> 255) or ((r or g or b) <> 0) then
865 begin
866 glEnable(GL_BLEND);
867 glDisable(GL_TEXTURE_2D);
868 glColor4ub(r, g, b, a);
869 if ((r or g or b) <> 0) then
870 begin
871 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
872 glBegin(GL_QUADS);
873 glVertex2i(x, y);
874 glVertex2i(x+w, y);
875 glVertex2i(x+w, y+h);
876 glVertex2i(x, y+h);
877 glEnd();
878 end;
879 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
880 glBegin(GL_QUADS);
881 glVertex2i(x, y);
882 glVertex2i(x+w, y);
883 glVertex2i(x+w, y+h);
884 glVertex2i(x, y+h);
885 glEnd();
886 glDisable(GL_BLEND);
887 end;
888 end;
891 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
892 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
893 begin
894 if e_NoGraphics then Exit;
896 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
898 if (Alpha > 0) or (AlphaChannel) or (Blending) then
899 glEnable(GL_BLEND)
900 else
901 glDisable(GL_BLEND);
903 if (AlphaChannel) or (Alpha > 0) then
904 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
906 if Alpha > 0 then
907 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
909 if Blending then
910 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
912 if (Angle <> 0) and (RC <> nil) then
913 begin
914 glPushMatrix();
915 glTranslatef(X+RC.X, Y+RC.Y, 0);
916 glRotatef(Angle, 0, 0, 1);
917 glTranslatef(-(X+RC.X), -(Y+RC.Y), 0);
918 end;
920 glEnable(GL_TEXTURE_2D);
921 glBindTexture(GL_TEXTURE_2D, e_Textures[id].tx.id);
922 glBegin(GL_QUADS); //0-1 1-1
923 //00 10
924 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);
925 glEnd();
927 if Angle <> 0 then
928 glPopMatrix();
930 glDisable(GL_BLEND);
931 end;
933 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
934 begin
935 if e_NoGraphics then Exit;
936 glDisable(GL_TEXTURE_2D);
937 glColor3ub(Red, Green, Blue);
938 glPointSize(Size);
940 if (Size = 2) or (Size = 4) then
941 X := X + 1;
943 glBegin(GL_POINTS);
944 glVertex2f(X+0.3, Y+1.0);
945 glEnd();
947 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
948 end;
950 procedure e_LineCorrection(var X1, Y1, X2, Y2: Integer);
951 begin
952 // Make lines only top-left/bottom-right and top-right/bottom-left
953 if Y2 < Y1 then
954 begin
955 X1 := X1 xor X2;
956 X2 := X1 xor X2;
957 X1 := X1 xor X2;
959 Y1 := Y1 xor Y2;
960 Y2 := Y1 xor Y2;
961 Y1 := Y1 xor Y2;
962 end;
964 // Pixel-perfect hack
965 if X1 < X2 then
966 Inc(X2)
967 else
968 Inc(X1);
969 Inc(Y2);
970 end;
972 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
973 var
974 nX1, nY1, nX2, nY2: Integer;
975 begin
976 if e_NoGraphics then Exit;
977 // Only top-left/bottom-right quad
978 if X1 > X2 then
979 begin
980 X1 := X1 xor X2;
981 X2 := X1 xor X2;
982 X1 := X1 xor X2;
983 end;
984 if Y1 > Y2 then
985 begin
986 Y1 := Y1 xor Y2;
987 Y2 := Y1 xor Y2;
988 Y1 := Y1 xor Y2;
989 end;
991 if Alpha > 0 then
992 begin
993 glEnable(GL_BLEND);
994 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
995 end
996 else
997 glDisable(GL_BLEND);
999 glDisable(GL_TEXTURE_2D);
1000 glColor4ub(Red, Green, Blue, 255-Alpha);
1001 glLineWidth(1);
1002 glBegin(GL_LINES);
1003 nX1 := X1; nY1 := Y1;
1004 nX2 := X2; nY2 := Y1;
1005 e_LineCorrection(nX1, nY1, nX2, nY2); // Pixel-perfect lines
1006 glVertex2i(nX1, nY1);
1007 glVertex2i(nX2, nY2);
1009 nX1 := X2; nY1 := Y1;
1010 nX2 := X2; nY2 := Y2;
1011 e_LineCorrection(nX1, nY1, nX2, nY2);
1012 glVertex2i(nX1, nY1);
1013 glVertex2i(nX2, nY2);
1015 nX1 := X2; nY1 := Y2;
1016 nX2 := X1; nY2 := Y2;
1017 e_LineCorrection(nX1, nY1, nX2, nY2);
1018 glVertex2i(nX1, nY1);
1019 glVertex2i(nX2, nY2);
1021 nX1 := X1; nY1 := Y2;
1022 nX2 := X1; nY2 := Y1;
1023 e_LineCorrection(nX1, nY1, nX2, nY2);
1024 glVertex2i(nX1, nY1);
1025 glVertex2i(nX2, nY2);
1026 glEnd();
1027 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1028 glDisable(GL_BLEND);
1029 end;
1031 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
1032 Blending: TBlending = TBlending.None);
1033 begin
1034 if e_NoGraphics then Exit;
1035 if (Alpha > 0) or (Blending <> TBlending.None) then
1036 glEnable(GL_BLEND)
1037 else
1038 glDisable(GL_BLEND);
1040 case Blending of
1041 TBlending.None: if Alpha > 0 then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1042 TBlending.Blend: glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1043 TBlending.Invert: glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1044 TBlending.Filter: glBlendFunc(GL_ZERO, GL_SRC_COLOR);
1045 end;
1047 glDisable(GL_TEXTURE_2D);
1048 glColor4ub(Red, Green, Blue, 255-Alpha);
1050 X2 := X2 + 1;
1051 Y2 := Y2 + 1;
1053 glBegin(GL_QUADS);
1054 glVertex2i(X1, Y1);
1055 glVertex2i(X2, Y1);
1056 glVertex2i(X2, Y2);
1057 glVertex2i(X1, Y2);
1058 glEnd();
1060 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1061 glDisable(GL_BLEND);
1062 end;
1065 // ////////////////////////////////////////////////////////////////////////// //
1066 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
1067 begin
1068 if (a < 0) then a := 0;
1069 if (a > 255) then a := 255;
1070 glEnable(GL_BLEND);
1071 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
1072 glDisable(GL_TEXTURE_2D);
1073 glColor4ub(0, 0, 0, Byte(255-a));
1074 glBegin(GL_QUADS);
1075 glVertex2i(x0, y0);
1076 glVertex2i(x1, y0);
1077 glVertex2i(x1, y1);
1078 glVertex2i(x0, y1);
1079 glEnd();
1080 //glRect(x, y, x+w, y+h);
1081 glColor4ub(1, 1, 1, 1);
1082 glDisable(GL_BLEND);
1083 //glBlendEquation(GL_FUNC_ADD);
1084 end;
1086 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
1087 begin
1088 if (w > 0) and (h > 0) then e_DarkenQuad(x, y, x+w, y+h, a);
1089 end;
1092 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
1093 begin
1094 if e_NoGraphics then Exit;
1095 // Pixel-perfect lines
1096 if Width = 1 then
1097 e_LineCorrection(X1, Y1, X2, Y2);
1099 if Alpha > 0 then
1100 begin
1101 glEnable(GL_BLEND);
1102 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1103 end else
1104 glDisable(GL_BLEND);
1106 glDisable(GL_TEXTURE_2D);
1107 glColor4ub(Red, Green, Blue, 255-Alpha);
1108 glLineWidth(Width);
1109 glBegin(GL_LINES);
1110 glVertex2i(X1, Y1);
1111 glVertex2i(X2, Y2);
1112 glEnd();
1113 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1115 glDisable(GL_BLEND);
1116 end;
1118 //------------------------------------------------------------------
1119 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1120 //------------------------------------------------------------------
1121 procedure e_DeleteTexture(ID: DWORD);
1122 begin
1123 if not e_NoGraphics then
1124 glDeleteTextures(1, @e_Textures[ID].tx.id);
1125 e_Textures[ID].tx.id := 0;
1126 e_Textures[ID].tx.Width := 0;
1127 e_Textures[ID].tx.Height := 0;
1128 end;
1130 //------------------------------------------------------------------
1131 // Óäàëÿåò âñå òåêñòóðû
1132 //------------------------------------------------------------------
1133 procedure e_RemoveAllTextures();
1134 var
1135 i: integer;
1136 begin
1137 if e_Textures = nil then Exit;
1139 for i := 0 to High(e_Textures) do
1140 if e_Textures[i].tx.Width <> 0 then e_DeleteTexture(i);
1141 e_Textures := nil;
1142 end;
1144 //------------------------------------------------------------------
1145 // Óäàëÿåò äâèæîê
1146 //------------------------------------------------------------------
1147 procedure e_ReleaseEngine();
1148 begin
1149 e_RemoveAllTextures;
1150 e_RemoveAllTextureFont;
1151 end;
1153 procedure e_BeginRender();
1154 begin
1155 if e_NoGraphics then Exit;
1156 glEnable(GL_ALPHA_TEST);
1157 glAlphaFunc(GL_GREATER, 0.0);
1158 end;
1160 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
1161 begin
1162 if e_NoGraphics then Exit;
1163 glClearColor(Red, Green, Blue, 0);
1164 glClear(Mask);
1165 end;
1167 procedure e_Clear(); overload;
1168 begin
1169 if e_NoGraphics then Exit;
1170 glClearColor(0, 0, 0, 0);
1171 glClear(GL_COLOR_BUFFER_BIT);
1172 end;
1174 procedure e_EndRender();
1175 begin
1176 if e_NoGraphics then Exit;
1177 glPopMatrix();
1178 end;
1180 {$IFDEF USE_SDL2}
1181 function e_GetGamma(win: PSDL_Window): Byte;
1182 var
1183 ramp: array [0..256*3-1] of Word;
1184 rgb: array [0..2] of Double;
1185 sum: double;
1186 count: integer;
1187 min: integer;
1188 max: integer;
1189 A, B: double;
1190 i, j: integer;
1191 begin
1192 Result := 0;
1193 if e_NoGraphics then Exit;
1194 rgb[0] := 1.0;
1195 rgb[1] := 1.0;
1196 rgb[2] := 1.0;
1198 SDL_GetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1200 for i := 0 to 2 do
1201 begin
1202 sum := 0;
1203 count := 0;
1204 min := 256 * i;
1205 max := min + 256;
1207 for j := min to max - 1 do
1208 if ramp[j] > 0 then
1209 begin
1210 B := (j mod 256)/256;
1211 A := ramp[j]/65536;
1212 sum := sum + ln(A)/ln(B);
1213 inc(count);
1214 end;
1215 rgb[i] := sum / count;
1216 end;
1218 Result := 100 - Trunc(((rgb[0] + rgb[1] + rgb[2])/3 - 0.23) * 100/(2.7 - 0.23));
1219 end;
1221 procedure e_SetGamma(win: PSDL_Window; Gamma: Byte);
1222 var
1223 ramp: array [0..256*3-1] of Word;
1224 i: integer;
1225 r: double;
1226 g: double;
1227 begin
1228 if e_NoGraphics then Exit;
1229 g := (100 - Gamma)*(2.7 - 0.23)/100 + 0.23;
1231 for i := 0 to 255 do
1232 begin
1233 r := Exp(g * ln(i/256))*65536;
1234 if r < 0 then r := 0
1235 else if r > 65535 then r := 65535;
1236 ramp[i] := trunc(r);
1237 ramp[i + 256] := trunc(r);
1238 ramp[i + 512] := trunc(r);
1239 end;
1241 SDL_SetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1242 end;
1243 {$ENDIF}
1245 function e_CharFont_Create(sp: ShortInt=0): DWORD;
1246 var
1247 i, id: DWORD;
1248 begin
1249 e_WriteLog('Creating CharFont...', TMsgType.Notify);
1251 id := DWORD(-1);
1253 if e_CharFonts <> nil then
1254 for i := 0 to High(e_CharFonts) do
1255 if not e_CharFonts[i].alive then
1256 begin
1257 id := i;
1258 Break;
1259 end;
1261 if id = DWORD(-1) then
1262 begin
1263 SetLength(e_CharFonts, Length(e_CharFonts) + 1);
1264 id := High(e_CharFonts);
1265 end;
1267 with e_CharFonts[id] do
1268 begin
1269 for i := 0 to High(Chars) do
1270 with Chars[i] do
1271 begin
1272 TextureID := -1;
1273 Width := 0;
1274 end;
1276 Space := sp;
1277 alive := True;
1278 end;
1280 Result := id;
1281 end;
1283 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
1284 begin
1285 with e_CharFonts[FontID].Chars[Ord(c)] do
1286 begin
1287 TextureID := Texture;
1288 Width := w;
1289 end;
1290 end;
1292 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
1293 var
1294 a: Integer;
1295 begin
1296 if e_NoGraphics then Exit;
1297 if Text = '' then Exit;
1298 if e_CharFonts = nil then Exit;
1299 if Integer(FontID) > High(e_CharFonts) then Exit;
1301 with e_CharFonts[FontID] do
1302 begin
1303 for a := 1 to Length(Text) do
1304 with Chars[Ord(Text[a])] do
1305 if TextureID <> -1 then
1306 begin
1307 e_Draw(TextureID, X, Y, 0, True, False);
1308 X := X+Width+IfThen(a = Length(Text), 0, Space);
1309 end;
1310 end;
1311 end;
1313 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
1314 Color: TRGB; Scale: Single = 1.0);
1315 var
1316 a: Integer;
1317 c: TRGB;
1318 begin
1319 if e_NoGraphics then Exit;
1320 if Text = '' then Exit;
1321 if e_CharFonts = nil then Exit;
1322 if Integer(FontID) > High(e_CharFonts) then Exit;
1324 with e_CharFonts[FontID] do
1325 begin
1326 for a := 1 to Length(Text) do
1327 with Chars[Ord(Text[a])] do
1328 if TextureID <> -1 then
1329 begin
1330 if Scale <> 1.0 then
1331 begin
1332 glPushMatrix;
1333 glScalef(Scale, Scale, 0);
1334 end;
1336 c := e_Colors;
1337 e_Colors := Color;
1338 e_Draw(TextureID, X, Y, 0, True, False);
1339 e_Colors := c;
1341 if Scale <> 1.0 then glPopMatrix;
1343 X := X+Width+IfThen(a = Length(Text), 0, Space);
1344 end;
1345 end;
1346 end;
1348 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
1349 var
1350 a, TX, TY, len: Integer;
1351 tc, c: TRGB;
1352 w, h: Word;
1353 begin
1354 if e_NoGraphics then Exit;
1355 if Text = '' then Exit;
1356 if e_CharFonts = nil then Exit;
1357 if Integer(FontID) > High(e_CharFonts) then Exit;
1359 c.R := 255;
1360 c.G := 255;
1361 c.B := 255;
1363 TX := X;
1364 TY := Y;
1365 len := Length(Text);
1367 e_CharFont_GetSize(FontID, 'A', w, h);
1369 with e_CharFonts[FontID] do
1370 begin
1371 for a := 1 to len do
1372 begin
1373 case Text[a] of
1374 #10: // line feed
1375 begin
1376 TX := X;
1377 TY := TY + h;
1378 continue;
1379 end;
1380 #1: // black
1381 begin
1382 c.R := 0; c.G := 0; c.B := 0;
1383 continue;
1384 end;
1385 #2: // white
1386 begin
1387 c.R := 255; c.G := 255; c.B := 255;
1388 continue;
1389 end;
1390 #3: // darker
1391 begin
1392 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1393 continue;
1394 end;
1395 #4: // lighter
1396 begin
1397 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1398 continue;
1399 end;
1400 #18: // red
1401 begin
1402 c.R := 255; c.G := 0; c.B := 0;
1403 continue;
1404 end;
1405 #19: // green
1406 begin
1407 c.R := 0; c.G := 255; c.B := 0;
1408 continue;
1409 end;
1410 #20: // blue
1411 begin
1412 c.R := 0; c.G := 0; c.B := 255;
1413 continue;
1414 end;
1415 #21: // yellow
1416 begin
1417 c.R := 255; c.G := 255; c.B := 0;
1418 continue;
1419 end;
1420 end;
1422 with Chars[Ord(Text[a])] do
1423 if TextureID <> -1 then
1424 begin
1425 tc := e_Colors;
1426 e_Colors := c;
1427 e_Draw(TextureID, TX, TY, 0, True, False);
1428 e_Colors := tc;
1430 TX := TX+Width+IfThen(a = Length(Text), 0, Space);
1431 end;
1432 end;
1433 end;
1434 end;
1436 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
1437 var
1438 a: Integer;
1439 h2: Word;
1440 begin
1441 w := 0;
1442 h := 0;
1444 if Text = '' then Exit;
1445 if e_CharFonts = nil then Exit;
1446 if Integer(FontID) > High(e_CharFonts) then Exit;
1448 with e_CharFonts[FontID] do
1449 begin
1450 for a := 1 to Length(Text) do
1451 with Chars[Ord(Text[a])] do
1452 if TextureID <> -1 then
1453 begin
1454 w := w+Width+IfThen(a = Length(Text), 0, Space);
1455 e_GetTextureSize(TextureID, nil, @h2);
1456 if h2 > h then h := h2;
1457 end;
1458 end;
1459 end;
1461 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
1462 var
1463 a, lines, len: Integer;
1464 h2, w2, tw, th: Word;
1465 begin
1466 w2 := 0;
1467 h2 := 0;
1468 tw := 0;
1469 th := 0;
1471 if Text = '' then Exit;
1472 if e_CharFonts = nil then Exit;
1473 if Integer(FontID) > High(e_CharFonts) then Exit;
1475 lines := 1;
1476 len := Length(Text);
1478 with e_CharFonts[FontID] do
1479 begin
1480 for a := 1 to len do
1481 begin
1482 if Text[a] = #10 then
1483 begin
1484 Inc(lines);
1485 if w2 > tw then tw := w2;
1486 w2 := 0;
1487 continue;
1488 end;
1490 with Chars[Ord(Text[a])] do
1491 if TextureID <> -1 then
1492 begin
1493 w2 := w2 + Width + IfThen(a = len, 0, Space);
1494 e_GetTextureSize(TextureID, nil, @h2);
1495 if h2 > th then th := h2;
1496 end;
1497 end;
1498 end;
1500 if w2 > tw then
1501 tw := w2;
1503 w := tw;
1504 h := th * lines;
1505 end;
1507 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
1508 var
1509 a: Integer;
1510 begin
1511 Result := 0;
1513 if e_CharFonts = nil then Exit;
1514 if Integer(FontID) > High(e_CharFonts) then Exit;
1516 for a := 0 to High(e_CharFonts[FontID].Chars) do
1517 Result := Max(Result, e_CharFonts[FontID].Chars[a].Width);
1518 end;
1520 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
1521 var
1522 a: Integer;
1523 h2: Word;
1524 begin
1525 Result := 0;
1527 if e_CharFonts = nil then Exit;
1528 if Integer(FontID) > High(e_CharFonts) then Exit;
1530 for a := 0 to High(e_CharFonts[FontID].Chars) do
1531 begin
1532 if e_CharFonts[FontID].Chars[a].TextureID <> -1 then
1533 e_GetTextureSize(e_CharFonts[FontID].Chars[a].TextureID, nil, @h2)
1534 else h2 := 0;
1535 if h2 > Result then Result := h2;
1536 end;
1537 end;
1539 procedure e_CharFont_Remove(FontID: DWORD);
1540 var
1541 a: Integer;
1542 begin
1543 with e_CharFonts[FontID] do
1544 for a := 0 to High(Chars) do
1545 if Chars[a].TextureID <> -1 then e_DeleteTexture(Chars[a].TextureID);
1547 e_CharFonts[FontID].alive := False;
1548 end;
1550 procedure e_CharFont_RemoveAll();
1551 var
1552 a: Integer;
1553 begin
1554 if e_CharFonts = nil then Exit;
1556 for a := 0 to High(e_CharFonts) do
1557 e_CharFont_Remove(a);
1559 e_CharFonts := nil;
1560 end;
1562 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
1563 Space: ShortInt=0);
1564 var
1565 {$IFDEF NOGL_LISTS}
1566 loop1 : GLuint;
1567 cx, cy : real;
1568 {$ENDIF}
1569 i, id: DWORD;
1570 begin
1571 if e_NoGraphics then Exit;
1572 e_WriteLog('Creating texture font...', TMsgType.Notify);
1574 id := DWORD(-1);
1576 if e_TextureFonts <> nil then
1577 for i := 0 to High(e_TextureFonts) do
1578 if e_TextureFonts[i].Base = 0 then
1579 begin
1580 id := i;
1581 Break;
1582 end;
1584 if id = DWORD(-1) then
1585 begin
1586 SetLength(e_TextureFonts, Length(e_TextureFonts) + 1);
1587 id := High(e_TextureFonts);
1588 end;
1590 with e_TextureFonts[id] do
1591 begin
1592 {$IFDEF NOGL_LISTS}
1593 Base := glGenLists(XCount*YCount);
1594 {$ENDIF}
1595 TextureID := e_Textures[Tex].tx.id;
1596 CharWidth := (e_Textures[Tex].tx.Width div XCount)+Space;
1597 CharHeight := e_Textures[Tex].tx.Height div YCount;
1598 XC := XCount;
1599 YC := YCount;
1600 Texture := Tex;
1601 SPC := Space;
1602 end;
1604 {$IFDEF NOGL_LISTS}
1605 glBindTexture(GL_TEXTURE_2D, e_Textures[Tex].tx.id);
1606 for loop1 := 0 to XCount*YCount-1 do
1607 begin
1608 cx := (loop1 mod XCount)/XCount;
1609 cy := (loop1 div YCount)/YCount;
1611 glNewList(e_TextureFonts[id].Base+loop1, GL_COMPILE);
1612 glBegin(GL_QUADS);
1613 glTexCoord2f(cx, 1.0-cy-1/YCount);
1614 glVertex2i(0, e_Textures[Tex].tx.Height div YCount);
1616 glTexCoord2f(cx+1/XCount, 1.0-cy-1/YCount);
1617 glVertex2i(e_Textures[Tex].tx.Width div XCount, e_Textures[Tex].tx.Height div YCount);
1619 glTexCoord2f(cx+1/XCount, 1.0-cy);
1620 glVertex2i(e_Textures[Tex].tx.Width div XCount, 0);
1622 glTexCoord2f(cx, 1.0-cy);
1623 glVertex2i(0, 0);
1624 glEnd();
1625 glTranslated((e_Textures[Tex].tx.Width div XCount)+Space, 0, 0);
1626 glEndList();
1627 end;
1628 {$ENDIF}
1630 FontID := id;
1631 end;
1633 procedure e_TextureFontKill(FontID: DWORD);
1634 begin
1635 if e_NoGraphics then Exit;
1636 {$IFDEF NOGL_LISTS}
1637 glDeleteLists(e_TextureFonts[FontID].Base, 256);
1638 {$ENDIF}
1639 e_TextureFonts[FontID].Base := 0;
1640 end;
1642 {$IFNDEF NOGL_LISTS}
1643 procedure e_TextureFontDrawChar(ch: Char; FontID: DWORD);
1644 var
1645 index: Integer;
1646 cx, cy: GLfloat;
1647 Tex: Integer;
1648 Width, Height: Integer;
1649 XCount, YCount: Integer;
1650 begin
1651 index := Ord(ch) - 32;
1652 Tex := e_TextureFonts[FontID].Texture;
1653 Width := e_Textures[Tex].tx.Width;
1654 Height := e_Textures[Tex].tx.Height;
1655 XCount := e_TextureFonts[FontID].XC;
1656 YCount := e_TextureFonts[FontID].YC;
1657 cx := (index mod XCount)/XCount;
1658 cy := (index div YCount)/YCount;
1659 glBegin(GL_QUADS);
1660 glTexCoord2f(cx, 1 - cy - 1/YCount);
1661 glVertex2i(0, Height div YCount);
1662 glTexCoord2f(cx + 1/XCount, 1 - cy - 1/YCount);
1663 glVertex2i(Width div XCount, Height div YCount);
1664 glTexCoord2f(cx + 1/XCount, 1 - cy);
1665 glVertex2i(Width div XCount, 0);
1666 glTexCoord2f(cx, 1 - cy);
1667 glVertex2i(0, 0);
1668 glEnd();
1669 glTranslatef((e_Textures[Tex].tx.Width div XCount) + e_TextureFonts[FontID].SPC, 0, 0);
1670 end;
1672 procedure e_TextureFontDrawString(Text: String; FontID: DWORD);
1673 var
1674 i: Integer;
1675 begin
1676 for i := 1 to High(Text) do
1677 e_TextureFontDrawChar(Text[i], FontID);
1678 end;
1679 {$ENDIF}
1681 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
1682 begin
1683 if e_NoGraphics then Exit;
1684 if Integer(FontID) > High(e_TextureFonts) then Exit;
1685 if Text = '' then Exit;
1687 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1688 glEnable(GL_BLEND);
1690 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1692 glPushMatrix;
1693 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1694 glEnable(GL_TEXTURE_2D);
1695 glTranslatef(x, y, 0);
1696 {$IFDEF NOGL_LISTS}
1697 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1698 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1699 {$ELSE}
1700 e_TextureFontDrawString(Text, FontID);
1701 {$ENDIF}
1702 glDisable(GL_TEXTURE_2D);
1703 glPopMatrix;
1705 glDisable(GL_BLEND);
1706 end;
1708 // god forgive me for this, but i cannot figure out how to do it without lists
1709 procedure e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1710 begin
1711 if e_NoGraphics then Exit;
1712 glPushMatrix;
1714 if Shadow then
1715 begin
1716 glColor4ub(0, 0, 0, 128);
1717 glTranslatef(X+1, Y+1, 0);
1718 {$IFDEF NOGL_LISTS}
1719 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1720 {$ELSE}
1721 e_TextureFontDrawChar(Ch, FontID);
1722 {$ENDIF}
1723 glPopMatrix;
1724 glPushMatrix;
1725 end;
1727 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1728 glTranslatef(X, Y, 0);
1729 {$IFDEF NOGL_LISTS}
1730 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1731 {$ELSE}
1732 e_TextureFontDrawChar(Ch, FontID);
1733 {$ENDIF}
1735 glPopMatrix;
1736 end;
1738 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1739 begin
1740 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1741 glEnable(GL_TEXTURE_2D);
1742 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1744 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1745 glEnable(GL_BLEND);
1746 e_TextureFontPrintChar(X, Y, Ch, FontID, Shadow);
1747 glDisable(GL_TEXTURE_2D);
1748 glDisable(GL_BLEND);
1749 end;
1751 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
1752 begin
1753 result := e_TextureFonts[FontID].CharWidth;
1754 end;
1756 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD;
1757 Shadow: Boolean = False; Newlines: Boolean = False);
1758 var
1759 a, TX, TY, len: Integer;
1760 tc, c: TRGB;
1761 w, h: Word;
1762 begin
1763 if e_NoGraphics then Exit;
1764 if Text = '' then Exit;
1765 if e_TextureFonts = nil then Exit;
1766 if Integer(FontID) > High(e_TextureFonts) then Exit;
1768 c.R := 255;
1769 c.G := 255;
1770 c.B := 255;
1772 TX := X;
1773 TY := Y;
1774 len := Length(Text);
1776 w := e_TextureFonts[FontID].CharWidth;
1777 h := e_TextureFonts[FontID].CharHeight;
1779 with e_TextureFonts[FontID] do
1780 begin
1781 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1782 glEnable(GL_TEXTURE_2D);
1784 {$IFDEF NOGL_LISTS}
1785 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1786 {$ENDIF}
1788 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1789 glEnable(GL_BLEND);
1791 for a := 1 to len do
1792 begin
1793 case Text[a] of
1794 #10: // line feed
1795 begin
1796 if Newlines then
1797 begin
1798 TX := X;
1799 TY := TY + h;
1800 continue;
1801 end;
1802 end;
1803 #1: // black
1804 begin
1805 c.R := 0; c.G := 0; c.B := 0;
1806 continue;
1807 end;
1808 #2: // white
1809 begin
1810 c.R := 255; c.G := 255; c.B := 255;
1811 continue;
1812 end;
1813 #3: // darker
1814 begin
1815 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1816 continue;
1817 end;
1818 #4: // lighter
1819 begin
1820 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1821 continue;
1822 end;
1823 #18: // red
1824 begin
1825 c.R := 255; c.G := 0; c.B := 0;
1826 continue;
1827 end;
1828 #19: // green
1829 begin
1830 c.R := 0; c.G := 255; c.B := 0;
1831 continue;
1832 end;
1833 #20: // blue
1834 begin
1835 c.R := 0; c.G := 0; c.B := 255;
1836 continue;
1837 end;
1838 #21: // yellow
1839 begin
1840 c.R := 255; c.G := 255; c.B := 0;
1841 continue;
1842 end;
1843 end;
1845 tc := e_Colors;
1846 e_Colors := c;
1847 e_TextureFontPrintChar(TX, TY, Text[a], FontID, Shadow);
1848 e_Colors := tc;
1850 TX := TX+w;
1851 end;
1852 glDisable(GL_TEXTURE_2D);
1853 glDisable(GL_BLEND);
1854 end;
1855 end;
1857 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
1858 Blue: Byte; Scale: Single; Shadow: Boolean = False);
1859 begin
1860 if e_NoGraphics then Exit;
1861 if Text = '' then Exit;
1863 glPushMatrix;
1864 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1865 glEnable(GL_TEXTURE_2D);
1867 {$IFDEF NOGL_LISTS}
1868 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1869 {$ENDIF}
1871 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1872 glEnable(GL_BLEND);
1874 if Shadow then
1875 begin
1876 glColor4ub(0, 0, 0, 128);
1877 glTranslatef(x+1, y+1, 0);
1878 glScalef(Scale, Scale, 0);
1879 {$IFDEF NOGL_LISTS}
1880 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1881 {$ELSE}
1882 e_TextureFontDrawString(Text, FontID);
1883 {$ENDIF}
1884 glPopMatrix;
1885 glPushMatrix;
1886 end;
1888 glColor4ub(Red, Green, Blue, 255);
1889 glTranslatef(x, y, 0);
1890 glScalef(Scale, Scale, 0);
1891 {$IFDEF NOGL_LISTS}
1892 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1893 {$ELSE}
1894 e_TextureFontDrawString(Text, FontID);
1895 {$ENDIF}
1897 glDisable(GL_TEXTURE_2D);
1898 glPopMatrix;
1899 glColor3ub(e_Colors.R, e_Colors.G, e_Colors.B);
1900 glDisable(GL_BLEND);
1901 end;
1903 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
1904 begin
1905 CharWidth := 16;
1906 CharHeight := 16;
1907 if e_NoGraphics then Exit;
1908 if Integer(ID) > High(e_TextureFonts) then
1909 Exit;
1910 CharWidth := e_TextureFonts[ID].CharWidth;
1911 CharHeight := e_TextureFonts[ID].CharHeight;
1912 end;
1914 procedure e_RemoveAllTextureFont();
1915 var
1916 i: integer;
1917 begin
1918 if e_NoGraphics then Exit;
1919 if e_TextureFonts = nil then Exit;
1921 for i := 0 to High(e_TextureFonts) do
1922 if e_TextureFonts[i].Base <> 0 then
1923 begin
1924 {$IFDEF NOGL_LISTS}
1925 glDeleteLists(e_TextureFonts[i].Base, 256);
1926 {$ENDIF}
1927 e_TextureFonts[i].Base := 0;
1928 end;
1930 e_TextureFonts := nil;
1931 end;
1933 function _RGB(Red, Green, Blue: Byte): TRGB;
1934 begin
1935 Result.R := Red;
1936 Result.G := Green;
1937 Result.B := Blue;
1938 end;
1940 function _Point(X, Y: Integer): TPoint2i;
1941 begin
1942 Result.X := X;
1943 Result.Y := Y;
1944 end;
1946 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
1947 begin
1948 Result.X := X;
1949 Result.Y := Y;
1950 Result.Width := Width;
1951 Result.Height := Height;
1952 end;
1954 function _TRect(L, T, R, B: LongInt): TRect;
1955 begin
1956 Result.Top := T;
1957 Result.Left := L;
1958 Result.Right := R;
1959 Result.Bottom := B;
1960 end;
1963 procedure e_MakeScreenshot (st: TStream; Width, Height: Word);
1964 var
1965 pixels, obuf, scln, ps, pd: PByte;
1966 obufsize: Integer;
1967 dlen: Cardinal;
1968 i, x, y, res: Integer;
1969 sign: array [0..7] of Byte;
1970 hbuf: array [0..12] of Byte;
1971 crc: LongWord;
1972 img: TImageData;
1973 clr: TColor32Rec;
1974 begin
1975 if e_NoGraphics then Exit;
1976 obuf := nil;
1978 // first, extract and pack graphics data
1979 if (Width mod 4) > 0 then Width := Width+4-(Width mod 4);
1981 GetMem(pixels, Width*Height*3);
1982 try
1983 FillChar(pixels^, Width*Height*3, 0);
1984 glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
1985 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1987 if e_FastScreenshots then
1988 begin
1989 // create scanlines
1990 GetMem(scln, (Width*3+1)*Height);
1991 try
1992 ps := pixels;
1993 pd := scln;
1994 Inc(ps, (Width*3)*(Height-1));
1995 for i := 0 to Height-1 do
1996 begin
1997 pd^ := 0; // filter
1998 Inc(pd);
1999 Move(ps^, pd^, Width*3);
2000 Dec(ps, Width*3);
2001 Inc(pd, Width*3);
2002 end;
2003 except
2004 FreeMem(scln);
2005 raise;
2006 end;
2007 FreeMem(pixels);
2008 pixels := scln;
2010 // pack it
2011 obufsize := (Width*3+1)*Height*2;
2012 GetMem(obuf, obufsize);
2013 try
2014 while true do
2015 begin
2016 dlen := obufsize;
2017 res := compress2(Pointer(obuf), dlen, Pointer(pixels), (Width*3+1)*Height, 9);
2018 if res = Z_OK then break;
2019 if res <> Z_BUF_ERROR then raise Exception.Create('can''t pack data for PNG');
2020 obufsize := obufsize*2;
2021 FreeMem(obuf);
2022 obuf := nil;
2023 GetMem(obuf, obufsize);
2024 end;
2025 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
2027 // now write PNG
2029 // signature
2030 sign[0] := 137;
2031 sign[1] := 80;
2032 sign[2] := 78;
2033 sign[3] := 71;
2034 sign[4] := 13;
2035 sign[5] := 10;
2036 sign[6] := 26;
2037 sign[7] := 10;
2038 st.writeBuffer(sign, 8);
2039 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2041 // header
2042 writeIntBE(st, LongWord(13));
2043 sign[0] := 73;
2044 sign[1] := 72;
2045 sign[2] := 68;
2046 sign[3] := 82;
2047 st.writeBuffer(sign, 4);
2048 crc := crc32(0, @sign[0], 4);
2049 hbuf[0] := 0;
2050 hbuf[1] := 0;
2051 hbuf[2] := (Width shr 8) and $ff;
2052 hbuf[3] := Width and $ff;
2053 hbuf[4] := 0;
2054 hbuf[5] := 0;
2055 hbuf[6] := (Height shr 8) and $ff;
2056 hbuf[7] := Height and $ff;
2057 hbuf[8] := 8; // bit depth
2058 hbuf[9] := 2; // RGB
2059 hbuf[10] := 0; // compression method
2060 hbuf[11] := 0; // filter method
2061 hbuf[12] := 0; // no interlace
2062 crc := crc32(crc, @hbuf[0], 13);
2063 st.writeBuffer(hbuf, 13);
2064 writeIntBE(st, crc);
2065 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2067 // image data
2068 writeIntBE(st, LongWord(dlen));
2069 sign[0] := 73;
2070 sign[1] := 68;
2071 sign[2] := 65;
2072 sign[3] := 84;
2073 st.writeBuffer(sign, 4);
2074 crc := crc32(0, @sign[0], 4);
2075 crc := crc32(crc, obuf, dlen);
2076 st.writeBuffer(obuf^, dlen);
2077 writeIntBE(st, crc);
2078 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2080 // image data end
2081 writeIntBE(st, LongWord(0));
2082 sign[0] := 73;
2083 sign[1] := 69;
2084 sign[2] := 78;
2085 sign[3] := 68;
2086 st.writeBuffer(sign, 4);
2087 crc := crc32(0, @sign[0], 4);
2088 writeIntBE(st, crc);
2089 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2090 finally
2091 if obuf <> nil then FreeMem(obuf);
2092 end;
2093 end
2094 else
2095 begin
2096 Imaging.SetOption(ImagingPNGCompressLevel, 9);
2097 Imaging.SetOption(ImagingPNGPreFilter, 6);
2098 InitImage(img);
2099 try
2100 NewImage(Width, Height, TImageFormat.ifR8G8B8, img);
2101 ps := pixels;
2102 //writeln(stderr, 'moving pixels...');
2103 for y := Height-1 downto 0 do
2104 begin
2105 for x := 0 to Width-1 do
2106 begin
2107 clr.r := ps^; Inc(ps);
2108 clr.g := ps^; Inc(ps);
2109 clr.b := ps^; Inc(ps);
2110 clr.a := 255;
2111 SetPixel32(img, x, y, clr);
2112 end;
2113 end;
2114 GlobalMetadata.ClearMetaItems();
2115 GlobalMetadata.ClearMetaItemsForSaving();
2116 //writeln(stderr, 'compressing image...');
2117 if not SaveImageToStream('png', st, img) then raise Exception.Create('screenshot writing error');
2118 //writeln(stderr, 'done!');
2119 finally
2120 FreeImage(img);
2121 end;
2122 end;
2123 finally
2124 FreeMem(pixels);
2125 end;
2126 end;
2129 end.