DEADSOFTWARE

1b1c6262bdf17196cfabf8e88714e664c512d54b
[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 glPushMatrix;
141 glTranslatef(x, y, 0);
142 glScalef(w / img.width, h / img.height, 1);
143 for j := 0 to img.lines - 1 do
144 begin
145 for i := 0 to img.cols - 1 do
146 begin
147 n := img.GetTile(i, j);
148 ASSERT(n <> nil);
149 DrawTile(n, 0, 0, n.width, n.height, flip, r, g, b, a, blend);
150 glTranslatef(n.width, 0, 0);
151 offx := offx + n.width;
152 end;
153 glTranslatef(-offx, n.height, 0);
154 offx := 0;
155 offy := offy + n.height;
156 end;
157 glPopMatrix;
158 end
159 end;
161 procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
162 var i, j: Integer;
163 begin
164 ASSERT(w >= 0);
165 ASSERT(h >= 0);
166 if img = nil then
167 r_Draw_Texture(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
168 else
169 for j := 0 to (h - 1) div img.height do
170 for i := 0 to (w - 1) div img.width do
171 r_Draw_Texture(img, x + i * img.width, y + j * img.height, img.width, img.height, flip, r, g, b, a, blend);
172 end;
174 procedure r_Draw_TextureRepeatRotate (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
175 begin
176 ASSERT(w >= 0);
177 ASSERT(h >= 0);
178 if a <> 0 then
179 begin
180 glPushMatrix;
181 glTranslatef(x + rx, y + ry, 0);
182 glRotatef(angle, 0, 0, 1);
183 glTranslatef(-(x + rx), -(y + ry), 0);
184 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
185 glPopMatrix;
186 end
187 else
188 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
189 end;
191 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);
192 var img: TGLTexture; frame: LongInt;
193 begin
194 ASSERT(anim.IsValid());
195 if m = nil then
196 r_Draw_TextureRepeat(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
197 else
198 begin
199 g_Anim_GetFrameFromState(anim, backanim, frame);
200 ASSERT(frame >= 0);
201 ASSERT(frame < m.count);
202 img := m.GetTexture(frame);
203 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
204 end
205 end;
207 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);
208 begin
209 ASSERT(w >= 0);
210 ASSERT(h >= 0);
211 if a <> 0 then
212 begin
213 glPushMatrix;
214 glTranslatef(x + rx, y + ry, 0);
215 glRotatef(angle, 0, 0, 1);
216 glTranslatef(-(x + rx), -(y + ry), 0);
217 r_Draw_MultiTextureRepeat(m, anim, backanim, x, y, w, h, flip, r, g, b, a, blend);
218 glPopMatrix;
219 end
220 else
221 r_Draw_MultiTextureRepeat(m, anim, backanim, x, y, w, h, flip, r, g, b, a, blend);
222 end;
224 procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
225 begin
226 ASSERT(r >= l);
227 ASSERT(b >= t);
228 glEnable(GL_BLEND);
229 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
230 glDisable(GL_TEXTURE_2D);
231 glColor4ub(rr, gg, bb, aa);
232 glBegin(GL_QUADS);
233 glVertex2i(l, t);
234 glVertex2i(r, t);
235 glVertex2i(r, b);
236 glVertex2i(l, b);
237 glEnd;
238 end;
240 procedure r_Draw_Rect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
241 begin
242 ASSERT(r >= l);
243 ASSERT(b >= t);
244 glEnable(GL_BLEND);
245 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
246 glDisable(GL_TEXTURE_2D);
247 glColor4ub(rr, gg, bb, aa);
248 glBegin(GL_LINE_LOOP);
250 glVertex2i(l, t);
251 glVertex2i(r, t);
252 glVertex2i(r, b);
253 glVertex2i(l, b);
255 glVertex2f(l + 0.5, t + 0.5);
256 glVertex2f(r - 0.5, t + 0.5);
257 glVertex2f(r - 0.5, b - 0.5);
258 glVertex2f(l + 0.5, b - 0.5);
259 glEnd;
260 end;
262 procedure r_Draw_FillRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
263 begin
264 ASSERT(r >= l);
265 ASSERT(b >= t);
266 glEnable(GL_BLEND);
267 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
268 glDisable(GL_TEXTURE_2D);
269 glColor4ub(rr, gg, bb, aa);
270 glBegin(GL_QUADS);
272 glVertex2i(l, t);
273 glVertex2i(r, t);
274 glVertex2i(r, b);
275 glVertex2i(l, b);
278 glVertex2f(l + 0.5, t + 0.5);
279 glVertex2f(r - 0.5, t + 0.5);
280 glVertex2f(r - 0.5, b - 0.5);
281 glVertex2f(l + 0.5, b - 0.5);
283 glVertex2f(l + 0, t + 0);
284 glVertex2f(r + 0.75, t + 0);
285 glVertex2f(r + 0.75, b + 0.75);
286 glVertex2f(l + 0, b + 0.75);
287 glEnd;
288 end;
290 procedure r_Draw_InvertRect (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
291 begin
292 ASSERT(r >= l);
293 ASSERT(b >= t);
294 glEnable(GL_BLEND);
295 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
296 glDisable(GL_TEXTURE_2D);
297 glColor4ub(rr, gg, bb, aa);
298 glBegin(GL_QUADS);
299 glVertex2i(l, t);
300 glVertex2i(r, t);
301 glVertex2i(r, b);
302 glVertex2i(l, b);
303 glEnd;
304 end;
306 procedure r_Draw_Text (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont);
307 var i, xoff, spc: Integer; t: TGLTexture; ch: AnsiChar;
308 begin
309 xoff := x; spc := MAX(0, f.GetSpace());
310 for i := 1 to Length(text) do
311 begin
312 ch := text[i];
313 t := f.GetChar(ch);
314 if t <> nil then
315 r_Draw_Texture(t, xoff, y, t.width, t.height, false, r, g, b, a, false);
316 Inc(xoff, f.GetWidth(ch) + spc);
317 end;
318 end;
320 procedure r_Draw_GetTextSize (const text: AnsiString; f: TGLFont; out w, h: Integer);
321 var i, spc, len: Integer;
322 begin
323 w := 0;
324 h := f.GetMaxHeight();
325 len := Length(text);
326 if len > 0 then
327 begin
328 spc := MAX(0, f.GetSpace());
329 for i := 1 to len - 1 do
330 Inc(w, f.GetWidth(text[i]) + spc);
331 Inc(w, f.GetWidth(text[len]));
332 end;
333 end;
335 procedure r_Draw_SetRect (l, t, r, b: Integer);
336 var w, h: Integer;
337 begin
338 ASSERT(l <= r);
339 ASSERT(t <= b);
340 w := r - l + 1;
341 h := b - t + 1;
342 glScissor(l, ScreenHeight - h - t, w, h);
343 sl := l; st := t; sr := r; sb := b;
344 end;
346 procedure r_Draw_GetRect (out l, t, r, b: Integer);
347 begin
348 l := sl; t := st; r := sr; b := sb;
349 end;
351 end.