labyrinth

view 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 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 int level_collide(struct level *lvl, float rad, float x, float y, float *dxp, float *dyp)
153 {
154 int i, val, cxy[2], collided = 0;
155 float pos[2], dir[2], center[2], len;
156 int adj_cxy[2];
158 pos[0] = x;
159 pos[1] = y;
160 dir[0] = *dxp;
161 dir[1] = *dyp;
163 /* clamp the direction magnitude (manhattan) to the cell size */
164 for(i=0; i<2; i++) {
165 if(dir[i] > lvl->cell_size) dir[i] = lvl->cell_size;
166 if(dir[i] < -lvl->cell_size) dir[i] = -lvl->cell_size;
167 }
169 level_pos_to_cell(lvl, pos[0], pos[1], cxy, cxy + 1);
170 level_cell_to_pos(lvl, cxy[0], cxy[1], center, center + 1);
172 /* check collision and clamp dx/dy with each of the 4 directions */
173 for(i=0; i<4; i++) {
174 float wall_pos, dist;
175 int axis = (i & 2) >> 1;
176 int sign = (i & 1) ? -1 : 1;
178 adj_cxy[0] = cxy[0];
179 adj_cxy[1] = cxy[1];
180 adj_cxy[axis] += sign;
182 val = level_cell(lvl, adj_cxy[0], adj_cxy[1]);
184 if(!IS_SOLID(val)) continue;
186 wall_pos = center[axis] + (lvl->cell_size * 0.5 - rad) * (float)sign;
187 dist = fabs(wall_pos - pos[axis]);
189 if(dir[axis] * (float)sign > dist) {
190 dir[axis] = (float)sign * dist;
191 collided = 1;
192 }
193 }
195 /* finally to make sure we don't slip through cracks in corners, also check
196 * the destination cell
197 */
198 len = sqrt(dir[0] * dir[0] + dir[1] * dir[1]);
199 pos[0] += dir[0] + dir[0] / len * rad;
200 pos[1] += dir[1] + dir[1] / len * rad;
202 val = level_cell_at(lvl, pos[0], pos[1]);
203 if(IS_SOLID(val)) {
204 dir[0] = dir[1] = 0;
205 }
208 *dxp = dir[0];
209 *dyp = dir[1];
210 return collided;
211 }
213 static void wall_faces(float x, float y, float width, float height, float uscale, float vscale)
214 {
215 float u0 = 0.5 - 0.5 * uscale;
216 float u1 = 0.5 + 0.5 * uscale;
217 float v0 = 0.5 - 0.5 * vscale;
218 float v1 = 0.5 + 0.5 * vscale;
220 width /= 2.0;
222 glNormal3f(0, 0, 1);
223 glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y + width);
224 glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y + width);
225 glTexCoord2f(u1, v0); glVertex3f(x + width, height, y + width);
226 glTexCoord2f(u0, v0); glVertex3f(x - width, height, y + width);
227 glNormal3f(0, 0, -1);
228 glTexCoord2f(u0, v0); glVertex3f(x - width, height, y - width);
229 glTexCoord2f(u1, v0); glVertex3f(x + width, height, y - width);
230 glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y - width);
231 glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y - width);
232 glNormal3f(1, 0, 0);
233 glTexCoord2f(u0, v1); glVertex3f(x + width, 0, y + width);
234 glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y - width);
235 glTexCoord2f(u1, v0); glVertex3f(x + width, height, y - width);
236 glTexCoord2f(u0, v0); glVertex3f(x + width, height, y + width);
237 glNormal3f(-1, 0, 0);
238 glTexCoord2f(u0, v0); glVertex3f(x - width, height, y + width);
239 glTexCoord2f(u1, v0); glVertex3f(x - width, height, y - width);
240 glTexCoord2f(u1, v1); glVertex3f(x - width, 0, y - width);
241 glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y + width);
242 }
244 void level_draw(struct level *lvl)
245 {
246 static int first = 1;
247 int i, j, k;
249 set_mtl_diffuse(1, 1, 1, 1);
250 set_mtl_specular(0, 0, 0);
252 glMatrixMode(GL_TEXTURE);
253 glLoadIdentity();
254 glScalef(lvl->wall_tex_scale, lvl->wall_tex_scale, lvl->wall_tex_scale);
256 glEnable(GL_TEXTURE_2D);
258 /* draw walls */
259 glBindTexture(GL_TEXTURE_2D, lvl->wall_tex);
260 glBegin(GL_QUADS);
261 for(i=0; i<lvl->num_cells[0]; i++) {
262 int cy = i;
263 for(j=0; j<lvl->num_cells[1]; j++) {
264 float x, y;
265 int cx = j;
266 int c = level_cell(lvl, cx, cy);
268 level_cell_to_pos(lvl, cx, cy, &x, &y);
270 if(c == C_WALL) {
271 wall_faces(x, y, lvl->cell_size, lvl->cell_height, 1, 1);
272 } else if(c == C_PILLAR) {
273 wall_faces(x, y, 0.2, lvl->cell_height, 0.04, 1);
274 }
276 if(first) putchar(c);
277 }
278 if(first) putchar('\n');
279 }
280 glEnd();
282 /* draw floor & ceiling */
283 for(k=0; k<2; k++) {
284 int do_floor = k == 0;
285 float height, normy;
287 if(do_floor) {
288 glBindTexture(GL_TEXTURE_2D, lvl->floor_tex);
289 glScalef(lvl->floor_tex_scale, lvl->floor_tex_scale, lvl->floor_tex_scale);
290 height = 0;
291 normy = 1;
292 glFrontFace(GL_CCW);
293 } else {
294 glBindTexture(GL_TEXTURE_2D, lvl->ceil_tex);
295 glScalef(lvl->ceil_tex_scale, lvl->ceil_tex_scale, lvl->ceil_tex_scale);
296 height = lvl->cell_height;
297 normy = -1;
298 glFrontFace(GL_CW);
299 }
301 glBegin(GL_TRIANGLES);
302 for(i=0; i<lvl->num_cells[0]; i++) {
303 int cy = i;
304 for(j=0; j<lvl->num_cells[1]; j++) {
305 float x, y;
306 int cx = j;
307 int c = level_cell(lvl, cx, cy);
309 level_cell_to_pos(lvl, cx, cy, &x, &y);
311 if(c != C_WALL) {
312 float hsz = lvl->cell_size / 2.0f;
314 glNormal3f(0, normy, 0);
315 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
316 glTexCoord2f(0.0, 0.0); glVertex3f(x - hsz, height, y + hsz);
317 glTexCoord2f(1.0, 0.0); glVertex3f(x + hsz, height, y + hsz);
318 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
319 glTexCoord2f(1.0, 0.0); glVertex3f(x + hsz, height, y + hsz);
320 glTexCoord2f(1.0, 1.0); glVertex3f(x + hsz, height, y - hsz);
321 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
322 glTexCoord2f(1.0, 1.0); glVertex3f(x + hsz, height, y - hsz);
323 glTexCoord2f(0.0, 1.0); glVertex3f(x - hsz, height, y - hsz);
324 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
325 glTexCoord2f(0.0, 1.0); glVertex3f(x - hsz, height, y - hsz);
326 glTexCoord2f(0.0, 0.0); glVertex3f(x - hsz, height, y + hsz);
327 }
328 }
329 }
330 glEnd();
331 }
333 glFrontFace(GL_CCW); /* restore front-face mode */
334 glDisable(GL_TEXTURE_2D);
336 glLoadIdentity(); /* restore the texture matrix to identity */
337 glMatrixMode(GL_MODELVIEW);
339 /* draw other objects
340 for(i=0; i<lvl->num_cells[0]; i++) {
341 int cy = i;
342 for(j=0; j<lvl->num_cells[1]; j++) {
343 float x, y;
344 int cx = j;
345 int c = level_cell(lvl, cx, cy);
347 level_cell_to_pos(lvl, cx, cy, &x, &y);
349 switch(c) {
350 case C_PILLAR:
351 break;
352 }
353 }
354 }
355 */
357 first = 0;
358 }