DEADSOFTWARE

55d9dad6432db0bc2d1832aa6a3fc8af3fee831c
[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 (w, h: Integer);
41 procedure r_Draw_SetRect (l, t, r, b: Integer);
42 procedure r_Draw_GetRect (out l, t, r, b: Integer);
44 implementation
46 uses
47 {$IFDEF USE_GLES1}
48 GLES11,
49 {$ELSE}
50 GL, GLEXT,
51 {$ENDIF}
52 SysUtils, Classes, Math,
53 e_log, utils
54 ;
56 const
57 NTR = $FF;
58 NTG = $00;
59 NTB = $00;
60 NTA = $FF;
62 var
63 sl, st, sr, sb: Integer;
64 ScreenWidth, ScreenHeight: Integer;
66 procedure r_Draw_Setup (w, h: Integer);
67 begin
68 ASSERT(w >= 0);
69 ASSERT(h >= 0);
70 ScreenWidth := w;
71 ScreenHeight := h;
72 glScissor(0, 0, w, h);
73 glViewport(0, 0, w, h);
74 glMatrixMode(GL_PROJECTION);
75 glLoadIdentity;
76 glOrtho(0, w, h, 0, 0, 1);
77 glMatrixMode(GL_MODELVIEW);
78 glLoadIdentity;
79 glEnable(GL_SCISSOR_TEST);
80 r_Draw_SetRect(0, 0, w - 1, h - 1);
81 end;
83 procedure DrawQuad (x, y, w, h: Integer);
84 begin
85 glBegin(GL_QUADS);
86 glVertex2i(x + w, y);
87 glVertex2i(x, y);
88 glVertex2i(x, y + h);
89 glVertex2i(x + w, y + h);
90 glEnd();
91 end;
93 procedure DrawTile (tile: TGLAtlasNode; x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend: Boolean);
94 var nw, nh, ax, bx, ay, by: GLfloat; l, t, r, b: Integer;
95 begin
96 if tile = nil then
97 begin
98 glColor4ub(rr, gg, bb, aa);
99 if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
100 glDisable(GL_TEXTURE_2D);
101 glEnable(GL_BLEND);
102 DrawQuad(x, y, w, h);
103 end
104 else
105 begin
106 nw := tile.base.w;
107 nh := tile.base.h;
108 ax := IfThen(flip, tile.l, tile.r + 1) / nw;
109 bx := IfThen(flip, tile.r + 1, tile.l) / nh;
110 ay := (tile.t) / nw;
111 by := (tile.b + 1) / nh;
112 l := x; t := y; r := x + w; b := y + h;
113 glBindTexture(GL_TEXTURE_2D, tile.id);
114 glColor4ub(rr, gg, bb, aa);
115 if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
116 glEnable(GL_TEXTURE_2D);
117 glEnable(GL_BLEND);
118 glBegin(GL_QUADS);
119 glTexCoord2f(ax, ay); glVertex2i(r, t);
120 glTexCoord2f(bx, ay); glVertex2i(l, t);
121 glTexCoord2f(bx, by); glVertex2i(l, b);
122 glTexCoord2f(ax, by); glVertex2i(r, b);
123 glEnd();
124 glDisable(GL_TEXTURE_2D);
125 glBindTexture(GL_TEXTURE_2D, 0);
126 end
127 end;
129 procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
130 var i, j, offx, offy: Integer; n: TGLAtlasNode;
131 begin
132 ASSERT(w >= 0);
133 ASSERT(h >= 0);
134 if img = nil then
135 DrawTile(nil, x, y, w, h, flip, NTR, NTB, NTG, NTA, blend)
136 else
137 begin
138 offx := 0;
139 offy := 0;
140 for j := 0 to img.lines - 1 do
141 begin
142 for i := 0 to img.cols - 1 do
143 begin
144 n := img.GetTile(i, j);
145 ASSERT(n <> nil);
146 glPushMatrix;
147 glTranslatef(x + offx, y + offy, 0);
148 glScalef(w / img.width, h / img.height, 1);
149 DrawTile(n, 0, 0, n.width, n.height, flip, r, g, b, a, blend);
150 glPopMatrix;
151 offx := offx + n.width;
152 end;
153 offx := 0;
154 offy := offy + n.height;
155 end;
156 end
157 end;
159 procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
160 var i, j: Integer;
161 begin
162 ASSERT(w >= 0);
163 ASSERT(h >= 0);
164 if img = nil then
165 r_Draw_Texture(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
166 else
167 for j := 0 to (h - 1) div img.height do
168 for i := 0 to (w - 1) div img.width do
169 r_Draw_Texture(img, x + i * img.width, y + j * img.height, img.width, img.height, flip, r, g, b, a, blend);
170 end;
172 procedure r_Draw_TextureRepeatRotate (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
173 begin
174 ASSERT(w >= 0);
175 ASSERT(h >= 0);
176 if a <> 0 then
177 begin
178 glPushMatrix;
179 glTranslatef(x + rx, y + ry, 0);
180 glRotatef(angle, 0, 0, 1);
181 glTranslatef(-(x + rx), -(y + ry), 0);
182 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
183 glPopMatrix;
184 end
185 else
186 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
187 end;
189 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);
190 var img: TGLTexture; frame: LongInt;
191 begin
192 ASSERT(anim.IsValid());
193 if m = nil then
194 r_Draw_TextureRepeat(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
195 else
196 begin
197 g_Anim_GetFrameFromState(anim, backanim, frame);
198 ASSERT(frame >= 0);
199 ASSERT(frame < m.count);
200 img := m.GetTexture(frame);
201 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
202 end
203 end;
205 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);
206 begin
207 ASSERT(w >= 0);
208 ASSERT(h >= 0);
209 if a <> 0 then
210 begin
211 glPushMatrix;
212 glTranslatef(x + rx, y + ry, 0);
213 glRotatef(angle, 0, 0, 1);
214 glTranslatef(-(x + rx), -(y + ry), 0);
215 r_Draw_MultiTextureRepeat(m, anim, backanim, x, y, w, h, flip, r, g, b, a, blend);
216 glPopMatrix;
217 end
218 else
219 r_Draw_MultiTextureRepeat(m, anim, backanim, x, y, w, h, flip, r, g, b, a, blend);
220 end;
222 procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
223 begin
224 ASSERT(r >= l);
225 ASSERT(b >= t);
226 glEnable(GL_BLEND);
227 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
228 glDisable(GL_TEXTURE_2D);
229 glColor4ub(rr, gg, bb, aa);
230 glBegin(GL_QUADS);
231 glVertex2i(l, t);
232 glVertex2i(r, t);
233 glVertex2i(r, b);
234 glVertex2i(l, b);
235 glEnd;
236 end;
238 procedure r_Draw_Rect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
239 begin
240 ASSERT(r >= l);
241 ASSERT(b >= t);
242 glEnable(GL_BLEND);
243 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
244 glDisable(GL_TEXTURE_2D);
245 glColor4ub(rr, gg, bb, aa);
246 glBegin(GL_LINE_LOOP);
248 glVertex2i(l, t);
249 glVertex2i(r, t);
250 glVertex2i(r, b);
251 glVertex2i(l, b);
253 glVertex2f(l + 0.5, t + 0.5);
254 glVertex2f(r - 0.5, t + 0.5);
255 glVertex2f(r - 0.5, b - 0.5);
256 glVertex2f(l + 0.5, b - 0.5);
257 glEnd;
258 end;
260 procedure r_Draw_FillRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
261 begin
262 ASSERT(r >= l);
263 ASSERT(b >= t);
264 glEnable(GL_BLEND);
265 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
266 glDisable(GL_TEXTURE_2D);
267 glColor4ub(rr, gg, bb, aa);
268 glBegin(GL_QUADS);
270 glVertex2i(l, t);
271 glVertex2i(r, t);
272 glVertex2i(r, b);
273 glVertex2i(l, b);
276 glVertex2f(l + 0.5, t + 0.5);
277 glVertex2f(r - 0.5, t + 0.5);
278 glVertex2f(r - 0.5, b - 0.5);
279 glVertex2f(l + 0.5, b - 0.5);
281 glVertex2f(l + 0, t + 0);
282 glVertex2f(r + 0.75, t + 0);
283 glVertex2f(r + 0.75, b + 0.75);
284 glVertex2f(l + 0, b + 0.75);
285 glEnd;
286 end;
288 procedure r_Draw_InvertRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
289 begin
290 ASSERT(r >= l);
291 ASSERT(b >= t);
292 glEnable(GL_BLEND);
293 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
294 glDisable(GL_TEXTURE_2D);
295 glColor4ub(rr, gg, bb, aa);
296 glBegin(GL_QUADS);
297 glVertex2i(l, t);
298 glVertex2i(r, t);
299 glVertex2i(r, b);
300 glVertex2i(l, b);
301 glEnd;
302 end;
304 procedure r_Draw_Text (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont);
305 var i, xoff, spc: Integer; t: TGLTexture; ch: AnsiChar;
306 begin
307 xoff := x; spc := MAX(0, f.GetSpace());
308 for i := 1 to Length(text) do
309 begin
310 ch := text[i];
311 t := f.GetChar(ch);
312 if t <> nil then
313 r_Draw_Texture(t, xoff, y, t.width, t.height, false, r, g, b, a, false);
314 Inc(xoff, f.GetWidth(ch) + spc);
315 end;
316 end;
318 procedure r_Draw_GetTextSize (const text: AnsiString; f: TGLFont; out w, h: Integer);
319 var i, spc, len: Integer;
320 begin
321 w := 0;
322 h := f.GetMaxHeight();
323 len := Length(text);
324 if len > 0 then
325 begin
326 spc := MAX(0, f.GetSpace());
327 for i := 1 to len - 1 do
328 Inc(w, f.GetWidth(text[i]) + spc);
329 Inc(w, f.GetWidth(text[len]));
330 end;
331 end;
333 procedure r_Draw_SetRect (l, t, r, b: Integer);
334 var w, h: Integer;
335 begin
336 ASSERT(l <= r);
337 ASSERT(t <= b);
338 w := r - l + 1;
339 h := b - t + 1;
340 glScissor(l, ScreenHeight - h - t, w, h);
341 sl := l; st := t; sr := r; sb := b;
342 end;
344 procedure r_Draw_GetRect (out l, t, r, b: Integer);
345 begin
346 l := sl; t := st; r := sr; b := sb;
347 end;
349 end.