dungeon_crawler
diff prototype/src/level.cc @ 5:252a00508411
more stuff
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 12 Aug 2012 07:07:57 +0300 |
parents | 96de911d05d4 |
children | 8fb37db44fd8 |
line diff
1.1 --- a/prototype/src/level.cc Sat Aug 11 05:44:52 2012 +0300 1.2 +++ b/prototype/src/level.cc Sun Aug 12 07:07:57 2012 +0300 1.3 @@ -1,8 +1,10 @@ 1.4 #include <stdio.h> 1.5 #include <string.h> 1.6 +#include <errno.h> 1.7 #include "opengl.h" 1.8 #include "level.h" 1.9 #include "tile.h" 1.10 +#include "tileset.h" 1.11 1.12 Level::Level() 1.13 { 1.14 @@ -19,14 +21,60 @@ 1.15 1.16 bool Level::load(const char *fname) 1.17 { 1.18 - xsz = ysz = 64; 1.19 + if(!fname) { 1.20 + return false; 1.21 + } 1.22 + 1.23 + TileSet *tileset = get_active_tileset(); 1.24 + if(!tileset) { 1.25 + fprintf(stderr, "level loading failed: no active tileset\n"); 1.26 + return false; 1.27 + } 1.28 + Tile *deftile = tileset->get_tile("default"); 1.29 + if(!deftile) { 1.30 + fprintf(stderr, "level loading failed: active tileset has no default tile\n"); 1.31 + return false; 1.32 + } 1.33 + 1.34 + FILE *fp = fopen(fname, "r"); 1.35 + if(!fp) { 1.36 + fprintf(stderr, "failed to open level: %s: %s\n", fname, strerror(errno)); 1.37 + return false; 1.38 + } 1.39 + 1.40 + char buf[512]; 1.41 + fgets(buf, sizeof buf, fp); 1.42 + if(sscanf(buf, "SIZE %dx%d", &xsz, &ysz) != 2) { 1.43 + fprintf(stderr, "invalid or corrupt level file: %s\n", fname); 1.44 + fclose(fp); 1.45 + return false; 1.46 + } 1.47 + printf("level size: %dx%d\n", xsz, ysz); 1.48 1.49 cells = new GridCell*[xsz * ysz]; 1.50 memset(cells, 0, xsz * ysz * sizeof *cells); 1.51 1.52 - GridCell *g = new GridCell; 1.53 - g->add_tile(new Tile); 1.54 - cells[ysz / 2 * xsz + xsz / 2] = g; 1.55 + int y = 0; 1.56 + GridCell **grid_row = cells; 1.57 + 1.58 + while(fgets(buf, sizeof buf, fp)) { 1.59 + for(int i=0; i<xsz; i++) { 1.60 + if(!buf[i] || buf[i] == '\r' || buf[i] == '\n') { 1.61 + grid_row += xsz - i; 1.62 + break; 1.63 + } 1.64 + 1.65 + if(isalpha(buf[i]) || buf[i] == ' ') { 1.66 + *grid_row = new GridCell(deftile); 1.67 + } 1.68 + grid_row++; 1.69 + } 1.70 + 1.71 + if(++y >= ysz) { 1.72 + break; 1.73 + } 1.74 + } 1.75 + fclose(fp); 1.76 1.77 return true; 1.78 } 1.79 @@ -65,7 +113,22 @@ 1.80 glPushMatrix(); 1.81 glTranslatef(pos.x, pos.y, pos.z); 1.82 glScalef(cell_size, cell_size, cell_size); 1.83 - cell->draw(); 1.84 + 1.85 + unsigned int dmask = 0; 1.86 + if(i <= 0 || get_cell(j, i - 1) == 0) { 1.87 + dmask |= TILE_NORTH; 1.88 + } 1.89 + if(i > ysz || get_cell(j, i + 1) == 0) { 1.90 + dmask |= TILE_SOUTH; 1.91 + } 1.92 + if(j <= 0 || get_cell(j - 1, i) == 0) { 1.93 + dmask |= TILE_WEST; 1.94 + } 1.95 + if(j > xsz || get_cell(j + 1, i) == 0) { 1.96 + dmask |= TILE_EAST; 1.97 + } 1.98 + 1.99 + cell->draw(dmask); 1.100 glPopMatrix(); 1.101 } 1.102 } 1.103 @@ -101,14 +164,22 @@ 1.104 glPopAttrib(); 1.105 } 1.106 1.107 + 1.108 +GridCell::GridCell(const Tile *tile) 1.109 +{ 1.110 + if(tile) { 1.111 + tiles.push_back(tile); 1.112 + } 1.113 +} 1.114 + 1.115 void GridCell::add_tile(const Tile *tile) 1.116 { 1.117 tiles.push_back(tile); 1.118 } 1.119 1.120 -void GridCell::draw() const 1.121 +void GridCell::draw(unsigned int draw_mask) const 1.122 { 1.123 for(size_t i=0; i<tiles.size(); i++) { 1.124 - tiles[i]->draw(); 1.125 + tiles[i]->draw(draw_mask); 1.126 } 1.127 }