DEADSOFTWARE

gl: rotate shots
[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_textures,
22 r_textures
23 ;
25 procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean);
26 procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean);
28 procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const a: TAnimState; x, y, w, h: Integer; flip: Boolean);
29 procedure r_Draw_MultiTextureRepeatRotate (img: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; rx, ry, a: Integer);
31 procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
33 implementation
35 uses
36 {$IFDEF USE_GLES1}
37 GLES11,
38 {$ELSE}
39 GL, GLEXT,
40 {$ENDIF}
41 SysUtils, Classes, Math,
42 e_log, utils,
43 g_game // gScreenWidth, gScreenHeight
44 ;
46 procedure SetupMatrix;
47 begin
48 glScissor(0, 0, gScreenWidth, gScreenHeight);
49 glViewport(0, 0, gScreenWidth, gScreenHeight);
50 glMatrixMode(GL_PROJECTION);
51 glLoadIdentity;
52 glOrtho(0, gScreenWidth, gScreenHeight, 0, 0, 1);
53 glMatrixMode(GL_MODELVIEW);
54 glLoadIdentity;
55 end;
57 procedure DrawQuad (x, y, w, h: Integer);
58 begin
59 glBegin(GL_QUADS);
60 glVertex2i(x + w, y);
61 glVertex2i(x, y);
62 glVertex2i(x, y + h);
63 glVertex2i(x + w, y + h);
64 glEnd();
65 end;
67 procedure DrawTile (tile: TGLAtlasNode; x, y, w, h: Integer; flip: Boolean);
68 var nw, nh, ax, bx, ay, by: GLfloat; l, t, r, b: Integer;
69 begin
70 if tile = nil then
71 begin
72 glColor3ub(255, 0, 0);
73 glDisable(GL_BLEND);
74 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
75 glDisable(GL_TEXTURE_2D);
76 DrawQuad(x, y, w, h);
77 end
78 else
79 begin
80 nw := tile.base.w;
81 nh := tile.base.h;
82 ax := IfThen(flip, tile.l, tile.r + 1) / nw;
83 bx := IfThen(flip, tile.r + 1, tile.l) / nh;
84 ay := (tile.t) / nw;
85 by := (tile.b + 1) / nh;
86 l := x; t := y; r := x + w; b := y + h;
87 glColor3ub(255, 255, 255);
88 glBindTexture(GL_TEXTURE_2D, tile.id);
89 glEnable(GL_TEXTURE_2D);
90 glEnable(GL_BLEND);
91 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
92 glBegin(GL_QUADS);
93 glTexCoord2f(ax, ay); glVertex2i(r, t);
94 glTexCoord2f(bx, ay); glVertex2i(l, t);
95 glTexCoord2f(bx, by); glVertex2i(l, b);
96 glTexCoord2f(ax, by); glVertex2i(r, b);
97 glEnd();
98 glDisable(GL_TEXTURE_2D);
99 glBindTexture(GL_TEXTURE_2D, 0);
100 end
101 end;
103 (*
104 procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean);
105 var i, j, offx, offy, nw, nh: Integer; n: TGLAtlasNode;
106 begin
107 ASSERT(w >= 0);
108 ASSERT(h >= 0);
109 if img = nil then
110 DrawTile(nil, x, y, w, h, flip)
111 else
112 begin
113 offx := 0;
114 offy := 0;
115 nw := w div img.cols;
116 nh := h div img.lines;
117 for j := 0 to img.lines - 1 do
118 begin
119 for i := 0 to img.cols - 1 do
120 begin
121 n := img.GetTile(i, j);
122 ASSERT(n <> nil);
123 DrawTile(n, x + offx, y + offy, nw, nh, flip);
124 offx := offx + nw;
125 end;
126 offx := 0;
127 offy := offy + nh;
128 end
129 end
130 end;
131 *)
133 procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean);
134 var i, j, offx, offy: Integer; n: TGLAtlasNode;
135 begin
136 ASSERT(w >= 0);
137 ASSERT(h >= 0);
138 if img = nil then
139 DrawTile(nil, x, y, w, h, flip)
140 else
141 begin
142 glPushMatrix;
143 glScalef(w / img.width, h / img.height, 1);
144 offx := 0;
145 offy := 0;
146 for j := 0 to img.lines - 1 do
147 begin
148 for i := 0 to img.cols - 1 do
149 begin
150 n := img.GetTile(i, j);
151 ASSERT(n <> nil);
152 DrawTile(n, x + offx, y + offy, n.width, n.height, flip);
153 offx := offx + n.width;
154 end;
155 offx := 0;
156 offy := offy + n.height;
157 end;
158 glPopMatrix;
159 end
160 end;
162 procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean);
163 var i, j: Integer;
164 begin
165 ASSERT(w >= 0);
166 ASSERT(h >= 0);
167 if img = nil then
168 r_Draw_Texture(nil, x, y, w, h, flip)
169 else
170 for j := 0 to h div img.height - 1 do
171 for i := 0 to w div img.width - 1 do
172 r_Draw_Texture(img, x + i * img.width, y + j * img.height, img.width, img.height, flip);
173 end;
175 procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const a: TAnimState; x, y, w, h: Integer; flip: Boolean);
176 var img: TGLTexture; cur, total, i: Integer;
177 begin
178 ASSERT(a.IsValid());
179 if m = nil then
180 r_Draw_TextureRepeat(nil, x, y, w, h, flip)
181 else
182 begin
183 if m.BackAnim then
184 begin
185 total := m.count * 2 - 1;
186 cur := a.CurrentFrame mod total;
187 if cur < m.count then i := cur else i := total - cur - 1;
188 end
189 else
190 i := a.CurrentFrame mod m.count;
191 img := m.GetTexture(i);
192 r_Draw_TextureRepeat(img, x, y, w, h, flip)
193 end
194 end;
196 procedure r_Draw_MultiTextureRepeatRotate (img: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; rx, ry, a: Integer);
197 var i, j: Integer;
198 begin
199 ASSERT(w >= 0);
200 ASSERT(h >= 0);
201 if a <> 0 then
202 begin
203 glPushMatrix;
204 glTranslatef(x + rx, y + ry, 0);
205 glRotatef(a, 0, 0, 1);
206 glTranslatef(-(x + rx), -(y + ry), 0);
207 r_Draw_MultiTextureRepeat(img, anim, x, y, w, h, flip);
208 glPopMatrix;
209 end
210 else
211 r_Draw_MultiTextureRepeat(img, anim, x, y, w, h, flip);
212 end;
214 procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
215 begin
216 ASSERT(r >= l);
217 ASSERT(b >= t);
218 glEnable(GL_BLEND);
219 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
220 glDisable(GL_TEXTURE_2D);
221 glColor4ub(rr, gg, bb, aa);
222 glBegin(GL_QUADS);
223 glVertex2i(l, t);
224 glVertex2i(r, t);
225 glVertex2i(r, b);
226 glVertex2i(l, b);
227 glEnd;
228 end;
230 end.