DEADSOFTWARE

Added touchscreen controls
[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 {$IFDEF USE_NANOGL}
962 v: array [0..15] of GLfloat;
963 {$ENDIF}
964 begin
965 if e_NoGraphics then Exit;
966 // Only top-left/bottom-right quad
967 if X1 > X2 then
968 begin
969 X1 := X1 xor X2;
970 X2 := X1 xor X2;
971 X1 := X1 xor X2;
972 end;
973 if Y1 > Y2 then
974 begin
975 Y1 := Y1 xor Y2;
976 Y2 := Y1 xor Y2;
977 Y1 := Y1 xor Y2;
978 end;
980 if Alpha > 0 then
981 begin
982 glEnable(GL_BLEND);
983 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
984 end else
985 glDisable(GL_BLEND);
987 glDisable(GL_TEXTURE_2D);
988 glColor4ub(Red, Green, Blue, 255-Alpha);
989 glLineWidth(1);
990 {$IFDEF USE_NANOGL}
991 nX1 := X1; nY1 := Y1;
992 nX2 := X2; nY2 := Y1;
993 e_LineCorrection(nX1, nY1, nX2, nY2);
994 v[0] := nX1; v[1] := nY1; v[2] := nX2; v[3] := nY2;
996 nX1 := X2; nY1 := Y1;
997 nX2 := X2; nY2 := Y2;
998 e_LineCorrection(nX1, nY1, nX2, nY2);
999 v[4] := nX1; v[5] := nY1; v[6] := nX2; v[7] := nY2;
1001 nX1 := X2; nY1 := Y2;
1002 nX2 := X1; nY2 := Y2;
1003 e_LineCorrection(nX1, nY1, nX2, nY2);
1004 v[8] := nX1; v[9] := nY1; v[10] := nX2; v[11] := nY2;
1006 nX1 := X1; nY1 := Y2;
1007 nX2 := X1; nY2 := Y1;
1008 e_LineCorrection(nX1, nY1, nX2, nY2);
1009 v[12] := nX1; v[13] := nY1; v[14] := nX2; v[15] := nY2;
1011 glVertexPointer(2, GL_FLOAT, 0, @v[0]);
1012 glEnableClientState(GL_VERTEX_ARRAY);
1013 glDisableClientState(GL_COLOR_ARRAY);
1014 glDisableClientState(GL_NORMAL_ARRAY);
1015 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1016 glDrawArrays(GL_LINES, 0, 16);
1017 {$ELSE}
1018 glBegin(GL_LINES);
1019 nX1 := X1; nY1 := Y1;
1020 nX2 := X2; nY2 := Y1;
1021 e_LineCorrection(nX1, nY1, nX2, nY2); // Pixel-perfect lines
1022 glVertex2i(nX1, nY1);
1023 glVertex2i(nX2, nY2);
1025 nX1 := X2; nY1 := Y1;
1026 nX2 := X2; nY2 := Y2;
1027 e_LineCorrection(nX1, nY1, nX2, nY2);
1028 glVertex2i(nX1, nY1);
1029 glVertex2i(nX2, nY2);
1031 nX1 := X2; nY1 := Y2;
1032 nX2 := X1; nY2 := Y2;
1033 e_LineCorrection(nX1, nY1, nX2, nY2);
1034 glVertex2i(nX1, nY1);
1035 glVertex2i(nX2, nY2);
1037 nX1 := X1; nY1 := Y2;
1038 nX2 := X1; nY2 := Y1;
1039 e_LineCorrection(nX1, nY1, nX2, nY2);
1040 glVertex2i(nX1, nY1);
1041 glVertex2i(nX2, nY2);
1042 glEnd();
1043 {$ENDIF}
1045 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1047 glDisable(GL_BLEND);
1048 end;
1050 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
1051 Blending: TBlending = TBlending.None);
1052 begin
1053 if e_NoGraphics then Exit;
1054 if (Alpha > 0) or (Blending <> TBlending.None) then
1055 glEnable(GL_BLEND)
1056 else
1057 glDisable(GL_BLEND);
1059 if Blending = TBlending.Blend then
1060 glBlendFunc(GL_SRC_ALPHA, GL_ONE)
1061 else
1062 if Blending = TBlending.Filter then
1063 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR)
1064 else
1065 if Blending = TBlending.Invert then
1066 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO)
1067 else
1068 if Alpha > 0 then
1069 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1071 glDisable(GL_TEXTURE_2D);
1072 glColor4ub(Red, Green, Blue, 255-Alpha);
1074 X2 := X2 + 1;
1075 Y2 := Y2 + 1;
1077 glBegin(GL_QUADS);
1078 glVertex2i(X1, Y1);
1079 glVertex2i(X2, Y1);
1080 glVertex2i(X2, Y2);
1081 glVertex2i(X1, Y2);
1082 glEnd();
1084 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1086 glDisable(GL_BLEND);
1087 end;
1090 // ////////////////////////////////////////////////////////////////////////// //
1091 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
1092 begin
1093 if (a < 0) then a := 0;
1094 if (a > 255) then a := 255;
1095 glEnable(GL_BLEND);
1096 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
1097 glDisable(GL_TEXTURE_2D);
1098 glColor4ub(0, 0, 0, Byte(255-a));
1099 glBegin(GL_QUADS);
1100 glVertex2i(x0, y0);
1101 glVertex2i(x1, y0);
1102 glVertex2i(x1, y1);
1103 glVertex2i(x0, y1);
1104 glEnd();
1105 //glRect(x, y, x+w, y+h);
1106 glColor4ub(1, 1, 1, 1);
1107 glDisable(GL_BLEND);
1108 //glBlendEquation(GL_FUNC_ADD);
1109 end;
1111 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
1112 begin
1113 if (w > 0) and (h > 0) then e_DarkenQuad(x, y, x+w, y+h, a);
1114 end;
1117 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
1118 {$IFDEF USE_NANOGL}
1119 var
1120 v: array [0..3] of GLfloat;
1121 {$ENDIF}
1122 begin
1123 if e_NoGraphics then Exit;
1124 // Pixel-perfect lines
1125 if Width = 1 then
1126 e_LineCorrection(X1, Y1, X2, Y2);
1128 if Alpha > 0 then
1129 begin
1130 glEnable(GL_BLEND);
1131 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1132 end else
1133 glDisable(GL_BLEND);
1135 glDisable(GL_TEXTURE_2D);
1136 glColor4ub(Red, Green, Blue, 255-Alpha);
1137 glLineWidth(Width);
1139 {$IFDEF USE_NANOGL}
1140 v[0] := X1; v[1] := Y1; v[2] := X2; v[3] := Y2;
1141 glVertexPointer(2, GL_FLOAT, 0, @v[0]);
1142 glEnableClientState(GL_VERTEX_ARRAY);
1143 glDisableClientState(GL_COLOR_ARRAY);
1144 glDisableClientState(GL_NORMAL_ARRAY);
1145 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1146 glDrawArrays(GL_LINES, 0, 4);
1147 {$ELSE}
1148 glBegin(GL_LINES);
1149 glVertex2i(X1, Y1);
1150 glVertex2i(X2, Y2);
1151 glEnd();
1152 {$ENDIF}
1154 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1156 glDisable(GL_BLEND);
1157 end;
1159 //------------------------------------------------------------------
1160 // Óäàëÿåò òåêñòóðó èç ìàññèâà
1161 //------------------------------------------------------------------
1162 procedure e_DeleteTexture(ID: DWORD);
1163 begin
1164 if not e_NoGraphics then
1165 glDeleteTextures(1, @e_Textures[ID].tx.id);
1166 e_Textures[ID].tx.id := 0;
1167 e_Textures[ID].tx.Width := 0;
1168 e_Textures[ID].tx.Height := 0;
1169 end;
1171 //------------------------------------------------------------------
1172 // Óäàëÿåò âñå òåêñòóðû
1173 //------------------------------------------------------------------
1174 procedure e_RemoveAllTextures();
1175 var
1176 i: integer;
1177 begin
1178 if e_Textures = nil then Exit;
1180 for i := 0 to High(e_Textures) do
1181 if e_Textures[i].tx.Width <> 0 then e_DeleteTexture(i);
1182 e_Textures := nil;
1183 end;
1185 //------------------------------------------------------------------
1186 // Óäàëÿåò äâèæîê
1187 //------------------------------------------------------------------
1188 procedure e_ReleaseEngine();
1189 begin
1190 e_RemoveAllTextures;
1191 e_RemoveAllTextureFont;
1192 end;
1194 procedure e_BeginRender();
1195 begin
1196 if e_NoGraphics then Exit;
1197 glEnable(GL_ALPHA_TEST);
1198 glAlphaFunc(GL_GREATER, 0.0);
1199 end;
1201 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
1202 begin
1203 if e_NoGraphics then Exit;
1204 glClearColor(Red, Green, Blue, 0);
1205 glClear(Mask);
1206 end;
1208 procedure e_Clear(); overload;
1209 begin
1210 if e_NoGraphics then Exit;
1211 glClearColor(0, 0, 0, 0);
1212 glClear(GL_COLOR_BUFFER_BIT);
1213 end;
1215 procedure e_EndRender();
1216 begin
1217 if e_NoGraphics then Exit;
1218 glPopMatrix();
1219 end;
1221 function e_GetGamma(win: PSDL_Window): Byte;
1222 var
1223 ramp: array [0..256*3-1] of Word;
1224 rgb: array [0..2] of Double;
1225 sum: double;
1226 count: integer;
1227 min: integer;
1228 max: integer;
1229 A, B: double;
1230 i, j: integer;
1231 begin
1232 Result := 0;
1233 if e_NoGraphics then Exit;
1234 rgb[0] := 1.0;
1235 rgb[1] := 1.0;
1236 rgb[2] := 1.0;
1238 SDL_GetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1240 for i := 0 to 2 do
1241 begin
1242 sum := 0;
1243 count := 0;
1244 min := 256 * i;
1245 max := min + 256;
1247 for j := min to max - 1 do
1248 if ramp[j] > 0 then
1249 begin
1250 B := (j mod 256)/256;
1251 A := ramp[j]/65536;
1252 sum := sum + ln(A)/ln(B);
1253 inc(count);
1254 end;
1255 rgb[i] := sum / count;
1256 end;
1258 Result := 100 - Trunc(((rgb[0] + rgb[1] + rgb[2])/3 - 0.23) * 100/(2.7 - 0.23));
1259 end;
1261 procedure e_SetGamma(win: PSDL_Window; Gamma: Byte);
1262 var
1263 ramp: array [0..256*3-1] of Word;
1264 i: integer;
1265 r: double;
1266 g: double;
1267 begin
1268 if e_NoGraphics then Exit;
1269 g := (100 - Gamma)*(2.7 - 0.23)/100 + 0.23;
1271 for i := 0 to 255 do
1272 begin
1273 r := Exp(g * ln(i/256))*65536;
1274 if r < 0 then r := 0
1275 else if r > 65535 then r := 65535;
1276 ramp[i] := trunc(r);
1277 ramp[i + 256] := trunc(r);
1278 ramp[i + 512] := trunc(r);
1279 end;
1281 SDL_SetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1282 end;
1284 function e_CharFont_Create(sp: ShortInt=0): DWORD;
1285 var
1286 i, id: DWORD;
1287 begin
1288 e_WriteLog('Creating CharFont...', TMsgType.Notify);
1290 id := DWORD(-1);
1292 if e_CharFonts <> nil then
1293 for i := 0 to High(e_CharFonts) do
1294 if not e_CharFonts[i].alive then
1295 begin
1296 id := i;
1297 Break;
1298 end;
1300 if id = DWORD(-1) then
1301 begin
1302 SetLength(e_CharFonts, Length(e_CharFonts) + 1);
1303 id := High(e_CharFonts);
1304 end;
1306 with e_CharFonts[id] do
1307 begin
1308 for i := 0 to High(Chars) do
1309 with Chars[i] do
1310 begin
1311 TextureID := -1;
1312 Width := 0;
1313 end;
1315 Space := sp;
1316 alive := True;
1317 end;
1319 Result := id;
1320 end;
1322 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
1323 begin
1324 with e_CharFonts[FontID].Chars[Ord(c)] do
1325 begin
1326 TextureID := Texture;
1327 Width := w;
1328 end;
1329 end;
1331 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
1332 var
1333 a: Integer;
1334 begin
1335 if e_NoGraphics then Exit;
1336 if Text = '' then Exit;
1337 if e_CharFonts = nil then Exit;
1338 if Integer(FontID) > High(e_CharFonts) then Exit;
1340 with e_CharFonts[FontID] do
1341 begin
1342 for a := 1 to Length(Text) do
1343 with Chars[Ord(Text[a])] do
1344 if TextureID <> -1 then
1345 begin
1346 e_Draw(TextureID, X, Y, 0, True, False);
1347 X := X+Width+IfThen(a = Length(Text), 0, Space);
1348 end;
1349 end;
1350 end;
1352 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
1353 Color: TRGB; Scale: Single = 1.0);
1354 var
1355 a: Integer;
1356 c: TRGB;
1357 begin
1358 if e_NoGraphics then Exit;
1359 if Text = '' then Exit;
1360 if e_CharFonts = nil then Exit;
1361 if Integer(FontID) > High(e_CharFonts) then Exit;
1363 with e_CharFonts[FontID] do
1364 begin
1365 for a := 1 to Length(Text) do
1366 with Chars[Ord(Text[a])] do
1367 if TextureID <> -1 then
1368 begin
1369 if Scale <> 1.0 then
1370 begin
1371 glPushMatrix;
1372 glScalef(Scale, Scale, 0);
1373 end;
1375 c := e_Colors;
1376 e_Colors := Color;
1377 e_Draw(TextureID, X, Y, 0, True, False);
1378 e_Colors := c;
1380 if Scale <> 1.0 then glPopMatrix;
1382 X := X+Width+IfThen(a = Length(Text), 0, Space);
1383 end;
1384 end;
1385 end;
1387 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
1388 var
1389 a, TX, TY, len: Integer;
1390 tc, c: TRGB;
1391 w, h: Word;
1392 begin
1393 if e_NoGraphics then Exit;
1394 if Text = '' then Exit;
1395 if e_CharFonts = nil then Exit;
1396 if Integer(FontID) > High(e_CharFonts) then Exit;
1398 c.R := 255;
1399 c.G := 255;
1400 c.B := 255;
1402 TX := X;
1403 TY := Y;
1404 len := Length(Text);
1406 e_CharFont_GetSize(FontID, 'A', w, h);
1408 with e_CharFonts[FontID] do
1409 begin
1410 for a := 1 to len do
1411 begin
1412 case Text[a] of
1413 #10: // line feed
1414 begin
1415 TX := X;
1416 TY := TY + h;
1417 continue;
1418 end;
1419 #1: // black
1420 begin
1421 c.R := 0; c.G := 0; c.B := 0;
1422 continue;
1423 end;
1424 #2: // white
1425 begin
1426 c.R := 255; c.G := 255; c.B := 255;
1427 continue;
1428 end;
1429 #3: // darker
1430 begin
1431 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1432 continue;
1433 end;
1434 #4: // lighter
1435 begin
1436 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1437 continue;
1438 end;
1439 #18: // red
1440 begin
1441 c.R := 255; c.G := 0; c.B := 0;
1442 continue;
1443 end;
1444 #19: // green
1445 begin
1446 c.R := 0; c.G := 255; c.B := 0;
1447 continue;
1448 end;
1449 #20: // blue
1450 begin
1451 c.R := 0; c.G := 0; c.B := 255;
1452 continue;
1453 end;
1454 #21: // yellow
1455 begin
1456 c.R := 255; c.G := 255; c.B := 0;
1457 continue;
1458 end;
1459 end;
1461 with Chars[Ord(Text[a])] do
1462 if TextureID <> -1 then
1463 begin
1464 tc := e_Colors;
1465 e_Colors := c;
1466 e_Draw(TextureID, TX, TY, 0, True, False);
1467 e_Colors := tc;
1469 TX := TX+Width+IfThen(a = Length(Text), 0, Space);
1470 end;
1471 end;
1472 end;
1473 end;
1475 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
1476 var
1477 a: Integer;
1478 h2: Word;
1479 begin
1480 w := 0;
1481 h := 0;
1483 if Text = '' then Exit;
1484 if e_CharFonts = nil then Exit;
1485 if Integer(FontID) > High(e_CharFonts) then Exit;
1487 with e_CharFonts[FontID] do
1488 begin
1489 for a := 1 to Length(Text) do
1490 with Chars[Ord(Text[a])] do
1491 if TextureID <> -1 then
1492 begin
1493 w := w+Width+IfThen(a = Length(Text), 0, Space);
1494 e_GetTextureSize(TextureID, nil, @h2);
1495 if h2 > h then h := h2;
1496 end;
1497 end;
1498 end;
1500 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
1501 var
1502 a, lines, len: Integer;
1503 h2, w2, tw, th: Word;
1504 begin
1505 w2 := 0;
1506 h2 := 0;
1507 tw := 0;
1508 th := 0;
1510 if Text = '' then Exit;
1511 if e_CharFonts = nil then Exit;
1512 if Integer(FontID) > High(e_CharFonts) then Exit;
1514 lines := 1;
1515 len := Length(Text);
1517 with e_CharFonts[FontID] do
1518 begin
1519 for a := 1 to len do
1520 begin
1521 if Text[a] = #10 then
1522 begin
1523 Inc(lines);
1524 if w2 > tw then tw := w2;
1525 w2 := 0;
1526 continue;
1527 end;
1529 with Chars[Ord(Text[a])] do
1530 if TextureID <> -1 then
1531 begin
1532 w2 := w2 + Width + IfThen(a = len, 0, Space);
1533 e_GetTextureSize(TextureID, nil, @h2);
1534 if h2 > th then th := h2;
1535 end;
1536 end;
1537 end;
1539 if w2 > tw then
1540 tw := w2;
1542 w := tw;
1543 h := th * lines;
1544 end;
1546 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
1547 var
1548 a: Integer;
1549 begin
1550 Result := 0;
1552 if e_CharFonts = nil then Exit;
1553 if Integer(FontID) > High(e_CharFonts) then Exit;
1555 for a := 0 to High(e_CharFonts[FontID].Chars) do
1556 Result := Max(Result, e_CharFonts[FontID].Chars[a].Width);
1557 end;
1559 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
1560 var
1561 a: Integer;
1562 h2: Word;
1563 begin
1564 Result := 0;
1566 if e_CharFonts = nil then Exit;
1567 if Integer(FontID) > High(e_CharFonts) then Exit;
1569 for a := 0 to High(e_CharFonts[FontID].Chars) do
1570 begin
1571 if e_CharFonts[FontID].Chars[a].TextureID <> -1 then
1572 e_GetTextureSize(e_CharFonts[FontID].Chars[a].TextureID, nil, @h2)
1573 else h2 := 0;
1574 if h2 > Result then Result := h2;
1575 end;
1576 end;
1578 procedure e_CharFont_Remove(FontID: DWORD);
1579 var
1580 a: Integer;
1581 begin
1582 with e_CharFonts[FontID] do
1583 for a := 0 to High(Chars) do
1584 if Chars[a].TextureID <> -1 then e_DeleteTexture(Chars[a].TextureID);
1586 e_CharFonts[FontID].alive := False;
1587 end;
1589 procedure e_CharFont_RemoveAll();
1590 var
1591 a: Integer;
1592 begin
1593 if e_CharFonts = nil then Exit;
1595 for a := 0 to High(e_CharFonts) do
1596 e_CharFont_Remove(a);
1598 e_CharFonts := nil;
1599 end;
1601 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
1602 Space: ShortInt=0);
1603 var
1604 loop1 : GLuint;
1605 cx, cy : real;
1606 i, id: DWORD;
1607 begin
1608 if e_NoGraphics then Exit;
1609 e_WriteLog('Creating texture font...', TMsgType.Notify);
1611 id := DWORD(-1);
1613 if e_TextureFonts <> nil then
1614 for i := 0 to High(e_TextureFonts) do
1615 if e_TextureFonts[i].Base = 0 then
1616 begin
1617 id := i;
1618 Break;
1619 end;
1621 if id = DWORD(-1) then
1622 begin
1623 SetLength(e_TextureFonts, Length(e_TextureFonts) + 1);
1624 id := High(e_TextureFonts);
1625 end;
1627 with e_TextureFonts[id] do
1628 begin
1629 {$IF not DEFINED(USE_NANOGL)}
1630 Base := glGenLists(XCount*YCount);
1631 {$ENDIF}
1632 TextureID := e_Textures[Tex].tx.id;
1633 CharWidth := (e_Textures[Tex].tx.Width div XCount)+Space;
1634 CharHeight := e_Textures[Tex].tx.Height div YCount;
1635 XC := XCount;
1636 YC := YCount;
1637 Texture := Tex;
1638 SPC := Space;
1639 end;
1641 {$IF not DEFINED(USE_NANOGL)}
1642 glBindTexture(GL_TEXTURE_2D, e_Textures[Tex].tx.id);
1643 for loop1 := 0 to XCount*YCount-1 do
1644 begin
1645 cx := (loop1 mod XCount)/XCount;
1646 cy := (loop1 div YCount)/YCount;
1648 glNewList(e_TextureFonts[id].Base+loop1, GL_COMPILE);
1649 glBegin(GL_QUADS);
1650 glTexCoord2f(cx, 1.0-cy-1/YCount);
1651 glVertex2i(0, e_Textures[Tex].tx.Height div YCount);
1653 glTexCoord2f(cx+1/XCount, 1.0-cy-1/YCount);
1654 glVertex2i(e_Textures[Tex].tx.Width div XCount, e_Textures[Tex].tx.Height div YCount);
1656 glTexCoord2f(cx+1/XCount, 1.0-cy);
1657 glVertex2i(e_Textures[Tex].tx.Width div XCount, 0);
1659 glTexCoord2f(cx, 1.0-cy);
1660 glVertex2i(0, 0);
1661 glEnd();
1662 glTranslated((e_Textures[Tex].tx.Width div XCount)+Space, 0, 0);
1663 glEndList();
1664 end;
1665 {$ENDIF}
1667 FontID := id;
1668 end;
1670 procedure e_TextureFontKill(FontID: DWORD);
1671 begin
1672 if e_NoGraphics then Exit;
1673 {$IF not DEFINED(USE_NANOGL)}
1674 glDeleteLists(e_TextureFonts[FontID].Base, 256);
1675 {$ENDIF}
1676 e_TextureFonts[FontID].Base := 0;
1677 end;
1679 {$IFDEF USE_NANOGL}
1680 procedure e_TextureFontDrawChar(ch: Char; FontID: DWORD);
1681 var
1682 index: Integer;
1683 cx, cy: GLfloat;
1684 Tex: Integer;
1685 Width, Height: Integer;
1686 XCount, YCount: Integer;
1687 begin
1688 index := Ord(ch) - 32;
1689 Tex := e_TextureFonts[FontID].Texture;
1690 Width := e_Textures[Tex].tx.Width;
1691 Height := e_Textures[Tex].tx.Height;
1692 XCount := e_TextureFonts[FontID].XC;
1693 YCount := e_TextureFonts[FontID].YC;
1694 cx := (index mod XCount)/XCount;
1695 cy := (index div YCount)/YCount;
1696 glBegin(GL_QUADS);
1697 glTexCoord2f(cx, 1 - cy - 1/YCount);
1698 glVertex2i(0, Height div YCount);
1699 glTexCoord2f(cx + 1/XCount, 1 - cy - 1/YCount);
1700 glVertex2i(Width div XCount, Height div YCount);
1701 glTexCoord2f(cx + 1/XCount, 1 - cy);
1702 glVertex2i(Width div XCount, 0);
1703 glTexCoord2f(cx, 1 - cy);
1704 glVertex2i(0, 0);
1705 glEnd();
1706 glTranslatef((e_Textures[Tex].tx.Width div XCount) + e_TextureFonts[FontID].SPC, 0, 0);
1707 end;
1709 procedure e_TextureFontDrawString(Text: String; FontID: DWORD);
1710 var
1711 i: Integer;
1712 begin
1713 for i := 1 to High(Text) do
1714 e_TextureFontDrawChar(Text[i], FontID);
1715 end;
1716 {$ENDIF}
1718 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
1719 begin
1720 if e_NoGraphics then Exit;
1721 if Integer(FontID) > High(e_TextureFonts) then Exit;
1722 if Text = '' then Exit;
1724 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1725 glEnable(GL_BLEND);
1727 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1729 glPushMatrix;
1730 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1731 glEnable(GL_TEXTURE_2D);
1732 glTranslatef(x, y, 0);
1733 {$IFDEF USE_NANOGL}
1734 e_TextureFontDrawString(Text, FontID);
1735 {$ELSE}
1736 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1737 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1738 {$ENDIF}
1739 glDisable(GL_TEXTURE_2D);
1740 glPopMatrix;
1742 glDisable(GL_BLEND);
1743 end;
1745 // god forgive me for this, but i cannot figure out how to do it without lists
1746 procedure e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1747 begin
1748 if e_NoGraphics then Exit;
1749 glPushMatrix;
1751 if Shadow then
1752 begin
1753 glColor4ub(0, 0, 0, 128);
1754 glTranslatef(X+1, Y+1, 0);
1755 {$IFDEF USE_NANOGL}
1756 e_TextureFontDrawChar(Ch, FontID);
1757 {$ELSE}
1758 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1759 {$ENDIF}
1760 glPopMatrix;
1761 glPushMatrix;
1762 end;
1764 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1765 glTranslatef(X, Y, 0);
1766 {$IFDEF USE_NANOGL}
1767 e_TextureFontDrawChar(Ch, FontID);
1768 {$ELSE}
1769 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1770 {$ENDIF}
1772 glPopMatrix;
1773 end;
1775 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1776 begin
1777 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1778 glEnable(GL_TEXTURE_2D);
1779 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1781 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1782 glEnable(GL_BLEND);
1783 e_TextureFontPrintChar(X, Y, Ch, FontID, Shadow);
1784 glDisable(GL_TEXTURE_2D);
1785 glDisable(GL_BLEND);
1786 end;
1788 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
1789 begin
1790 result := e_TextureFonts[FontID].CharWidth;
1791 end;
1793 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD; Shadow: Boolean = False);
1794 var
1795 a, TX, TY, len: Integer;
1796 tc, c: TRGB;
1797 w: Word;
1798 begin
1799 if e_NoGraphics then Exit;
1800 if Text = '' then Exit;
1801 if e_TextureFonts = nil then Exit;
1802 if Integer(FontID) > High(e_TextureFonts) then Exit;
1804 c.R := 255;
1805 c.G := 255;
1806 c.B := 255;
1808 TX := X;
1809 TY := Y;
1810 len := Length(Text);
1812 w := e_TextureFonts[FontID].CharWidth;
1814 with e_TextureFonts[FontID] do
1815 begin
1816 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1817 glEnable(GL_TEXTURE_2D);
1819 {$IF not DEFINED(USE_NANOGL)}
1820 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1821 {$ENDIF}
1823 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1824 glEnable(GL_BLEND);
1826 for a := 1 to len do
1827 begin
1828 case Text[a] of
1829 {#10: // line feed
1830 begin
1831 TX := X;
1832 TY := TY + h;
1833 continue;
1834 end;}
1835 #1: // black
1836 begin
1837 c.R := 0; c.G := 0; c.B := 0;
1838 continue;
1839 end;
1840 #2: // white
1841 begin
1842 c.R := 255; c.G := 255; c.B := 255;
1843 continue;
1844 end;
1845 #3: // darker
1846 begin
1847 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1848 continue;
1849 end;
1850 #4: // lighter
1851 begin
1852 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1853 continue;
1854 end;
1855 #18: // red
1856 begin
1857 c.R := 255; c.G := 0; c.B := 0;
1858 continue;
1859 end;
1860 #19: // green
1861 begin
1862 c.R := 0; c.G := 255; c.B := 0;
1863 continue;
1864 end;
1865 #20: // blue
1866 begin
1867 c.R := 0; c.G := 0; c.B := 255;
1868 continue;
1869 end;
1870 #21: // yellow
1871 begin
1872 c.R := 255; c.G := 255; c.B := 0;
1873 continue;
1874 end;
1875 end;
1877 tc := e_Colors;
1878 e_Colors := c;
1879 e_TextureFontPrintChar(TX, TY, Text[a], FontID, Shadow);
1880 e_Colors := tc;
1882 TX := TX+w;
1883 end;
1884 glDisable(GL_TEXTURE_2D);
1885 glDisable(GL_BLEND);
1886 end;
1887 end;
1889 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
1890 Blue: Byte; Scale: Single; Shadow: Boolean = False);
1891 begin
1892 if e_NoGraphics then Exit;
1893 if Text = '' then Exit;
1895 glPushMatrix;
1896 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1897 glEnable(GL_TEXTURE_2D);
1899 {$IF not DEFINED(USE_NANOGL)}
1900 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1901 {$ENDIF}
1903 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1904 glEnable(GL_BLEND);
1906 if Shadow then
1907 begin
1908 glColor4ub(0, 0, 0, 128);
1909 glTranslatef(x+1, y+1, 0);
1910 glScalef(Scale, Scale, 0);
1911 {$IFDEF USE_NANOGL}
1912 e_TextureFontDrawString(Text, FontID);
1913 {$ELSE}
1914 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1915 {$ENDIF}
1916 glPopMatrix;
1917 glPushMatrix;
1918 end;
1920 glColor4ub(Red, Green, Blue, 255);
1921 glTranslatef(x, y, 0);
1922 glScalef(Scale, Scale, 0);
1923 {$IFDEF USE_NANOGL}
1924 e_TextureFontDrawString(Text, FontID);
1925 {$ELSE}
1926 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1927 {$ENDIF}
1929 glDisable(GL_TEXTURE_2D);
1930 glPopMatrix;
1931 glColor3ub(e_Colors.R, e_Colors.G, e_Colors.B);
1932 glDisable(GL_BLEND);
1933 end;
1935 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
1936 begin
1937 CharWidth := 16;
1938 CharHeight := 16;
1939 if e_NoGraphics then Exit;
1940 if Integer(ID) > High(e_TextureFonts) then
1941 Exit;
1942 CharWidth := e_TextureFonts[ID].CharWidth;
1943 CharHeight := e_TextureFonts[ID].CharHeight;
1944 end;
1946 procedure e_RemoveAllTextureFont();
1947 var
1948 i: integer;
1949 begin
1950 if e_NoGraphics then Exit;
1951 if e_TextureFonts = nil then Exit;
1953 for i := 0 to High(e_TextureFonts) do
1954 if e_TextureFonts[i].Base <> 0 then
1955 begin
1956 {$IFNDEF USE_NANOGL}
1957 glDeleteLists(e_TextureFonts[i].Base, 256);
1958 {$ENDIF}
1959 e_TextureFonts[i].Base := 0;
1960 end;
1962 e_TextureFonts := nil;
1963 end;
1965 function _RGB(Red, Green, Blue: Byte): TRGB;
1966 begin
1967 Result.R := Red;
1968 Result.G := Green;
1969 Result.B := Blue;
1970 end;
1972 function _Point(X, Y: Integer): TPoint2i;
1973 begin
1974 Result.X := X;
1975 Result.Y := Y;
1976 end;
1978 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
1979 begin
1980 Result.X := X;
1981 Result.Y := Y;
1982 Result.Width := Width;
1983 Result.Height := Height;
1984 end;
1986 function _TRect(L, T, R, B: LongInt): TRect;
1987 begin
1988 Result.Top := T;
1989 Result.Left := L;
1990 Result.Right := R;
1991 Result.Bottom := B;
1992 end;
1995 procedure e_MakeScreenshot (st: TStream; Width, Height: Word);
1996 var
1997 pixels, obuf, scln, ps, pd: PByte;
1998 obufsize: Integer;
1999 dlen: Cardinal;
2000 i, x, y, res: Integer;
2001 sign: array [0..7] of Byte;
2002 hbuf: array [0..12] of Byte;
2003 crc: LongWord;
2004 img: TImageData;
2005 clr: TColor32Rec;
2006 begin
2007 if e_NoGraphics then Exit;
2008 obuf := nil;
2010 // first, extract and pack graphics data
2011 if (Width mod 4) > 0 then Width := Width+4-(Width mod 4);
2013 GetMem(pixels, Width*Height*3);
2014 try
2015 FillChar(pixels^, Width*Height*3, 0);
2016 glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
2017 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
2019 if e_FastScreenshots then
2020 begin
2021 // create scanlines
2022 GetMem(scln, (Width*3+1)*Height);
2023 try
2024 ps := pixels;
2025 pd := scln;
2026 Inc(ps, (Width*3)*(Height-1));
2027 for i := 0 to Height-1 do
2028 begin
2029 pd^ := 0; // filter
2030 Inc(pd);
2031 Move(ps^, pd^, Width*3);
2032 Dec(ps, Width*3);
2033 Inc(pd, Width*3);
2034 end;
2035 except
2036 FreeMem(scln);
2037 raise;
2038 end;
2039 FreeMem(pixels);
2040 pixels := scln;
2042 // pack it
2043 obufsize := (Width*3+1)*Height*2;
2044 GetMem(obuf, obufsize);
2045 try
2046 while true do
2047 begin
2048 dlen := obufsize;
2049 res := compress2(Pointer(obuf), dlen, Pointer(pixels), (Width*3+1)*Height, 9);
2050 if res = Z_OK then break;
2051 if res <> Z_BUF_ERROR then raise Exception.Create('can''t pack data for PNG');
2052 obufsize := obufsize*2;
2053 FreeMem(obuf);
2054 obuf := nil;
2055 GetMem(obuf, obufsize);
2056 end;
2057 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
2059 // now write PNG
2061 // signature
2062 sign[0] := 137;
2063 sign[1] := 80;
2064 sign[2] := 78;
2065 sign[3] := 71;
2066 sign[4] := 13;
2067 sign[5] := 10;
2068 sign[6] := 26;
2069 sign[7] := 10;
2070 st.writeBuffer(sign, 8);
2071 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
2073 // header
2074 writeIntBE(st, LongWord(13));
2075 sign[0] := 73;
2076 sign[1] := 72;
2077 sign[2] := 68;
2078 sign[3] := 82;
2079 st.writeBuffer(sign, 4);
2080 crc := crc32(0, @sign[0], 4);
2081 hbuf[0] := 0;
2082 hbuf[1] := 0;
2083 hbuf[2] := (Width shr 8) and $ff;
2084 hbuf[3] := Width and $ff;
2085 hbuf[4] := 0;
2086 hbuf[5] := 0;
2087 hbuf[6] := (Height shr 8) and $ff;
2088 hbuf[7] := Height and $ff;
2089 hbuf[8] := 8; // bit depth
2090 hbuf[9] := 2; // RGB
2091 hbuf[10] := 0; // compression method
2092 hbuf[11] := 0; // filter method
2093 hbuf[12] := 0; // no interlace
2094 crc := crc32(crc, @hbuf[0], 13);
2095 st.writeBuffer(hbuf, 13);
2096 writeIntBE(st, crc);
2097 //e_WriteLog('PNG: header written', MSG_NOTIFY);
2099 // image data
2100 writeIntBE(st, LongWord(dlen));
2101 sign[0] := 73;
2102 sign[1] := 68;
2103 sign[2] := 65;
2104 sign[3] := 84;
2105 st.writeBuffer(sign, 4);
2106 crc := crc32(0, @sign[0], 4);
2107 crc := crc32(crc, obuf, dlen);
2108 st.writeBuffer(obuf^, dlen);
2109 writeIntBE(st, crc);
2110 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
2112 // image data end
2113 writeIntBE(st, LongWord(0));
2114 sign[0] := 73;
2115 sign[1] := 69;
2116 sign[2] := 78;
2117 sign[3] := 68;
2118 st.writeBuffer(sign, 4);
2119 crc := crc32(0, @sign[0], 4);
2120 writeIntBE(st, crc);
2121 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
2122 finally
2123 if obuf <> nil then FreeMem(obuf);
2124 end;
2125 end
2126 else
2127 begin
2128 Imaging.SetOption(ImagingPNGCompressLevel, 9);
2129 Imaging.SetOption(ImagingPNGPreFilter, 6);
2130 InitImage(img);
2131 try
2132 NewImage(Width, Height, TImageFormat.ifR8G8B8, img);
2133 ps := pixels;
2134 //writeln(stderr, 'moving pixels...');
2135 for y := Height-1 downto 0 do
2136 begin
2137 for x := 0 to Width-1 do
2138 begin
2139 clr.r := ps^; Inc(ps);
2140 clr.g := ps^; Inc(ps);
2141 clr.b := ps^; Inc(ps);
2142 clr.a := 255;
2143 SetPixel32(img, x, y, clr);
2144 end;
2145 end;
2146 GlobalMetadata.ClearMetaItems();
2147 GlobalMetadata.ClearMetaItemsForSaving();
2148 //writeln(stderr, 'compressing image...');
2149 if not SaveImageToStream('png', st, img) then raise Exception.Create('screenshot writing error');
2150 //writeln(stderr, 'done!');
2151 finally
2152 FreeImage(img);
2153 end;
2154 end;
2155 finally
2156 FreeMem(pixels);
2157 end;
2158 end;
2161 end.