DEADSOFTWARE

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