labyrinth

view src/level.c @ 1:d46f0947a96d

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