# HG changeset patch # User John Tsiombikas # Date 1347507231 -10800 # Node ID f3030df271101df62233541c55ebfe253ff89d85 # Parent dfd3a413ef9e7e6a60324f9b842615045d77682a debugging the particles diff -r dfd3a413ef9e -r f3030df27110 prototype/data/fire.psys --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/data/fire.psys Thu Sep 13 06:33:51 2012 +0300 @@ -0,0 +1,27 @@ +# fire particle system definition +# +# lines of key = value pairs. +# animated attributes can be defined as key(time) = value. +# time defaults to 0 if missing. time values can have an optional s suffix, +# signifying seconds, otherwise they are assumed to be in milliseconds. +# +# string values enclosed in double quotes +# vector values enclosed in square brackets [x y z] +# randomized values have a pair of values/vectors separated by a tilde: +# center ~ range. If the range is missing, it's assumed to be 0. + +texture = "fire_particle.png" +rate = 20 +life = 1 +grav = [0 0.5 0] +spawn_range = 0.025 + +size = 0.1 + +pcolor(0) = [0.91 0.89 0.65] +pcolor(700) = [1.0 0.3 0.12] +pcolor(1000) = [1.0 0.3 0.12] + +palpha(0) = 1 +palpha(700) = 1 +palpha(1000) = 0 diff -r dfd3a413ef9e -r f3030df27110 prototype/data/fire_particle.png Binary file prototype/data/fire_particle.png has changed diff -r dfd3a413ef9e -r f3030df27110 prototype/src/level.cc --- a/prototype/src/level.cc Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/level.cc Thu Sep 13 06:33:51 2012 +0300 @@ -100,8 +100,10 @@ Vector3 Level::get_cell_pos(int x, int y) const { - float posx = (x - xsz / 2) * cell_size; - float posy = (y - ysz / 2) * cell_size; + float posx = (float)x * cell_size; + float posy = (float)y * cell_size; + posx -= cell_size * (float)xsz / 2.0f; + posy -= cell_size * (float)ysz / 2.0f; return Vector3(posx, 0, posy); } @@ -175,6 +177,28 @@ } } +void Level::draw_post() const +{ + glMatrixMode(GL_MODELVIEW); + + for(int i=0; idraw_post(dmask); + glPopMatrix(); + } + } + } +} + void Level::draw_grid() const { float xlen = xsz * cell_size; @@ -221,7 +245,7 @@ tiles.push_back(tile); /* instanciate any particle systems */ - const struct psys_attributes **psattr = tile->get_unique_psys(); + const struct psys_attributes * const *psattr = tile->get_unique_psys(); int num_psattr = tile->get_unique_psys_count(); for(int i=0; iupdate(msec, dt); - } for(auto ps : psys) { psys_update(ps, (float)msec / 1000.0f); } diff -r dfd3a413ef9e -r f3030df27110 prototype/src/light.cc --- a/prototype/src/light.cc Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/light.cc Thu Sep 13 06:33:51 2012 +0300 @@ -86,6 +86,11 @@ this->pos = pos; } +const Vector3 &PointLight::get_position() const +{ + return pos; +} + void PointLight::set_attenuation(float att_const, float att_lin, float att_quad) { atten[0] = att_const; diff -r dfd3a413ef9e -r f3030df27110 prototype/src/light.h --- a/prototype/src/light.h Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/light.h Thu Sep 13 06:33:51 2012 +0300 @@ -43,6 +43,8 @@ PointLight(const Vector3 &pos, const Color &col = 1.0); void set_position(const Vector3 &pos); + const Vector3 &get_position() const; + void set_attenuation(float att_const, float att_lin, float att_quad); void set_radius(float rad); @@ -64,6 +66,7 @@ DirLight(const Vector3 &dir, const Color &col = 1.0); void set_direction(const Vector3 &dir); + const Vector3 &get_direction() const; virtual void use(int id = 0) const; }; diff -r dfd3a413ef9e -r f3030df27110 prototype/src/main.cc --- a/prototype/src/main.cc Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/main.cc Thu Sep 13 06:33:51 2012 +0300 @@ -3,7 +3,9 @@ #include #include #include "opengl.h" +#include "psys/psys.h" #include "level.h" +#include "texman.h" #include "camera.h" #include "datapath.h" #include "tileset.h" @@ -27,6 +29,7 @@ void keyb_con(unsigned char key, int x, int y); void mouse(int bn, int state, int x, int y); void motion(int x, int y); +unsigned int load_psys_tex(const char *fname, void *cls); static TileSet *tileset; static Level *level; @@ -104,6 +107,8 @@ return false; } + psys_texture_loader(load_psys_tex, 0, 0); + // load a tileset tileset = new TileSet; printf("loading tileset: %s\n", cfg.tileset_file); @@ -238,6 +243,7 @@ cam.input_move(dx, 0, dy); + tileset->update_tiles(msec); level->update(msec, dt); last_upd = msec; @@ -355,3 +361,9 @@ glutPostRedisplay(); } } + +unsigned int load_psys_tex(const char *fname, void *cls) +{ + TextureSet *texset = tileset->get_textures(); + return texset->get_texture(fname); +} diff -r dfd3a413ef9e -r f3030df27110 prototype/src/renderer.cc --- a/prototype/src/renderer.cc Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/renderer.cc Thu Sep 13 06:33:51 2012 +0300 @@ -90,9 +90,10 @@ glEnable(GL_LIGHTING); level->draw(); + glUseProgram(0); + level->draw_post(); + glPopAttrib(); - - glUseProgram(0); } static unsigned int load_sdr(const char *vfname, const char *pfname) diff -r dfd3a413ef9e -r f3030df27110 prototype/src/texman.cc --- a/prototype/src/texman.cc Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/texman.cc Thu Sep 13 06:33:51 2012 +0300 @@ -23,8 +23,13 @@ const char *path, *slash; if((slash = strrchr(fname, '/'))) { path = slash + 1; + } else { + path = fname; } - path = datafile_path(path); + if(!(path = datafile_path(path))) { + fprintf(stderr, "can't find texture: %s\n", fname); + return 0; + } printf("loading texture: %s\n", path); unsigned int tex = load_texture(path); diff -r dfd3a413ef9e -r f3030df27110 prototype/src/tile.cc --- a/prototype/src/tile.cc Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/tile.cc Thu Sep 13 06:33:51 2012 +0300 @@ -1,4 +1,5 @@ #include +#include #include #include "opengl.h" #include @@ -7,6 +8,7 @@ #include "tile.h" #include "tileset.h" #include "renderer.h" +#include "datapath.h" using std::map; @@ -18,6 +20,7 @@ Tile::Tile(TileSet *tileset) { tset = tileset; + last_upd = LONG_MIN; } Tile::~Tile() @@ -28,18 +31,20 @@ for(auto lt : lights) { delete lt; } - for(auto ps : psattr) { - psys_destroy_attr(ps); - delete ps; + for(auto psa : psattr) { + psys_free_attr(psa); + } + for(auto ps : psys_global) { + psys_free(ps); } } -const struct psys_attributes **get_unique_psys() const +const struct psys_attributes * const *Tile::get_unique_psys() const { return &psattr[0]; } -int get_unique_psys_count() const +int Tile::get_unique_psys_count() const { return (int)psattr.size(); } @@ -85,7 +90,10 @@ void Tile::update(unsigned long msec, float dt) { - // TODO particle system update + // update particle systems + for(auto ps : psys_global) { + psys_update(ps, (float)msec / 1000.0f); + } } void Tile::draw(unsigned int draw_mask) const @@ -106,6 +114,16 @@ } } +void Tile::draw_post(unsigned int draw_mask) const +{ + // draw global particle systems (simulated once) + for(size_t i=0; iattr, datafile_path("fire.psys")) == 0) { + Vector3 lpos = lt->get_position(); + psys_set_pos(ps, v3_cons(lpos.x, lpos.y, lpos.z), 0); + psys_global.push_back(ps); + psys_side.push_back(side); + } else { + fprintf(stderr, "failed to create global particle system\n"); + } + } else { meshes.push_back(mesh); mesh_side.push_back(side); diff -r dfd3a413ef9e -r f3030df27110 prototype/src/tile.h --- a/prototype/src/tile.h Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/tile.h Thu Sep 13 06:33:51 2012 +0300 @@ -23,12 +23,13 @@ TileSet *tset; std::vector meshes; - std::vector mesh_side, light_side; + std::vector mesh_side, light_side, psys_side; std::vector lights; // attributes for gridcell-unique particle systems std::vector psattr; // global particle systems (simulated once, drawn for each tile instance). std::vector psys_global; + long last_upd; int load_lights(const aiScene *scn); int load_meshes(const aiScene *scn, const std::map &nmap); @@ -37,7 +38,7 @@ Tile(TileSet *tileset = 0); ~Tile(); - const struct psys_attributes **get_unique_psys() const; + const struct psys_attributes * const *get_unique_psys() const; int get_unique_psys_count() const; bool load(const char *fname); @@ -46,7 +47,7 @@ void draw(unsigned int drawmask) const; void draw_lights(unsigned int drawmask) const; - void draw_psys(unsigned int drawmask) const; + void draw_post(unsigned int drawmask) const; }; diff -r dfd3a413ef9e -r f3030df27110 prototype/src/tileset.cc --- a/prototype/src/tileset.cc Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/tileset.cc Thu Sep 13 06:33:51 2012 +0300 @@ -72,6 +72,13 @@ return res != tiles.end() ? res->second : 0; } +void TileSet::update_tiles(unsigned long msec) +{ + for(auto tilepair : tiles) { + tilepair.second->update(msec, 0.0f); + } +} + void set_active_tileset(TileSet *set) { active_tileset = set; diff -r dfd3a413ef9e -r f3030df27110 prototype/src/tileset.h --- a/prototype/src/tileset.h Wed Sep 12 06:04:20 2012 +0300 +++ b/prototype/src/tileset.h Thu Sep 13 06:33:51 2012 +0300 @@ -21,6 +21,8 @@ const TextureSet *get_textures() const; Tile *get_tile(const char *name) const; + + void update_tiles(unsigned long msec); }; void set_active_tileset(TileSet *set);