dungeon_crawler

changeset 5:252a00508411

more stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 12 Aug 2012 07:07:57 +0300
parents 158de53b4e18
children e8434c6b208d
files prototype/Makefile prototype/src/datapath.cc prototype/src/datapath.h prototype/src/level.cc prototype/src/level.h prototype/src/main.cc prototype/src/mesh.cc prototype/src/tile.cc prototype/src/tile.h prototype/src/tileset.cc prototype/src/tileset.h
diffstat 11 files changed, 271 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/prototype/Makefile	Sat Aug 11 05:44:52 2012 +0300
     1.2 +++ b/prototype/Makefile	Sun Aug 12 07:07:57 2012 +0300
     1.3 @@ -5,8 +5,8 @@
     1.4  bin = proto
     1.5  
     1.6  CFLAGS = -pedantic -Wall -g -Ivmath
     1.7 -CXXFLAGS = $(CFLAGS)
     1.8 -LDFLAGS = $(libgl) -lm
     1.9 +CXXFLAGS = $(CFLAGS) -std=c++11
    1.10 +LDFLAGS = $(libgl) -lm -lassimp
    1.11  
    1.12  ifeq ($(shell uname -s), Darwin)
    1.13  	libgl = -framework OpenGL -framework GLUT -lglew
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/prototype/src/datapath.cc	Sun Aug 12 07:07:57 2012 +0300
     2.3 @@ -0,0 +1,26 @@
     2.4 +#include <stdio.h>
     2.5 +#include <vector>
     2.6 +#include <string>
     2.7 +#include "datapath.h"
     2.8 +
     2.9 +static std::vector<std::string> pathlist;
    2.10 +
    2.11 +void add_data_path(const char *path)
    2.12 +{
    2.13 +	pathlist.push_back(path);
    2.14 +}
    2.15 +
    2.16 +const char *datafile_path(const char *fname)
    2.17 +{
    2.18 +	static std::string res;
    2.19 +
    2.20 +	for(auto path : pathlist) {
    2.21 +		res = path + "/" + std::string(fname);
    2.22 +		FILE *fp = fopen(res.c_str(), "r");
    2.23 +		if(fp) {
    2.24 +			fclose(fp);
    2.25 +			return res.c_str();
    2.26 +		}
    2.27 +	}
    2.28 +	return 0;
    2.29 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/prototype/src/datapath.h	Sun Aug 12 07:07:57 2012 +0300
     3.3 @@ -0,0 +1,8 @@
     3.4 +#ifndef DATAPATH_H_
     3.5 +#define DATAPATH_H_
     3.6 +
     3.7 +void add_data_path(const char *path);
     3.8 +
     3.9 +const char *datafile_path(const char *fname);
    3.10 +
    3.11 +#endif	// DATAPATH_H_
     4.1 --- a/prototype/src/level.cc	Sat Aug 11 05:44:52 2012 +0300
     4.2 +++ b/prototype/src/level.cc	Sun Aug 12 07:07:57 2012 +0300
     4.3 @@ -1,8 +1,10 @@
     4.4  #include <stdio.h>
     4.5  #include <string.h>
     4.6 +#include <errno.h>
     4.7  #include "opengl.h"
     4.8  #include "level.h"
     4.9  #include "tile.h"
    4.10 +#include "tileset.h"
    4.11  
    4.12  Level::Level()
    4.13  {
    4.14 @@ -19,14 +21,60 @@
    4.15  
    4.16  bool Level::load(const char *fname)
    4.17  {
    4.18 -	xsz = ysz = 64;
    4.19 +	if(!fname) {
    4.20 +		return false;
    4.21 +	}
    4.22 +
    4.23 +	TileSet *tileset = get_active_tileset();
    4.24 +	if(!tileset) {
    4.25 +		fprintf(stderr, "level loading failed: no active tileset\n");
    4.26 +		return false;
    4.27 +	}
    4.28 +	Tile *deftile = tileset->get_tile("default");
    4.29 +	if(!deftile) {
    4.30 +		fprintf(stderr, "level loading failed: active tileset has no default tile\n");
    4.31 +		return false;
    4.32 +	}
    4.33 +
    4.34 +	FILE *fp = fopen(fname, "r");
    4.35 +	if(!fp) {
    4.36 +		fprintf(stderr, "failed to open level: %s: %s\n", fname, strerror(errno));
    4.37 +		return false;
    4.38 +	}
    4.39 +
    4.40 +	char buf[512];
    4.41 +	fgets(buf, sizeof buf, fp);
    4.42 +	if(sscanf(buf, "SIZE %dx%d", &xsz, &ysz) != 2) {
    4.43 +		fprintf(stderr, "invalid or corrupt level file: %s\n", fname);
    4.44 +		fclose(fp);
    4.45 +		return false;
    4.46 +	}
    4.47 +	printf("level size: %dx%d\n", xsz, ysz);
    4.48  
    4.49  	cells = new GridCell*[xsz * ysz];
    4.50  	memset(cells, 0, xsz * ysz * sizeof *cells);
    4.51  
    4.52 -	GridCell *g = new GridCell;
    4.53 -	g->add_tile(new Tile);
    4.54 -	cells[ysz / 2 * xsz + xsz / 2] = g;
    4.55 +	int y = 0;
    4.56 +	GridCell **grid_row = cells;
    4.57 +
    4.58 +	while(fgets(buf, sizeof buf, fp)) {
    4.59 +		for(int i=0; i<xsz; i++) {
    4.60 +			if(!buf[i] || buf[i] == '\r' || buf[i] == '\n') {
    4.61 +				grid_row += xsz - i;
    4.62 +				break;
    4.63 +			}
    4.64 +
    4.65 +			if(isalpha(buf[i]) || buf[i] == ' ') {
    4.66 +				*grid_row = new GridCell(deftile);
    4.67 +			}
    4.68 +			grid_row++;
    4.69 +		}
    4.70 +
    4.71 +		if(++y >= ysz) {
    4.72 +			break;
    4.73 +		}
    4.74 +	}
    4.75 +	fclose(fp);
    4.76  
    4.77  	return true;
    4.78  }
    4.79 @@ -65,7 +113,22 @@
    4.80  				glPushMatrix();
    4.81  				glTranslatef(pos.x, pos.y, pos.z);
    4.82  				glScalef(cell_size, cell_size, cell_size);
    4.83 -				cell->draw();
    4.84 +
    4.85 +				unsigned int dmask = 0;
    4.86 +				if(i <= 0 || get_cell(j, i - 1) == 0) {
    4.87 +					dmask |= TILE_NORTH;
    4.88 +				}
    4.89 +				if(i > ysz || get_cell(j, i + 1) == 0) {
    4.90 +					dmask |= TILE_SOUTH;
    4.91 +				}
    4.92 +				if(j <= 0 || get_cell(j - 1, i) == 0) {
    4.93 +					dmask |= TILE_WEST;
    4.94 +				}
    4.95 +				if(j > xsz || get_cell(j + 1, i) == 0) {
    4.96 +					dmask |= TILE_EAST;
    4.97 +				}
    4.98 +
    4.99 +				cell->draw(dmask);
   4.100  				glPopMatrix();
   4.101  			}
   4.102  		}
   4.103 @@ -101,14 +164,22 @@
   4.104  	glPopAttrib();
   4.105  }
   4.106  
   4.107 +
   4.108 +GridCell::GridCell(const Tile *tile)
   4.109 +{
   4.110 +	if(tile) {
   4.111 +		tiles.push_back(tile);
   4.112 +	}
   4.113 +}
   4.114 +
   4.115  void GridCell::add_tile(const Tile *tile)
   4.116  {
   4.117  	tiles.push_back(tile);
   4.118  }
   4.119  
   4.120 -void GridCell::draw() const
   4.121 +void GridCell::draw(unsigned int draw_mask) const
   4.122  {
   4.123  	for(size_t i=0; i<tiles.size(); i++) {
   4.124 -		tiles[i]->draw();
   4.125 +		tiles[i]->draw(draw_mask);
   4.126  	}
   4.127  }
     5.1 --- a/prototype/src/level.h	Sat Aug 11 05:44:52 2012 +0300
     5.2 +++ b/prototype/src/level.h	Sun Aug 12 07:07:57 2012 +0300
     5.3 @@ -36,9 +36,11 @@
     5.4  	std::vector<const Tile*> tiles;
     5.5  
     5.6  public:
     5.7 +	GridCell(const Tile *tile = 0);
     5.8 +
     5.9  	void add_tile(const Tile *tile);
    5.10  
    5.11 -	void draw() const;
    5.12 +	void draw(unsigned int draw_mask) const;
    5.13  };
    5.14  
    5.15  #endif	// LEVEL_H_
     6.1 --- a/prototype/src/main.cc	Sat Aug 11 05:44:52 2012 +0300
     6.2 +++ b/prototype/src/main.cc	Sun Aug 12 07:07:57 2012 +0300
     6.3 @@ -4,19 +4,31 @@
     6.4  #include "opengl.h"
     6.5  #include "level.h"
     6.6  #include "camera.h"
     6.7 +#include "datapath.h"
     6.8 +#include "tileset.h"
     6.9  
    6.10 +bool init();
    6.11 +void cleanup();
    6.12  void disp();
    6.13  void reshape(int x, int y);
    6.14  void keyb(unsigned char key, int x, int y);
    6.15  void mouse(int bn, int state, int x, int y);
    6.16  void motion(int x, int y);
    6.17  
    6.18 +static TileSet *tileset;
    6.19  static Level *level;
    6.20  static OrbitCamera cam;
    6.21  
    6.22 +static const char *level_file = "0.level";
    6.23 +
    6.24  int main(int argc, char **argv)
    6.25  {
    6.26  	glutInit(&argc, argv);
    6.27 +
    6.28 +	if(argc > 1) {
    6.29 +		level_file = argv[1];
    6.30 +	}
    6.31 +
    6.32  	glutInitWindowSize(800, 600);
    6.33  	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
    6.34  	glutCreateWindow("prototype");
    6.35 @@ -29,6 +41,15 @@
    6.36  
    6.37  	glewInit();
    6.38  
    6.39 +	if(!init()) {
    6.40 +		return 1;
    6.41 +	}
    6.42 +
    6.43 +	glutMainLoop();
    6.44 +}
    6.45 +
    6.46 +bool init()
    6.47 +{
    6.48  	glEnable(GL_LIGHTING);
    6.49  	glEnable(GL_LIGHT0);
    6.50  	float ldir[] = {-1, 1, 2, 0};
    6.51 @@ -39,12 +60,21 @@
    6.52  	glEnable(GL_CULL_FACE);
    6.53  	glEnable(GL_MULTISAMPLE);
    6.54  
    6.55 +	add_data_path("data");
    6.56 +
    6.57 +	// load a tileset
    6.58 +	tileset = new TileSet;
    6.59 +	if(!tileset->load(datafile_path("default.tileset"))) {
    6.60 +		return false;
    6.61 +	}
    6.62 +	set_active_tileset(tileset);
    6.63 +
    6.64  	level = new Level;
    6.65 -	if(!level->load("foobar")) {
    6.66 -		return 1;
    6.67 +	if(!level->load(datafile_path(level_file))) {
    6.68 +		return false;
    6.69  	}
    6.70  
    6.71 -	glutMainLoop();
    6.72 +	return true;
    6.73  }
    6.74  
    6.75  void disp()
     7.1 --- a/prototype/src/mesh.cc	Sat Aug 11 05:44:52 2012 +0300
     7.2 +++ b/prototype/src/mesh.cc	Sun Aug 12 07:07:57 2012 +0300
     7.3 @@ -27,6 +27,8 @@
     7.4  		destroy();
     7.5  	}
     7.6  
     7.7 +	name = aim->mName.data;
     7.8 +
     7.9  	nverts = aim->mNumVertices;
    7.10  	nfaces = aim->mNumFaces;
    7.11  
     8.1 --- a/prototype/src/tile.cc	Sat Aug 11 05:44:52 2012 +0300
     8.2 +++ b/prototype/src/tile.cc	Sun Aug 12 07:07:57 2012 +0300
     8.3 @@ -7,6 +7,10 @@
     8.4  
     8.5  bool Tile::load(const char *fname)
     8.6  {
     8.7 +	if(!fname) {
     8.8 +		return false;
     8.9 +	}
    8.10 +
    8.11  	unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
    8.12  		aiProcess_PreTransformVertices | aiProcess_Triangulate |
    8.13  		aiProcess_GenNormals | aiProcess_SortByPType | aiProcess_FlipUVs;
    8.14 @@ -25,11 +29,11 @@
    8.15  	return true;
    8.16  }
    8.17  
    8.18 -void Tile::draw(unsigned int drawmask) const
    8.19 +void Tile::draw(unsigned int draw_mask) const
    8.20  {
    8.21  	for(size_t i=0; i<meshes.size(); i++) {
    8.22  		if(mesh_side[i] & draw_mask) {
    8.23 -			meshes[i].draw();
    8.24 +			meshes[i]->draw();
    8.25  		}
    8.26  	}
    8.27  }
    8.28 @@ -81,6 +85,7 @@
    8.29  
    8.30  		// find which side is this mesh on
    8.31  		const char *name = mesh->get_name();
    8.32 +		printf("name: %s ... ", name);
    8.33  		unsigned int side;
    8.34  
    8.35  		if(strstr(name, "NORTH") == name) {
    8.36 @@ -94,6 +99,7 @@
    8.37  		} else {
    8.38  			side = TILE_ALL;
    8.39  		}
    8.40 +		printf("side: %x\n", side);
    8.41  		mesh_side.push_back(side);
    8.42  
    8.43  		count++;
     9.1 --- a/prototype/src/tile.h	Sat Aug 11 05:44:52 2012 +0300
     9.2 +++ b/prototype/src/tile.h	Sun Aug 12 07:07:57 2012 +0300
     9.3 @@ -29,4 +29,5 @@
     9.4  	void draw(unsigned int drawmask) const;
     9.5  };
     9.6  
     9.7 +
     9.8  #endif	// TILE_H_
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/prototype/src/tileset.cc	Sun Aug 12 07:07:57 2012 +0300
    10.3 @@ -0,0 +1,87 @@
    10.4 +#include <stdio.h>
    10.5 +#include "tileset.h"
    10.6 +#include "datapath.h"
    10.7 +
    10.8 +static char *strip_space(char *ptr);
    10.9 +
   10.10 +static TileSet *active_tileset;
   10.11 +
   10.12 +
   10.13 +TileSet::~TileSet()
   10.14 +{
   10.15 +	for(auto iter : tiles) {
   10.16 +		delete iter.second;
   10.17 +	}
   10.18 +}
   10.19 +
   10.20 +bool TileSet::load(const char *fname)
   10.21 +{
   10.22 +	FILE *fp;
   10.23 +	if(!(fp = fopen(fname, "r"))) {
   10.24 +		fprintf(stderr, "failed to open tileset: %s\n", fname);
   10.25 +		return false;
   10.26 +	}
   10.27 +
   10.28 +	int linenum = 0;
   10.29 +	char buf[512];
   10.30 +	while(fgets(buf, sizeof buf, fp)) {
   10.31 +		linenum++;
   10.32 +
   10.33 +		char *line = strip_space(buf);
   10.34 +		if(!*line || *line == '#') {
   10.35 +			continue;
   10.36 +		}
   10.37 +
   10.38 +		char *tilefile = strchr(line, ':');
   10.39 +		if(!tilefile) {
   10.40 +			fprintf(stderr, "error parsing tileset %s, line %d: %s\n", fname,
   10.41 +					linenum, line);
   10.42 +			fclose(fp);
   10.43 +			return false;
   10.44 +		}
   10.45 +		*tilefile++ = 0;
   10.46 +
   10.47 +		printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile);
   10.48 +		Tile *tile = new Tile;
   10.49 +		if(!tile->load(datafile_path(tilefile))) {
   10.50 +			fprintf(stderr, "failed to load tile: %s\n", tilefile);
   10.51 +			delete tile;
   10.52 +			continue;
   10.53 +		}
   10.54 +
   10.55 +		tiles[line] = tile;
   10.56 +	}
   10.57 +
   10.58 +	fclose(fp);
   10.59 +	return true;
   10.60 +}
   10.61 +
   10.62 +Tile *TileSet::get_tile(const char *name) const
   10.63 +{
   10.64 +	auto res = tiles.find(name);
   10.65 +	return res != tiles.end() ? res->second : 0;
   10.66 +}
   10.67 +
   10.68 +void set_active_tileset(TileSet *set)
   10.69 +{
   10.70 +	active_tileset = set;
   10.71 +}
   10.72 +
   10.73 +TileSet *get_active_tileset()
   10.74 +{
   10.75 +	return active_tileset;
   10.76 +}
   10.77 +
   10.78 +static char *strip_space(char *ptr)
   10.79 +{
   10.80 +	while(isspace(*ptr)) {
   10.81 +		ptr++;
   10.82 +	}
   10.83 +
   10.84 +	char *nl = strrchr(ptr, '\n');
   10.85 +	if(nl) *nl = 0;
   10.86 +	char *cr = strrchr(ptr, '\r');
   10.87 +	if(cr) *cr = 0;
   10.88 +
   10.89 +	return ptr;
   10.90 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/prototype/src/tileset.h	Sun Aug 12 07:07:57 2012 +0300
    11.3 @@ -0,0 +1,23 @@
    11.4 +#ifndef TILESET_H_
    11.5 +#define TILESET_H_
    11.6 +
    11.7 +#include <string>
    11.8 +#include <map>
    11.9 +#include "tile.h"
   11.10 +
   11.11 +class TileSet {
   11.12 +private:
   11.13 +	std::map<std::string, Tile*> tiles;
   11.14 +
   11.15 +public:
   11.16 +	~TileSet();
   11.17 +
   11.18 +	bool load(const char *fname);
   11.19 +
   11.20 +	Tile *get_tile(const char *name) const;
   11.21 +};
   11.22 +
   11.23 +void set_active_tileset(TileSet *set);
   11.24 +TileSet *get_active_tileset();
   11.25 +
   11.26 +#endif	// TILESET_H_