dungeon_crawler

annotate prototype/src/level.cc @ 45:dfd3a413ef9e

particle system 1
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 12 Sep 2012 06:04:20 +0300
parents 862461b686f4
children f3030df27110
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@38 20
nuclear@38 21 for(auto cell : cell_list) {
nuclear@38 22 delete cell;
nuclear@38 23 }
nuclear@1 24 }
nuclear@1 25
nuclear@1 26 bool Level::load(const char *fname)
nuclear@1 27 {
nuclear@5 28 if(!fname) {
nuclear@5 29 return false;
nuclear@5 30 }
nuclear@5 31
nuclear@5 32 TileSet *tileset = get_active_tileset();
nuclear@5 33 if(!tileset) {
nuclear@5 34 fprintf(stderr, "level loading failed: no active tileset\n");
nuclear@5 35 return false;
nuclear@5 36 }
nuclear@5 37 Tile *deftile = tileset->get_tile("default");
nuclear@5 38 if(!deftile) {
nuclear@5 39 fprintf(stderr, "level loading failed: active tileset has no default tile\n");
nuclear@5 40 return false;
nuclear@5 41 }
nuclear@5 42
nuclear@5 43 FILE *fp = fopen(fname, "r");
nuclear@5 44 if(!fp) {
nuclear@5 45 fprintf(stderr, "failed to open level: %s: %s\n", fname, strerror(errno));
nuclear@5 46 return false;
nuclear@5 47 }
nuclear@5 48
nuclear@5 49 char buf[512];
nuclear@5 50 fgets(buf, sizeof buf, fp);
nuclear@5 51 if(sscanf(buf, "SIZE %dx%d", &xsz, &ysz) != 2) {
nuclear@5 52 fprintf(stderr, "invalid or corrupt level file: %s\n", fname);
nuclear@5 53 fclose(fp);
nuclear@5 54 return false;
nuclear@5 55 }
nuclear@5 56 printf("level size: %dx%d\n", xsz, ysz);
nuclear@1 57
nuclear@1 58 cells = new GridCell*[xsz * ysz];
nuclear@1 59 memset(cells, 0, xsz * ysz * sizeof *cells);
nuclear@1 60
nuclear@5 61 int y = 0;
nuclear@5 62 GridCell **grid_row = cells;
nuclear@5 63
nuclear@5 64 while(fgets(buf, sizeof buf, fp)) {
nuclear@5 65 for(int i=0; i<xsz; i++) {
nuclear@5 66 if(!buf[i] || buf[i] == '\r' || buf[i] == '\n') {
nuclear@5 67 grid_row += xsz - i;
nuclear@5 68 break;
nuclear@5 69 }
nuclear@5 70
nuclear@5 71 if(isalpha(buf[i]) || buf[i] == ' ') {
nuclear@38 72 GridCell *cell = new GridCell(deftile);
nuclear@38 73 *grid_row = cell;
nuclear@38 74 cell_list.push_back(*grid_row);
nuclear@5 75 }
nuclear@5 76 grid_row++;
nuclear@5 77 }
nuclear@5 78
nuclear@5 79 if(++y >= ysz) {
nuclear@5 80 break;
nuclear@5 81 }
nuclear@5 82 }
nuclear@5 83 fclose(fp);
nuclear@1 84
nuclear@1 85 return true;
nuclear@1 86 }
nuclear@1 87
nuclear@1 88 bool Level::save(const char *fname) const
nuclear@1 89 {
nuclear@1 90 return false;
nuclear@1 91 }
nuclear@1 92
nuclear@1 93 const GridCell *Level::get_cell(int x, int y) const
nuclear@1 94 {
nuclear@1 95 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
nuclear@1 96 return 0;
nuclear@1 97 }
nuclear@1 98 return cells[y * xsz + x];
nuclear@1 99 }
nuclear@1 100
nuclear@1 101 Vector3 Level::get_cell_pos(int x, int y) const
nuclear@1 102 {
nuclear@1 103 float posx = (x - xsz / 2) * cell_size;
nuclear@1 104 float posy = (y - ysz / 2) * cell_size;
nuclear@1 105 return Vector3(posx, 0, posy);
nuclear@1 106 }
nuclear@1 107
nuclear@23 108 unsigned int Level::get_cell_dirmask(int x, int y) const
nuclear@23 109 {
nuclear@23 110 unsigned int dmask = TILE_ALL;
nuclear@23 111 if(y > 0 && get_cell(x, y - 1)) {
nuclear@23 112 dmask &= ~TILE_NORTH;
nuclear@23 113 }
nuclear@23 114 if(y < ysz - 1 && get_cell(x, y + 1)) {
nuclear@23 115 dmask &= ~TILE_SOUTH;
nuclear@23 116 }
nuclear@23 117 if(x > 0 && get_cell(x - 1, y)) {
nuclear@23 118 dmask &= ~TILE_WEST;
nuclear@23 119 }
nuclear@23 120 if(x < xsz - 1 && get_cell(x + 1, y)) {
nuclear@23 121 dmask &= ~TILE_EAST;
nuclear@23 122 }
nuclear@23 123 return dmask;
nuclear@23 124 }
nuclear@23 125
nuclear@38 126 void Level::update(unsigned long msec, float dt)
nuclear@38 127 {
nuclear@38 128 for(auto cell : cell_list) {
nuclear@38 129 cell->update(msec, dt);
nuclear@38 130 }
nuclear@38 131 }
nuclear@38 132
nuclear@1 133 void Level::draw() const
nuclear@1 134 {
nuclear@1 135 glMatrixMode(GL_MODELVIEW);
nuclear@1 136
nuclear@1 137 for(int i=0; i<ysz; i++) {
nuclear@1 138 for(int j=0; j<xsz; j++) {
nuclear@1 139 const GridCell *cell = get_cell(j, i);
nuclear@1 140 if(cell) {
nuclear@1 141 Vector3 pos = get_cell_pos(j, i);
nuclear@1 142 glPushMatrix();
nuclear@1 143 glTranslatef(pos.x, pos.y, pos.z);
nuclear@1 144 glScalef(cell_size, cell_size, cell_size);
nuclear@5 145
nuclear@23 146 unsigned int dmask = get_cell_dirmask(j, i);
nuclear@5 147
nuclear@5 148 cell->draw(dmask);
nuclear@1 149 glPopMatrix();
nuclear@1 150 }
nuclear@1 151 }
nuclear@1 152 }
nuclear@1 153 }
nuclear@1 154
nuclear@23 155 void Level::draw_lights() const
nuclear@23 156 {
nuclear@23 157 glMatrixMode(GL_MODELVIEW);
nuclear@23 158
nuclear@23 159 for(int i=0; i<ysz; i++) {
nuclear@23 160 for(int j=0; j<xsz; j++) {
nuclear@23 161 const GridCell *cell = get_cell(j, i);
nuclear@23 162 if(cell) {
nuclear@23 163 Vector3 pos = get_cell_pos(j, i);
nuclear@23 164
nuclear@23 165 glPushMatrix();
nuclear@23 166 glTranslatef(pos.x, pos.y, pos.z);
nuclear@23 167 glScalef(cell_size, cell_size, cell_size);
nuclear@23 168
nuclear@23 169 unsigned int dmask = get_cell_dirmask(j, i);
nuclear@23 170 cell->draw_lights(dmask);
nuclear@23 171
nuclear@23 172 glPopMatrix();
nuclear@23 173 }
nuclear@23 174 }
nuclear@23 175 }
nuclear@23 176 }
nuclear@23 177
nuclear@1 178 void Level::draw_grid() const
nuclear@1 179 {
nuclear@1 180 float xlen = xsz * cell_size;
nuclear@1 181 float ylen = ysz * cell_size;
nuclear@1 182
nuclear@1 183 glPushAttrib(GL_ENABLE_BIT);
nuclear@1 184 glDisable(GL_LIGHTING);
nuclear@1 185
nuclear@1 186 glBegin(GL_LINES);
nuclear@1 187 glColor3f(0.4, 0.4, 0.4);
nuclear@1 188
nuclear@1 189 float y = -ylen / 2.0 - cell_size / 2.0;
nuclear@1 190 for(int i=0; i<ysz; i++) {
nuclear@1 191 glVertex3f(-xlen / 2.0, 0, y);
nuclear@1 192 glVertex3f(xlen / 2.0, 0, y);
nuclear@1 193 y += cell_size;
nuclear@1 194 }
nuclear@1 195
nuclear@1 196 float x = -xlen / 2.0 - cell_size / 2.0;
nuclear@1 197 for(int i=0; i<xsz; i++) {
nuclear@1 198 glVertex3f(x, 0, -ylen / 2.0);
nuclear@1 199 glVertex3f(x, 0, ylen / 2.0);
nuclear@1 200 x += cell_size;
nuclear@1 201 }
nuclear@1 202 glEnd();
nuclear@1 203
nuclear@1 204 glPopAttrib();
nuclear@1 205 }
nuclear@1 206
nuclear@5 207
nuclear@38 208 GridCell::GridCell(Tile *tile)
nuclear@5 209 {
nuclear@5 210 if(tile) {
nuclear@45 211 add_tile(tile);
nuclear@5 212 }
nuclear@5 213 }
nuclear@5 214
nuclear@38 215 void GridCell::add_tile(Tile *tile)
nuclear@1 216 {
nuclear@45 217 if(!tile) {
nuclear@45 218 return;
nuclear@45 219 }
nuclear@45 220
nuclear@1 221 tiles.push_back(tile);
nuclear@45 222
nuclear@45 223 /* instanciate any particle systems */
nuclear@45 224 const struct psys_attributes **psattr = tile->get_unique_psys();
nuclear@45 225 int num_psattr = tile->get_unique_psys_count();
nuclear@45 226
nuclear@45 227 for(int i=0; i<num_psattr; i++) {
nuclear@45 228 struct psys_emitter *emitter = psys_create();
nuclear@45 229 emitter->attr = *psattr[i];
nuclear@45 230 psys.push_back(emitter);
nuclear@45 231 }
nuclear@1 232 }
nuclear@1 233
nuclear@38 234 void GridCell::update(unsigned long msec, float dt)
nuclear@38 235 {
nuclear@38 236 for(auto tile : tiles) {
nuclear@38 237 tile->update(msec, dt);
nuclear@38 238 }
nuclear@45 239 for(auto ps : psys) {
nuclear@45 240 psys_update(ps, (float)msec / 1000.0f);
nuclear@45 241 }
nuclear@38 242 }
nuclear@38 243
nuclear@5 244 void GridCell::draw(unsigned int draw_mask) const
nuclear@1 245 {
nuclear@23 246 for(auto tile : tiles) {
nuclear@23 247 tile->draw(draw_mask);
nuclear@1 248 }
nuclear@1 249 }
nuclear@23 250
nuclear@23 251 void GridCell::draw_lights(unsigned int draw_mask) const
nuclear@23 252 {
nuclear@23 253 for(auto tile : tiles) {
nuclear@23 254 tile->draw_lights(draw_mask);
nuclear@23 255 }
nuclear@23 256 }
nuclear@45 257
nuclear@45 258 void GridCell::draw_post(unsigned int draw_mask) const
nuclear@45 259 {
nuclear@45 260 for(auto tile : tiles) {
nuclear@45 261 tile->draw_post(draw_mask);
nuclear@45 262 }
nuclear@45 263 for(auto ps : psys) {
nuclear@45 264 psys_draw(ps);
nuclear@45 265 }
nuclear@45 266 }