DEADSOFTWARE

gl: more animation checks
[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_Rect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
287 begin
288 ASSERT(l <= r);
289 ASSERT(t <= b);
290 if (l < r) and (t < b) then
291 begin
292 glEnable(GL_BLEND);
293 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
294 r_Draw_EnableTexture2D(false);
295 r_Draw_SetColor(rr, gg, bb, aa);
296 glBegin(GL_QUADS);
297 (* top *)
298 glVertex2i(l, t);
299 glVertex2i(r, t);
300 glVertex2i(r, t+1);
301 glVertex2i(l, t+1);
302 (* bottom *)
303 glVertex2i(l, b-1);
304 glVertex2i(r, b-1);
305 glVertex2i(r, b);
306 glVertex2i(l, b);
307 (* left *)
308 glVertex2i(l, t+1);
309 glVertex2i(l+1, t+1);
310 glVertex2i(l+1, b-1);
311 glVertex2i(l, b-1);
312 (* right *)
313 glVertex2i(r-1, t+1);
314 glVertex2i(r, t+1);
315 glVertex2i(r, b-1);
316 glVertex2i(r-1, b-1);
317 glEnd;
318 end;
319 end;
321 procedure r_Draw_FillRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
322 begin
323 ASSERT(l <= r);
324 ASSERT(t <= b);
325 if (l < r) and (t < b) then
326 begin
327 glEnable(GL_BLEND);
328 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
329 r_Draw_EnableTexture2D(false);
330 r_Draw_SetColor(rr, gg, bb, aa);
331 glBegin(GL_QUADS);
332 glVertex2i(l, t);
333 glVertex2i(r, t);
334 glVertex2i(r, b);
335 glVertex2i(l, b);
336 glEnd;
337 end;
338 end;
340 procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
341 begin
342 ASSERT(l <= r);
343 ASSERT(t <= b);
344 if (l < r) and (t < b) then
345 begin
346 glEnable(GL_BLEND);
347 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
348 r_Draw_EnableTexture2D(false);
349 r_Draw_SetColor(rr, gg, bb, aa);
350 glBegin(GL_QUADS);
351 glVertex2i(l, t);
352 glVertex2i(r, t);
353 glVertex2i(r, b);
354 glVertex2i(l, b);
355 glEnd;
356 end;
357 end;
359 procedure r_Draw_InvertRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
360 begin
361 ASSERT(l <= r);
362 ASSERT(t <= b);
363 if (l < r) and (t < b) then
364 begin
365 glEnable(GL_BLEND);
366 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
367 r_Draw_EnableTexture2D(false);
368 r_Draw_SetColor(rr, gg, bb, aa);
369 glBegin(GL_QUADS);
370 glVertex2i(l, t);
371 glVertex2i(r, t);
372 glVertex2i(r, b);
373 glVertex2i(l, b);
374 glEnd;
375 end;
376 end;
378 procedure r_Draw_Text (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont);
379 var i, xoff, spc: Integer; t: TGLTexture; ch: AnsiChar;
380 begin
381 xoff := x; spc := MAX(0, f.GetSpace());
382 for i := 1 to Length(text) do
383 begin
384 ch := text[i];
385 t := f.GetChar(ch);
386 if t <> nil then
387 r_Draw_Texture(t, xoff, y, t.width, t.height, false, r, g, b, a, false);
388 Inc(xoff, f.GetWidth(ch) + spc);
389 end;
390 end;
392 procedure r_Draw_GetTextSize (const text: AnsiString; f: TGLFont; out w, h: Integer);
393 var i, spc, len: Integer;
394 begin
395 w := 0;
396 h := f.GetMaxHeight();
397 len := Length(text);
398 if len > 0 then
399 begin
400 spc := MAX(0, f.GetSpace());
401 for i := 1 to len - 1 do
402 Inc(w, f.GetWidth(text[i]) + spc);
403 Inc(w, f.GetWidth(text[len]));
404 end;
405 end;
407 procedure r_Draw_SetRect (l, t, r, b: Integer);
408 var x, y, w, h: Integer;
409 begin
410 ASSERT(l <= r);
411 ASSERT(t <= b);
412 x := l * ScreenWidth div GameWidth;
413 y := t * ScreenHeight div GameHeight;
414 w := (r - l + 1) * ScreenWidth div GameWidth;
415 h := (b - t + 1) * ScreenHeight div GameHeight;
416 glScissor(x, ScreenHeight - h - y, w, h);
417 sl := l; st := t; sr := r; sb := b;
418 end;
420 procedure r_Draw_GetRect (out l, t, r, b: Integer);
421 begin
422 l := sl; t := st; r := sr; b := sb;
423 end;
425 end.