DEADSOFTWARE

no more old mapreader: use textmap reader both for text and for binary maps
[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 SysUtils, Classes, Math, e_log, e_texture, SDL2, GL, GLExt, MAPDEF, ImagingTypes, Imaging, ImagingUtility;
24 type
25 TMirrorType=(M_NONE, M_HORIZONTAL, M_VERTICAL);
26 TBlending=(B_NONE, B_BLEND, B_FILTER, B_INVERT);
28 TPoint2i = record
29 X, Y: Integer;
30 end;
32 TPoint2f = record
33 X, Y: Double;
34 end;
36 TRect = record
37 Left, Top, Right, Bottom: Integer;
38 end;
40 TRectWH = record
41 X, Y: Integer;
42 Width, Height: Word;
43 end;
45 TRGB = packed record
46 R, G, B: Byte;
47 end;
49 PDFPoint = ^TDFPoint;
50 PPoint2f = ^TPoint2f;
51 PRect = ^TRect;
52 PRectWH = ^TRectWH;
55 //------------------------------------------------------------------
56 // ïðîòîòèïû ôóíêöèé
57 //------------------------------------------------------------------
58 procedure e_InitGL();
59 procedure e_SetViewPort(X, Y, Width, Height: Word);
60 procedure e_ResizeWindow(Width, Height: Integer);
62 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
63 Blending: Boolean; Mirror: TMirrorType = M_NONE);
64 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
65 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = M_NONE);
66 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
67 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = M_NONE);
68 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
69 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = M_NONE);
70 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
71 AlphaChannel: Boolean; Blending: Boolean);
72 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
73 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
74 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
75 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
76 Blending: TBlending = B_NONE);
77 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
78 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
80 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
81 function e_CreateTexture(FileName: string; var ID: DWORD): Boolean;
82 function e_CreateTextureEx(FileName: string; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
83 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
84 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
85 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
86 function e_GetTextureSize2(ID: DWORD): TRectWH;
87 procedure e_DeleteTexture(ID: DWORD);
88 procedure e_RemoveAllTextures();
90 // CharFont
91 function e_CharFont_Create(sp: ShortInt=0): DWORD;
92 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
93 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
94 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
95 Color: TRGB; Scale: Single = 1.0);
96 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
97 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
98 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
99 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
100 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
101 procedure e_CharFont_Remove(FontID: DWORD);
102 procedure e_CharFont_RemoveAll();
104 // TextureFont
105 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
106 Space: ShortInt=0);
107 procedure e_TextureFontKill(FontID: DWORD);
108 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
109 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
110 Blue: Byte; Scale: Single; Shadow: Boolean = False);
111 procedure e_TextureFontPrintFmt(X, Y: GLint; Text: string; FontID: DWORD; Shadow: Boolean = False);
112 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
113 procedure e_RemoveAllTextureFont();
115 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
116 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
118 procedure e_ReleaseEngine();
119 procedure e_BeginRender();
120 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
121 procedure e_Clear(); overload;
122 procedure e_EndRender();
124 function e_GetGamma(win: PSDL_Window): Byte;
125 procedure e_SetGamma(win: PSDL_Window;Gamma: Byte);
127 procedure e_MakeScreenshot(st: TStream; Width, Height: Word);
129 function _RGB(Red, Green, Blue: Byte): TRGB;
130 function _Point(X, Y: Integer): TPoint2i;
131 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
132 function _TRect(L, T, R, B: LongInt): TRect;
134 //function e_getTextGLId (ID: DWORD): GLuint;
136 var
137 e_Colors: TRGB;
138 e_NoGraphics: Boolean = False;
139 e_FastScreenshots: Boolean = true; // it's REALLY SLOW with `false`
142 implementation
144 uses
145 paszlib, crc, utils;
148 type
149 TTexture = record
150 tx: GLTexture;
151 end;
153 TTextureFont = record
154 Texture: DWORD;
155 TextureID: DWORD;
156 Base: Uint32;
157 CharWidth: Byte;
158 CharHeight: Byte;
159 XC, YC, SPC: Word;
160 end;
162 TCharFont = record
163 Chars: array[0..255] of
164 record
165 TextureID: Integer;
166 Width: Byte;
167 end;
168 Space: ShortInt;
169 Height: ShortInt;
170 Live: Boolean;
171 end;
173 TSavedTexture = record
174 TexID: DWORD;
175 OldID: DWORD;
176 Pixels: Pointer;
177 end;
179 var
180 e_Textures: array of TTexture = nil;
181 e_TextureFonts: array of TTextureFont = nil;
182 e_CharFonts: array of TCharFont;
183 //e_SavedTextures: array of TSavedTexture;
185 //function e_getTextGLId (ID: DWORD): GLuint; begin result := e_Textures[ID].tx.id; end;
187 //------------------------------------------------------------------
188 // Èíèöèàëèçèðóåò OpenGL
189 //------------------------------------------------------------------
190 procedure e_InitGL();
191 begin
192 if e_NoGraphics then
193 begin
194 e_DummyTextures := True;
195 Exit;
196 end;
197 e_Colors.R := 255;
198 e_Colors.G := 255;
199 e_Colors.B := 255;
200 glDisable(GL_DEPTH_TEST);
201 glEnable(GL_SCISSOR_TEST);
202 glClearColor(0, 0, 0, 0);
203 end;
205 procedure e_SetViewPort(X, Y, Width, Height: Word);
206 var
207 mat: Array [0..15] of GLDouble;
209 begin
210 if e_NoGraphics then Exit;
211 glLoadIdentity();
212 glScissor(X, Y, Width, Height);
213 glViewport(X, Y, Width, Height);
214 //gluOrtho2D(0, Width, Height, 0);
216 glMatrixMode(GL_PROJECTION);
218 mat[ 0] := 2.0 / Width;
219 mat[ 1] := 0.0;
220 mat[ 2] := 0.0;
221 mat[ 3] := 0.0;
223 mat[ 4] := 0.0;
224 mat[ 5] := -2.0 / Height;
225 mat[ 6] := 0.0;
226 mat[ 7] := 0.0;
228 mat[ 8] := 0.0;
229 mat[ 9] := 0.0;
230 mat[10] := 1.0;
231 mat[11] := 0.0;
233 mat[12] := -1.0;
234 mat[13] := 1.0;
235 mat[14] := 0.0;
236 mat[15] := 1.0;
238 glLoadMatrixd(@mat[0]);
240 glMatrixMode(GL_MODELVIEW);
241 glLoadIdentity();
242 end;
244 //------------------------------------------------------------------
245 // Èùåò ñâîáîäíûé ýëåìåíò â ìàññèâå òåêñòóð
246 //------------------------------------------------------------------
247 function FindTexture(): DWORD;
248 var
249 i: integer;
250 begin
251 if e_Textures <> nil then
252 for i := 0 to High(e_Textures) do
253 if e_Textures[i].tx.Width = 0 then
254 begin
255 Result := i;
256 Exit;
257 end;
259 if e_Textures = nil then
260 begin
261 SetLength(e_Textures, 32);
262 Result := 0;
263 end
264 else
265 begin
266 Result := High(e_Textures) + 1;
267 SetLength(e_Textures, Length(e_Textures) + 32);
268 end;
269 end;
271 //------------------------------------------------------------------
272 // Ñîçäàåò òåêñòóðó
273 //------------------------------------------------------------------
274 function e_CreateTexture(FileName: String; var ID: DWORD): Boolean;
275 var
276 find_id: DWORD;
277 fmt: Word;
278 begin
279 Result := False;
281 e_WriteLog('Loading texture from '+FileName, MSG_NOTIFY);
283 find_id := FindTexture();
285 if not LoadTexture(FileName, e_Textures[find_id].tx, e_Textures[find_id].tx.Width,
286 e_Textures[find_id].tx.Height, @fmt) then Exit;
288 ID := find_id;
290 Result := True;
291 end;
293 function e_CreateTextureEx(FileName: String; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
294 var
295 find_id: DWORD;
296 fmt: Word;
297 begin
298 Result := False;
300 find_id := FindTexture();
302 if not LoadTextureEx(FileName, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
304 ID := find_id;
306 Result := True;
307 end;
309 function e_CreateTextureMem(pData: Pointer; dataSize: LongInt; var ID: DWORD): Boolean;
310 var
311 find_id: DWORD;
312 fmt: Word;
313 begin
314 Result := False;
316 find_id := FindTexture;
318 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;
320 id := find_id;
322 Result := True;
323 end;
325 function e_CreateTextureMemEx(pData: Pointer; dataSize: LongInt; var ID: DWORD; fX, fY, fWidth, fHeight: Word): Boolean;
326 var
327 find_id: DWORD;
328 fmt: Word;
329 begin
330 Result := False;
332 find_id := FindTexture();
334 if not LoadTextureMemEx(pData, dataSize, e_Textures[find_id].tx, fX, fY, fWidth, fHeight, @fmt) then exit;
336 ID := find_id;
338 Result := True;
339 end;
341 function e_CreateTextureImg (var img: TImageData; var ID: DWORD): Boolean;
342 var
343 find_id: DWORD;
344 fmt, tw, th: Word;
345 begin
346 result := false;
347 find_id := FindTexture();
348 if not LoadTextureImg(img, e_Textures[find_id].tx, tw, th, @fmt) then exit;
349 ID := find_id;
350 result := True;
351 end;
353 procedure e_GetTextureSize(ID: DWORD; Width, Height: PWord);
354 begin
355 if Width <> nil then Width^ := e_Textures[ID].tx.Width;
356 if Height <> nil then Height^ := e_Textures[ID].tx.Height;
357 end;
359 function e_GetTextureSize2(ID: DWORD): TRectWH;
360 var
361 data: PChar;
362 x, y: Integer;
363 w, h: Word;
364 a: Boolean;
365 lastline: Integer;
366 begin
367 w := e_Textures[ID].tx.Width;
368 h := e_Textures[ID].tx.Height;
370 Result.Y := 0;
371 Result.X := 0;
372 Result.Width := w;
373 Result.Height := h;
375 if e_NoGraphics then Exit;
377 data := GetMemory(w*h*4);
378 glEnable(GL_TEXTURE_2D);
379 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
380 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
382 for y := h-1 downto 0 do
383 begin
384 lastline := y;
385 a := True;
387 for x := 1 to w-4 do
388 begin
389 a := Byte((data+y*w*4+x*4+3)^) <> 0;
390 if a then Break;
391 end;
393 if a then
394 begin
395 Result.Y := h-lastline;
396 Break;
397 end;
398 end;
400 for y := 0 to h-1 do
401 begin
402 lastline := y;
403 a := True;
405 for x := 1 to w-4 do
406 begin
407 a := Byte((data+y*w*4+x*4+3)^) <> 0;
408 if a then Break;
409 end;
411 if a then
412 begin
413 Result.Height := h-lastline-Result.Y;
414 Break;
415 end;
416 end;
418 for x := 0 to w-1 do
419 begin
420 lastline := x;
421 a := True;
423 for y := 1 to h-4 do
424 begin
425 a := Byte((data+y*w*4+x*4+3)^) <> 0;
426 if a then Break;
427 end;
429 if a then
430 begin
431 Result.X := lastline+1;
432 Break;
433 end;
434 end;
436 for x := w-1 downto 0 do
437 begin
438 lastline := x;
439 a := True;
441 for y := 1 to h-4 do
442 begin
443 a := Byte((data+y*w*4+x*4+3)^) <> 0;
444 if a then Break;
445 end;
447 if a then
448 begin
449 Result.Width := lastline-Result.X+1;
450 Break;
451 end;
452 end;
454 FreeMemory(data);
455 end;
457 procedure e_ResizeWindow(Width, Height: Integer);
458 begin
459 if Height = 0 then
460 Height := 1;
461 e_SetViewPort(0, 0, Width, Height);
462 end;
464 procedure drawTxQuad (x0, y0, w, h: Integer; u, v: single; Mirror: TMirrorType);
465 var
466 x1, y1, tmp: Integer;
467 begin
468 if (w < 1) or (h < 1) then exit;
469 x1 := x0+w;
470 y1 := y0+h;
471 if Mirror = M_HORIZONTAL then begin tmp := x1; x1 := x0; x0 := tmp; end
472 else if Mirror = M_VERTICAL then begin tmp := y1; y1 := y0; y0 := tmp; end;
473 glTexCoord2f(0, v); glVertex2i(x0, y0);
474 glTexCoord2f(0, 0); glVertex2i(x0, y1);
475 glTexCoord2f(u, 0); glVertex2i(x1, y1);
476 glTexCoord2f(u, v); glVertex2i(x1, y0);
477 end;
479 procedure e_Draw(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
480 Blending: Boolean; Mirror: TMirrorType = M_NONE);
481 begin
482 if e_NoGraphics then Exit;
483 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
485 if (Alpha > 0) or (AlphaChannel) or (Blending) then
486 glEnable(GL_BLEND)
487 else
488 glDisable(GL_BLEND);
490 if (AlphaChannel) or (Alpha > 0) then
491 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
493 if Alpha > 0 then
494 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
496 if Blending then
497 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
499 glEnable(GL_TEXTURE_2D);
500 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
501 glBegin(GL_QUADS);
503 drawTxQuad(X, Y, e_Textures[id].tx.width, e_Textures[id].tx.height, e_Textures[ID].tx.u, e_Textures[ID].tx.v, Mirror);
505 //u := e_Textures[ID].tx.u;
506 //v := e_Textures[ID].tx.v;
509 if Mirror = M_NONE then
510 begin
511 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
512 glTexCoord2f(0, 0); glVertex2i(X, Y);
513 glTexCoord2f(0, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
514 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
515 end
516 else
517 if Mirror = M_HORIZONTAL then
518 begin
519 glTexCoord2f(u, 0); glVertex2i(X, Y);
520 glTexCoord2f(0, 0); glVertex2i(X + e_Textures[id].tx.Width, Y);
521 glTexCoord2f(0, -v); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
522 glTexCoord2f(u, -v); glVertex2i(X, Y + e_Textures[id].tx.Height);
523 end
524 else
525 if Mirror = M_VERTICAL then
526 begin
527 glTexCoord2f(u, -v); glVertex2i(X + e_Textures[id].tx.Width, Y);
528 glTexCoord2f(0, -v); glVertex2i(X, Y);
529 glTexCoord2f(0, 0); glVertex2i(X, Y + e_Textures[id].tx.Height);
530 glTexCoord2f(u, 0); glVertex2i(X + e_Textures[id].tx.Width, Y + e_Textures[id].tx.Height);
531 end;
534 glEnd();
536 glDisable(GL_BLEND);
537 end;
539 procedure e_DrawSize(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
540 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = M_NONE);
541 var
542 u, v: Single;
543 begin
544 if e_NoGraphics then Exit;
545 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
547 if (Alpha > 0) or (AlphaChannel) or (Blending) then
548 glEnable(GL_BLEND)
549 else
550 glDisable(GL_BLEND);
552 if (AlphaChannel) or (Alpha > 0) then
553 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
555 if Alpha > 0 then
556 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
558 if Blending then
559 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
561 glEnable(GL_TEXTURE_2D);
562 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
564 u := e_Textures[ID].tx.u;
565 v := e_Textures[ID].tx.v;
567 glBegin(GL_QUADS);
568 glTexCoord2f(0, v); glVertex2i(X, Y);
569 glTexCoord2f(u, v); glVertex2i(X + Width, Y);
570 glTexCoord2f(u, 0); glVertex2i(X + Width, Y + Height);
571 glTexCoord2f(0, 0); glVertex2i(X, Y + Height);
572 glEnd();
574 glDisable(GL_BLEND);
575 end;
577 procedure e_DrawSizeMirror(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
578 Blending: Boolean; Width, Height: Word; Mirror: TMirrorType = M_NONE);
579 begin
580 if e_NoGraphics then Exit;
581 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
583 if (Alpha > 0) or (AlphaChannel) or (Blending) then
584 glEnable(GL_BLEND)
585 else
586 glDisable(GL_BLEND);
588 if (AlphaChannel) or (Alpha > 0) then
589 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
591 if Alpha > 0 then
592 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
594 if Blending then
595 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
597 glEnable(GL_TEXTURE_2D);
598 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
599 glBegin(GL_QUADS);
600 drawTxQuad(X, Y, Width, Height, e_Textures[ID].tx.u, e_Textures[ID].tx.v, Mirror);
601 glEnd();
603 glDisable(GL_BLEND);
604 end;
606 procedure e_DrawFill(ID: DWORD; X, Y: Integer; XCount, YCount: Word; Alpha: Integer;
607 AlphaChannel: Boolean; Blending: Boolean);
608 var
609 X2, Y2, dx, w, h: Integer;
610 u, v: Single;
611 begin
612 if e_NoGraphics then Exit;
613 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
615 if (Alpha > 0) or (AlphaChannel) or (Blending) then
616 glEnable(GL_BLEND)
617 else
618 glDisable(GL_BLEND);
620 if (AlphaChannel) or (Alpha > 0) then
621 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
623 if Alpha > 0 then
624 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
626 if Blending then
627 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
629 if XCount = 0 then
630 XCount := 1;
632 if YCount = 0 then
633 YCount := 1;
635 glEnable(GL_TEXTURE_2D);
636 glBindTexture(GL_TEXTURE_2D, e_Textures[ID].tx.id);
638 X2 := X + e_Textures[ID].tx.width * XCount;
639 Y2 := Y + e_Textures[ID].tx.height * YCount;
641 //k8: this SHOULD work... i hope
642 if (e_Textures[ID].tx.width = e_Textures[ID].tx.glwidth) and (e_Textures[ID].tx.height = e_Textures[ID].tx.glheight) then
643 begin
644 glBegin(GL_QUADS);
645 glTexCoord2i(0, YCount); glVertex2i(X, Y);
646 glTexCoord2i(XCount, YCount); glVertex2i(X2, Y);
647 glTexCoord2i(XCount, 0); glVertex2i(X2, Y2);
648 glTexCoord2i(0, 0); glVertex2i(X, Y2);
649 glEnd();
650 end
651 else
652 begin
653 glBegin(GL_QUADS);
654 // hard day's night
655 u := e_Textures[ID].tx.u;
656 v := e_Textures[ID].tx.v;
657 w := e_Textures[ID].tx.width;
658 h := e_Textures[ID].tx.height;
659 while YCount > 0 do
660 begin
661 dx := XCount;
662 x2 := X;
663 while dx > 0 do
664 begin
665 glTexCoord2f(0, v); glVertex2i(X, Y);
666 glTexCoord2f(u, v); glVertex2i(X+w, Y);
667 glTexCoord2f(u, 0); glVertex2i(X+w, Y+h);
668 glTexCoord2f(0, 0); glVertex2i(X, Y+h);
669 Inc(X, w);
670 Dec(dx);
671 end;
672 X := x2;
673 Inc(Y, h);
674 Dec(YCount);
675 end;
676 glEnd();
677 end;
679 glDisable(GL_BLEND);
680 end;
682 procedure e_DrawAdv(ID: DWORD; X, Y: Integer; Alpha: Byte; AlphaChannel: Boolean;
683 Blending: Boolean; Angle: Single; RC: PDFPoint; Mirror: TMirrorType = M_NONE);
684 begin
685 if e_NoGraphics then Exit;
687 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
689 if (Alpha > 0) or (AlphaChannel) or (Blending) then
690 glEnable(GL_BLEND)
691 else
692 glDisable(GL_BLEND);
694 if (AlphaChannel) or (Alpha > 0) then
695 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
697 if Alpha > 0 then
698 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255-Alpha);
700 if Blending then
701 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
703 if (Angle <> 0) and (RC <> nil) then
704 begin
705 glPushMatrix();
706 glTranslatef(X+RC.X, Y+RC.Y, 0);
707 glRotatef(Angle, 0, 0, 1);
708 glTranslatef(-(X+RC.X), -(Y+RC.Y), 0);
709 end;
711 glEnable(GL_TEXTURE_2D);
712 glBindTexture(GL_TEXTURE_2D, e_Textures[id].tx.id);
713 glBegin(GL_QUADS); //0-1 1-1
714 //00 10
715 drawTxQuad(X, Y, e_Textures[id].tx.width, e_Textures[id].tx.height, e_Textures[ID].tx.u, e_Textures[ID].tx.v, Mirror);
716 glEnd();
718 if Angle <> 0 then
719 glPopMatrix();
721 glDisable(GL_BLEND);
722 end;
724 procedure e_DrawPoint(Size: Byte; X, Y: Integer; Red, Green, Blue: Byte);
725 begin
726 if e_NoGraphics then Exit;
727 glDisable(GL_TEXTURE_2D);
728 glColor3ub(Red, Green, Blue);
729 glPointSize(Size);
731 if (Size = 2) or (Size = 4) then
732 X := X + 1;
734 glBegin(GL_POINTS);
735 glVertex2f(X+0.3, Y+1.0);
736 glEnd();
738 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
739 end;
741 procedure e_LineCorrection(var X1, Y1, X2, Y2: Integer);
742 begin
743 // Make lines only top-left/bottom-right and top-right/bottom-left
744 if Y2 < Y1 then
745 begin
746 X1 := X1 xor X2;
747 X2 := X1 xor X2;
748 X1 := X1 xor X2;
750 Y1 := Y1 xor Y2;
751 Y2 := Y1 xor Y2;
752 Y1 := Y1 xor Y2;
753 end;
755 // Pixel-perfect hack
756 if X1 < X2 then
757 Inc(X2)
758 else
759 Inc(X1);
760 Inc(Y2);
761 end;
763 procedure e_DrawQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
764 var
765 nX1, nY1, nX2, nY2: Integer;
766 begin
767 if e_NoGraphics then Exit;
768 // Only top-left/bottom-right quad
769 if X1 > X2 then
770 begin
771 X1 := X1 xor X2;
772 X2 := X1 xor X2;
773 X1 := X1 xor X2;
774 end;
775 if Y1 > Y2 then
776 begin
777 Y1 := Y1 xor Y2;
778 Y2 := Y1 xor Y2;
779 Y1 := Y1 xor Y2;
780 end;
782 if Alpha > 0 then
783 begin
784 glEnable(GL_BLEND);
785 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
786 end else
787 glDisable(GL_BLEND);
789 glDisable(GL_TEXTURE_2D);
790 glColor4ub(Red, Green, Blue, 255-Alpha);
791 glLineWidth(1);
793 glBegin(GL_LINES);
794 nX1 := X1; nY1 := Y1;
795 nX2 := X2; nY2 := Y1;
796 e_LineCorrection(nX1, nY1, nX2, nY2); // Pixel-perfect lines
797 glVertex2i(nX1, nY1);
798 glVertex2i(nX2, nY2);
800 nX1 := X2; nY1 := Y1;
801 nX2 := X2; nY2 := Y2;
802 e_LineCorrection(nX1, nY1, nX2, nY2);
803 glVertex2i(nX1, nY1);
804 glVertex2i(nX2, nY2);
806 nX1 := X2; nY1 := Y2;
807 nX2 := X1; nY2 := Y2;
808 e_LineCorrection(nX1, nY1, nX2, nY2);
809 glVertex2i(nX1, nY1);
810 glVertex2i(nX2, nY2);
812 nX1 := X1; nY1 := Y2;
813 nX2 := X1; nY2 := Y1;
814 e_LineCorrection(nX1, nY1, nX2, nY2);
815 glVertex2i(nX1, nY1);
816 glVertex2i(nX2, nY2);
817 glEnd();
819 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
821 glDisable(GL_BLEND);
822 end;
824 procedure e_DrawFillQuad(X1, Y1, X2, Y2: Integer; Red, Green, Blue, Alpha: Byte;
825 Blending: TBlending = B_NONE);
826 begin
827 if e_NoGraphics then Exit;
828 if (Alpha > 0) or (Blending <> B_NONE) then
829 glEnable(GL_BLEND)
830 else
831 glDisable(GL_BLEND);
833 if Blending = B_BLEND then
834 glBlendFunc(GL_SRC_ALPHA, GL_ONE)
835 else
836 if Blending = B_FILTER then
837 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR)
838 else
839 if Blending = B_INVERT then
840 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO)
841 else
842 if Alpha > 0 then
843 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
845 glDisable(GL_TEXTURE_2D);
846 glColor4ub(Red, Green, Blue, 255-Alpha);
848 X2 := X2 + 1;
849 Y2 := Y2 + 1;
851 glBegin(GL_QUADS);
852 glVertex2i(X1, Y1);
853 glVertex2i(X2, Y1);
854 glVertex2i(X2, Y2);
855 glVertex2i(X1, Y2);
856 glEnd();
858 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
860 glDisable(GL_BLEND);
861 end;
864 // ////////////////////////////////////////////////////////////////////////// //
865 procedure e_DarkenQuad (x0, y0, x1, y1: Integer; a: Integer);
866 begin
867 if (a < 0) then a := 0;
868 if (a > 255) then a := 255;
869 glEnable(GL_BLEND);
870 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
871 glDisable(GL_TEXTURE_2D);
872 glColor4ub(0, 0, 0, Byte(255-a));
873 glBegin(GL_QUADS);
874 glVertex2i(x0, y0);
875 glVertex2i(x1, y0);
876 glVertex2i(x1, y1);
877 glVertex2i(x0, y1);
878 glEnd();
879 //glRect(x, y, x+w, y+h);
880 glColor4ub(1, 1, 1, 1);
881 glDisable(GL_BLEND);
882 //glBlendEquation(GL_FUNC_ADD);
883 end;
885 procedure e_DarkenQuadWH (x, y, w, h: Integer; a: Integer);
886 begin
887 if (w > 0) and (h > 0) then e_DarkenQuad(x, y, x+w, y+h, a);
888 end;
891 procedure e_DrawLine(Width: Byte; X1, Y1, X2, Y2: Integer; Red, Green, Blue: Byte; Alpha: Byte = 0);
892 begin
893 if e_NoGraphics then Exit;
894 // Pixel-perfect lines
895 if Width = 1 then
896 e_LineCorrection(X1, Y1, X2, Y2);
898 if Alpha > 0 then
899 begin
900 glEnable(GL_BLEND);
901 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
902 end else
903 glDisable(GL_BLEND);
905 glDisable(GL_TEXTURE_2D);
906 glColor4ub(Red, Green, Blue, 255-Alpha);
907 glLineWidth(Width);
909 glBegin(GL_LINES);
910 glVertex2i(X1, Y1);
911 glVertex2i(X2, Y2);
912 glEnd();
914 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
916 glDisable(GL_BLEND);
917 end;
919 //------------------------------------------------------------------
920 // Óäàëÿåò òåêñòóðó èç ìàññèâà
921 //------------------------------------------------------------------
922 procedure e_DeleteTexture(ID: DWORD);
923 begin
924 if not e_NoGraphics then
925 glDeleteTextures(1, @e_Textures[ID].tx.id);
926 e_Textures[ID].tx.id := 0;
927 e_Textures[ID].tx.Width := 0;
928 e_Textures[ID].tx.Height := 0;
929 end;
931 //------------------------------------------------------------------
932 // Óäàëÿåò âñå òåêñòóðû
933 //------------------------------------------------------------------
934 procedure e_RemoveAllTextures();
935 var
936 i: integer;
937 begin
938 if e_Textures = nil then Exit;
940 for i := 0 to High(e_Textures) do
941 if e_Textures[i].tx.Width <> 0 then e_DeleteTexture(i);
942 e_Textures := nil;
943 end;
945 //------------------------------------------------------------------
946 // Óäàëÿåò äâèæîê
947 //------------------------------------------------------------------
948 procedure e_ReleaseEngine();
949 begin
950 e_RemoveAllTextures;
951 e_RemoveAllTextureFont;
952 end;
954 procedure e_BeginRender();
955 begin
956 if e_NoGraphics then Exit;
957 glEnable(GL_ALPHA_TEST);
958 glAlphaFunc(GL_GREATER, 0.0);
959 end;
961 procedure e_Clear(Mask: TGLbitfield; Red, Green, Blue: Single); overload;
962 begin
963 if e_NoGraphics then Exit;
964 glClearColor(Red, Green, Blue, 0);
965 glClear(Mask);
966 end;
968 procedure e_Clear(); overload;
969 begin
970 if e_NoGraphics then Exit;
971 glClearColor(0, 0, 0, 0);
972 glClear(GL_COLOR_BUFFER_BIT);
973 end;
975 procedure e_EndRender();
976 begin
977 if e_NoGraphics then Exit;
978 glPopMatrix();
979 end;
981 function e_GetGamma(win: PSDL_Window): Byte;
982 var
983 ramp: array [0..256*3-1] of Word;
984 rgb: array [0..2] of Double;
985 sum: double;
986 count: integer;
987 min: integer;
988 max: integer;
989 A, B: double;
990 i, j: integer;
991 begin
992 Result := 0;
993 if e_NoGraphics then Exit;
994 rgb[0] := 1.0;
995 rgb[1] := 1.0;
996 rgb[2] := 1.0;
998 SDL_GetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1000 for i := 0 to 2 do
1001 begin
1002 sum := 0;
1003 count := 0;
1004 min := 256 * i;
1005 max := min + 256;
1007 for j := min to max - 1 do
1008 if ramp[j] > 0 then
1009 begin
1010 B := (j mod 256)/256;
1011 A := ramp[j]/65536;
1012 sum := sum + ln(A)/ln(B);
1013 inc(count);
1014 end;
1015 rgb[i] := sum / count;
1016 end;
1018 Result := 100 - Trunc(((rgb[0] + rgb[1] + rgb[2])/3 - 0.23) * 100/(2.7 - 0.23));
1019 end;
1021 procedure e_SetGamma(win: PSDL_Window; Gamma: Byte);
1022 var
1023 ramp: array [0..256*3-1] of Word;
1024 i: integer;
1025 r: double;
1026 g: double;
1027 begin
1028 if e_NoGraphics then Exit;
1029 g := (100 - Gamma)*(2.7 - 0.23)/100 + 0.23;
1031 for i := 0 to 255 do
1032 begin
1033 r := Exp(g * ln(i/256))*65536;
1034 if r < 0 then r := 0
1035 else if r > 65535 then r := 65535;
1036 ramp[i] := trunc(r);
1037 ramp[i + 256] := trunc(r);
1038 ramp[i + 512] := trunc(r);
1039 end;
1041 SDL_SetWindowGammaRamp(win, @ramp[0], @ramp[256], @ramp[512]);
1042 end;
1044 function e_CharFont_Create(sp: ShortInt=0): DWORD;
1045 var
1046 i, id: DWORD;
1047 begin
1048 e_WriteLog('Creating CharFont...', MSG_NOTIFY);
1050 id := DWORD(-1);
1052 if e_CharFonts <> nil then
1053 for i := 0 to High(e_CharFonts) do
1054 if not e_CharFonts[i].Live then
1055 begin
1056 id := i;
1057 Break;
1058 end;
1060 if id = DWORD(-1) then
1061 begin
1062 SetLength(e_CharFonts, Length(e_CharFonts) + 1);
1063 id := High(e_CharFonts);
1064 end;
1066 with e_CharFonts[id] do
1067 begin
1068 for i := 0 to High(Chars) do
1069 with Chars[i] do
1070 begin
1071 TextureID := -1;
1072 Width := 0;
1073 end;
1075 Space := sp;
1076 Live := True;
1077 end;
1079 Result := id;
1080 end;
1082 procedure e_CharFont_AddChar(FontID: DWORD; Texture: Integer; c: Char; w: Byte);
1083 begin
1084 with e_CharFonts[FontID].Chars[Ord(c)] do
1085 begin
1086 TextureID := Texture;
1087 Width := w;
1088 end;
1089 end;
1091 procedure e_CharFont_Print(FontID: DWORD; X, Y: Integer; Text: string);
1092 var
1093 a: Integer;
1094 begin
1095 if e_NoGraphics then Exit;
1096 if Text = '' then Exit;
1097 if e_CharFonts = nil then Exit;
1098 if Integer(FontID) > High(e_CharFonts) then Exit;
1100 with e_CharFonts[FontID] do
1101 begin
1102 for a := 1 to Length(Text) do
1103 with Chars[Ord(Text[a])] do
1104 if TextureID <> -1 then
1105 begin
1106 e_Draw(TextureID, X, Y, 0, True, False);
1107 X := X+Width+IfThen(a = Length(Text), 0, Space);
1108 end;
1109 end;
1110 end;
1112 procedure e_CharFont_PrintEx(FontID: DWORD; X, Y: Integer; Text: string;
1113 Color: TRGB; Scale: Single = 1.0);
1114 var
1115 a: Integer;
1116 c: TRGB;
1117 begin
1118 if e_NoGraphics then Exit;
1119 if Text = '' then Exit;
1120 if e_CharFonts = nil then Exit;
1121 if Integer(FontID) > High(e_CharFonts) then Exit;
1123 with e_CharFonts[FontID] do
1124 begin
1125 for a := 1 to Length(Text) do
1126 with Chars[Ord(Text[a])] do
1127 if TextureID <> -1 then
1128 begin
1129 if Scale <> 1.0 then
1130 begin
1131 glPushMatrix;
1132 glScalef(Scale, Scale, 0);
1133 end;
1135 c := e_Colors;
1136 e_Colors := Color;
1137 e_Draw(TextureID, X, Y, 0, True, False);
1138 e_Colors := c;
1140 if Scale <> 1.0 then glPopMatrix;
1142 X := X+Width+IfThen(a = Length(Text), 0, Space);
1143 end;
1144 end;
1145 end;
1147 procedure e_CharFont_PrintFmt(FontID: DWORD; X, Y: Integer; Text: string);
1148 var
1149 a, TX, TY, len: Integer;
1150 tc, c: TRGB;
1151 w, h: Word;
1152 begin
1153 if e_NoGraphics then Exit;
1154 if Text = '' then Exit;
1155 if e_CharFonts = nil then Exit;
1156 if Integer(FontID) > High(e_CharFonts) then Exit;
1158 c.R := 255;
1159 c.G := 255;
1160 c.B := 255;
1162 TX := X;
1163 TY := Y;
1164 len := Length(Text);
1166 e_CharFont_GetSize(FontID, 'A', w, h);
1168 with e_CharFonts[FontID] do
1169 begin
1170 for a := 1 to len do
1171 begin
1172 case Text[a] of
1173 #10: // line feed
1174 begin
1175 TX := X;
1176 TY := TY + h;
1177 continue;
1178 end;
1179 #1: // black
1180 begin
1181 c.R := 0; c.G := 0; c.B := 0;
1182 continue;
1183 end;
1184 #2: // white
1185 begin
1186 c.R := 255; c.G := 255; c.B := 255;
1187 continue;
1188 end;
1189 #3: // darker
1190 begin
1191 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1192 continue;
1193 end;
1194 #4: // lighter
1195 begin
1196 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1197 continue;
1198 end;
1199 #18: // red
1200 begin
1201 c.R := 255; c.G := 0; c.B := 0;
1202 continue;
1203 end;
1204 #19: // green
1205 begin
1206 c.R := 0; c.G := 255; c.B := 0;
1207 continue;
1208 end;
1209 #20: // blue
1210 begin
1211 c.R := 0; c.G := 0; c.B := 255;
1212 continue;
1213 end;
1214 #21: // yellow
1215 begin
1216 c.R := 255; c.G := 255; c.B := 0;
1217 continue;
1218 end;
1219 end;
1221 with Chars[Ord(Text[a])] do
1222 if TextureID <> -1 then
1223 begin
1224 tc := e_Colors;
1225 e_Colors := c;
1226 e_Draw(TextureID, TX, TY, 0, True, False);
1227 e_Colors := tc;
1229 TX := TX+Width+IfThen(a = Length(Text), 0, Space);
1230 end;
1231 end;
1232 end;
1233 end;
1235 procedure e_CharFont_GetSize(FontID: DWORD; Text: string; var w, h: Word);
1236 var
1237 a: Integer;
1238 h2: Word;
1239 begin
1240 w := 0;
1241 h := 0;
1243 if Text = '' then Exit;
1244 if e_CharFonts = nil then Exit;
1245 if Integer(FontID) > High(e_CharFonts) then Exit;
1247 with e_CharFonts[FontID] do
1248 begin
1249 for a := 1 to Length(Text) do
1250 with Chars[Ord(Text[a])] do
1251 if TextureID <> -1 then
1252 begin
1253 w := w+Width+IfThen(a = Length(Text), 0, Space);
1254 e_GetTextureSize(TextureID, nil, @h2);
1255 if h2 > h then h := h2;
1256 end;
1257 end;
1258 end;
1260 procedure e_CharFont_GetSizeFmt(FontID: DWORD; Text: string; var w, h: Word);
1261 var
1262 a, lines, len: Integer;
1263 h2, w2: Word;
1264 begin
1265 w2 := 0;
1266 w := 0;
1267 h := 0;
1269 if Text = '' then Exit;
1270 if e_CharFonts = nil then Exit;
1271 if Integer(FontID) > High(e_CharFonts) then Exit;
1273 lines := 1;
1274 len := Length(Text);
1276 with e_CharFonts[FontID] do
1277 begin
1278 for a := 1 to len do
1279 begin
1280 if Text[a] = #10 then
1281 begin
1282 Inc(lines);
1283 if w2 > w then
1284 begin
1285 w := w2;
1286 w2 := 0;
1287 end;
1288 continue;
1289 end
1290 else if Text[a] in [#1, #2, #3, #4, #18, #19, #20, #21] then
1291 continue;
1293 with Chars[Ord(Text[a])] do
1294 if TextureID <> -1 then
1295 begin
1296 w2 := w2 + Width + IfThen(a = len, 0, Space);
1297 e_GetTextureSize(TextureID, nil, @h2);
1298 if h2 > h then h := h2;
1299 end;
1300 end;
1301 end;
1303 if w2 > w then
1304 w := w2;
1305 h := h * lines;
1306 end;
1308 function e_CharFont_GetMaxWidth(FontID: DWORD): Word;
1309 var
1310 a: Integer;
1311 begin
1312 Result := 0;
1314 if e_CharFonts = nil then Exit;
1315 if Integer(FontID) > High(e_CharFonts) then Exit;
1317 for a := 0 to High(e_CharFonts[FontID].Chars) do
1318 Result := Max(Result, e_CharFonts[FontID].Chars[a].Width);
1319 end;
1321 function e_CharFont_GetMaxHeight(FontID: DWORD): Word;
1322 var
1323 a: Integer;
1324 h2: Word;
1325 begin
1326 Result := 0;
1328 if e_CharFonts = nil then Exit;
1329 if Integer(FontID) > High(e_CharFonts) then Exit;
1331 for a := 0 to High(e_CharFonts[FontID].Chars) do
1332 begin
1333 if e_CharFonts[FontID].Chars[a].TextureID <> -1 then
1334 e_GetTextureSize(e_CharFonts[FontID].Chars[a].TextureID, nil, @h2)
1335 else h2 := 0;
1336 if h2 > Result then Result := h2;
1337 end;
1338 end;
1340 procedure e_CharFont_Remove(FontID: DWORD);
1341 var
1342 a: Integer;
1343 begin
1344 with e_CharFonts[FontID] do
1345 for a := 0 to High(Chars) do
1346 if Chars[a].TextureID <> -1 then e_DeleteTexture(Chars[a].TextureID);
1348 e_CharFonts[FontID].Live := False;
1349 end;
1351 procedure e_CharFont_RemoveAll();
1352 var
1353 a: Integer;
1354 begin
1355 if e_CharFonts = nil then Exit;
1357 for a := 0 to High(e_CharFonts) do
1358 e_CharFont_Remove(a);
1360 e_CharFonts := nil;
1361 end;
1363 procedure e_TextureFontBuild(Tex: DWORD; var FontID: DWORD; XCount, YCount: Word;
1364 Space: ShortInt=0);
1365 var
1366 loop1 : GLuint;
1367 cx, cy : real;
1368 i, id: DWORD;
1369 begin
1370 if e_NoGraphics then Exit;
1371 e_WriteLog('Creating texture font...', MSG_NOTIFY);
1373 id := DWORD(-1);
1375 if e_TextureFonts <> nil then
1376 for i := 0 to High(e_TextureFonts) do
1377 if e_TextureFonts[i].Base = 0 then
1378 begin
1379 id := i;
1380 Break;
1381 end;
1383 if id = DWORD(-1) then
1384 begin
1385 SetLength(e_TextureFonts, Length(e_TextureFonts) + 1);
1386 id := High(e_TextureFonts);
1387 end;
1389 with e_TextureFonts[id] do
1390 begin
1391 Base := glGenLists(XCount*YCount);
1392 TextureID := e_Textures[Tex].tx.id;
1393 CharWidth := (e_Textures[Tex].tx.Width div XCount)+Space;
1394 CharHeight := e_Textures[Tex].tx.Height div YCount;
1395 XC := XCount;
1396 YC := YCount;
1397 Texture := Tex;
1398 SPC := Space;
1399 end;
1401 glBindTexture(GL_TEXTURE_2D, e_Textures[Tex].tx.id);
1402 for loop1 := 0 to XCount*YCount-1 do
1403 begin
1404 cx := (loop1 mod XCount)/XCount;
1405 cy := (loop1 div YCount)/YCount;
1407 glNewList(e_TextureFonts[id].Base+loop1, GL_COMPILE);
1408 glBegin(GL_QUADS);
1409 glTexCoord2f(cx, 1.0-cy-1/YCount);
1410 glVertex2i(0, e_Textures[Tex].tx.Height div YCount);
1412 glTexCoord2f(cx+1/XCount, 1.0-cy-1/YCount);
1413 glVertex2i(e_Textures[Tex].tx.Width div XCount, e_Textures[Tex].tx.Height div YCount);
1415 glTexCoord2f(cx+1/XCount, 1.0-cy);
1416 glVertex2i(e_Textures[Tex].tx.Width div XCount, 0);
1418 glTexCoord2f(cx, 1.0-cy);
1419 glVertex2i(0, 0);
1420 glEnd();
1421 glTranslated((e_Textures[Tex].tx.Width div XCount)+Space, 0, 0);
1422 glEndList();
1423 end;
1425 FontID := id;
1426 end;
1428 procedure e_TextureFontKill(FontID: DWORD);
1429 begin
1430 if e_NoGraphics then Exit;
1431 glDeleteLists(e_TextureFonts[FontID].Base, 256);
1432 e_TextureFonts[FontID].Base := 0;
1433 end;
1435 procedure e_TextureFontPrint(X, Y: GLint; Text: string; FontID: DWORD);
1436 begin
1437 if e_NoGraphics then Exit;
1438 if Integer(FontID) > High(e_TextureFonts) then Exit;
1439 if Text = '' then Exit;
1441 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1442 glEnable(GL_BLEND);
1444 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1446 glPushMatrix;
1447 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1448 glEnable(GL_TEXTURE_2D);
1449 glTranslated(x, y, 0);
1450 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1451 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1452 glDisable(GL_TEXTURE_2D);
1453 glPopMatrix;
1455 glDisable(GL_BLEND);
1456 end;
1458 // god forgive me for this, but i cannot figure out how to do it without lists
1459 procedure e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1460 begin
1461 if e_NoGraphics then Exit;
1462 glPushMatrix;
1464 if Shadow then
1465 begin
1466 glColor4ub(0, 0, 0, 128);
1467 glTranslated(X+1, Y+1, 0);
1468 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1469 glPopMatrix;
1470 glPushMatrix;
1471 end;
1473 glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
1474 glTranslated(X, Y, 0);
1475 glCallLists(1, GL_UNSIGNED_BYTE, @Ch);
1477 glPopMatrix;
1478 end;
1480 procedure e_TextureFontPrintCharEx (X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1481 begin
1482 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1483 glEnable(GL_TEXTURE_2D);
1484 //glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1486 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1487 glEnable(GL_BLEND);
1488 e_TextureFontPrintChar(X, Y, Ch, FontID, Shadow);
1489 glDisable(GL_TEXTURE_2D);
1490 glDisable(GL_BLEND);
1491 end;
1493 function e_TextureFontCharWidth (ch: Char; FontID: DWORD): Integer;
1494 begin
1495 result := e_TextureFonts[FontID].CharWidth;
1496 end;
1498 procedure e_TextureFontPrintFmt(X, Y: Integer; Text: string; FontID: DWORD; Shadow: Boolean = False);
1499 var
1500 a, TX, TY, len: Integer;
1501 tc, c: TRGB;
1502 w: Word;
1503 begin
1504 if e_NoGraphics then Exit;
1505 if Text = '' then Exit;
1506 if e_TextureFonts = nil then Exit;
1507 if Integer(FontID) > High(e_TextureFonts) then Exit;
1509 c.R := 255;
1510 c.G := 255;
1511 c.B := 255;
1513 TX := X;
1514 TY := Y;
1515 len := Length(Text);
1517 w := e_TextureFonts[FontID].CharWidth;
1519 with e_TextureFonts[FontID] do
1520 begin
1521 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1522 glEnable(GL_TEXTURE_2D);
1523 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1525 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1526 glEnable(GL_BLEND);
1528 for a := 1 to len do
1529 begin
1530 case Text[a] of
1531 {#10: // line feed
1532 begin
1533 TX := X;
1534 TY := TY + h;
1535 continue;
1536 end;}
1537 #1: // black
1538 begin
1539 c.R := 0; c.G := 0; c.B := 0;
1540 continue;
1541 end;
1542 #2: // white
1543 begin
1544 c.R := 255; c.G := 255; c.B := 255;
1545 continue;
1546 end;
1547 #3: // darker
1548 begin
1549 c.R := c.R div 2; c.G := c.G div 2; c.B := c.B div 2;
1550 continue;
1551 end;
1552 #4: // lighter
1553 begin
1554 c.R := Min(c.R * 2, 255); c.G := Min(c.G * 2, 255); c.B := Min(c.B * 2, 255);
1555 continue;
1556 end;
1557 #18: // red
1558 begin
1559 c.R := 255; c.G := 0; c.B := 0;
1560 continue;
1561 end;
1562 #19: // green
1563 begin
1564 c.R := 0; c.G := 255; c.B := 0;
1565 continue;
1566 end;
1567 #20: // blue
1568 begin
1569 c.R := 0; c.G := 0; c.B := 255;
1570 continue;
1571 end;
1572 #21: // yellow
1573 begin
1574 c.R := 255; c.G := 255; c.B := 0;
1575 continue;
1576 end;
1577 end;
1579 tc := e_Colors;
1580 e_Colors := c;
1581 e_TextureFontPrintChar(TX, TY, Text[a], FontID, Shadow);
1582 e_Colors := tc;
1584 TX := TX+w;
1585 end;
1586 glDisable(GL_TEXTURE_2D);
1587 glDisable(GL_BLEND);
1588 end;
1589 end;
1591 procedure e_TextureFontPrintEx(X, Y: GLint; Text: string; FontID: DWORD; Red, Green,
1592 Blue: Byte; Scale: Single; Shadow: Boolean = False);
1593 begin
1594 if e_NoGraphics then Exit;
1595 if Text = '' then Exit;
1597 glPushMatrix;
1598 glBindTexture(GL_TEXTURE_2D, e_TextureFonts[FontID].TextureID);
1599 glEnable(GL_TEXTURE_2D);
1600 glListBase(DWORD(Integer(e_TextureFonts[FontID].Base)-32));
1602 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1603 glEnable(GL_BLEND);
1605 if Shadow then
1606 begin
1607 glColor4ub(0, 0, 0, 128);
1608 glTranslated(x+1, y+1, 0);
1609 glScalef(Scale, Scale, 0);
1610 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1611 glPopMatrix;
1612 glPushMatrix;
1613 end;
1615 glColor4ub(Red, Green, Blue, 255);
1616 glTranslated(x, y, 0);
1617 glScalef(Scale, Scale, 0);
1618 glCallLists(Length(Text), GL_UNSIGNED_BYTE, PChar(Text));
1620 glDisable(GL_TEXTURE_2D);
1621 glPopMatrix;
1622 glColor3ub(e_Colors.R, e_Colors.G, e_Colors.B);
1623 glDisable(GL_BLEND);
1624 end;
1626 procedure e_TextureFontGetSize(ID: DWORD; out CharWidth, CharHeight: Byte);
1627 begin
1628 CharWidth := 16;
1629 CharHeight := 16;
1630 if e_NoGraphics then Exit;
1631 if Integer(ID) > High(e_TextureFonts) then
1632 Exit;
1633 CharWidth := e_TextureFonts[ID].CharWidth;
1634 CharHeight := e_TextureFonts[ID].CharHeight;
1635 end;
1637 procedure e_RemoveAllTextureFont();
1638 var
1639 i: integer;
1640 begin
1641 if e_NoGraphics then Exit;
1642 if e_TextureFonts = nil then Exit;
1644 for i := 0 to High(e_TextureFonts) do
1645 if e_TextureFonts[i].Base <> 0 then
1646 begin
1647 glDeleteLists(e_TextureFonts[i].Base, 256);
1648 e_TextureFonts[i].Base := 0;
1649 end;
1651 e_TextureFonts := nil;
1652 end;
1654 function _RGB(Red, Green, Blue: Byte): TRGB;
1655 begin
1656 Result.R := Red;
1657 Result.G := Green;
1658 Result.B := Blue;
1659 end;
1661 function _Point(X, Y: Integer): TPoint2i;
1662 begin
1663 Result.X := X;
1664 Result.Y := Y;
1665 end;
1667 function _Rect(X, Y: Integer; Width, Height: Word): TRectWH;
1668 begin
1669 Result.X := X;
1670 Result.Y := Y;
1671 Result.Width := Width;
1672 Result.Height := Height;
1673 end;
1675 function _TRect(L, T, R, B: LongInt): TRect;
1676 begin
1677 Result.Top := T;
1678 Result.Left := L;
1679 Result.Right := R;
1680 Result.Bottom := B;
1681 end;
1684 procedure e_MakeScreenshot (st: TStream; Width, Height: Word);
1685 var
1686 pixels, obuf, scln, ps, pd: PByte;
1687 obufsize: Integer;
1688 dlen: Cardinal;
1689 i, x, y, res: Integer;
1690 sign: array [0..7] of Byte;
1691 hbuf: array [0..12] of Byte;
1692 crc: LongWord;
1693 img: TImageData;
1694 clr: TColor32Rec;
1695 begin
1696 if e_NoGraphics then Exit;
1697 obuf := nil;
1699 // first, extract and pack graphics data
1700 if (Width mod 4) > 0 then Width := Width+4-(Width mod 4);
1702 GetMem(pixels, Width*Height*3);
1703 try
1704 FillChar(pixels^, Width*Height*3, 0);
1705 glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
1706 //e_WriteLog('PNG: pixels read', MSG_NOTIFY);
1708 if e_FastScreenshots then
1709 begin
1710 // create scanlines
1711 GetMem(scln, (Width*3+1)*Height);
1712 try
1713 ps := pixels;
1714 pd := scln;
1715 Inc(ps, (Width*3)*(Height-1));
1716 for i := 0 to Height-1 do
1717 begin
1718 pd^ := 0; // filter
1719 Inc(pd);
1720 Move(ps^, pd^, Width*3);
1721 Dec(ps, Width*3);
1722 Inc(pd, Width*3);
1723 end;
1724 except
1725 FreeMem(scln);
1726 raise;
1727 end;
1728 FreeMem(pixels);
1729 pixels := scln;
1731 // pack it
1732 obufsize := (Width*3+1)*Height*2;
1733 GetMem(obuf, obufsize);
1734 try
1735 while true do
1736 begin
1737 dlen := obufsize;
1738 res := compress2(Pointer(obuf), dlen, Pointer(pixels), (Width*3+1)*Height, 9);
1739 if res = Z_OK then break;
1740 if res <> Z_BUF_ERROR then raise Exception.Create('can''t pack data for PNG');
1741 obufsize := obufsize*2;
1742 FreeMem(obuf);
1743 obuf := nil;
1744 GetMem(obuf, obufsize);
1745 end;
1746 //e_WriteLog(Format('PNG: pixels compressed from %d to %d', [Integer(Width*Height*3), Integer(dlen)]), MSG_NOTIFY);
1748 // now write PNG
1750 // signature
1751 sign[0] := 137;
1752 sign[1] := 80;
1753 sign[2] := 78;
1754 sign[3] := 71;
1755 sign[4] := 13;
1756 sign[5] := 10;
1757 sign[6] := 26;
1758 sign[7] := 10;
1759 st.writeBuffer(sign, 8);
1760 //e_WriteLog('PNG: signature written', MSG_NOTIFY);
1762 // header
1763 writeIntBE(st, LongWord(13));
1764 sign[0] := 73;
1765 sign[1] := 72;
1766 sign[2] := 68;
1767 sign[3] := 82;
1768 st.writeBuffer(sign, 4);
1769 crc := crc32(0, @sign[0], 4);
1770 hbuf[0] := 0;
1771 hbuf[1] := 0;
1772 hbuf[2] := (Width shr 8) and $ff;
1773 hbuf[3] := Width and $ff;
1774 hbuf[4] := 0;
1775 hbuf[5] := 0;
1776 hbuf[6] := (Height shr 8) and $ff;
1777 hbuf[7] := Height and $ff;
1778 hbuf[8] := 8; // bit depth
1779 hbuf[9] := 2; // RGB
1780 hbuf[10] := 0; // compression method
1781 hbuf[11] := 0; // filter method
1782 hbuf[12] := 0; // no interlace
1783 crc := crc32(crc, @hbuf[0], 13);
1784 st.writeBuffer(hbuf, 13);
1785 writeIntBE(st, crc);
1786 //e_WriteLog('PNG: header written', MSG_NOTIFY);
1788 // image data
1789 writeIntBE(st, LongWord(dlen));
1790 sign[0] := 73;
1791 sign[1] := 68;
1792 sign[2] := 65;
1793 sign[3] := 84;
1794 st.writeBuffer(sign, 4);
1795 crc := crc32(0, @sign[0], 4);
1796 crc := crc32(crc, obuf, dlen);
1797 st.writeBuffer(obuf^, dlen);
1798 writeIntBE(st, crc);
1799 //e_WriteLog('PNG: image data written', MSG_NOTIFY);
1801 // image data end
1802 writeIntBE(st, LongWord(0));
1803 sign[0] := 73;
1804 sign[1] := 69;
1805 sign[2] := 78;
1806 sign[3] := 68;
1807 st.writeBuffer(sign, 4);
1808 crc := crc32(0, @sign[0], 4);
1809 writeIntBE(st, crc);
1810 //e_WriteLog('PNG: end marker written', MSG_NOTIFY);
1811 finally
1812 if obuf <> nil then FreeMem(obuf);
1813 end;
1814 end
1815 else
1816 begin
1817 Imaging.SetOption(ImagingPNGCompressLevel, 9);
1818 Imaging.SetOption(ImagingPNGPreFilter, 6);
1819 InitImage(img);
1820 try
1821 NewImage(Width, Height, TImageFormat.ifR8G8B8, img);
1822 ps := pixels;
1823 //writeln(stderr, 'moving pixels...');
1824 for y := Height-1 downto 0 do
1825 begin
1826 for x := 0 to Width-1 do
1827 begin
1828 clr.r := ps^; Inc(ps);
1829 clr.g := ps^; Inc(ps);
1830 clr.b := ps^; Inc(ps);
1831 clr.a := 255;
1832 SetPixel32(img, x, y, clr);
1833 end;
1834 end;
1835 GlobalMetadata.ClearMetaItems();
1836 GlobalMetadata.ClearMetaItemsForSaving();
1837 //writeln(stderr, 'compressing image...');
1838 if not SaveImageToStream('png', st, img) then raise Exception.Create('screenshot writing error');
1839 //writeln(stderr, 'done!');
1840 finally
1841 FreeImage(img);
1842 end;
1843 end;
1844 finally
1845 FreeMem(pixels);
1846 end;
1847 end;
1850 end.