DEADSOFTWARE

Fix font rendering with nanoGL
[d2df-sdl.git] / src / engine / e_graphics.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit e_graphics;
19 interface
21 uses
22 {$IFDEF USE_NANOGL}
23 nanoGL,
24 {$ELSE}
25 GL, GLExt,
26 {$ENDIF}
27 SysUtils, Classes, Math, e_log, e_texture, SDL2, MAPDEF, ImagingTypes, Imaging, ImagingUtility;
29 type
30 TMirrorType=(None, Horizontal, Vertical);
31 TBlending=(None, Blend, Filter, Invert);
33 TPoint2i = record
34 X, Y: Integer;
35 end;
37 TPoint2f = record
38 X, Y: Double;
39 end;
41 TRect = record
42 Left, Top, Right, Bottom: Integer;
43 end;
45 TRectWH = record
46 X, Y: Integer;
47 Width, Height: Word;
48 end;
50 TRGB = packed record
51 R, G, B: Byte;
52 end;
54 PDFPoint = ^TDFPoint;
55 PPoint2f = ^TPoint2f;
56 PRect = ^TRect;
57 PRectWH = ^TRectWH;
60 //------------------------------------------------------------------
61 // ïðîòîòèïû ôóíêöèé
62 //------------------------------------------------------------------
63 procedure e_InitGL();
64 procedure e_SetViewPort(X, Y, Width, Height: Word);
65 procedure e_ResizeWindow(Width, Height: Integer);
67 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
68 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
69 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
70 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
71 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
72 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
73 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
74 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
76 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
77 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
79 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
80 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
82 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
84 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
85 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
86 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
87 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
88 Blending: TBlending = TBlending.None);
89 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
90 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
92 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
93 function e_CreateTexture(FileName: string; var ID: DWORD): Boolean;
94 function e_CreateTextureEx(FileName: string; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
95 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
96 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
97 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
98 function e_GetTextureSize2(ID: DWORD): TRectWH;
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; Shadow: Boolean = False);
124 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
125 procedure e_RemoveAllTextureFont();
127 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
128 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
130 procedure e_ReleaseEngine();
131 procedure e_BeginRender();
132 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
133 procedure e_Clear(); overload;
134 procedure e_EndRender();
136 function e_GetGamma(win: PSDL_Window): Byte;
137 procedure e_SetGamma(win: PSDL_Window;Gamma: Byte);
139 procedure e_MakeScreenshot(st: TStream; Width, Height: Word);
141 function _RGB(Red, Green, Blue: Byte): TRGB;
142 function _Point(X, Y: Integer): TPoint2i;
143 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
144 function _TRect(L, T, R, B: LongInt): TRect;
146 //function e_getTextGLId (ID: DWORD): GLuint;
148 var
149 e_Colors: TRGB;
150 e_NoGraphics: Boolean = False;
151 e_FastScreenshots: Boolean = true; // it's REALLY SLOW with `false`
152 g_dbg_scale: Single = 1.0;
155 implementation
157 uses
158 paszlib, crc, utils;
161 type
162 TTexture = record
163 tx: GLTexture;
164 end;
166 TTextureFont = record
167 Texture: DWORD;
168 TextureID: DWORD;
169 Base: Uint32;
170 CharWidth: Byte;
171 CharHeight: Byte;
172 XC, YC: WORD;
173 SPC: ShortInt;
174 end;
176 TCharFont = record
177 Chars: array[0..255] of
178 record
179 TextureID: Integer;
180 Width: Byte;
181 end;
182 Space: ShortInt;
183 Height: ShortInt;
184 alive: Boolean;
185 end;
187 TSavedTexture = record
188 TexID: DWORD;
189 OldID: DWORD;
190 Pixels: Pointer;
191 end;
193 var
194 e_Textures: array of TTexture = nil;
195 e_TextureFonts: array of TTextureFont = nil;
196 e_CharFonts: array of TCharFont;
197 //e_SavedTextures: array of TSavedTexture;
199 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
201 //------------------------------------------------------------------
202 // Èíèöèàëèçèðóåò OpenGL
203 //------------------------------------------------------------------
204 procedure e_InitGL();
205 begin
206 if e_NoGraphics then
207 begin
208 e_DummyTextures := True;
209 Exit;
210 end;
211 e_Colors.R := 255;
212 e_Colors.G := 255;
213 e_Colors.B := 255;
214 glDisable(GL_DEPTH_TEST);
215 glEnable(GL_SCISSOR_TEST);
216 glClearColor(0, 0, 0, 0);
217 end;
219 procedure e_SetViewPort(X, Y, Width, Height: Word);
220 var
221 mat: Array [0..15] of GLDouble;
223 begin
224 if e_NoGraphics then Exit;
225 glLoadIdentity();
226 glScissor(X, Y, Width, Height);
227 glViewport(X, Y, Width, Height);
228 //gluOrtho2D(0, Width, Height, 0);
230 glMatrixMode(GL_PROJECTION);
232 mat[ 0] := 2.0 / Width;
233 mat[ 1] := 0.0;
234 mat[ 2] := 0.0;
235 mat[ 3] := 0.0;
237 mat[ 4] := 0.0;
238 mat[ 5] := -2.0 / Height;
239 mat[ 6] := 0.0;
240 mat[ 7] := 0.0;
242 mat[ 8] := 0.0;
243 mat[ 9] := 0.0;
244 mat[10] := 1.0;
245 mat[11] := 0.0;
247 mat[12] := -1.0;
248 mat[13] := 1.0;
249 mat[14] := 0.0;
250 mat[15] := 1.0;
252 glLoadMatrixd(@mat[0]);
254 glMatrixMode(GL_MODELVIEW);
255 glLoadIdentity();
256 end;
258 //------------------------------------------------------------------
259 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
260 //------------------------------------------------------------------
261 function FindTexture(): DWORD;
262 var
263 i: integer;
264 begin
265 if e_Textures <> nil then
266 for i := 0 to High(e_Textures) do
267 if e_Textures[i].tx.Width = 0 then
268 begin
269 Result := i;
270 Exit;
271 end;
273 if e_Textures = nil then
274 begin
275 SetLength(e_Textures, 32);
276 Result := 0;
277 end
278 else
279 begin
280 Result := High(e_Textures) + 1;
281 SetLength(e_Textures, Length(e_Textures) + 32);
282 end;
283 end;
285 //------------------------------------------------------------------
286 // Ñîçäàåò òåêñòóðó
287 //------------------------------------------------------------------
288 function e_CreateTexture(FileName: String; var ID: DWORD): Boolean;
289 var
290 find_id: DWORD;
291 fmt: Word;
292 begin
293 Result := False;
295 e_WriteLog('Loading texture from '+FileName, TMsgType.Notify);
297 find_id := FindTexture();
299 if not LoadTexture(FileName, e_Textures[find_id].tx, e_Textures[find_id].tx.Width,
300 e_Textures[find_id].tx.Height, @fmt) then Exit;
302 ID := find_id;
304 Result := True;
305 end;
307 function e_CreateTextureEx(FileName: String; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
308 var
309 find_id: DWORD;
310 fmt: Word;
311 begin
312 Result := False;
314 find_id := FindTexture();
316 if not LoadTextureEx(FileName, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
318 ID := find_id;
320 Result := True;
321 end;
323 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
324 var
325 find_id: DWORD;
326 fmt: Word;
327 begin
328 Result := False;
330 find_id := FindTexture;
332 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;
334 id := find_id;
336 Result := True;
337 end;
339 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
340 var
341 find_id: DWORD;
342 fmt: Word;
343 begin
344 Result := False;
346 find_id := FindTexture();
348 if not LoadTextureMemEx(pData, dataSize, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
350 ID := find_id;
352 Result := True;
353 end;
355 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
356 var
357 find_id: DWORD;
358 fmt, tw, th: Word;
359 begin
360 result := false;
361 find_id := FindTexture();
362 if not LoadTextureImg(img, e_Textures[find_id].tx, tw, th, @fmt) then exit;
363 ID := find_id;
364 result := True;
365 end;
367 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
368 begin
369 if Width <> nil then Width^ := e_Textures[ID].tx.Width;
370 if Height <> nil then Height^ := e_Textures[ID].tx.Height;
371 end;
373 function e_GetTextureSize2(ID: DWORD): TRectWH;
374 var
375 data: PChar;
376 x, y: Integer;
377 w, h: Word;
378 a: Boolean;
379 lastline: Integer;
380 begin
381 w := e_Textures[ID].tx.Width;
382 h := e_Textures[ID].tx.Height;
384 Result.Y := 0;
385 Result.X := 0;
386 Result.Width := w;
387 Result.Height := h;
389 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support glGetTexImage
390 if e_NoGraphics then Exit;
392 data := GetMemory(w*h*4);
393 glEnable(GL_TEXTURE_2D);
394 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
395 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
397 for y := h-1 downto 0 do
398 begin
399 lastline := y;
400 a := True;
402 for x := 1 to w-4 do
403 begin
404 a := Byte((data+y*w*4+x*4+3)^) <> 0;
405 if a then Break;
406 end;
408 if a then
409 begin
410 Result.Y := h-lastline;
411 Break;
412 end;
413 end;
415 for y := 0 to h-1 do
416 begin
417 lastline := y;
418 a := True;
420 for x := 1 to w-4 do
421 begin
422 a := Byte((data+y*w*4+x*4+3)^) <> 0;
423 if a then Break;
424 end;
426 if a then
427 begin
428 Result.Height := h-lastline-Result.Y;
429 Break;
430 end;
431 end;
433 for x := 0 to w-1 do
434 begin
435 lastline := x;
436 a := True;
438 for y := 1 to h-4 do
439 begin
440 a := Byte((data+y*w*4+x*4+3)^) <> 0;
441 if a then Break;
442 end;
444 if a then
445 begin
446 Result.X := lastline+1;
447 Break;
448 end;
449 end;
451 for x := w-1 downto 0 do
452 begin
453 lastline := x;
454 a := True;
456 for y := 1 to h-4 do
457 begin
458 a := Byte((data+y*w*4+x*4+3)^) <> 0;
459 if a then Break;
460 end;
462 if a then
463 begin
464 Result.Width := lastline-Result.X+1;
465 Break;
466 end;
467 end;
469 FreeMemory(data);
470 {$ENDIF}
471 end;
473 procedure e_ResizeWindow(Width, Height: Integer);
474 begin
475 if Height = 0 then
476 Height := 1;
477 e_SetViewPort(0, 0, Width, Height);
478 end;
480 procedure drawTxQuad (x0, y0, w, h, tw, th: Integer; u, v: single; Mirror: TMirrorType);
481 var
482 x1, y1, tmp: Integer;
483 begin
484 if (w < 1) or (h < 1) then exit;
485 x1 := x0+w;
486 y1 := y0+h;
487 if Mirror = TMirrorType.Horizontal then begin tmp := x1; x1 := x0; x0 := tmp; end
488 else if Mirror = TMirrorType.Vertical then begin tmp := y1; y1 := y0; y0 := tmp; end;
489 //HACK: make texture one pixel shorter, so it won't wrap
490 if (g_dbg_scale <> 1.0) then
491 begin
492 u := u*tw/(tw+1);
493 v := v*th/(th+1);
494 end;
495 glTexCoord2f(0, v); glVertex2i(x0, y0);
496 glTexCoord2f(0, 0); glVertex2i(x0, y1);
497 glTexCoord2f(u, 0); glVertex2i(x1, y1);
498 glTexCoord2f(u, v); glVertex2i(x1, y0);
499 end;
501 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
502 Blending: Boolean; Mirror: TMirrorType = TMirrorType.None);
503 begin
504 if e_NoGraphics then Exit;
505 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
507 if (Alpha > 0) or (AlphaChannel) or (Blending) then
508 glEnable(GL_BLEND)
509 else
510 glDisable(GL_BLEND);
512 if (AlphaChannel) or (Alpha > 0) then
513 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
515 if Alpha > 0 then
516 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
518 if Blending then
519 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
521 glEnable(GL_TEXTURE_2D);
522 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
523 glBegin(GL_QUADS);
525 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);
527 //u := e_Textures[ID].tx.u;
528 //v := e_Textures[ID].tx.v;
531 if Mirror = M_NONE then
532 begin
533 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
534 glTexCoord2f(0, 0); glVertex2i(X, Y);
535 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
536 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
537 end
538 else
539 if Mirror = M_HORIZONTAL then
540 begin
541 glTexCoord2f(u, 0); glVertex2i(X, Y);
542 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
543 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
544 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
545 end
546 else
547 if Mirror = M_VERTICAL then
548 begin
549 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
550 glTexCoord2f(0, -v); glVertex2i(X, Y);
551 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
552 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
553 end;
556 glEnd();
558 glDisable(GL_BLEND);
559 end;
561 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
562 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
563 var
564 u, v: Single;
565 begin
566 if e_NoGraphics then Exit;
567 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
569 if (Alpha > 0) or (AlphaChannel) or (Blending) then
570 glEnable(GL_BLEND)
571 else
572 glDisable(GL_BLEND);
574 if (AlphaChannel) or (Alpha > 0) then
575 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
577 if Alpha > 0 then
578 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
580 if Blending then
581 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
583 glEnable(GL_TEXTURE_2D);
584 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
586 u := e_Textures[ID].tx.u;
587 v := e_Textures[ID].tx.v;
589 glBegin(GL_QUADS);
590 glTexCoord2f(0, v); glVertex2i(X, Y);
591 glTexCoord2f(u, v); glVertex2i(X + Width, Y);
592 glTexCoord2f(u, 0); glVertex2i(X + Width, Y + Height);
593 glTexCoord2f(0, 0); glVertex2i(X, Y + Height);
594 glEnd();
596 glDisable(GL_BLEND);
597 end;
599 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
600 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = TMirrorType.None);
601 begin
602 if e_NoGraphics then Exit;
603 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
605 if (Alpha > 0) or (AlphaChannel) or (Blending) then
606 glEnable(GL_BLEND)
607 else
608 glDisable(GL_BLEND);
610 if (AlphaChannel) or (Alpha > 0) then
611 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
613 if Alpha > 0 then
614 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
616 if Blending then
617 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
619 glEnable(GL_TEXTURE_2D);
620 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
621 glBegin(GL_QUADS);
622 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);
623 glEnd();
625 glDisable(GL_BLEND);
626 end;
628 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
629 AlphaChannel: Boolean; Blending: Boolean; ambientBlendMode: Boolean=false);
630 var
631 X2, Y2, dx, w, h: Integer;
632 u, v: Single;
633 begin
634 if e_NoGraphics then Exit;
635 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
636 ambientBlendMode := false;
638 if (Alpha > 0) or AlphaChannel or Blending then
639 begin
640 glEnable(GL_BLEND);
641 end
642 else
643 begin
644 if not ambientBlendMode then glDisable(GL_BLEND);
645 end;
646 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
647 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
648 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
650 if (XCount = 0) then XCount := 1;
651 if (YCount = 0) then YCount := 1;
653 glEnable(GL_TEXTURE_2D);
654 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
656 X2 := X+e_Textures[ID].tx.width*XCount;
657 Y2 := Y+e_Textures[ID].tx.height*YCount;
659 //k8: this SHOULD work... i hope
660 if (e_Textures[ID].tx.width = e_Textures[ID].tx.glwidth) and (e_Textures[ID].tx.height = e_Textures[ID].tx.glheight) then
661 begin
662 glBegin(GL_QUADS);
663 glTexCoord2i(0, YCount); glVertex2i(X, Y);
664 glTexCoord2i(XCount, YCount); glVertex2i(X2, Y);
665 glTexCoord2i(XCount, 0); glVertex2i(X2, Y2);
666 glTexCoord2i(0, 0); glVertex2i(X, Y2);
667 glEnd();
668 end
669 else
670 begin
671 glBegin(GL_QUADS);
672 // hard day's night
673 u := e_Textures[ID].tx.u;
674 v := e_Textures[ID].tx.v;
675 w := e_Textures[ID].tx.width;
676 h := e_Textures[ID].tx.height;
677 while YCount > 0 do
678 begin
679 dx := XCount;
680 x2 := X;
681 while dx > 0 do
682 begin
683 glTexCoord2f(0, v); glVertex2i(X, Y);
684 glTexCoord2f(u, v); glVertex2i(X+w, Y);
685 glTexCoord2f(u, 0); glVertex2i(X+w, Y+h);
686 glTexCoord2f(0, 0); glVertex2i(X, Y+h);
687 Inc(X, w);
688 Dec(dx);
689 end;
690 X := x2;
691 Inc(Y, h);
692 Dec(YCount);
693 end;
694 glEnd();
695 end;
697 glDisable(GL_BLEND);
698 end;
701 //TODO: overflow checks
702 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean;
703 var
704 ex0, ey0: Integer;
705 begin
706 result := false;
707 if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit;
708 // check for intersection
709 if (x0+w0 <= x1) or (y0+h0 <= y1) or (x1+w1 <= x0) or (y1+h1 <= y0) then exit;
710 if (x0 >= x1+w1) or (y0 >= y1+h1) or (x1 >= x0+h0) or (y1 >= y0+h0) then exit;
711 // ok, intersects
712 ex0 := x0+w0;
713 ey0 := y0+h0;
714 if (x0 < x1) then x0 := x1;
715 if (y0 < y1) then y0 := y1;
716 if (ex0 > x1+w1) then ex0 := x1+w1;
717 if (ey0 > y1+h1) then ey0 := y1+h1;
718 w0 := ex0-x0;
719 h0 := ey0-y0;
720 result := (w0 > 0) and (h0 > 0);
721 end;
724 procedure e_DrawFillX (id: DWORD; x, y, wdt, hgt: Integer; alpha: Integer; alphachannel: Boolean;
725 blending: Boolean; scale: Single; ambientBlendMode: Boolean=false);
726 var
727 x2, y2: Integer;
729 wassc: Boolean;
730 scxywh: array[0..3] of GLint;
731 vpxywh: array[0..3] of GLint;
733 w, h, dw, cw, ch, yofs: Integer;
734 u, v, cu, cv: Single;
735 onlyOneY: Boolean;
738 procedure setScissorGLInternal (x, y, w, h: Integer);
739 begin
740 //if not scallowed then exit;
741 x := trunc(x*scale);
742 y := trunc(y*scale);
743 w := trunc(w*scale);
744 h := trunc(h*scale);
745 y := vpxywh[3]-(y+h);
746 if not intersectRect(x, y, w, h, scxywh[0], scxywh[1], scxywh[2], scxywh[3]) then
747 begin
748 glScissor(0, 0, 0, 0);
749 end
750 else
751 begin
752 //writeln(' (', x, ',', y, ')-(', w, ',', h, ')');
753 glScissor(x, y, w, h);
754 end;
755 end;
758 begin
759 if e_NoGraphics then exit;
760 ambientBlendMode := false;
762 if (wdt < 1) or (hgt < 1) then exit;
764 if (wdt mod e_Textures[ID].tx.width = 0) and (hgt mod e_Textures[ID].tx.height = 0) then
765 begin
766 e_DrawFill(id, x, y, wdt div e_Textures[ID].tx.width, hgt div e_Textures[ID].tx.height, alpha, alphachannel, blending, ambientBlendMode);
767 exit;
768 end;
770 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
772 if (Alpha > 0) or AlphaChannel or Blending then
773 begin
774 glEnable(GL_BLEND);
775 end
776 else
777 begin
778 if not ambientBlendMode then glDisable(GL_BLEND);
779 end;
780 if AlphaChannel or (Alpha > 0) then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
781 if (Alpha > 0) then glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
782 if Blending then glBlendFunc(GL_SRC_ALPHA, GL_ONE);
784 glEnable(GL_TEXTURE_2D);
785 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
787 x2 := x+wdt;
788 y2 := y+hgt;
790 //k8: this SHOULD work... i hope
791 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
792 begin
793 glBegin(GL_QUADS);
794 glTexCoord2f(0, hgt/e_Textures[ID].tx.height); glVertex2i(x, y);
795 glTexCoord2f(wdt/e_Textures[ID].tx.width, hgt/e_Textures[ID].tx.height); glVertex2i(x2, y);
796 glTexCoord2f(wdt/e_Textures[ID].tx.width, 0); glVertex2i(x2, y2);
797 glTexCoord2f(0, 0); glVertex2i(x, y2);
798 glEnd();
799 end
800 else
801 begin
802 // hard day's night; setup scissor
804 glGetIntegerv(GL_VIEWPORT, @vpxywh[0]);
805 wassc := (glIsEnabled(GL_SCISSOR_TEST) <> 0);
806 if wassc then glGetIntegerv(GL_SCISSOR_BOX, @scxywh[0]) else glGetIntegerv(GL_VIEWPORT, @scxywh[0]);
807 //writeln('(', scxywh[0], ',', scxywh[1], ')-(', scxywh[2], ',', scxywh[3], ')');
808 //glEnable(GL_SCISSOR_TEST);
809 setScissorGLInternal(x, y, wdt, hgt);
811 // draw quads
812 u := e_Textures[ID].tx.u;
813 v := e_Textures[ID].tx.v;
814 w := e_Textures[ID].tx.width;
815 h := e_Textures[ID].tx.height;
816 x2 := x;
817 if (hgt > h) then begin y += hgt-h; onlyOneY := false; end else onlyOneY := true;
818 glBegin(GL_QUADS);
819 while (hgt > 0) do
820 begin
821 if (hgt >= h) then begin ch := h; cv := v; yofs := 0; end else begin ch := hgt; cv := v/(h/hgt); yofs := h-hgt; end;
822 if onlyOneY then yofs := 0;
823 Dec(hgt, h);
824 dw := wdt;
825 x := x2;
826 while (dw > 0) do
827 begin
828 if (dw >= w) then begin cw := w; cu := u; end else begin cw := dw; cu := u/(w/dw); end;
829 Dec(dw, w);
830 glTexCoord2f(0, cv); glVertex2i(X, Y+yofs);
831 glTexCoord2f(cu, cv); glVertex2i(X+cw, Y+yofs);
832 glTexCoord2f(cu, 0); glVertex2i(X+cw, Y+ch+yofs);
833 glTexCoord2f(0, 0); glVertex2i(X, Y+ch+yofs);
834 Inc(X, w);
835 end;
836 Dec(Y, h);
837 end;
838 glEnd();
839 //if wassc then glEnable(GL_SCISSOR_TEST) else glDisable(GL_SCISSOR_TEST);
840 end;
842 glDisable(GL_BLEND);
843 end;
846 procedure e_AmbientQuad (x, y, w, h: Integer; r, g, b, a: Byte);
847 begin
848 if e_NoGraphics then exit;
849 if (w < 1) or (h < 1) then exit;
850 if (a <> 255) or ((r or g or b) <> 0) then
851 begin
852 glEnable(GL_BLEND);
853 glDisable(GL_TEXTURE_2D);
854 glColor4ub(r, g, b, a);
855 if ((r or g or b) <> 0) then
856 begin
857 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
858 glBegin(GL_QUADS);
859 glVertex2i(x, y);
860 glVertex2i(x+w, y);
861 glVertex2i(x+w, y+h);
862 glVertex2i(x, y+h);
863 glEnd();
864 end;
865 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
866 glBegin(GL_QUADS);
867 glVertex2i(x, y);
868 glVertex2i(x+w, y);
869 glVertex2i(x+w, y+h);
870 glVertex2i(x, y+h);
871 glEnd();
872 glDisable(GL_BLEND);
873 end;
874 end;
877 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
878 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = TMirrorType.None);
879 begin
880 if e_NoGraphics then Exit;
882 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
884 if (Alpha > 0) or (AlphaChannel) or (Blending) then
885 glEnable(GL_BLEND)
886 else
887 glDisable(GL_BLEND);
889 if (AlphaChannel) or (Alpha > 0) then
890 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
892 if Alpha > 0 then
893 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
895 if Blending then
896 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
898 if (Angle <> 0) and (RC <> nil) then
899 begin
900 glPushMatrix();
901 glTranslatef(X+RC.X, Y+RC.Y, 0);
902 glRotatef(Angle, 0, 0, 1);
903 glTranslatef(-(X+RC.X), -(Y+RC.Y), 0);
904 end;
906 glEnable(GL_TEXTURE_2D);
907 glBindTexture(GL_TEXTURE_2D, e_Textures[id].tx.id);
908 glBegin(GL_QUADS); //0-1 1-1
909 //00 10
910 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);
911 glEnd();
913 if Angle <> 0 then
914 glPopMatrix();
916 glDisable(GL_BLEND);
917 end;
919 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
920 begin
921 if e_NoGraphics then Exit;
922 glDisable(GL_TEXTURE_2D);
923 glColor3ub(Red, Green, Blue);
924 glPointSize(Size);
926 if (Size = 2) or (Size = 4) then
927 X := X + 1;
929 glBegin(GL_POINTS);
930 glVertex2f(X+0.3, Y+1.0);
931 glEnd();
933 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
934 end;
936 procedure e_LineCorrection(var X1, Y1, X2, Y2: Integer);
937 begin
938 // Make lines only top-left/bottom-right and top-right/bottom-left
939 if Y2 < Y1 then
940 begin
941 X1 := X1 xor X2;
942 X2 := X1 xor X2;
943 X1 := X1 xor X2;
945 Y1 := Y1 xor Y2;
946 Y2 := Y1 xor Y2;
947 Y1 := Y1 xor Y2;
948 end;
950 // Pixel-perfect hack
951 if X1 < X2 then
952 Inc(X2)
953 else
954 Inc(X1);
955 Inc(Y2);
956 end;
958 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
959 var
960 nX1, nY1, nX2, nY2: Integer;
961 begin
962 {$IFNDEF USE_NANOGL} // FIXIT: nanoGL doesn't support glBegin(GL_LINES)
963 if e_NoGraphics then Exit;
964 // Only top-left/bottom-right quad
965 if X1 > X2 then
966 begin
967 X1 := X1 xor X2;
968 X2 := X1 xor X2;
969 X1 := X1 xor X2;
970 end;
971 if Y1 > Y2 then
972 begin
973 Y1 := Y1 xor Y2;
974 Y2 := Y1 xor Y2;
975 Y1 := Y1 xor Y2;
976 end;
978 if Alpha > 0 then
979 begin
980 glEnable(GL_BLEND);
981 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
982 end else
983 glDisable(GL_BLEND);
985 glDisable(GL_TEXTURE_2D);
986 glColor4ub(Red, Green, Blue, 255-Alpha);
987 glLineWidth(1);
989 glBegin(GL_LINES);
990 nX1 := X1; nY1 := Y1;
991 nX2 := X2; nY2 := Y1;
992 e_LineCorrection(nX1, nY1, nX2, nY2); // Pixel-perfect lines
993 glVertex2i(nX1, nY1);
994 glVertex2i(nX2, nY2);
996 nX1 := X2; nY1 := Y1;
997 nX2 := X2; nY2 := Y2;
998 e_LineCorrection(nX1, nY1, nX2, nY2);
999 glVertex2i(nX1, nY1);
1000 glVertex2i(nX2, nY2);
1002 nX1 := X2; nY1 := Y2;
1003 nX2 := X1; nY2 := Y2;
1004 e_LineCorrection(nX1, nY1, nX2, nY2);
1005 glVertex2i(nX1, nY1);
1006 glVertex2i(nX2, nY2);
1008 nX1 := X1; nY1 := Y2;
1009 nX2 := X1; nY2 := Y1;
1010 e_LineCorrection(nX1, nY1, nX2, nY2);
1011 glVertex2i(nX1, nY1);
1012 glVertex2i(nX2, nY2);
1013 glEnd();
1015 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1017 glDisable(GL_BLEND);
1018 {$ENDIF}
1019 end;
1021 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
1022 Blending: TBlending = TBlending.None);
1023 begin
1024 if e_NoGraphics then Exit;
1025 if (Alpha > 0) or (Blending <> TBlending.None) then
1026 glEnable(GL_BLEND)
1027 else
1028 glDisable(GL_BLEND);
1030 if Blending = TBlending.Blend then
1031 glBlendFunc(GL_SRC_ALPHA, GL_ONE)
1032 else
1033 if Blending = TBlending.Filter then
1034 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR)
1035 else
1036 if Blending = TBlending.Invert then
1037 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO)
1038 else
1039 if Alpha > 0 then
1040 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1042 glDisable(GL_TEXTURE_2D);
1043 glColor4ub(Red, Green, Blue, 255-Alpha);
1045 X2 := X2 + 1;
1046 Y2 := Y2 + 1;
1048 glBegin(GL_QUADS);
1049 glVertex2i(X1, Y1);
1050 glVertex2i(X2, Y1);
1051 glVertex2i(X2, Y2);
1052 glVertex2i(X1, Y2);
1053 glEnd();
1055 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1057 glDisable(GL_BLEND);
1058 end;
1061 // ////////////////////////////////////////////////////////////////////////// //
1062 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
1063 begin
1064 if (a < 0) then a := 0;
1065 if (a > 255) then a := 255;
1066 glEnable(GL_BLEND);
1067 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
1068 glDisable(GL_TEXTURE_2D);
1069 glColor4ub(0, 0, 0, Byte(255-a));
1070 glBegin(GL_QUADS);
1071 glVertex2i(x0, y0);
1072 glVertex2i(x1, y0);
1073 glVertex2i(x1, y1);
1074 glVertex2i(x0, y1);
1075 glEnd();
1076 //glRect(x, y, x+w, y+h);
1077 glColor4ub(1, 1, 1, 1);
1078 glDisable(GL_BLEND);
1079 //glBlendEquation(GL_FUNC_ADD);
1080 end;
1082 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
1083 begin
1084 if (w > 0) and (h > 0) then e_DarkenQuad(x, y, x+w, y+h, a);
1085 end;
1088 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
1089 {$IFDEF USE_NANOGL}
1090 var
1091 v: array [0..3] of GLfloat;
1092 {$ENDIF}
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);
1110 {$IFDEF USE_NANOGL}
1111 v[0] := X1; v[1] := Y1; v[2] := X2; v[3] := Y2;
1112 glVertexPointer(2, GL_FLOAT, 0, @v[0]);
1113 glEnableClientState(GL_VERTEX_ARRAY);
1114 glDisableClientState(GL_COLOR_ARRAY);
1115 glDisableClientState(GL_NORMAL_ARRAY);
1116 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1117 glDrawArrays(GL_LINES, 0, 4);
1118 {$ELSE}
1119 glBegin(GL_LINES);
1120 glVertex2i(X1, Y1);
1121 glVertex2i(X2, Y2);
1122 glEnd();
1123 {$ENDIF}
1125 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1127 glDisable(GL_BLEND);
1128 end;
1130 //------------------------------------------------------------------
1131 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1132 //------------------------------------------------------------------
1133 procedure e_DeleteTexture(ID: DWORD);
1134 begin
1135 if not e_NoGraphics then
1136 glDeleteTextures(1, @e_Textures[ID].tx.id);
1137 e_Textures[ID].tx.id := 0;
1138 e_Textures[ID].tx.Width := 0;
1139 e_Textures[ID].tx.Height := 0;
1140 end;
1142 //------------------------------------------------------------------
1143 // Óäàëÿåò âñå òåêñòóðû
1144 //------------------------------------------------------------------
1145 procedure e_RemoveAllTextures();
1146 var
1147 i: integer;
1148 begin
1149 if e_Textures = nil then Exit;
1151 for i := 0 to High(e_Textures) do
1152 if e_Textures[i].tx.Width <> 0 then e_DeleteTexture(i);
1153 e_Textures := nil;
1154 end;
1156 //------------------------------------------------------------------
1157 // Óäàëÿåò äâèæîê
1158 //------------------------------------------------------------------
1159 procedure e_ReleaseEngine();
1160 begin
1161 e_RemoveAllTextures;
1162 e_RemoveAllTextureFont;
1163 end;
1165 procedure e_BeginRender();
1166 begin
1167 if e_NoGraphics then Exit;
1168 glEnable(GL_ALPHA_TEST);
1169 glAlphaFunc(GL_GREATER, 0.0);
1170 end;
1172 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
1173 begin
1174 if e_NoGraphics then Exit;
1175 glClearColor(Red, Green, Blue, 0);
1176 glClear(Mask);
1177 end;
1179 procedure e_Clear(); overload;
1180 begin
1181 if e_NoGraphics then Exit;
1182 glClearColor(0, 0, 0, 0);
1183 glClear(GL_COLOR_BUFFER_BIT);
1184 end;
1186 procedure e_EndRender();
1187 begin
1188 if e_NoGraphics then Exit;
1189 glPopMatrix();
1190 end;
1192 function e_GetGamma(win: PSDL_Window): Byte;
1193 var
1194 ramp: array [0..256*3-1] of Word;
1195 rgb: array [0..2] of Double;
1196 sum: double;
1197 count: integer;
1198 min: integer;
1199 max: integer;
1200 A, B: double;
1201 i, j: integer;
1202 begin
1203 Result := 0;
1204 if e_NoGraphics then Exit;
1205 rgb[0] := 1.0;
1206 rgb[1] := 1.0;
1207 rgb[2] := 1.0;
1209 SDL_GetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1211 for i := 0 to 2 do
1212 begin
1213 sum := 0;
1214 count := 0;
1215 min := 256 * i;
1216 max := min + 256;
1218 for j := min to max - 1 do
1219 if ramp[j] > 0 then
1220 begin
1221 B := (j mod 256)/256;
1222 A := ramp[j]/65536;
1223 sum := sum + ln(A)/ln(B);
1224 inc(count);
1225 end;
1226 rgb[i] := sum / count;
1227 end;
1229 Result := 100 - Trunc(((rgb[0] + rgb[1] + rgb[2])/3 - 0.23) * 100/(2.7 - 0.23));
1230 end;
1232 procedure e_SetGamma(win: PSDL_Window; Gamma: Byte);
1233 var
1234 ramp: array [0..256*3-1] of Word;
1235 i: integer;
1236 r: double;
1237 g: double;
1238 begin
1239 if e_NoGraphics then Exit;
1240 g := (100 - Gamma)*(2.7 - 0.23)/100 + 0.23;
1242 for i := 0 to 255 do
1243 begin
1244 r := Exp(g * ln(i/256))*65536;
1245 if r < 0 then r := 0
1246 else if r > 65535 then r := 65535;
1247 ramp[i] := trunc(r);
1248 ramp[i + 256] := trunc(r);
1249 ramp[i + 512] := trunc(r);
1250 end;
1252 SDL_SetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1253 end;
1255 function e_CharFont_Create(sp: ShortInt=0): DWORD;
1256 var
1257 i, id: DWORD;
1258 begin
1259 e_WriteLog('Creating CharFont...', TMsgType.Notify);
1261 id := DWORD(-1);
1263 if e_CharFonts <> nil then
1264 for i := 0 to High(e_CharFonts) do
1265 if not e_CharFonts[i].alive then
1266 begin
1267 id := i;
1268 Break;
1269 end;
1271 if id = DWORD(-1) then
1272 begin
1273 SetLength(e_CharFonts, Length(e_CharFonts) + 1);
1274 id := High(e_CharFonts);
1275 end;
1277 with e_CharFonts[id] do
1278 begin
1279 for i := 0 to High(Chars) do
1280 with Chars[i] do
1281 begin
1282 TextureID := -1;
1283 Width := 0;
1284 end;
1286 Space := sp;
1287 alive := True;
1288 end;
1290 Result := id;
1291 end;
1293 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
1294 begin
1295 with e_CharFonts[FontID].Chars[Ord(c)] do
1296 begin
1297 TextureID := Texture;
1298 Width := w;
1299 end;
1300 end;
1302 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
1303 var
1304 a: Integer;
1305 begin
1306 if e_NoGraphics then Exit;
1307 if Text = '' then Exit;
1308 if e_CharFonts = nil then Exit;
1309 if Integer(FontID) > High(e_CharFonts) then Exit;
1311 with e_CharFonts[FontID] do
1312 begin
1313 for a := 1 to Length(Text) do
1314 with Chars[Ord(Text[a])] do
1315 if TextureID <> -1 then
1316 begin
1317 e_Draw(TextureID, X, Y, 0, True, False);
1318 X := X+Width+IfThen(a = Length(Text), 0, Space);
1319 end;
1320 end;
1321 end;
1323 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
1324 Color: TRGB; Scale: Single = 1.0);
1325 var
1326 a: Integer;
1327 c: TRGB;
1328 begin
1329 if e_NoGraphics then Exit;
1330 if Text = '' then Exit;
1331 if e_CharFonts = nil then Exit;
1332 if Integer(FontID) > High(e_CharFonts) then Exit;
1334 with e_CharFonts[FontID] do
1335 begin
1336 for a := 1 to Length(Text) do
1337 with Chars[Ord(Text[a])] do
1338 if TextureID <> -1 then
1339 begin
1340 if Scale <> 1.0 then
1341 begin
1342 glPushMatrix;
1343 glScalef(Scale, Scale, 0);
1344 end;
1346 c := e_Colors;
1347 e_Colors := Color;
1348 e_Draw(TextureID, X, Y, 0, True, False);
1349 e_Colors := c;
1351 if Scale <> 1.0 then glPopMatrix;
1353 X := X+Width+IfThen(a = Length(Text), 0, Space);
1354 end;
1355 end;
1356 end;
1358 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
1359 var
1360 a, TX, TY, len: Integer;
1361 tc, c: TRGB;
1362 w, h: Word;
1363 begin
1364 if e_NoGraphics then Exit;
1365 if Text = '' then Exit;
1366 if e_CharFonts = nil then Exit;
1367 if Integer(FontID) > High(e_CharFonts) then Exit;
1369 c.R := 255;
1370 c.G := 255;
1371 c.B := 255;
1373 TX := X;
1374 TY := Y;
1375 len := Length(Text);
1377 e_CharFont_GetSize(FontID, 'A', w, h);
1379 with e_CharFonts[FontID] do
1380 begin
1381 for a := 1 to len do
1382 begin
1383 case Text[a] of
1384 #10: // line feed
1385 begin
1386 TX := X;
1387 TY := TY + h;
1388 continue;
1389 end;
1390 #1: // black
1391 begin
1392 c.R := 0; c.G := 0; c.B := 0;
1393 continue;
1394 end;
1395 #2: // white
1396 begin
1397 c.R := 255; c.G := 255; c.B := 255;
1398 continue;
1399 end;
1400 #3: // darker
1401 begin
1402 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1403 continue;
1404 end;
1405 #4: // lighter
1406 begin
1407 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1408 continue;
1409 end;
1410 #18: // red
1411 begin
1412 c.R := 255; c.G := 0; c.B := 0;
1413 continue;
1414 end;
1415 #19: // green
1416 begin
1417 c.R := 0; c.G := 255; c.B := 0;
1418 continue;
1419 end;
1420 #20: // blue
1421 begin
1422 c.R := 0; c.G := 0; c.B := 255;
1423 continue;
1424 end;
1425 #21: // yellow
1426 begin
1427 c.R := 255; c.G := 255; c.B := 0;
1428 continue;
1429 end;
1430 end;
1432 with Chars[Ord(Text[a])] do
1433 if TextureID <> -1 then
1434 begin
1435 tc := e_Colors;
1436 e_Colors := c;
1437 e_Draw(TextureID, TX, TY, 0, True, False);
1438 e_Colors := tc;
1440 TX := TX+Width+IfThen(a = Length(Text), 0, Space);
1441 end;
1442 end;
1443 end;
1444 end;
1446 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
1447 var
1448 a: Integer;
1449 h2: Word;
1450 begin
1451 w := 0;
1452 h := 0;
1454 if Text = '' then Exit;
1455 if e_CharFonts = nil then Exit;
1456 if Integer(FontID) > High(e_CharFonts) then Exit;
1458 with e_CharFonts[FontID] do
1459 begin
1460 for a := 1 to Length(Text) do
1461 with Chars[Ord(Text[a])] do
1462 if TextureID <> -1 then
1463 begin
1464 w := w+Width+IfThen(a = Length(Text), 0, Space);
1465 e_GetTextureSize(TextureID, nil, @h2);
1466 if h2 > h then h := h2;
1467 end;
1468 end;
1469 end;
1471 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
1472 var
1473 a, lines, len: Integer;
1474 h2, w2, tw, th: Word;
1475 begin
1476 w2 := 0;
1477 h2 := 0;
1478 tw := 0;
1479 th := 0;
1481 if Text = '' then Exit;
1482 if e_CharFonts = nil then Exit;
1483 if Integer(FontID) > High(e_CharFonts) then Exit;
1485 lines := 1;
1486 len := Length(Text);
1488 with e_CharFonts[FontID] do
1489 begin
1490 for a := 1 to len do
1491 begin
1492 if Text[a] = #10 then
1493 begin
1494 Inc(lines);
1495 if w2 > tw then tw := w2;
1496 w2 := 0;
1497 continue;
1498 end;
1500 with Chars[Ord(Text[a])] do
1501 if TextureID <> -1 then
1502 begin
1503 w2 := w2 + Width + IfThen(a = len, 0, Space);
1504 e_GetTextureSize(TextureID, nil, @h2);
1505 if h2 > th then th := h2;
1506 end;
1507 end;
1508 end;
1510 if w2 > tw then
1511 tw := w2;
1513 w := tw;
1514 h := th * lines;
1515 end;
1517 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
1518 var
1519 a: Integer;
1520 begin
1521 Result := 0;
1523 if e_CharFonts = nil then Exit;
1524 if Integer(FontID) > High(e_CharFonts) then Exit;
1526 for a := 0 to High(e_CharFonts[FontID].Chars) do
1527 Result := Max(Result, e_CharFonts[FontID].Chars[a].Width);
1528 end;
1530 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
1531 var
1532 a: Integer;
1533 h2: Word;
1534 begin
1535 Result := 0;
1537 if e_CharFonts = nil then Exit;
1538 if Integer(FontID) > High(e_CharFonts) then Exit;
1540 for a := 0 to High(e_CharFonts[FontID].Chars) do
1541 begin
1542 if e_CharFonts[FontID].Chars[a].TextureID <> -1 then
1543 e_GetTextureSize(e_CharFonts[FontID].Chars[a].TextureID, nil, @h2)
1544 else h2 := 0;
1545 if h2 > Result then Result := h2;
1546 end;
1547 end;
1549 procedure e_CharFont_Remove(FontID: DWORD);
1550 var
1551 a: Integer;
1552 begin
1553 with e_CharFonts[FontID] do
1554 for a := 0 to High(Chars) do
1555 if Chars[a].TextureID <> -1 then e_DeleteTexture(Chars[a].TextureID);
1557 e_CharFonts[FontID].alive := False;
1558 end;
1560 procedure e_CharFont_RemoveAll();
1561 var
1562 a: Integer;
1563 begin
1564 if e_CharFonts = nil then Exit;
1566 for a := 0 to High(e_CharFonts) do
1567 e_CharFont_Remove(a);
1569 e_CharFonts := nil;
1570 end;
1572 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
1573 Space: ShortInt=0);
1574 var
1575 loop1 : GLuint;
1576 cx, cy : real;
1577 i, id: DWORD;
1578 begin
1579 if e_NoGraphics then Exit;
1580 e_WriteLog('Creating texture font...', TMsgType.Notify);
1582 id := DWORD(-1);
1584 if e_TextureFonts <> nil then
1585 for i := 0 to High(e_TextureFonts) do
1586 if e_TextureFonts[i].Base = 0 then
1587 begin
1588 id := i;
1589 Break;
1590 end;
1592 if id = DWORD(-1) then
1593 begin
1594 SetLength(e_TextureFonts, Length(e_TextureFonts) + 1);
1595 id := High(e_TextureFonts);
1596 end;
1598 with e_TextureFonts[id] do
1599 begin
1600 {$IF not DEFINED(USE_NANOGL)}
1601 Base := glGenLists(XCount*YCount);
1602 {$ENDIF}
1603 TextureID := e_Textures[Tex].tx.id;
1604 CharWidth := (e_Textures[Tex].tx.Width div XCount)+Space;
1605 CharHeight := e_Textures[Tex].tx.Height div YCount;
1606 XC := XCount;
1607 YC := YCount;
1608 Texture := Tex;
1609 SPC := Space;
1610 end;
1612 {$IF not DEFINED(USE_NANOGL)}
1613 glBindTexture(GL_TEXTURE_2D, e_Textures[Tex].tx.id);
1614 for loop1 := 0 to XCount*YCount-1 do
1615 begin
1616 cx := (loop1 mod XCount)/XCount;
1617 cy := (loop1 div YCount)/YCount;
1619 glNewList(e_TextureFonts[id].Base+loop1, GL_COMPILE);
1620 glBegin(GL_QUADS);
1621 glTexCoord2f(cx, 1.0-cy-1/YCount);
1622 glVertex2i(0, e_Textures[Tex].tx.Height div YCount);
1624 glTexCoord2f(cx+1/XCount, 1.0-cy-1/YCount);
1625 glVertex2i(e_Textures[Tex].tx.Width div XCount, e_Textures[Tex].tx.Height div YCount);
1627 glTexCoord2f(cx+1/XCount, 1.0-cy);
1628 glVertex2i(e_Textures[Tex].tx.Width div XCount, 0);
1630 glTexCoord2f(cx, 1.0-cy);
1631 glVertex2i(0, 0);
1632 glEnd();
1633 glTranslated((e_Textures[Tex].tx.Width div XCount)+Space, 0, 0);
1634 glEndList();
1635 end;
1636 {$ENDIF}
1638 FontID := id;
1639 end;
1641 procedure e_TextureFontKill(FontID: DWORD);
1642 begin
1643 if e_NoGraphics then Exit;
1644 {$IF not DEFINED(USE_NANOGL)}
1645 glDeleteLists(e_TextureFonts[FontID].Base, 256);
1646 {$ENDIF}
1647 e_TextureFonts[FontID].Base := 0;
1648 end;
1650 {$IFDEF USE_NANOGL}
1651 procedure e_TextureFontDrawChar(ch: Char; FontID: DWORD);
1652 var
1653 index: Integer;
1654 cx, cy: GLfloat;
1655 Tex: Integer;
1656 Width, Height: Integer;
1657 XCount, YCount: Integer;
1658 begin
1659 index := Ord(ch) - 32;
1660 Tex := e_TextureFonts[FontID].Texture;
1661 Width := e_Textures[Tex].tx.Width;
1662 Height := e_Textures[Tex].tx.Height;
1663 XCount := e_TextureFonts[FontID].XC;
1664 YCount := e_TextureFonts[FontID].YC;
1665 cx := (index mod XCount)/XCount;
1666 cy := (index div YCount)/YCount;
1667 glBegin(GL_QUADS);
1668 glTexCoord2f(cx, 1 - cy - 1/YCount);
1669 glVertex2i(0, Height div YCount);
1670 glTexCoord2f(cx + 1/XCount, 1 - cy - 1/YCount);
1671 glVertex2i(Width div XCount, Height div YCount);
1672 glTexCoord2f(cx + 1/XCount, 1 - cy);
1673 glVertex2i(Width div XCount, 0);
1674 glTexCoord2f(cx, 1 - cy);
1675 glVertex2i(0, 0);
1676 glEnd();
1677 glTranslatef((e_Textures[Tex].tx.Width div XCount) + e_TextureFonts[FontID].SPC, 0, 0);
1678 end;
1680 procedure e_TextureFontDrawString(Text: String; FontID: DWORD);
1681 var
1682 i: Integer;
1683 begin
1684 for i := 1 to High(Text) do
1685 e_TextureFontDrawChar(Text[i], FontID);
1686 end;
1687 {$ENDIF}
1689 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
1690 begin
1691 if e_NoGraphics then Exit;
1692 if Integer(FontID) > High(e_TextureFonts) then Exit;
1693 if Text = '' then Exit;
1695 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1696 glEnable(GL_BLEND);
1698 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1700 glPushMatrix;
1701 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1702 glEnable(GL_TEXTURE_2D);
1703 glTranslatef(x, y, 0);
1704 {$IFDEF USE_NANOGL}
1705 e_TextureFontDrawString(Text, FontID);
1706 {$ELSE}
1707 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1708 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1709 {$ENDIF}
1710 glDisable(GL_TEXTURE_2D);
1711 glPopMatrix;
1713 glDisable(GL_BLEND);
1714 end;
1716 // god forgive me for this, but i cannot figure out how to do it without lists
1717 procedure e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1718 begin
1719 if e_NoGraphics then Exit;
1720 glPushMatrix;
1722 if Shadow then
1723 begin
1724 glColor4ub(0, 0, 0, 128);
1725 glTranslatef(X+1, Y+1, 0);
1726 {$IFDEF USE_NANOGL}
1727 e_TextureFontDrawChar(Ch, FontID);
1728 {$ELSE}
1729 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1730 {$ENDIF}
1731 glPopMatrix;
1732 glPushMatrix;
1733 end;
1735 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1736 glTranslatef(X, Y, 0);
1737 {$IFDEF USE_NANOGL}
1738 e_TextureFontDrawChar(Ch, FontID);
1739 {$ELSE}
1740 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1741 {$ENDIF}
1743 glPopMatrix;
1744 end;
1746 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1747 begin
1748 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1749 glEnable(GL_TEXTURE_2D);
1750 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1752 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1753 glEnable(GL_BLEND);
1754 e_TextureFontPrintChar(X, Y, Ch, FontID, Shadow);
1755 glDisable(GL_TEXTURE_2D);
1756 glDisable(GL_BLEND);
1757 end;
1759 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
1760 begin
1761 result := e_TextureFonts[FontID].CharWidth;
1762 end;
1764 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD; Shadow: Boolean = False);
1765 var
1766 a, TX, TY, len: Integer;
1767 tc, c: TRGB;
1768 w: Word;
1769 begin
1770 if e_NoGraphics then Exit;
1771 if Text = '' then Exit;
1772 if e_TextureFonts = nil then Exit;
1773 if Integer(FontID) > High(e_TextureFonts) then Exit;
1775 c.R := 255;
1776 c.G := 255;
1777 c.B := 255;
1779 TX := X;
1780 TY := Y;
1781 len := Length(Text);
1783 w := e_TextureFonts[FontID].CharWidth;
1785 with e_TextureFonts[FontID] do
1786 begin
1787 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1788 glEnable(GL_TEXTURE_2D);
1790 {$IF not DEFINED(USE_NANOGL)}
1791 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1792 {$ENDIF}
1794 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1795 glEnable(GL_BLEND);
1797 for a := 1 to len do
1798 begin
1799 case Text[a] of
1800 {#10: // line feed
1801 begin
1802 TX := X;
1803 TY := TY + h;
1804 continue;
1805 end;}
1806 #1: // black
1807 begin
1808 c.R := 0; c.G := 0; c.B := 0;
1809 continue;
1810 end;
1811 #2: // white
1812 begin
1813 c.R := 255; c.G := 255; c.B := 255;
1814 continue;
1815 end;
1816 #3: // darker
1817 begin
1818 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1819 continue;
1820 end;
1821 #4: // lighter
1822 begin
1823 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1824 continue;
1825 end;
1826 #18: // red
1827 begin
1828 c.R := 255; c.G := 0; c.B := 0;
1829 continue;
1830 end;
1831 #19: // green
1832 begin
1833 c.R := 0; c.G := 255; c.B := 0;
1834 continue;
1835 end;
1836 #20: // blue
1837 begin
1838 c.R := 0; c.G := 0; c.B := 255;
1839 continue;
1840 end;
1841 #21: // yellow
1842 begin
1843 c.R := 255; c.G := 255; c.B := 0;
1844 continue;
1845 end;
1846 end;
1848 tc := e_Colors;
1849 e_Colors := c;
1850 e_TextureFontPrintChar(TX, TY, Text[a], FontID, Shadow);
1851 e_Colors := tc;
1853 TX := TX+w;
1854 end;
1855 glDisable(GL_TEXTURE_2D);
1856 glDisable(GL_BLEND);
1857 end;
1858 end;
1860 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
1861 Blue: Byte; Scale: Single; Shadow: Boolean = False);
1862 begin
1863 if e_NoGraphics then Exit;
1864 if Text = '' then Exit;
1866 glPushMatrix;
1867 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1868 glEnable(GL_TEXTURE_2D);
1870 {$IF not DEFINED(USE_NANOGL)}
1871 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1872 {$ENDIF}
1874 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1875 glEnable(GL_BLEND);
1877 if Shadow then
1878 begin
1879 glColor4ub(0, 0, 0, 128);
1880 glTranslatef(x+1, y+1, 0);
1881 glScalef(Scale, Scale, 0);
1882 {$IFDEF USE_NANOGL}
1883 e_TextureFontDrawString(Text, FontID);
1884 {$ELSE}
1885 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1886 {$ENDIF}
1887 glPopMatrix;
1888 glPushMatrix;
1889 end;
1891 glColor4ub(Red, Green, Blue, 255);
1892 glTranslatef(x, y, 0);
1893 glScalef(Scale, Scale, 0);
1894 {$IFDEF USE_NANOGL}
1895 e_TextureFontDrawString(Text, FontID);
1896 {$ELSE}
1897 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1898 {$ENDIF}
1900 glDisable(GL_TEXTURE_2D);
1901 glPopMatrix;
1902 glColor3ub(e_Colors.R, e_Colors.G, e_Colors.B);
1903 glDisable(GL_BLEND);
1904 end;
1906 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
1907 begin
1908 CharWidth := 16;
1909 CharHeight := 16;
1910 if e_NoGraphics then Exit;
1911 if Integer(ID) > High(e_TextureFonts) then
1912 Exit;
1913 CharWidth := e_TextureFonts[ID].CharWidth;
1914 CharHeight := e_TextureFonts[ID].CharHeight;
1915 end;
1917 procedure e_RemoveAllTextureFont();
1918 var
1919 i: integer;
1920 begin
1921 if e_NoGraphics then Exit;
1922 if e_TextureFonts = nil then Exit;
1924 for i := 0 to High(e_TextureFonts) do
1925 if e_TextureFonts[i].Base <> 0 then
1926 begin
1927 {$IFNDEF USE_NANOGL}
1928 glDeleteLists(e_TextureFonts[i].Base, 256);
1929 {$ENDIF}
1930 e_TextureFonts[i].Base := 0;
1931 end;
1933 e_TextureFonts := nil;
1934 end;
1936 function _RGB(Red, Green, Blue: Byte): TRGB;
1937 begin
1938 Result.R := Red;
1939 Result.G := Green;
1940 Result.B := Blue;
1941 end;
1943 function _Point(X, Y: Integer): TPoint2i;
1944 begin
1945 Result.X := X;
1946 Result.Y := Y;
1947 end;
1949 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
1950 begin
1951 Result.X := X;
1952 Result.Y := Y;
1953 Result.Width := Width;
1954 Result.Height := Height;
1955 end;
1957 function _TRect(L, T, R, B: LongInt): TRect;
1958 begin
1959 Result.Top := T;
1960 Result.Left := L;
1961 Result.Right := R;
1962 Result.Bottom := B;
1963 end;
1966 procedure e_MakeScreenshot (st: TStream; Width, Height: Word);
1967 var
1968 pixels, obuf, scln, ps, pd: PByte;
1969 obufsize: Integer;
1970 dlen: Cardinal;
1971 i, x, y, res: Integer;
1972 sign: array [0..7] of Byte;
1973 hbuf: array [0..12] of Byte;
1974 crc: LongWord;
1975 img: TImageData;
1976 clr: TColor32Rec;
1977 begin
1978 if e_NoGraphics then Exit;
1979 obuf := nil;
1981 // first, extract and pack graphics data
1982 if (Width mod 4) > 0 then Width := Width+4-(Width mod 4);
1984 GetMem(pixels, Width*Height*3);
1985 try
1986 FillChar(pixels^, Width*Height*3, 0);
1987 glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
1988 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1990 if e_FastScreenshots then
1991 begin
1992 // create scanlines
1993 GetMem(scln, (Width*3+1)*Height);
1994 try
1995 ps := pixels;
1996 pd := scln;
1997 Inc(ps, (Width*3)*(Height-1));
1998 for i := 0 to Height-1 do
1999 begin
2000 pd^ := 0; // filter
2001 Inc(pd);
2002 Move(ps^, pd^, Width*3);
2003 Dec(ps, Width*3);
2004 Inc(pd, Width*3);
2005 end;
2006 except
2007 FreeMem(scln);
2008 raise;
2009 end;
2010 FreeMem(pixels);
2011 pixels := scln;
2013 // pack it
2014 obufsize := (Width*3+1)*Height*2;
2015 GetMem(obuf, obufsize);
2016 try
2017 while true do
2018 begin
2019 dlen := obufsize;
2020 res := compress2(Pointer(obuf), dlen, Pointer(pixels), (Width*3+1)*Height, 9);
2021 if res = Z_OK then break;
2022 if res <> Z_BUF_ERROR then raise Exception.Create('can''t pack data for PNG');
2023 obufsize := obufsize*2;
2024 FreeMem(obuf);
2025 obuf := nil;
2026 GetMem(obuf, obufsize);
2027 end;
2028 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
2030 // now write PNG
2032 // signature
2033 sign[0] := 137;
2034 sign[1] := 80;
2035 sign[2] := 78;
2036 sign[3] := 71;
2037 sign[4] := 13;
2038 sign[5] := 10;
2039 sign[6] := 26;
2040 sign[7] := 10;
2041 st.writeBuffer(sign, 8);
2042 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2044 // header
2045 writeIntBE(st, LongWord(13));
2046 sign[0] := 73;
2047 sign[1] := 72;
2048 sign[2] := 68;
2049 sign[3] := 82;
2050 st.writeBuffer(sign, 4);
2051 crc := crc32(0, @sign[0], 4);
2052 hbuf[0] := 0;
2053 hbuf[1] := 0;
2054 hbuf[2] := (Width shr 8) and $ff;
2055 hbuf[3] := Width and $ff;
2056 hbuf[4] := 0;
2057 hbuf[5] := 0;
2058 hbuf[6] := (Height shr 8) and $ff;
2059 hbuf[7] := Height and $ff;
2060 hbuf[8] := 8; // bit depth
2061 hbuf[9] := 2; // RGB
2062 hbuf[10] := 0; // compression method
2063 hbuf[11] := 0; // filter method
2064 hbuf[12] := 0; // no interlace
2065 crc := crc32(crc, @hbuf[0], 13);
2066 st.writeBuffer(hbuf, 13);
2067 writeIntBE(st, crc);
2068 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2070 // image data
2071 writeIntBE(st, LongWord(dlen));
2072 sign[0] := 73;
2073 sign[1] := 68;
2074 sign[2] := 65;
2075 sign[3] := 84;
2076 st.writeBuffer(sign, 4);
2077 crc := crc32(0, @sign[0], 4);
2078 crc := crc32(crc, obuf, dlen);
2079 st.writeBuffer(obuf^, dlen);
2080 writeIntBE(st, crc);
2081 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2083 // image data end
2084 writeIntBE(st, LongWord(0));
2085 sign[0] := 73;
2086 sign[1] := 69;
2087 sign[2] := 78;
2088 sign[3] := 68;
2089 st.writeBuffer(sign, 4);
2090 crc := crc32(0, @sign[0], 4);
2091 writeIntBE(st, crc);
2092 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2093 finally
2094 if obuf <> nil then FreeMem(obuf);
2095 end;
2096 end
2097 else
2098 begin
2099 Imaging.SetOption(ImagingPNGCompressLevel, 9);
2100 Imaging.SetOption(ImagingPNGPreFilter, 6);
2101 InitImage(img);
2102 try
2103 NewImage(Width, Height, TImageFormat.ifR8G8B8, img);
2104 ps := pixels;
2105 //writeln(stderr, 'moving pixels...');
2106 for y := Height-1 downto 0 do
2107 begin
2108 for x := 0 to Width-1 do
2109 begin
2110 clr.r := ps^; Inc(ps);
2111 clr.g := ps^; Inc(ps);
2112 clr.b := ps^; Inc(ps);
2113 clr.a := 255;
2114 SetPixel32(img, x, y, clr);
2115 end;
2116 end;
2117 GlobalMetadata.ClearMetaItems();
2118 GlobalMetadata.ClearMetaItemsForSaving();
2119 //writeln(stderr, 'compressing image...');
2120 if not SaveImageToStream('png', st, img) then raise Exception.Create('screenshot writing error');
2121 //writeln(stderr, 'done!');
2122 finally
2123 FreeImage(img);
2124 end;
2125 end;
2126 finally
2127 FreeMem(pixels);
2128 end;
2129 end;
2132 end.