DEADSOFTWARE

gl: fix build for gles (still not work on android)
[d2df-sdl.git] / src / game / renders / opengl / r_draw.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../../../shared/a_modes.inc}
16 unit r_draw;
18 interface
20 uses
21 g_animations,
22 r_textures
23 ;
25 procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
26 procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
27 procedure r_Draw_TextureRepeatRotate (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
29 procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const anim: TAnimState; backanim: Boolean; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
30 procedure r_Draw_MultiTextureRepeatRotate (m: TGLMultiTexture; const anim: TAnimState; backanim: Boolean; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
32 procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
33 procedure r_Draw_Rect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
34 procedure r_Draw_FillRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
35 procedure r_Draw_InvertRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
37 procedure r_Draw_Text (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont);
38 procedure r_Draw_GetTextSize (const text: AnsiString; f: TGLFont; out w, h: Integer);
40 procedure r_Draw_Setup (sw, sh, gw, gh: Integer);
41 procedure r_Draw_SetRect (l, t, r, b: Integer);
42 procedure r_Draw_GetRect (out l, t, r, b: Integer);
44 procedure r_Draw_EnableTexture2D (enable: Boolean);
45 procedure r_Draw_SetColor (r, g, b, a: Byte);
47 implementation
49 uses
50 {$I ../../../nogl/noGLuses.inc}
51 SysUtils, Classes, Math,
52 e_log, utils
53 ;
55 const
56 NTR = $FF;
57 NTG = $00;
58 NTB = $00;
59 NTA = $FF;
61 var
62 sl, st, sr, sb: Integer;
63 ScreenWidth, ScreenHeight: Integer;
64 GameWidth, GameHeight: Integer;
66 enableTexture2D: Boolean;
67 curR, curG, curB, curA: Byte;
69 procedure r_Draw_EnableTexture2D (enable: Boolean);
70 begin
71 if enable <> enableTexture2D then
72 begin
73 if enable then glEnable(GL_TEXTURE_2D) else glDisable(GL_TEXTURE_2D);
74 enableTexture2D := enable;
75 end;
76 end;
78 procedure r_Draw_SetColor (r, g, b, a: Byte);
79 begin
80 if (r <> curR) or (g <> curG) or (b <> curB) or (curA <> a) then
81 begin
82 glColor4ub(r, g, b, a);
83 curR := r;
84 curG := g;
85 curB := b;
86 curA := a;
87 end;
88 end;
90 procedure r_Draw_Setup (sw, sh, gw, gh: Integer);
91 begin
92 ASSERT((sw >= 0) and (sh >= 0)); // screen/window size
93 ASSERT((gw >= 0) and (gh >= 0)); // virtual screen size
94 ScreenWidth := sw;
95 ScreenHeight := sh;
96 GameWidth := gw;
97 GameHeight := gh;
98 glScissor(0, 0, sw, sh);
99 glViewport(0, 0, sw, sh);
100 glMatrixMode(GL_PROJECTION);
101 glLoadIdentity;
102 glOrtho(0, gw, gh, 0, 0, 1);
103 glMatrixMode(GL_MODELVIEW);
104 glLoadIdentity;
105 glEnable(GL_SCISSOR_TEST);
106 r_Draw_SetRect(0, 0, gw - 1, gh - 1);
107 end;
109 procedure DrawQuad (x, y, w, h: Integer);
110 begin
111 glBegin(GL_QUADS);
112 glVertex2i(x + w, y);
113 glVertex2i(x, y);
114 glVertex2i(x, y + h);
115 glVertex2i(x + w, y + h);
116 glEnd();
117 end;
119 procedure DrawTile (tile: TGLAtlasNode; x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend: Boolean);
120 var nw, nh, ax, bx, ay, by: GLfloat; l, t, r, b: Integer;
121 begin
122 if tile = nil then
123 begin
124 r_Draw_SetColor(rr, gg, bb, aa);
125 if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
126 r_Draw_EnableTexture2D(false);
127 glEnable(GL_BLEND);
128 DrawQuad(x, y, w, h);
129 end
130 else
131 begin
132 nw := tile.base.w;
133 nh := tile.base.h;
134 ax := IfThen(flip, tile.l, tile.r + 1) / nw;
135 bx := IfThen(flip, tile.r + 1, tile.l) / nh;
136 ay := (tile.t) / nw;
137 by := (tile.b + 1) / nh;
138 l := x; t := y; r := x + w; b := y + h;
139 r_Textures_GL_Bind(tile.id);
140 r_Draw_SetColor(rr, gg, bb, aa);
141 r_Draw_EnableTexture2D(true);
142 if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
143 glEnable(GL_BLEND);
144 glBegin(GL_QUADS);
145 glTexCoord2f(ax, ay); glVertex2i(r, t);
146 glTexCoord2f(bx, ay); glVertex2i(l, t);
147 glTexCoord2f(bx, by); glVertex2i(l, b);
148 glTexCoord2f(ax, by); glVertex2i(r, b);
149 glEnd();
150 end
151 end;
153 procedure DrawHWTexture (gltex: GLint; nw, nh, x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend: Boolean);
154 var ax, bx, ay, by: GLfloat; l, t, r, b: Integer;
155 begin
156 ax := IfThen(flip, 0, w) / nw;
157 bx := IfThen(flip, w, 0) / nh;
158 ay := 0 / nw;
159 by := h / nh;
160 l := x; t := y; r := x + w; b := y + h;
161 r_Textures_GL_Bind(gltex);
162 r_Draw_SetColor(rr, gg, bb, aa);
163 r_Draw_EnableTexture2D(true);
164 if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
165 glEnable(GL_BLEND);
166 glBegin(GL_QUADS);
167 glTexCoord2f(ax, ay); glVertex2i(r, t);
168 glTexCoord2f(bx, ay); glVertex2i(l, t);
169 glTexCoord2f(bx, by); glVertex2i(l, b);
170 glTexCoord2f(ax, by); glVertex2i(r, b);
171 glEnd();
172 end;
174 procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
175 var i, j, first, last, step: Integer; n: TGLAtlasNode;
176 begin
177 ASSERT(w >= 0);
178 ASSERT(h >= 0);
179 if img = nil then
180 DrawTile(nil, x, y, w, h, flip, NTR, NTB, NTG, NTA, blend)
181 else
182 begin
183 if flip then first := img.cols - 1 else first := 0;
184 if flip then last := -1 else last := img.cols;
185 if flip then step := -1 else step := +1;
186 glPushMatrix;
187 glTranslatef(x, y, 0);
188 glScalef(w / img.width, h / img.height, 1);
189 for j := 0 to img.lines - 1 do
190 begin
191 i := first;
192 repeat
193 n := img.GetTile(i, j);
194 ASSERT(n <> nil);
195 DrawTile(n, 0, 0, n.width, n.height, flip, r, g, b, a, blend);
196 glTranslatef(n.width, 0, 0);
197 i := i + step;
198 until i = last;
199 glTranslatef(-img.width, n.height, 0);
200 end;
201 glPopMatrix;
202 end
203 end;
205 function r_Draw_IsHWRepeatable (img: TGLTexture): Boolean;
206 var n: TGLAtlasNode; a: TGLAtlas;
207 begin
208 ASSERT(img <> nil);
209 result := false;
210 if (img.cols = 1) and (img.lines = 1) then
211 begin
212 n := img.GetTile(0, 0);
213 if (n.width = img.width) and (n.height = img.height) then
214 begin
215 a := n.base;
216 result := (a.GetWidth() = img.width) and (a.GetHeight() = img.height)
217 end;
218 end;
219 end;
221 procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
222 var i, j: Integer;
223 begin
224 ASSERT(w >= 0);
225 ASSERT(h >= 0);
226 if img = nil then
227 r_Draw_Texture(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
228 else if r_Draw_IsHWRepeatable(img) then
229 DrawHWTexture(img.GetTile(0, 0).base.id, img.width, img.height, x, y, w, h, flip, r, g, b, a, blend)
230 else
231 for j := 0 to (h - 1) div img.height do
232 for i := 0 to (w - 1) div img.width do
233 r_Draw_Texture(img, x + i * img.width, y + j * img.height, img.width, img.height, flip, r, g, b, a, blend);
234 end;
236 procedure r_Draw_TextureRepeatRotate (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
237 begin
238 ASSERT(w >= 0);
239 ASSERT(h >= 0);
240 if a <> 0 then
241 begin
242 glPushMatrix;
243 glTranslatef(x + rx, y + ry, 0);
244 glRotatef(angle, 0, 0, 1);
245 glTranslatef(-(x + rx), -(y + ry), 0);
246 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
247 glPopMatrix;
248 end
249 else
250 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
251 end;
253 procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const anim: TAnimState; backanim: Boolean; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
254 var img: TGLTexture; frame: LongInt;
255 begin
256 ASSERT(anim.IsValid());
257 if m = nil then
258 r_Draw_TextureRepeat(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
259 else
260 begin
261 g_Anim_GetFrameFromState(anim, backanim, frame);
262 ASSERT(frame >= 0);
263 ASSERT(frame < m.count);
264 img := m.GetTexture(frame);
265 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
266 end
267 end;
269 procedure r_Draw_MultiTextureRepeatRotate (m: TGLMultiTexture; const anim: TAnimState; backanim: Boolean; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
270 begin
271 ASSERT(w >= 0);
272 ASSERT(h >= 0);
273 if a <> 0 then
274 begin
275 glPushMatrix;
276 glTranslatef(x + rx, y + ry, 0);
277 glRotatef(angle, 0, 0, 1);
278 glTranslatef(-(x + rx), -(y + ry), 0);
279 r_Draw_MultiTextureRepeat(m, anim, backanim, x, y, w, h, flip, r, g, b, a, blend);
280 glPopMatrix;
281 end
282 else
283 r_Draw_MultiTextureRepeat(m, anim, backanim, x, y, w, h, flip, r, g, b, a, blend);
284 end;
286 procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
287 begin
288 ASSERT(r >= l);
289 ASSERT(b >= t);
290 glEnable(GL_BLEND);
291 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
292 r_Draw_EnableTexture2D(false);
293 r_Draw_SetColor(rr, gg, bb, aa);
294 glBegin(GL_QUADS);
295 glVertex2i(l, t);
296 glVertex2i(r, t);
297 glVertex2i(r, b);
298 glVertex2i(l, b);
299 glEnd;
300 end;
302 procedure r_Draw_Rect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
303 begin
304 ASSERT(r >= l);
305 ASSERT(b >= t);
306 glEnable(GL_BLEND);
307 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
308 r_Draw_EnableTexture2D(false);
309 r_Draw_SetColor(rr, gg, bb, aa);
310 glBegin(GL_LINE_LOOP);
312 glVertex2i(l, t);
313 glVertex2i(r, t);
314 glVertex2i(r, b);
315 glVertex2i(l, b);
317 glVertex2f(l + 0.5, t + 0.5);
318 glVertex2f(r - 0.5, t + 0.5);
319 glVertex2f(r - 0.5, b - 0.5);
320 glVertex2f(l + 0.5, b - 0.5);
321 glEnd;
322 end;
324 procedure r_Draw_FillRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
325 begin
326 ASSERT(r >= l);
327 ASSERT(b >= t);
328 glEnable(GL_BLEND);
329 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
330 r_Draw_EnableTexture2D(false);
331 r_Draw_SetColor(rr, gg, bb, aa);
332 glBegin(GL_QUADS);
334 glVertex2i(l, t);
335 glVertex2i(r, t);
336 glVertex2i(r, b);
337 glVertex2i(l, b);
340 glVertex2f(l + 0.5, t + 0.5);
341 glVertex2f(r - 0.5, t + 0.5);
342 glVertex2f(r - 0.5, b - 0.5);
343 glVertex2f(l + 0.5, b - 0.5);
345 glVertex2f(l + 0, t + 0);
346 glVertex2f(r + 0.75, t + 0);
347 glVertex2f(r + 0.75, b + 0.75);
348 glVertex2f(l + 0, b + 0.75);
349 glEnd;
350 end;
352 procedure r_Draw_InvertRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
353 begin
354 ASSERT(r >= l);
355 ASSERT(b >= t);
356 glEnable(GL_BLEND);
357 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
358 r_Draw_EnableTexture2D(false);
359 r_Draw_SetColor(rr, gg, bb, aa);
360 glBegin(GL_QUADS);
361 glVertex2i(l, t);
362 glVertex2i(r, t);
363 glVertex2i(r, b);
364 glVertex2i(l, b);
365 glEnd;
366 end;
368 procedure r_Draw_Text (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont);
369 var i, xoff, spc: Integer; t: TGLTexture; ch: AnsiChar;
370 begin
371 xoff := x; spc := MAX(0, f.GetSpace());
372 for i := 1 to Length(text) do
373 begin
374 ch := text[i];
375 t := f.GetChar(ch);
376 if t <> nil then
377 r_Draw_Texture(t, xoff, y, t.width, t.height, false, r, g, b, a, false);
378 Inc(xoff, f.GetWidth(ch) + spc);
379 end;
380 end;
382 procedure r_Draw_GetTextSize (const text: AnsiString; f: TGLFont; out w, h: Integer);
383 var i, spc, len: Integer;
384 begin
385 w := 0;
386 h := f.GetMaxHeight();
387 len := Length(text);
388 if len > 0 then
389 begin
390 spc := MAX(0, f.GetSpace());
391 for i := 1 to len - 1 do
392 Inc(w, f.GetWidth(text[i]) + spc);
393 Inc(w, f.GetWidth(text[len]));
394 end;
395 end;
397 procedure r_Draw_SetRect (l, t, r, b: Integer);
398 var x, y, w, h: Integer;
399 begin
400 ASSERT(l <= r);
401 ASSERT(t <= b);
402 x := l * ScreenWidth div GameWidth;
403 y := t * ScreenHeight div GameHeight;
404 w := (r - l + 1) * ScreenWidth div GameWidth;
405 h := (b - t + 1) * ScreenHeight div GameHeight;
406 glScissor(x, ScreenHeight - h - y, w, h);
407 sl := l; st := t; sr := r; sb := b;
408 end;
410 procedure r_Draw_GetRect (out l, t, r, b: Integer);
411 begin
412 l := sl; t := st; r := sr; b := sb;
413 end;
415 end.