DEADSOFTWARE

gl: draw flags
[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; 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);
28 procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
29 procedure r_Draw_MultiTextureRepeatRotate (m: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: 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 const
47 NTR = $FF;
48 NTG = $00;
49 NTB = $00;
50 NTA = $FF;
52 procedure SetupMatrix;
53 begin
54 glScissor(0, 0, gScreenWidth, gScreenHeight);
55 glViewport(0, 0, gScreenWidth, gScreenHeight);
56 glMatrixMode(GL_PROJECTION);
57 glLoadIdentity;
58 glOrtho(0, gScreenWidth, gScreenHeight, 0, 0, 1);
59 glMatrixMode(GL_MODELVIEW);
60 glLoadIdentity;
61 end;
63 procedure DrawQuad (x, y, w, h: Integer);
64 begin
65 glBegin(GL_QUADS);
66 glVertex2i(x + w, y);
67 glVertex2i(x, y);
68 glVertex2i(x, y + h);
69 glVertex2i(x + w, y + h);
70 glEnd();
71 end;
73 procedure DrawTile (tile: TGLAtlasNode; x, y, w, h: Integer; flip: Boolean; rr, gg, bb, aa: Byte; blend: Boolean);
74 var nw, nh, ax, bx, ay, by: GLfloat; l, t, r, b: Integer;
75 begin
76 if tile = nil then
77 begin
78 glColor4ub(rr, gg, bb, aa);
79 if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
80 glDisable(GL_TEXTURE_2D);
81 glEnable(GL_BLEND);
82 DrawQuad(x, y, w, h);
83 end
84 else
85 begin
86 nw := tile.base.w;
87 nh := tile.base.h;
88 ax := IfThen(flip, tile.l, tile.r + 1) / nw;
89 bx := IfThen(flip, tile.r + 1, tile.l) / nh;
90 ay := (tile.t) / nw;
91 by := (tile.b + 1) / nh;
92 l := x; t := y; r := x + w; b := y + h;
93 glBindTexture(GL_TEXTURE_2D, tile.id);
94 glColor4ub(rr, gg, bb, aa);
95 if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
96 glEnable(GL_TEXTURE_2D);
97 glEnable(GL_BLEND);
98 glBegin(GL_QUADS);
99 glTexCoord2f(ax, ay); glVertex2i(r, t);
100 glTexCoord2f(bx, ay); glVertex2i(l, t);
101 glTexCoord2f(bx, by); glVertex2i(l, b);
102 glTexCoord2f(ax, by); glVertex2i(r, b);
103 glEnd();
104 glDisable(GL_TEXTURE_2D);
105 glBindTexture(GL_TEXTURE_2D, 0);
106 end
107 end;
109 procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
110 var i, j, offx, offy: Integer; n: TGLAtlasNode;
111 begin
112 ASSERT(w >= 0);
113 ASSERT(h >= 0);
114 if img = nil then
115 DrawTile(nil, x, y, w, h, flip, NTR, NTB, NTG, NTA, blend)
116 else
117 begin
118 glPushMatrix;
119 glScalef(w / img.width, h / img.height, 1);
120 offx := 0;
121 offy := 0;
122 for j := 0 to img.lines - 1 do
123 begin
124 for i := 0 to img.cols - 1 do
125 begin
126 n := img.GetTile(i, j);
127 ASSERT(n <> nil);
128 DrawTile(n, x + offx, y + offy, n.width, n.height, flip, r, g, b, a, blend);
129 offx := offx + n.width;
130 end;
131 offx := 0;
132 offy := offy + n.height;
133 end;
134 glPopMatrix;
135 end
136 end;
138 procedure r_Draw_TextureRepeat (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
139 var i, j: Integer;
140 begin
141 ASSERT(w >= 0);
142 ASSERT(h >= 0);
143 if img = nil then
144 r_Draw_Texture(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
145 else
146 for j := 0 to h div img.height - 1 do
147 for i := 0 to w div img.width - 1 do
148 r_Draw_Texture(img, x + i * img.width, y + j * img.height, img.width, img.height, flip, r, g, b, a, blend);
149 end;
151 procedure r_Draw_MultiTextureRepeat (m: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean);
152 var img: TGLTexture; cur, total, i: Integer;
153 begin
154 ASSERT(anim.IsValid());
155 if m = nil then
156 r_Draw_TextureRepeat(nil, x, y, w, h, flip, NTR, NTG, NTB, NTB, blend)
157 else
158 begin
159 if m.BackAnim then
160 begin
161 total := m.count * 2 - 1;
162 cur := anim.CurrentFrame mod total;
163 if cur < m.count then i := cur else i := total - cur - 1;
164 end
165 else
166 i := anim.CurrentFrame mod m.count;
167 img := m.GetTexture(i);
168 r_Draw_TextureRepeat(img, x, y, w, h, flip, r, g, b, a, blend);
169 end
170 end;
172 procedure r_Draw_MultiTextureRepeatRotate (m: TGLMultiTexture; const anim: TAnimState; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean; rx, ry, angle: Integer);
173 var i, j: 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_MultiTextureRepeat(m, anim, x, y, w, h, flip, r, g, b, a, blend);
184 glPopMatrix;
185 end
186 else
187 r_Draw_MultiTextureRepeat(m, anim, x, y, w, h, flip, r, g, b, a, blend);
188 end;
190 procedure r_Draw_Filter (l, t, r, b: Integer; rr, gg, bb, aa: Byte);
191 begin
192 ASSERT(r >= l);
193 ASSERT(b >= t);
194 glEnable(GL_BLEND);
195 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
196 glDisable(GL_TEXTURE_2D);
197 glColor4ub(rr, gg, bb, aa);
198 glBegin(GL_QUADS);
199 glVertex2i(l, t);
200 glVertex2i(r, t);
201 glVertex2i(r, b);
202 glVertex2i(l, b);
203 glEnd;
204 end;
206 end.