DEADSOFTWARE

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