# HG changeset patch # User John Tsiombikas # Date 1344744477 -10800 # Node ID 252a0050841156b796031073afd94ca100a267b5 # Parent 158de53b4e181da620e4e74e3f892393082edc2b more stuff diff -r 158de53b4e18 -r 252a00508411 prototype/Makefile --- a/prototype/Makefile Sat Aug 11 05:44:52 2012 +0300 +++ b/prototype/Makefile Sun Aug 12 07:07:57 2012 +0300 @@ -5,8 +5,8 @@ bin = proto CFLAGS = -pedantic -Wall -g -Ivmath -CXXFLAGS = $(CFLAGS) -LDFLAGS = $(libgl) -lm +CXXFLAGS = $(CFLAGS) -std=c++11 +LDFLAGS = $(libgl) -lm -lassimp ifeq ($(shell uname -s), Darwin) libgl = -framework OpenGL -framework GLUT -lglew diff -r 158de53b4e18 -r 252a00508411 prototype/src/datapath.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/datapath.cc Sun Aug 12 07:07:57 2012 +0300 @@ -0,0 +1,26 @@ +#include +#include +#include +#include "datapath.h" + +static std::vector pathlist; + +void add_data_path(const char *path) +{ + pathlist.push_back(path); +} + +const char *datafile_path(const char *fname) +{ + static std::string res; + + for(auto path : pathlist) { + res = path + "/" + std::string(fname); + FILE *fp = fopen(res.c_str(), "r"); + if(fp) { + fclose(fp); + return res.c_str(); + } + } + return 0; +} diff -r 158de53b4e18 -r 252a00508411 prototype/src/datapath.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/datapath.h Sun Aug 12 07:07:57 2012 +0300 @@ -0,0 +1,8 @@ +#ifndef DATAPATH_H_ +#define DATAPATH_H_ + +void add_data_path(const char *path); + +const char *datafile_path(const char *fname); + +#endif // DATAPATH_H_ diff -r 158de53b4e18 -r 252a00508411 prototype/src/level.cc --- a/prototype/src/level.cc Sat Aug 11 05:44:52 2012 +0300 +++ b/prototype/src/level.cc Sun Aug 12 07:07:57 2012 +0300 @@ -1,8 +1,10 @@ #include #include +#include #include "opengl.h" #include "level.h" #include "tile.h" +#include "tileset.h" Level::Level() { @@ -19,14 +21,60 @@ bool Level::load(const char *fname) { - xsz = ysz = 64; + if(!fname) { + return false; + } + + TileSet *tileset = get_active_tileset(); + if(!tileset) { + fprintf(stderr, "level loading failed: no active tileset\n"); + return false; + } + Tile *deftile = tileset->get_tile("default"); + if(!deftile) { + fprintf(stderr, "level loading failed: active tileset has no default tile\n"); + return false; + } + + FILE *fp = fopen(fname, "r"); + if(!fp) { + fprintf(stderr, "failed to open level: %s: %s\n", fname, strerror(errno)); + return false; + } + + char buf[512]; + fgets(buf, sizeof buf, fp); + if(sscanf(buf, "SIZE %dx%d", &xsz, &ysz) != 2) { + fprintf(stderr, "invalid or corrupt level file: %s\n", fname); + fclose(fp); + return false; + } + printf("level size: %dx%d\n", xsz, ysz); cells = new GridCell*[xsz * ysz]; memset(cells, 0, xsz * ysz * sizeof *cells); - GridCell *g = new GridCell; - g->add_tile(new Tile); - cells[ysz / 2 * xsz + xsz / 2] = g; + int y = 0; + GridCell **grid_row = cells; + + while(fgets(buf, sizeof buf, fp)) { + for(int i=0; i= ysz) { + break; + } + } + fclose(fp); return true; } @@ -65,7 +113,22 @@ glPushMatrix(); glTranslatef(pos.x, pos.y, pos.z); glScalef(cell_size, cell_size, cell_size); - cell->draw(); + + unsigned int dmask = 0; + if(i <= 0 || get_cell(j, i - 1) == 0) { + dmask |= TILE_NORTH; + } + if(i > ysz || get_cell(j, i + 1) == 0) { + dmask |= TILE_SOUTH; + } + if(j <= 0 || get_cell(j - 1, i) == 0) { + dmask |= TILE_WEST; + } + if(j > xsz || get_cell(j + 1, i) == 0) { + dmask |= TILE_EAST; + } + + cell->draw(dmask); glPopMatrix(); } } @@ -101,14 +164,22 @@ glPopAttrib(); } + +GridCell::GridCell(const Tile *tile) +{ + if(tile) { + tiles.push_back(tile); + } +} + void GridCell::add_tile(const Tile *tile) { tiles.push_back(tile); } -void GridCell::draw() const +void GridCell::draw(unsigned int draw_mask) const { for(size_t i=0; idraw(); + tiles[i]->draw(draw_mask); } } diff -r 158de53b4e18 -r 252a00508411 prototype/src/level.h --- a/prototype/src/level.h Sat Aug 11 05:44:52 2012 +0300 +++ b/prototype/src/level.h Sun Aug 12 07:07:57 2012 +0300 @@ -36,9 +36,11 @@ std::vector tiles; public: + GridCell(const Tile *tile = 0); + void add_tile(const Tile *tile); - void draw() const; + void draw(unsigned int draw_mask) const; }; #endif // LEVEL_H_ diff -r 158de53b4e18 -r 252a00508411 prototype/src/main.cc --- a/prototype/src/main.cc Sat Aug 11 05:44:52 2012 +0300 +++ b/prototype/src/main.cc Sun Aug 12 07:07:57 2012 +0300 @@ -4,19 +4,31 @@ #include "opengl.h" #include "level.h" #include "camera.h" +#include "datapath.h" +#include "tileset.h" +bool init(); +void cleanup(); void disp(); void reshape(int x, int y); void keyb(unsigned char key, int x, int y); void mouse(int bn, int state, int x, int y); void motion(int x, int y); +static TileSet *tileset; static Level *level; static OrbitCamera cam; +static const char *level_file = "0.level"; + int main(int argc, char **argv) { glutInit(&argc, argv); + + if(argc > 1) { + level_file = argv[1]; + } + glutInitWindowSize(800, 600); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); glutCreateWindow("prototype"); @@ -29,6 +41,15 @@ glewInit(); + if(!init()) { + return 1; + } + + glutMainLoop(); +} + +bool init() +{ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); float ldir[] = {-1, 1, 2, 0}; @@ -39,12 +60,21 @@ glEnable(GL_CULL_FACE); glEnable(GL_MULTISAMPLE); + add_data_path("data"); + + // load a tileset + tileset = new TileSet; + if(!tileset->load(datafile_path("default.tileset"))) { + return false; + } + set_active_tileset(tileset); + level = new Level; - if(!level->load("foobar")) { - return 1; + if(!level->load(datafile_path(level_file))) { + return false; } - glutMainLoop(); + return true; } void disp() diff -r 158de53b4e18 -r 252a00508411 prototype/src/mesh.cc --- a/prototype/src/mesh.cc Sat Aug 11 05:44:52 2012 +0300 +++ b/prototype/src/mesh.cc Sun Aug 12 07:07:57 2012 +0300 @@ -27,6 +27,8 @@ destroy(); } + name = aim->mName.data; + nverts = aim->mNumVertices; nfaces = aim->mNumFaces; diff -r 158de53b4e18 -r 252a00508411 prototype/src/tile.cc --- a/prototype/src/tile.cc Sat Aug 11 05:44:52 2012 +0300 +++ b/prototype/src/tile.cc Sun Aug 12 07:07:57 2012 +0300 @@ -7,6 +7,10 @@ bool Tile::load(const char *fname) { + if(!fname) { + return false; + } + unsigned int proc_flags = aiProcess_JoinIdenticalVertices | aiProcess_PreTransformVertices | aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_SortByPType | aiProcess_FlipUVs; @@ -25,11 +29,11 @@ return true; } -void Tile::draw(unsigned int drawmask) const +void Tile::draw(unsigned int draw_mask) const { for(size_t i=0; idraw(); } } } @@ -81,6 +85,7 @@ // find which side is this mesh on const char *name = mesh->get_name(); + printf("name: %s ... ", name); unsigned int side; if(strstr(name, "NORTH") == name) { @@ -94,6 +99,7 @@ } else { side = TILE_ALL; } + printf("side: %x\n", side); mesh_side.push_back(side); count++; diff -r 158de53b4e18 -r 252a00508411 prototype/src/tile.h --- a/prototype/src/tile.h Sat Aug 11 05:44:52 2012 +0300 +++ b/prototype/src/tile.h Sun Aug 12 07:07:57 2012 +0300 @@ -29,4 +29,5 @@ void draw(unsigned int drawmask) const; }; + #endif // TILE_H_ diff -r 158de53b4e18 -r 252a00508411 prototype/src/tileset.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/tileset.cc Sun Aug 12 07:07:57 2012 +0300 @@ -0,0 +1,87 @@ +#include +#include "tileset.h" +#include "datapath.h" + +static char *strip_space(char *ptr); + +static TileSet *active_tileset; + + +TileSet::~TileSet() +{ + for(auto iter : tiles) { + delete iter.second; + } +} + +bool TileSet::load(const char *fname) +{ + FILE *fp; + if(!(fp = fopen(fname, "r"))) { + fprintf(stderr, "failed to open tileset: %s\n", fname); + return false; + } + + int linenum = 0; + char buf[512]; + while(fgets(buf, sizeof buf, fp)) { + linenum++; + + char *line = strip_space(buf); + if(!*line || *line == '#') { + continue; + } + + char *tilefile = strchr(line, ':'); + if(!tilefile) { + fprintf(stderr, "error parsing tileset %s, line %d: %s\n", fname, + linenum, line); + fclose(fp); + return false; + } + *tilefile++ = 0; + + printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile); + Tile *tile = new Tile; + if(!tile->load(datafile_path(tilefile))) { + fprintf(stderr, "failed to load tile: %s\n", tilefile); + delete tile; + continue; + } + + tiles[line] = tile; + } + + fclose(fp); + return true; +} + +Tile *TileSet::get_tile(const char *name) const +{ + auto res = tiles.find(name); + return res != tiles.end() ? res->second : 0; +} + +void set_active_tileset(TileSet *set) +{ + active_tileset = set; +} + +TileSet *get_active_tileset() +{ + return active_tileset; +} + +static char *strip_space(char *ptr) +{ + while(isspace(*ptr)) { + ptr++; + } + + char *nl = strrchr(ptr, '\n'); + if(nl) *nl = 0; + char *cr = strrchr(ptr, '\r'); + if(cr) *cr = 0; + + return ptr; +} diff -r 158de53b4e18 -r 252a00508411 prototype/src/tileset.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/tileset.h Sun Aug 12 07:07:57 2012 +0300 @@ -0,0 +1,23 @@ +#ifndef TILESET_H_ +#define TILESET_H_ + +#include +#include +#include "tile.h" + +class TileSet { +private: + std::map tiles; + +public: + ~TileSet(); + + bool load(const char *fname); + + Tile *get_tile(const char *name) const; +}; + +void set_active_tileset(TileSet *set); +TileSet *get_active_tileset(); + +#endif // TILESET_H_