dungeon_crawler

annotate prototype/src/level.cc @ 26:21999ef6636b

lalala
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 04:07:51 +0300
parents 22562582c82d
children 862461b686f4
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@1 2 #include <string.h>
nuclear@5 3 #include <errno.h>
nuclear@1 4 #include "opengl.h"
nuclear@1 5 #include "level.h"
nuclear@1 6 #include "tile.h"
nuclear@5 7 #include "tileset.h"
nuclear@1 8
nuclear@1 9 Level::Level()
nuclear@1 10 {
nuclear@1 11 cell_size = 1.0;
nuclear@1 12
nuclear@1 13 cells = 0;
nuclear@1 14 xsz = ysz = 0;
nuclear@1 15 }
nuclear@1 16
nuclear@1 17 Level::~Level()
nuclear@1 18 {
nuclear@1 19 delete [] cells;
nuclear@1 20 }
nuclear@1 21
nuclear@1 22 bool Level::load(const char *fname)
nuclear@1 23 {
nuclear@5 24 if(!fname) {
nuclear@5 25 return false;
nuclear@5 26 }
nuclear@5 27
nuclear@5 28 TileSet *tileset = get_active_tileset();
nuclear@5 29 if(!tileset) {
nuclear@5 30 fprintf(stderr, "level loading failed: no active tileset\n");
nuclear@5 31 return false;
nuclear@5 32 }
nuclear@5 33 Tile *deftile = tileset->get_tile("default");
nuclear@5 34 if(!deftile) {
nuclear@5 35 fprintf(stderr, "level loading failed: active tileset has no default tile\n");
nuclear@5 36 return false;
nuclear@5 37 }
nuclear@5 38
nuclear@5 39 FILE *fp = fopen(fname, "r");
nuclear@5 40 if(!fp) {
nuclear@5 41 fprintf(stderr, "failed to open level: %s: %s\n", fname, strerror(errno));
nuclear@5 42 return false;
nuclear@5 43 }
nuclear@5 44
nuclear@5 45 char buf[512];
nuclear@5 46 fgets(buf, sizeof buf, fp);
nuclear@5 47 if(sscanf(buf, "SIZE %dx%d", &xsz, &ysz) != 2) {
nuclear@5 48 fprintf(stderr, "invalid or corrupt level file: %s\n", fname);
nuclear@5 49 fclose(fp);
nuclear@5 50 return false;
nuclear@5 51 }
nuclear@5 52 printf("level size: %dx%d\n", xsz, ysz);
nuclear@1 53
nuclear@1 54 cells = new GridCell*[xsz * ysz];
nuclear@1 55 memset(cells, 0, xsz * ysz * sizeof *cells);
nuclear@1 56
nuclear@5 57 int y = 0;
nuclear@5 58 GridCell **grid_row = cells;
nuclear@5 59
nuclear@5 60 while(fgets(buf, sizeof buf, fp)) {
nuclear@5 61 for(int i=0; i<xsz; i++) {
nuclear@5 62 if(!buf[i] || buf[i] == '\r' || buf[i] == '\n') {
nuclear@5 63 grid_row += xsz - i;
nuclear@5 64 break;
nuclear@5 65 }
nuclear@5 66
nuclear@5 67 if(isalpha(buf[i]) || buf[i] == ' ') {
nuclear@5 68 *grid_row = new GridCell(deftile);
nuclear@5 69 }
nuclear@5 70 grid_row++;
nuclear@5 71 }
nuclear@5 72
nuclear@5 73 if(++y >= ysz) {
nuclear@5 74 break;
nuclear@5 75 }
nuclear@5 76 }
nuclear@5 77 fclose(fp);
nuclear@1 78
nuclear@1 79 return true;
nuclear@1 80 }
nuclear@1 81
nuclear@1 82 bool Level::save(const char *fname) const
nuclear@1 83 {
nuclear@1 84 return false;
nuclear@1 85 }
nuclear@1 86
nuclear@1 87 const GridCell *Level::get_cell(int x, int y) const
nuclear@1 88 {
nuclear@1 89 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
nuclear@1 90 return 0;
nuclear@1 91 }
nuclear@1 92 return cells[y * xsz + x];
nuclear@1 93 }
nuclear@1 94
nuclear@1 95 Vector3 Level::get_cell_pos(int x, int y) const
nuclear@1 96 {
nuclear@1 97 float posx = (x - xsz / 2) * cell_size;
nuclear@1 98 float posy = (y - ysz / 2) * cell_size;
nuclear@1 99 return Vector3(posx, 0, posy);
nuclear@1 100 }
nuclear@1 101
nuclear@23 102 unsigned int Level::get_cell_dirmask(int x, int y) const
nuclear@23 103 {
nuclear@23 104 unsigned int dmask = TILE_ALL;
nuclear@23 105 if(y > 0 && get_cell(x, y - 1)) {
nuclear@23 106 dmask &= ~TILE_NORTH;
nuclear@23 107 }
nuclear@23 108 if(y < ysz - 1 && get_cell(x, y + 1)) {
nuclear@23 109 dmask &= ~TILE_SOUTH;
nuclear@23 110 }
nuclear@23 111 if(x > 0 && get_cell(x - 1, y)) {
nuclear@23 112 dmask &= ~TILE_WEST;
nuclear@23 113 }
nuclear@23 114 if(x < xsz - 1 && get_cell(x + 1, y)) {
nuclear@23 115 dmask &= ~TILE_EAST;
nuclear@23 116 }
nuclear@23 117 return dmask;
nuclear@23 118 }
nuclear@23 119
nuclear@1 120 void Level::draw() const
nuclear@1 121 {
nuclear@1 122 glMatrixMode(GL_MODELVIEW);
nuclear@1 123
nuclear@10 124 //draw_grid();
nuclear@1 125
nuclear@1 126 for(int i=0; i<ysz; i++) {
nuclear@1 127 for(int j=0; j<xsz; j++) {
nuclear@1 128 const GridCell *cell = get_cell(j, i);
nuclear@1 129 if(cell) {
nuclear@1 130 Vector3 pos = get_cell_pos(j, i);
nuclear@1 131 glPushMatrix();
nuclear@1 132 glTranslatef(pos.x, pos.y, pos.z);
nuclear@1 133 glScalef(cell_size, cell_size, cell_size);
nuclear@5 134
nuclear@23 135 unsigned int dmask = get_cell_dirmask(j, i);
nuclear@5 136
nuclear@5 137 cell->draw(dmask);
nuclear@1 138 glPopMatrix();
nuclear@1 139 }
nuclear@1 140 }
nuclear@1 141 }
nuclear@1 142 }
nuclear@1 143
nuclear@23 144 void Level::draw_lights() const
nuclear@23 145 {
nuclear@23 146 glMatrixMode(GL_MODELVIEW);
nuclear@23 147
nuclear@23 148 for(int i=0; i<ysz; i++) {
nuclear@23 149 for(int j=0; j<xsz; j++) {
nuclear@23 150 const GridCell *cell = get_cell(j, i);
nuclear@23 151 if(cell) {
nuclear@23 152 Vector3 pos = get_cell_pos(j, i);
nuclear@23 153
nuclear@23 154 glPushMatrix();
nuclear@23 155 glTranslatef(pos.x, pos.y, pos.z);
nuclear@23 156 glScalef(cell_size, cell_size, cell_size);
nuclear@23 157
nuclear@23 158 unsigned int dmask = get_cell_dirmask(j, i);
nuclear@23 159 cell->draw_lights(dmask);
nuclear@23 160
nuclear@23 161 glPopMatrix();
nuclear@23 162 }
nuclear@23 163 }
nuclear@23 164 }
nuclear@23 165 }
nuclear@23 166
nuclear@1 167 void Level::draw_grid() const
nuclear@1 168 {
nuclear@1 169 float xlen = xsz * cell_size;
nuclear@1 170 float ylen = ysz * cell_size;
nuclear@1 171
nuclear@1 172 glPushAttrib(GL_ENABLE_BIT);
nuclear@1 173 glDisable(GL_LIGHTING);
nuclear@1 174
nuclear@1 175 glBegin(GL_LINES);
nuclear@1 176 glColor3f(0.4, 0.4, 0.4);
nuclear@1 177
nuclear@1 178 float y = -ylen / 2.0 - cell_size / 2.0;
nuclear@1 179 for(int i=0; i<ysz; i++) {
nuclear@1 180 glVertex3f(-xlen / 2.0, 0, y);
nuclear@1 181 glVertex3f(xlen / 2.0, 0, y);
nuclear@1 182 y += cell_size;
nuclear@1 183 }
nuclear@1 184
nuclear@1 185 float x = -xlen / 2.0 - cell_size / 2.0;
nuclear@1 186 for(int i=0; i<xsz; i++) {
nuclear@1 187 glVertex3f(x, 0, -ylen / 2.0);
nuclear@1 188 glVertex3f(x, 0, ylen / 2.0);
nuclear@1 189 x += cell_size;
nuclear@1 190 }
nuclear@1 191 glEnd();
nuclear@1 192
nuclear@1 193 glPopAttrib();
nuclear@1 194 }
nuclear@1 195
nuclear@5 196
nuclear@5 197 GridCell::GridCell(const Tile *tile)
nuclear@5 198 {
nuclear@5 199 if(tile) {
nuclear@5 200 tiles.push_back(tile);
nuclear@5 201 }
nuclear@5 202 }
nuclear@5 203
nuclear@1 204 void GridCell::add_tile(const Tile *tile)
nuclear@1 205 {
nuclear@1 206 tiles.push_back(tile);
nuclear@1 207 }
nuclear@1 208
nuclear@5 209 void GridCell::draw(unsigned int draw_mask) const
nuclear@1 210 {
nuclear@23 211 for(auto tile : tiles) {
nuclear@23 212 tile->draw(draw_mask);
nuclear@1 213 }
nuclear@1 214 }
nuclear@23 215
nuclear@23 216 void GridCell::draw_lights(unsigned int draw_mask) const
nuclear@23 217 {
nuclear@23 218 for(auto tile : tiles) {
nuclear@23 219 tile->draw_lights(draw_mask);
nuclear@23 220 }
nuclear@23 221 }