DEADSOFTWARE

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