DEADSOFTWARE

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