rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <string.h>
|
nuclear@0
|
4 #include <ctype.h>
|
nuclear@0
|
5 #include <math.h>
|
nuclear@0
|
6 #include "opengl.h"
|
nuclear@0
|
7 #include "level.h"
|
nuclear@0
|
8
|
nuclear@0
|
9 #define C_WALL '#'
|
nuclear@0
|
10 #define C_PILLAR 'o'
|
nuclear@0
|
11 #define C_START 's'
|
nuclear@0
|
12 #define C_GOLD 'x'
|
nuclear@0
|
13
|
nuclear@0
|
14 #define IS_SOLID(x) ((x) == C_WALL)
|
nuclear@0
|
15
|
nuclear@0
|
16
|
nuclear@0
|
17 void level_init(struct level *lvl)
|
nuclear@0
|
18 {
|
nuclear@0
|
19 memset(lvl, 0, sizeof *lvl);
|
nuclear@0
|
20 lvl->cell_size = 3.0;
|
nuclear@0
|
21 lvl->cell_height = 2.5;
|
nuclear@0
|
22 lvl->floor_tex_scale = lvl->wall_tex_scale = lvl->ceil_tex_scale = 1.0;
|
nuclear@0
|
23 }
|
nuclear@0
|
24
|
nuclear@0
|
25 static void clean_line(char *buf)
|
nuclear@0
|
26 {
|
nuclear@0
|
27 char *end = buf + strlen(buf) - 1;
|
nuclear@0
|
28
|
nuclear@0
|
29 if(end <= buf) return;
|
nuclear@0
|
30
|
nuclear@0
|
31 while(end >= buf && !isprint(*end)) {
|
nuclear@0
|
32 *end-- = 0;
|
nuclear@0
|
33 }
|
nuclear@0
|
34 }
|
nuclear@0
|
35
|
nuclear@0
|
36 int level_load(struct level *lvl, const char *fname)
|
nuclear@0
|
37 {
|
nuclear@0
|
38 FILE *fp;
|
nuclear@0
|
39 char buf[256];
|
nuclear@0
|
40 int i, size[2], nlines;
|
nuclear@0
|
41
|
nuclear@0
|
42 if(!(fp = fopen(fname, "r"))) {
|
nuclear@0
|
43 fprintf(stderr, "failed to open file: %s\n", fname);
|
nuclear@0
|
44 return -1;
|
nuclear@0
|
45 }
|
nuclear@0
|
46
|
nuclear@0
|
47 if(!fgets(buf, sizeof buf, fp)) {
|
nuclear@0
|
48 fprintf(stderr, "level file %s is empty\n", fname);
|
nuclear@0
|
49 fclose(fp);
|
nuclear@0
|
50 return -1;
|
nuclear@0
|
51 }
|
nuclear@0
|
52 if(sscanf(buf, "s %dx%d", size, size + 1) != 2) {
|
nuclear@0
|
53 fprintf(stderr, "level file %s doesn't start with size definition\n", fname);
|
nuclear@0
|
54 fclose(fp);
|
nuclear@0
|
55 return -1;
|
nuclear@0
|
56 }
|
nuclear@0
|
57 if(size[0] > MAX_LEVEL_SIZE || size[1] > MAX_LEVEL_SIZE) {
|
nuclear@0
|
58 fprintf(stderr, "level size %dx%d is larger than compile-time maximum (%d)\n", size[0], size[1], MAX_LEVEL_SIZE);
|
nuclear@0
|
59 fclose(fp);
|
nuclear@0
|
60 return -1;
|
nuclear@0
|
61 }
|
nuclear@0
|
62
|
nuclear@0
|
63 lvl->num_cells[0] = size[0];
|
nuclear@0
|
64 lvl->num_cells[1] = size[1];
|
nuclear@0
|
65
|
nuclear@0
|
66 nlines = 0;
|
nuclear@0
|
67 while(fgets(buf, sizeof buf, fp)) {
|
nuclear@0
|
68 if(nlines >= size[0]) {
|
nuclear@0
|
69 fprintf(stderr, "warning: level contains more lines than specified, ignoring the rest\n");
|
nuclear@0
|
70 break;
|
nuclear@0
|
71 }
|
nuclear@0
|
72 clean_line(buf);
|
nuclear@0
|
73
|
nuclear@0
|
74 for(i=0; buf[i]; i++) {
|
nuclear@0
|
75 if(i >= size[1]) {
|
nuclear@0
|
76 fprintf(stderr, "warning: line %d is longer than the level size definition says. Skipping the rest.\n", nlines + 1);
|
nuclear@0
|
77 break;
|
nuclear@0
|
78 }
|
nuclear@0
|
79 lvl->cells[nlines][i] = buf[i];
|
nuclear@0
|
80
|
nuclear@0
|
81 if(buf[i] == C_START) {
|
nuclear@0
|
82 lvl->start_pos[0] = i;
|
nuclear@0
|
83 lvl->start_pos[1] = nlines;
|
nuclear@0
|
84 printf("start cell found (%d,%d)\n", lvl->start_pos[0], lvl->start_pos[1]);
|
nuclear@0
|
85 }
|
nuclear@0
|
86 if(buf[i] == C_GOLD) {
|
nuclear@0
|
87 level_cell_to_pos(lvl, i, nlines, lvl->goal_pos, lvl->goal_pos + 1);
|
nuclear@0
|
88 printf("gold cell found (%d, %d)\n", i, nlines);
|
nuclear@0
|
89 }
|
nuclear@0
|
90 }
|
nuclear@0
|
91 nlines++;
|
nuclear@0
|
92 }
|
nuclear@0
|
93
|
nuclear@0
|
94 fclose(fp);
|
nuclear@0
|
95 return 0;
|
nuclear@0
|
96 }
|
nuclear@0
|
97
|
nuclear@0
|
98
|
nuclear@0
|
99 static int clamp(int x, int low, int high)
|
nuclear@0
|
100 {
|
nuclear@0
|
101 return x < low ? low : (x > high ? high : x);
|
nuclear@0
|
102 }
|
nuclear@0
|
103
|
nuclear@0
|
104 void level_pos_to_cell(struct level *lvl, float x, float y, int *res_cx, int *res_cy)
|
nuclear@0
|
105 {
|
nuclear@0
|
106 int cx = (int)(x / lvl->cell_size + 0.5);
|
nuclear@0
|
107 int cy = (int)(y / lvl->cell_size + 0.5);
|
nuclear@0
|
108
|
nuclear@0
|
109 *res_cx = clamp(cx, 0, lvl->num_cells[1] - 1);
|
nuclear@0
|
110 *res_cy = clamp(cy, 0, lvl->num_cells[0] - 1);
|
nuclear@0
|
111 }
|
nuclear@0
|
112
|
nuclear@0
|
113 void level_cell_to_pos(struct level *lvl, int cx, int cy, float *resx, float *resy)
|
nuclear@0
|
114 {
|
nuclear@0
|
115 cx = clamp(cx, 0, lvl->num_cells[1] - 1);
|
nuclear@0
|
116 cy = clamp(cy, 0, lvl->num_cells[0] - 1);
|
nuclear@0
|
117
|
nuclear@0
|
118 *resx = (float)cx * lvl->cell_size;
|
nuclear@0
|
119 *resy = (float)cy * lvl->cell_size;
|
nuclear@0
|
120 }
|
nuclear@0
|
121
|
nuclear@0
|
122 int level_cell(struct level *lvl, int cx, int cy)
|
nuclear@0
|
123 {
|
nuclear@0
|
124 cx = clamp(cx, 0, lvl->num_cells[1] - 1);
|
nuclear@0
|
125 cy = clamp(cy, 0, lvl->num_cells[0] - 1);
|
nuclear@0
|
126
|
nuclear@0
|
127 return lvl->cells[cy][cx];
|
nuclear@0
|
128 }
|
nuclear@0
|
129
|
nuclear@0
|
130 int level_cell_at(struct level *lvl, float x, float y)
|
nuclear@0
|
131 {
|
nuclear@0
|
132 int cx, cy;
|
nuclear@0
|
133 level_pos_to_cell(lvl, x, y, &cx, &cy);
|
nuclear@0
|
134 return level_cell(lvl, cx, cy);
|
nuclear@0
|
135 }
|
nuclear@0
|
136
|
nuclear@0
|
137 int level_obj_pos(struct level *lvl, int objname, float *resx, float *resy)
|
nuclear@0
|
138 {
|
nuclear@0
|
139 int i, j;
|
nuclear@0
|
140
|
nuclear@0
|
141 for(i=0; i<lvl->num_cells[0]; i++) {
|
nuclear@0
|
142 for(j=0; j<lvl->num_cells[1]; j++) {
|
nuclear@0
|
143 if(lvl->cells[i][j] == objname) {
|
nuclear@0
|
144 level_cell_to_pos(lvl, j, i, resx, resy);
|
nuclear@0
|
145 return 1;
|
nuclear@0
|
146 }
|
nuclear@0
|
147 }
|
nuclear@0
|
148 }
|
nuclear@0
|
149 return 0;
|
nuclear@0
|
150 }
|
nuclear@0
|
151
|
nuclear@1
|
152 /* NOTE: this function could be simpler */
|
nuclear@0
|
153 int level_collide(struct level *lvl, float rad, float x, float y, float *dxp, float *dyp)
|
nuclear@0
|
154 {
|
nuclear@0
|
155 int i, val, cxy[2], collided = 0;
|
nuclear@0
|
156 float pos[2], dir[2], center[2], len;
|
nuclear@0
|
157 int adj_cxy[2];
|
nuclear@0
|
158
|
nuclear@0
|
159 pos[0] = x;
|
nuclear@0
|
160 pos[1] = y;
|
nuclear@0
|
161 dir[0] = *dxp;
|
nuclear@0
|
162 dir[1] = *dyp;
|
nuclear@0
|
163
|
nuclear@0
|
164 /* clamp the direction magnitude (manhattan) to the cell size */
|
nuclear@0
|
165 for(i=0; i<2; i++) {
|
nuclear@0
|
166 if(dir[i] > lvl->cell_size) dir[i] = lvl->cell_size;
|
nuclear@0
|
167 if(dir[i] < -lvl->cell_size) dir[i] = -lvl->cell_size;
|
nuclear@0
|
168 }
|
nuclear@0
|
169
|
nuclear@0
|
170 level_pos_to_cell(lvl, pos[0], pos[1], cxy, cxy + 1);
|
nuclear@0
|
171 level_cell_to_pos(lvl, cxy[0], cxy[1], center, center + 1);
|
nuclear@0
|
172
|
nuclear@0
|
173 /* check collision and clamp dx/dy with each of the 4 directions */
|
nuclear@0
|
174 for(i=0; i<4; i++) {
|
nuclear@0
|
175 float wall_pos, dist;
|
nuclear@0
|
176 int axis = (i & 2) >> 1;
|
nuclear@0
|
177 int sign = (i & 1) ? -1 : 1;
|
nuclear@0
|
178
|
nuclear@0
|
179 adj_cxy[0] = cxy[0];
|
nuclear@0
|
180 adj_cxy[1] = cxy[1];
|
nuclear@0
|
181 adj_cxy[axis] += sign;
|
nuclear@0
|
182
|
nuclear@0
|
183 val = level_cell(lvl, adj_cxy[0], adj_cxy[1]);
|
nuclear@0
|
184
|
nuclear@0
|
185 if(!IS_SOLID(val)) continue;
|
nuclear@0
|
186
|
nuclear@0
|
187 wall_pos = center[axis] + (lvl->cell_size * 0.5 - rad) * (float)sign;
|
nuclear@0
|
188 dist = fabs(wall_pos - pos[axis]);
|
nuclear@0
|
189
|
nuclear@0
|
190 if(dir[axis] * (float)sign > dist) {
|
nuclear@0
|
191 dir[axis] = (float)sign * dist;
|
nuclear@0
|
192 collided = 1;
|
nuclear@0
|
193 }
|
nuclear@0
|
194 }
|
nuclear@0
|
195
|
nuclear@0
|
196 /* finally to make sure we don't slip through cracks in corners, also check
|
nuclear@0
|
197 * the destination cell
|
nuclear@0
|
198 */
|
nuclear@0
|
199 len = sqrt(dir[0] * dir[0] + dir[1] * dir[1]);
|
nuclear@0
|
200 pos[0] += dir[0] + dir[0] / len * rad;
|
nuclear@0
|
201 pos[1] += dir[1] + dir[1] / len * rad;
|
nuclear@0
|
202
|
nuclear@0
|
203 val = level_cell_at(lvl, pos[0], pos[1]);
|
nuclear@0
|
204 if(IS_SOLID(val)) {
|
nuclear@0
|
205 dir[0] = dir[1] = 0;
|
nuclear@0
|
206 }
|
nuclear@0
|
207
|
nuclear@0
|
208
|
nuclear@0
|
209 *dxp = dir[0];
|
nuclear@0
|
210 *dyp = dir[1];
|
nuclear@0
|
211 return collided;
|
nuclear@0
|
212 }
|
nuclear@0
|
213
|
nuclear@0
|
214 static void wall_faces(float x, float y, float width, float height, float uscale, float vscale)
|
nuclear@0
|
215 {
|
nuclear@0
|
216 float u0 = 0.5 - 0.5 * uscale;
|
nuclear@0
|
217 float u1 = 0.5 + 0.5 * uscale;
|
nuclear@0
|
218 float v0 = 0.5 - 0.5 * vscale;
|
nuclear@0
|
219 float v1 = 0.5 + 0.5 * vscale;
|
nuclear@0
|
220
|
nuclear@0
|
221 width /= 2.0;
|
nuclear@0
|
222
|
nuclear@0
|
223 glNormal3f(0, 0, 1);
|
nuclear@0
|
224 glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y + width);
|
nuclear@0
|
225 glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y + width);
|
nuclear@0
|
226 glTexCoord2f(u1, v0); glVertex3f(x + width, height, y + width);
|
nuclear@0
|
227 glTexCoord2f(u0, v0); glVertex3f(x - width, height, y + width);
|
nuclear@0
|
228 glNormal3f(0, 0, -1);
|
nuclear@0
|
229 glTexCoord2f(u0, v0); glVertex3f(x - width, height, y - width);
|
nuclear@0
|
230 glTexCoord2f(u1, v0); glVertex3f(x + width, height, y - width);
|
nuclear@0
|
231 glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y - width);
|
nuclear@0
|
232 glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y - width);
|
nuclear@0
|
233 glNormal3f(1, 0, 0);
|
nuclear@0
|
234 glTexCoord2f(u0, v1); glVertex3f(x + width, 0, y + width);
|
nuclear@0
|
235 glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y - width);
|
nuclear@0
|
236 glTexCoord2f(u1, v0); glVertex3f(x + width, height, y - width);
|
nuclear@0
|
237 glTexCoord2f(u0, v0); glVertex3f(x + width, height, y + width);
|
nuclear@0
|
238 glNormal3f(-1, 0, 0);
|
nuclear@0
|
239 glTexCoord2f(u0, v0); glVertex3f(x - width, height, y + width);
|
nuclear@0
|
240 glTexCoord2f(u1, v0); glVertex3f(x - width, height, y - width);
|
nuclear@0
|
241 glTexCoord2f(u1, v1); glVertex3f(x - width, 0, y - width);
|
nuclear@0
|
242 glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y + width);
|
nuclear@0
|
243 }
|
nuclear@0
|
244
|
nuclear@0
|
245 void level_draw(struct level *lvl)
|
nuclear@0
|
246 {
|
nuclear@0
|
247 static int first = 1;
|
nuclear@0
|
248 int i, j, k;
|
nuclear@0
|
249
|
nuclear@0
|
250 set_mtl_diffuse(1, 1, 1, 1);
|
nuclear@0
|
251 set_mtl_specular(0, 0, 0);
|
nuclear@0
|
252
|
nuclear@0
|
253 glMatrixMode(GL_TEXTURE);
|
nuclear@0
|
254 glLoadIdentity();
|
nuclear@0
|
255 glScalef(lvl->wall_tex_scale, lvl->wall_tex_scale, lvl->wall_tex_scale);
|
nuclear@0
|
256
|
nuclear@0
|
257 glEnable(GL_TEXTURE_2D);
|
nuclear@0
|
258
|
nuclear@0
|
259 /* draw walls */
|
nuclear@0
|
260 glBindTexture(GL_TEXTURE_2D, lvl->wall_tex);
|
nuclear@0
|
261 glBegin(GL_QUADS);
|
nuclear@0
|
262 for(i=0; i<lvl->num_cells[0]; i++) {
|
nuclear@0
|
263 int cy = i;
|
nuclear@0
|
264 for(j=0; j<lvl->num_cells[1]; j++) {
|
nuclear@0
|
265 float x, y;
|
nuclear@0
|
266 int cx = j;
|
nuclear@0
|
267 int c = level_cell(lvl, cx, cy);
|
nuclear@0
|
268
|
nuclear@0
|
269 level_cell_to_pos(lvl, cx, cy, &x, &y);
|
nuclear@0
|
270
|
nuclear@0
|
271 if(c == C_WALL) {
|
nuclear@0
|
272 wall_faces(x, y, lvl->cell_size, lvl->cell_height, 1, 1);
|
nuclear@0
|
273 } else if(c == C_PILLAR) {
|
nuclear@0
|
274 wall_faces(x, y, 0.2, lvl->cell_height, 0.04, 1);
|
nuclear@0
|
275 }
|
nuclear@0
|
276
|
nuclear@0
|
277 if(first) putchar(c);
|
nuclear@0
|
278 }
|
nuclear@0
|
279 if(first) putchar('\n');
|
nuclear@0
|
280 }
|
nuclear@0
|
281 glEnd();
|
nuclear@0
|
282
|
nuclear@0
|
283 /* draw floor & ceiling */
|
nuclear@0
|
284 for(k=0; k<2; k++) {
|
nuclear@0
|
285 int do_floor = k == 0;
|
nuclear@0
|
286 float height, normy;
|
nuclear@0
|
287
|
nuclear@0
|
288 if(do_floor) {
|
nuclear@0
|
289 glBindTexture(GL_TEXTURE_2D, lvl->floor_tex);
|
nuclear@0
|
290 glScalef(lvl->floor_tex_scale, lvl->floor_tex_scale, lvl->floor_tex_scale);
|
nuclear@0
|
291 height = 0;
|
nuclear@0
|
292 normy = 1;
|
nuclear@0
|
293 glFrontFace(GL_CCW);
|
nuclear@0
|
294 } else {
|
nuclear@0
|
295 glBindTexture(GL_TEXTURE_2D, lvl->ceil_tex);
|
nuclear@0
|
296 glScalef(lvl->ceil_tex_scale, lvl->ceil_tex_scale, lvl->ceil_tex_scale);
|
nuclear@0
|
297 height = lvl->cell_height;
|
nuclear@0
|
298 normy = -1;
|
nuclear@0
|
299 glFrontFace(GL_CW);
|
nuclear@0
|
300 }
|
nuclear@0
|
301
|
nuclear@0
|
302 glBegin(GL_TRIANGLES);
|
nuclear@0
|
303 for(i=0; i<lvl->num_cells[0]; i++) {
|
nuclear@0
|
304 int cy = i;
|
nuclear@0
|
305 for(j=0; j<lvl->num_cells[1]; j++) {
|
nuclear@0
|
306 float x, y;
|
nuclear@0
|
307 int cx = j;
|
nuclear@0
|
308 int c = level_cell(lvl, cx, cy);
|
nuclear@0
|
309
|
nuclear@0
|
310 level_cell_to_pos(lvl, cx, cy, &x, &y);
|
nuclear@0
|
311
|
nuclear@0
|
312 if(c != C_WALL) {
|
nuclear@0
|
313 float hsz = lvl->cell_size / 2.0f;
|
nuclear@0
|
314
|
nuclear@0
|
315 glNormal3f(0, normy, 0);
|
nuclear@0
|
316 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
|
nuclear@0
|
317 glTexCoord2f(0.0, 0.0); glVertex3f(x - hsz, height, y + hsz);
|
nuclear@0
|
318 glTexCoord2f(1.0, 0.0); glVertex3f(x + hsz, height, y + hsz);
|
nuclear@0
|
319 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
|
nuclear@0
|
320 glTexCoord2f(1.0, 0.0); glVertex3f(x + hsz, height, y + hsz);
|
nuclear@0
|
321 glTexCoord2f(1.0, 1.0); glVertex3f(x + hsz, height, y - hsz);
|
nuclear@0
|
322 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
|
nuclear@0
|
323 glTexCoord2f(1.0, 1.0); glVertex3f(x + hsz, height, y - hsz);
|
nuclear@0
|
324 glTexCoord2f(0.0, 1.0); glVertex3f(x - hsz, height, y - hsz);
|
nuclear@0
|
325 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
|
nuclear@0
|
326 glTexCoord2f(0.0, 1.0); glVertex3f(x - hsz, height, y - hsz);
|
nuclear@0
|
327 glTexCoord2f(0.0, 0.0); glVertex3f(x - hsz, height, y + hsz);
|
nuclear@0
|
328 }
|
nuclear@0
|
329 }
|
nuclear@0
|
330 }
|
nuclear@0
|
331 glEnd();
|
nuclear@0
|
332 }
|
nuclear@0
|
333
|
nuclear@0
|
334 glFrontFace(GL_CCW); /* restore front-face mode */
|
nuclear@0
|
335 glDisable(GL_TEXTURE_2D);
|
nuclear@0
|
336
|
nuclear@0
|
337 glLoadIdentity(); /* restore the texture matrix to identity */
|
nuclear@0
|
338 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
339
|
nuclear@0
|
340 /* draw other objects
|
nuclear@0
|
341 for(i=0; i<lvl->num_cells[0]; i++) {
|
nuclear@0
|
342 int cy = i;
|
nuclear@0
|
343 for(j=0; j<lvl->num_cells[1]; j++) {
|
nuclear@0
|
344 float x, y;
|
nuclear@0
|
345 int cx = j;
|
nuclear@0
|
346 int c = level_cell(lvl, cx, cy);
|
nuclear@0
|
347
|
nuclear@0
|
348 level_cell_to_pos(lvl, cx, cy, &x, &y);
|
nuclear@0
|
349
|
nuclear@0
|
350 switch(c) {
|
nuclear@0
|
351 case C_PILLAR:
|
nuclear@0
|
352 break;
|
nuclear@0
|
353 }
|
nuclear@0
|
354 }
|
nuclear@0
|
355 }
|
nuclear@0
|
356 */
|
nuclear@0
|
357
|
nuclear@0
|
358 first = 0;
|
nuclear@0
|
359 }
|