labyrinth
diff src/level.c @ 0:8ba79034e8a6
labyrinth example initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 15 Jan 2015 14:59:38 +0200 |
parents | |
children | d46f0947a96d |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/level.c Thu Jan 15 14:59:38 2015 +0200 1.3 @@ -0,0 +1,358 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <ctype.h> 1.8 +#include <math.h> 1.9 +#include "opengl.h" 1.10 +#include "level.h" 1.11 + 1.12 +#define C_WALL '#' 1.13 +#define C_PILLAR 'o' 1.14 +#define C_START 's' 1.15 +#define C_GOLD 'x' 1.16 + 1.17 +#define IS_SOLID(x) ((x) == C_WALL) 1.18 + 1.19 + 1.20 +void level_init(struct level *lvl) 1.21 +{ 1.22 + memset(lvl, 0, sizeof *lvl); 1.23 + lvl->cell_size = 3.0; 1.24 + lvl->cell_height = 2.5; 1.25 + lvl->floor_tex_scale = lvl->wall_tex_scale = lvl->ceil_tex_scale = 1.0; 1.26 +} 1.27 + 1.28 +static void clean_line(char *buf) 1.29 +{ 1.30 + char *end = buf + strlen(buf) - 1; 1.31 + 1.32 + if(end <= buf) return; 1.33 + 1.34 + while(end >= buf && !isprint(*end)) { 1.35 + *end-- = 0; 1.36 + } 1.37 +} 1.38 + 1.39 +int level_load(struct level *lvl, const char *fname) 1.40 +{ 1.41 + FILE *fp; 1.42 + char buf[256]; 1.43 + int i, size[2], nlines; 1.44 + 1.45 + if(!(fp = fopen(fname, "r"))) { 1.46 + fprintf(stderr, "failed to open file: %s\n", fname); 1.47 + return -1; 1.48 + } 1.49 + 1.50 + if(!fgets(buf, sizeof buf, fp)) { 1.51 + fprintf(stderr, "level file %s is empty\n", fname); 1.52 + fclose(fp); 1.53 + return -1; 1.54 + } 1.55 + if(sscanf(buf, "s %dx%d", size, size + 1) != 2) { 1.56 + fprintf(stderr, "level file %s doesn't start with size definition\n", fname); 1.57 + fclose(fp); 1.58 + return -1; 1.59 + } 1.60 + if(size[0] > MAX_LEVEL_SIZE || size[1] > MAX_LEVEL_SIZE) { 1.61 + fprintf(stderr, "level size %dx%d is larger than compile-time maximum (%d)\n", size[0], size[1], MAX_LEVEL_SIZE); 1.62 + fclose(fp); 1.63 + return -1; 1.64 + } 1.65 + 1.66 + lvl->num_cells[0] = size[0]; 1.67 + lvl->num_cells[1] = size[1]; 1.68 + 1.69 + nlines = 0; 1.70 + while(fgets(buf, sizeof buf, fp)) { 1.71 + if(nlines >= size[0]) { 1.72 + fprintf(stderr, "warning: level contains more lines than specified, ignoring the rest\n"); 1.73 + break; 1.74 + } 1.75 + clean_line(buf); 1.76 + 1.77 + for(i=0; buf[i]; i++) { 1.78 + if(i >= size[1]) { 1.79 + fprintf(stderr, "warning: line %d is longer than the level size definition says. Skipping the rest.\n", nlines + 1); 1.80 + break; 1.81 + } 1.82 + lvl->cells[nlines][i] = buf[i]; 1.83 + 1.84 + if(buf[i] == C_START) { 1.85 + lvl->start_pos[0] = i; 1.86 + lvl->start_pos[1] = nlines; 1.87 + printf("start cell found (%d,%d)\n", lvl->start_pos[0], lvl->start_pos[1]); 1.88 + } 1.89 + if(buf[i] == C_GOLD) { 1.90 + level_cell_to_pos(lvl, i, nlines, lvl->goal_pos, lvl->goal_pos + 1); 1.91 + printf("gold cell found (%d, %d)\n", i, nlines); 1.92 + } 1.93 + } 1.94 + nlines++; 1.95 + } 1.96 + 1.97 + fclose(fp); 1.98 + return 0; 1.99 +} 1.100 + 1.101 + 1.102 +static int clamp(int x, int low, int high) 1.103 +{ 1.104 + return x < low ? low : (x > high ? high : x); 1.105 +} 1.106 + 1.107 +void level_pos_to_cell(struct level *lvl, float x, float y, int *res_cx, int *res_cy) 1.108 +{ 1.109 + int cx = (int)(x / lvl->cell_size + 0.5); 1.110 + int cy = (int)(y / lvl->cell_size + 0.5); 1.111 + 1.112 + *res_cx = clamp(cx, 0, lvl->num_cells[1] - 1); 1.113 + *res_cy = clamp(cy, 0, lvl->num_cells[0] - 1); 1.114 +} 1.115 + 1.116 +void level_cell_to_pos(struct level *lvl, int cx, int cy, float *resx, float *resy) 1.117 +{ 1.118 + cx = clamp(cx, 0, lvl->num_cells[1] - 1); 1.119 + cy = clamp(cy, 0, lvl->num_cells[0] - 1); 1.120 + 1.121 + *resx = (float)cx * lvl->cell_size; 1.122 + *resy = (float)cy * lvl->cell_size; 1.123 +} 1.124 + 1.125 +int level_cell(struct level *lvl, int cx, int cy) 1.126 +{ 1.127 + cx = clamp(cx, 0, lvl->num_cells[1] - 1); 1.128 + cy = clamp(cy, 0, lvl->num_cells[0] - 1); 1.129 + 1.130 + return lvl->cells[cy][cx]; 1.131 +} 1.132 + 1.133 +int level_cell_at(struct level *lvl, float x, float y) 1.134 +{ 1.135 + int cx, cy; 1.136 + level_pos_to_cell(lvl, x, y, &cx, &cy); 1.137 + return level_cell(lvl, cx, cy); 1.138 +} 1.139 + 1.140 +int level_obj_pos(struct level *lvl, int objname, float *resx, float *resy) 1.141 +{ 1.142 + int i, j; 1.143 + 1.144 + for(i=0; i<lvl->num_cells[0]; i++) { 1.145 + for(j=0; j<lvl->num_cells[1]; j++) { 1.146 + if(lvl->cells[i][j] == objname) { 1.147 + level_cell_to_pos(lvl, j, i, resx, resy); 1.148 + return 1; 1.149 + } 1.150 + } 1.151 + } 1.152 + return 0; 1.153 +} 1.154 + 1.155 +int level_collide(struct level *lvl, float rad, float x, float y, float *dxp, float *dyp) 1.156 +{ 1.157 + int i, val, cxy[2], collided = 0; 1.158 + float pos[2], dir[2], center[2], len; 1.159 + int adj_cxy[2]; 1.160 + 1.161 + pos[0] = x; 1.162 + pos[1] = y; 1.163 + dir[0] = *dxp; 1.164 + dir[1] = *dyp; 1.165 + 1.166 + /* clamp the direction magnitude (manhattan) to the cell size */ 1.167 + for(i=0; i<2; i++) { 1.168 + if(dir[i] > lvl->cell_size) dir[i] = lvl->cell_size; 1.169 + if(dir[i] < -lvl->cell_size) dir[i] = -lvl->cell_size; 1.170 + } 1.171 + 1.172 + level_pos_to_cell(lvl, pos[0], pos[1], cxy, cxy + 1); 1.173 + level_cell_to_pos(lvl, cxy[0], cxy[1], center, center + 1); 1.174 + 1.175 + /* check collision and clamp dx/dy with each of the 4 directions */ 1.176 + for(i=0; i<4; i++) { 1.177 + float wall_pos, dist; 1.178 + int axis = (i & 2) >> 1; 1.179 + int sign = (i & 1) ? -1 : 1; 1.180 + 1.181 + adj_cxy[0] = cxy[0]; 1.182 + adj_cxy[1] = cxy[1]; 1.183 + adj_cxy[axis] += sign; 1.184 + 1.185 + val = level_cell(lvl, adj_cxy[0], adj_cxy[1]); 1.186 + 1.187 + if(!IS_SOLID(val)) continue; 1.188 + 1.189 + wall_pos = center[axis] + (lvl->cell_size * 0.5 - rad) * (float)sign; 1.190 + dist = fabs(wall_pos - pos[axis]); 1.191 + 1.192 + if(dir[axis] * (float)sign > dist) { 1.193 + dir[axis] = (float)sign * dist; 1.194 + collided = 1; 1.195 + } 1.196 + } 1.197 + 1.198 + /* finally to make sure we don't slip through cracks in corners, also check 1.199 + * the destination cell 1.200 + */ 1.201 + len = sqrt(dir[0] * dir[0] + dir[1] * dir[1]); 1.202 + pos[0] += dir[0] + dir[0] / len * rad; 1.203 + pos[1] += dir[1] + dir[1] / len * rad; 1.204 + 1.205 + val = level_cell_at(lvl, pos[0], pos[1]); 1.206 + if(IS_SOLID(val)) { 1.207 + dir[0] = dir[1] = 0; 1.208 + } 1.209 + 1.210 + 1.211 + *dxp = dir[0]; 1.212 + *dyp = dir[1]; 1.213 + return collided; 1.214 +} 1.215 + 1.216 +static void wall_faces(float x, float y, float width, float height, float uscale, float vscale) 1.217 +{ 1.218 + float u0 = 0.5 - 0.5 * uscale; 1.219 + float u1 = 0.5 + 0.5 * uscale; 1.220 + float v0 = 0.5 - 0.5 * vscale; 1.221 + float v1 = 0.5 + 0.5 * vscale; 1.222 + 1.223 + width /= 2.0; 1.224 + 1.225 + glNormal3f(0, 0, 1); 1.226 + glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y + width); 1.227 + glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y + width); 1.228 + glTexCoord2f(u1, v0); glVertex3f(x + width, height, y + width); 1.229 + glTexCoord2f(u0, v0); glVertex3f(x - width, height, y + width); 1.230 + glNormal3f(0, 0, -1); 1.231 + glTexCoord2f(u0, v0); glVertex3f(x - width, height, y - width); 1.232 + glTexCoord2f(u1, v0); glVertex3f(x + width, height, y - width); 1.233 + glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y - width); 1.234 + glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y - width); 1.235 + glNormal3f(1, 0, 0); 1.236 + glTexCoord2f(u0, v1); glVertex3f(x + width, 0, y + width); 1.237 + glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y - width); 1.238 + glTexCoord2f(u1, v0); glVertex3f(x + width, height, y - width); 1.239 + glTexCoord2f(u0, v0); glVertex3f(x + width, height, y + width); 1.240 + glNormal3f(-1, 0, 0); 1.241 + glTexCoord2f(u0, v0); glVertex3f(x - width, height, y + width); 1.242 + glTexCoord2f(u1, v0); glVertex3f(x - width, height, y - width); 1.243 + glTexCoord2f(u1, v1); glVertex3f(x - width, 0, y - width); 1.244 + glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y + width); 1.245 +} 1.246 + 1.247 +void level_draw(struct level *lvl) 1.248 +{ 1.249 + static int first = 1; 1.250 + int i, j, k; 1.251 + 1.252 + set_mtl_diffuse(1, 1, 1, 1); 1.253 + set_mtl_specular(0, 0, 0); 1.254 + 1.255 + glMatrixMode(GL_TEXTURE); 1.256 + glLoadIdentity(); 1.257 + glScalef(lvl->wall_tex_scale, lvl->wall_tex_scale, lvl->wall_tex_scale); 1.258 + 1.259 + glEnable(GL_TEXTURE_2D); 1.260 + 1.261 + /* draw walls */ 1.262 + glBindTexture(GL_TEXTURE_2D, lvl->wall_tex); 1.263 + glBegin(GL_QUADS); 1.264 + for(i=0; i<lvl->num_cells[0]; i++) { 1.265 + int cy = i; 1.266 + for(j=0; j<lvl->num_cells[1]; j++) { 1.267 + float x, y; 1.268 + int cx = j; 1.269 + int c = level_cell(lvl, cx, cy); 1.270 + 1.271 + level_cell_to_pos(lvl, cx, cy, &x, &y); 1.272 + 1.273 + if(c == C_WALL) { 1.274 + wall_faces(x, y, lvl->cell_size, lvl->cell_height, 1, 1); 1.275 + } else if(c == C_PILLAR) { 1.276 + wall_faces(x, y, 0.2, lvl->cell_height, 0.04, 1); 1.277 + } 1.278 + 1.279 + if(first) putchar(c); 1.280 + } 1.281 + if(first) putchar('\n'); 1.282 + } 1.283 + glEnd(); 1.284 + 1.285 + /* draw floor & ceiling */ 1.286 + for(k=0; k<2; k++) { 1.287 + int do_floor = k == 0; 1.288 + float height, normy; 1.289 + 1.290 + if(do_floor) { 1.291 + glBindTexture(GL_TEXTURE_2D, lvl->floor_tex); 1.292 + glScalef(lvl->floor_tex_scale, lvl->floor_tex_scale, lvl->floor_tex_scale); 1.293 + height = 0; 1.294 + normy = 1; 1.295 + glFrontFace(GL_CCW); 1.296 + } else { 1.297 + glBindTexture(GL_TEXTURE_2D, lvl->ceil_tex); 1.298 + glScalef(lvl->ceil_tex_scale, lvl->ceil_tex_scale, lvl->ceil_tex_scale); 1.299 + height = lvl->cell_height; 1.300 + normy = -1; 1.301 + glFrontFace(GL_CW); 1.302 + } 1.303 + 1.304 + glBegin(GL_TRIANGLES); 1.305 + for(i=0; i<lvl->num_cells[0]; i++) { 1.306 + int cy = i; 1.307 + for(j=0; j<lvl->num_cells[1]; j++) { 1.308 + float x, y; 1.309 + int cx = j; 1.310 + int c = level_cell(lvl, cx, cy); 1.311 + 1.312 + level_cell_to_pos(lvl, cx, cy, &x, &y); 1.313 + 1.314 + if(c != C_WALL) { 1.315 + float hsz = lvl->cell_size / 2.0f; 1.316 + 1.317 + glNormal3f(0, normy, 0); 1.318 + glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y); 1.319 + glTexCoord2f(0.0, 0.0); glVertex3f(x - hsz, height, y + hsz); 1.320 + glTexCoord2f(1.0, 0.0); glVertex3f(x + hsz, height, y + hsz); 1.321 + glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y); 1.322 + glTexCoord2f(1.0, 0.0); glVertex3f(x + hsz, height, y + hsz); 1.323 + glTexCoord2f(1.0, 1.0); glVertex3f(x + hsz, height, y - hsz); 1.324 + glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y); 1.325 + glTexCoord2f(1.0, 1.0); glVertex3f(x + hsz, height, y - hsz); 1.326 + glTexCoord2f(0.0, 1.0); glVertex3f(x - hsz, height, y - hsz); 1.327 + glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y); 1.328 + glTexCoord2f(0.0, 1.0); glVertex3f(x - hsz, height, y - hsz); 1.329 + glTexCoord2f(0.0, 0.0); glVertex3f(x - hsz, height, y + hsz); 1.330 + } 1.331 + } 1.332 + } 1.333 + glEnd(); 1.334 + } 1.335 + 1.336 + glFrontFace(GL_CCW); /* restore front-face mode */ 1.337 + glDisable(GL_TEXTURE_2D); 1.338 + 1.339 + glLoadIdentity(); /* restore the texture matrix to identity */ 1.340 + glMatrixMode(GL_MODELVIEW); 1.341 + 1.342 + /* draw other objects 1.343 + for(i=0; i<lvl->num_cells[0]; i++) { 1.344 + int cy = i; 1.345 + for(j=0; j<lvl->num_cells[1]; j++) { 1.346 + float x, y; 1.347 + int cx = j; 1.348 + int c = level_cell(lvl, cx, cy); 1.349 + 1.350 + level_cell_to_pos(lvl, cx, cy, &x, &y); 1.351 + 1.352 + switch(c) { 1.353 + case C_PILLAR: 1.354 + break; 1.355 + } 1.356 + } 1.357 + } 1.358 + */ 1.359 + 1.360 + first = 0; 1.361 +}