dungeon_crawler

changeset 38:862461b686f4

start work on particle systems
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 29 Aug 2012 03:22:36 +0300
parents 84a56fb24850
children 060a44040577
files prototype/Makefile.in prototype/src/level.cc prototype/src/level.h prototype/src/light.cc prototype/src/light.h prototype/src/main.cc prototype/src/renderer.h prototype/src/tile.cc prototype/src/tile.h prototype/src/timer.cc prototype/src/timer.h
diffstat 11 files changed, 98 insertions(+), 11 deletions(-) [+]
line diff
     1.1 --- a/prototype/Makefile.in	Wed Aug 29 01:04:01 2012 +0300
     1.2 +++ b/prototype/Makefile.in	Wed Aug 29 03:22:36 2012 +0300
     1.3 @@ -10,7 +10,7 @@
     1.4  
     1.5  CFLAGS = -pedantic $(warn) $(dbg) $(opt) $(inc)
     1.6  CXXFLAGS = $(CFLAGS) $(cxx11_cflags)
     1.7 -LDFLAGS = $(cxx11_ldflags) $(libgl) -lm -lassimp -limago `pkg-config --libs freetype2`
     1.8 +LDFLAGS = $(cxx11_ldflags) $(libgl) -lm -lassimp -limago -lpsys `pkg-config --libs freetype2`
     1.9  
    1.10  ifeq ($(shell uname -s), Darwin)
    1.11  	libgl = -framework OpenGL -framework GLUT -lglew
     2.1 --- a/prototype/src/level.cc	Wed Aug 29 01:04:01 2012 +0300
     2.2 +++ b/prototype/src/level.cc	Wed Aug 29 03:22:36 2012 +0300
     2.3 @@ -17,6 +17,10 @@
     2.4  Level::~Level()
     2.5  {
     2.6  	delete [] cells;
     2.7 +
     2.8 +	for(auto cell : cell_list) {
     2.9 +		delete cell;
    2.10 +	}
    2.11  }
    2.12  
    2.13  bool Level::load(const char *fname)
    2.14 @@ -65,7 +69,9 @@
    2.15  			}
    2.16  
    2.17  			if(isalpha(buf[i]) || buf[i] == ' ') {
    2.18 -				*grid_row = new GridCell(deftile);
    2.19 +				GridCell *cell = new GridCell(deftile);
    2.20 +				*grid_row = cell;
    2.21 +				cell_list.push_back(*grid_row);
    2.22  			}
    2.23  			grid_row++;
    2.24  		}
    2.25 @@ -117,12 +123,17 @@
    2.26  	return dmask;
    2.27  }
    2.28  
    2.29 +void Level::update(unsigned long msec, float dt)
    2.30 +{
    2.31 +	for(auto cell : cell_list) {
    2.32 +		cell->update(msec, dt);
    2.33 +	}
    2.34 +}
    2.35 +
    2.36  void Level::draw() const
    2.37  {
    2.38  	glMatrixMode(GL_MODELVIEW);
    2.39  
    2.40 -	//draw_grid();
    2.41 -
    2.42  	for(int i=0; i<ysz; i++) {
    2.43  		for(int j=0; j<xsz; j++) {
    2.44  			const GridCell *cell = get_cell(j, i);
    2.45 @@ -194,18 +205,25 @@
    2.46  }
    2.47  
    2.48  
    2.49 -GridCell::GridCell(const Tile *tile)
    2.50 +GridCell::GridCell(Tile *tile)
    2.51  {
    2.52  	if(tile) {
    2.53  		tiles.push_back(tile);
    2.54  	}
    2.55  }
    2.56  
    2.57 -void GridCell::add_tile(const Tile *tile)
    2.58 +void GridCell::add_tile(Tile *tile)
    2.59  {
    2.60  	tiles.push_back(tile);
    2.61  }
    2.62  
    2.63 +void GridCell::update(unsigned long msec, float dt)
    2.64 +{
    2.65 +	for(auto tile : tiles) {
    2.66 +		tile->update(msec, dt);
    2.67 +	}
    2.68 +}
    2.69 +
    2.70  void GridCell::draw(unsigned int draw_mask) const
    2.71  {
    2.72  	for(auto tile : tiles) {
     3.1 --- a/prototype/src/level.h	Wed Aug 29 01:04:01 2012 +0300
     3.2 +++ b/prototype/src/level.h	Wed Aug 29 03:22:36 2012 +0300
     3.3 @@ -15,6 +15,9 @@
     3.4  	int xsz, ysz;
     3.5  	float cell_size;
     3.6  
     3.7 +	// secondary data structure, simple list of all populated cells
     3.8 +	std::vector<GridCell*> cell_list;
     3.9 +
    3.10  	void draw_grid() const;
    3.11  
    3.12  public:
    3.13 @@ -28,6 +31,8 @@
    3.14  	Vector3 get_cell_pos(int x, int y) const;
    3.15  	unsigned int get_cell_dirmask(int x, int y) const;
    3.16  
    3.17 +	void update(unsigned long msec, float dt);
    3.18 +
    3.19  	void draw() const;
    3.20  	void draw_lights() const;
    3.21  };
    3.22 @@ -35,12 +40,14 @@
    3.23  class GridCell {
    3.24  private:
    3.25  	// each grid-cell might contain multiple tiles.
    3.26 -	std::vector<const Tile*> tiles;
    3.27 +	std::vector<Tile*> tiles;
    3.28  
    3.29  public:
    3.30 -	GridCell(const Tile *tile = 0);
    3.31 +	GridCell(Tile *tile = 0);
    3.32  
    3.33 -	void add_tile(const Tile *tile);
    3.34 +	void add_tile(Tile *tile);
    3.35 +
    3.36 +	void update(unsigned long msec, float dt);
    3.37  
    3.38  	void draw(unsigned int draw_mask) const;
    3.39  	void draw_lights(unsigned int draw_mask) const;
     4.1 --- a/prototype/src/light.cc	Wed Aug 29 01:04:01 2012 +0300
     4.2 +++ b/prototype/src/light.cc	Wed Aug 29 03:22:36 2012 +0300
     4.3 @@ -1,6 +1,8 @@
     4.4 +#include <stdlib.h>
     4.5  #include "opengl.h"
     4.6  #include "light.h"
     4.7  #include "renderer.h"
     4.8 +#include "timer.h"
     4.9  
    4.10  Light::Light(const Color &col)
    4.11  	: color(col)
    4.12 @@ -8,6 +10,8 @@
    4.13  	intensity = 1.0;
    4.14  	vbo = 0;
    4.15  	num_faces = 0;
    4.16 +
    4.17 +	flicker_offset = 2.0 * (float)rand() / (float)RAND_MAX;
    4.18  }
    4.19  
    4.20  Light::~Light() {}
    4.21 @@ -117,7 +121,9 @@
    4.22  			glUniform1f(loc, radius);
    4.23  		}
    4.24  		if((loc = glGetUniformLocation(sdr, "light_color")) != -1) {
    4.25 -			glUniform3f(loc, color.x, color.y, color.z);
    4.26 +			float t = get_time_msec() / 1000.0 + flicker_offset * 4.0;
    4.27 +			float intens = fbm1(t * 2.0, 2) * 0.5 + 1.0;
    4.28 +			glUniform3f(loc, color.x * intens, color.y * intens, color.z * intens);
    4.29  		}
    4.30  	}
    4.31  
     5.1 --- a/prototype/src/light.h	Wed Aug 29 01:04:01 2012 +0300
     5.2 +++ b/prototype/src/light.h	Wed Aug 29 03:22:36 2012 +0300
     5.3 @@ -8,6 +8,8 @@
     5.4  	float intensity;
     5.5  	Color color;
     5.6  
     5.7 +	float flicker_offset;
     5.8 +
     5.9  	// VBO for rendering the light source
    5.10  	unsigned int vbo;
    5.11  	unsigned int num_faces;
     6.1 --- a/prototype/src/main.cc	Wed Aug 29 01:04:01 2012 +0300
     6.2 +++ b/prototype/src/main.cc	Wed Aug 29 03:22:36 2012 +0300
     6.3 @@ -10,6 +10,7 @@
     6.4  #include "renderer.h"
     6.5  #include "cmdcon.h"
     6.6  #include "cfg.h"
     6.7 +#include "timer.h"
     6.8  
     6.9  bool init(int xsz, int ysz);
    6.10  void cleanup();
    6.11 @@ -126,7 +127,7 @@
    6.12  
    6.13  void disp()
    6.14  {
    6.15 -	update(glutGet(GLUT_ELAPSED_TIME));
    6.16 +	update(get_time_msec());
    6.17  
    6.18  	if(cfg.stereo) {
    6.19  		glDrawBuffer(GL_BACK_LEFT);
    6.20 @@ -227,6 +228,8 @@
    6.21  
    6.22  	cam.input_move(dx, 0, dy);
    6.23  
    6.24 +	level->update(msec, dt);
    6.25 +
    6.26  	last_upd = msec;
    6.27  }
    6.28  
     7.1 --- a/prototype/src/renderer.h	Wed Aug 29 01:04:01 2012 +0300
     7.2 +++ b/prototype/src/renderer.h	Wed Aug 29 03:22:36 2012 +0300
     7.3 @@ -11,6 +11,7 @@
     7.4  
     7.5  void resize_renderer(int xsz, int ysz);
     7.6  
     7.7 +void update_renderer(unsigned long msec, float dt);
     7.8  void render_deferred(const Level *level);
     7.9  
    7.10  #endif	// RENDERER_H_
     8.1 --- a/prototype/src/tile.cc	Wed Aug 29 01:04:01 2012 +0300
     8.2 +++ b/prototype/src/tile.cc	Wed Aug 29 03:22:36 2012 +0300
     8.3 @@ -20,6 +20,20 @@
     8.4  	tset = tileset;
     8.5  }
     8.6  
     8.7 +Tile::~Tile()
     8.8 +{
     8.9 +	for(auto m : meshes) {
    8.10 +		delete m;
    8.11 +	}
    8.12 +	for(auto lt : lights) {
    8.13 +		delete lt;
    8.14 +	}
    8.15 +	for(auto ps : psattr) {
    8.16 +		psys_destroy_attr(ps);
    8.17 +		delete ps;
    8.18 +	}
    8.19 +}
    8.20 +
    8.21  bool Tile::load(const char *fname)
    8.22  {
    8.23  	if(!fname) {
    8.24 @@ -54,9 +68,16 @@
    8.25  	load_meshes(scn, nodemap);
    8.26  
    8.27  	printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
    8.28 +
    8.29 +	aiReleaseImport(scn);
    8.30  	return true;
    8.31  }
    8.32  
    8.33 +void Tile::update(unsigned long msec, float dt)
    8.34 +{
    8.35 +	// TODO particle system update
    8.36 +}
    8.37 +
    8.38  void Tile::draw(unsigned int draw_mask) const
    8.39  {
    8.40  	for(size_t i=0; i<meshes.size(); i++) {
     9.1 --- a/prototype/src/tile.h	Wed Aug 29 01:04:01 2012 +0300
     9.2 +++ b/prototype/src/tile.h	Wed Aug 29 03:22:36 2012 +0300
     9.3 @@ -4,6 +4,7 @@
     9.4  #include <vector>
     9.5  #include <map>
     9.6  #include <assimp/scene.h>
     9.7 +#include <psys/psys.h>
     9.8  #include "mesh.h"
     9.9  #include "light.h"
    9.10  
    9.11 @@ -24,15 +25,19 @@
    9.12  	std::vector<Mesh*> meshes;
    9.13  	std::vector<unsigned int> mesh_side, light_side;
    9.14  	std::vector<Light*> lights;
    9.15 +	std::vector<struct psys_attributes*> psattr;
    9.16  
    9.17  	int load_lights(const aiScene *scn);
    9.18  	int load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap);
    9.19  
    9.20  public:
    9.21  	Tile(TileSet *tileset = 0);
    9.22 +	~Tile();
    9.23  
    9.24  	bool load(const char *fname);
    9.25  
    9.26 +	void update(unsigned long msec, float dt);
    9.27 +
    9.28  	void draw(unsigned int drawmask) const;
    9.29  	void draw_lights(unsigned int drawmask) const;
    9.30  };
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/prototype/src/timer.cc	Wed Aug 29 03:22:36 2012 +0300
    10.3 @@ -0,0 +1,18 @@
    10.4 +#include <chrono>
    10.5 +#include "timer.h"
    10.6 +
    10.7 +using namespace std::chrono;
    10.8 +
    10.9 +static bool timer_initialized;
   10.10 +static time_point<steady_clock> start_time;
   10.11 +
   10.12 +unsigned long get_time_msec(void)
   10.13 +{
   10.14 +	if(!timer_initialized) {
   10.15 +		start_time = steady_clock::now();
   10.16 +		timer_initialized = true;
   10.17 +	}
   10.18 +
   10.19 +	auto dur = steady_clock::now() - start_time;
   10.20 +	return duration_cast<milliseconds>(dur).count();
   10.21 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/prototype/src/timer.h	Wed Aug 29 03:22:36 2012 +0300
    11.3 @@ -0,0 +1,6 @@
    11.4 +#ifndef TIMER_H_
    11.5 +#define TIMER_H_
    11.6 +
    11.7 +unsigned long get_time_msec(void);
    11.8 +
    11.9 +#endif	// TIMER_H_