erebus

changeset 40:9d6368850fe1

minor enhancements and bugfixes to the console stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 10 Jun 2014 10:53:19 +0300
parents fd45cf0fc912
children 2e817711d0f6
files liberebus/src/erebus.cc liberebus/src/erebus.h liberebus/src/erebus_impl.h liberebus/src/scene.cc liberebus/src/scene.h src/console.cc src/main.cc
diffstat 7 files changed, 79 insertions(+), 11 deletions(-) [+]
line diff
     1.1 --- a/liberebus/src/erebus.cc	Mon Jun 09 23:57:24 2014 +0300
     1.2 +++ b/liberebus/src/erebus.cc	Tue Jun 10 10:53:19 2014 +0300
     1.3 @@ -37,6 +37,7 @@
     1.4  
     1.5  	rnd_gen.seed(time(0));
     1.6  
     1.7 +	ctx->rendering = false;
     1.8  	ctx->scn = 0;
     1.9  	ctx->cur_time = 0;
    1.10  	ctx->cur_frame = 0;
    1.11 @@ -60,6 +61,12 @@
    1.12  	}
    1.13  }
    1.14  
    1.15 +void erb_clear(struct erebus *ctx)
    1.16 +{
    1.17 +	erb_end_frame(ctx);
    1.18 +	ctx->scn->clear();
    1.19 +}
    1.20 +
    1.21  void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
    1.22  {
    1.23  	ctx->options[opt].ival = val;
    1.24 @@ -158,6 +165,16 @@
    1.25  	ctx->inv_gamma = 1.0f / erb_getoptf(ctx, ERB_OPT_GAMMA);
    1.26  
    1.27  	ctx->scn->update(ctx->cur_time);
    1.28 +	ctx->rendering = true;
    1.29 +}
    1.30 +
    1.31 +void erb_end_frame(struct erebus *ctx)
    1.32 +{
    1.33 +	if(ctx->tpool) {
    1.34 +		ctx->tpool->clear_work();
    1.35 +		ctx->tpool->wait();
    1.36 +	}
    1.37 +	ctx->rendering = false;
    1.38  }
    1.39  
    1.40  int erb_render(struct erebus *ctx, long timeout)
    1.41 @@ -169,6 +186,10 @@
    1.42  
    1.43  int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
    1.44  {
    1.45 +	if(!ctx->rendering) {
    1.46 +		return 0;
    1.47 +	}
    1.48 +
    1.49  	while(ctx->tpool->pending()) {
    1.50  		if(timeout > 0) {
    1.51  			long wait_interval = ctx->tpool->wait(timeout);
     2.1 --- a/liberebus/src/erebus.h	Mon Jun 09 23:57:24 2014 +0300
     2.2 +++ b/liberebus/src/erebus.h	Tue Jun 10 10:53:19 2014 +0300
     2.3 @@ -28,6 +28,8 @@
     2.4  struct erebus *erb_init(void);
     2.5  void erb_destroy(struct erebus *ctx);
     2.6  
     2.7 +void erb_clear(struct erebus *ctx);
     2.8 +
     2.9  void erb_setopti(struct erebus *ctx, enum erb_option opt, int val);
    2.10  void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val);
    2.11  void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec);
    2.12 @@ -39,6 +41,8 @@
    2.13  float *erb_get_framebuffer(struct erebus *ctx);
    2.14  
    2.15  void erb_begin_frame(struct erebus *ctx, long ms);
    2.16 +void erb_end_frame(struct erebus *ctx);
    2.17 +
    2.18  int erb_render(struct erebus *ctx, long timeout);
    2.19  int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout);
    2.20  
     3.1 --- a/liberebus/src/erebus_impl.h	Mon Jun 09 23:57:24 2014 +0300
     3.2 +++ b/liberebus/src/erebus_impl.h	Tue Jun 10 10:53:19 2014 +0300
     3.3 @@ -31,6 +31,7 @@
     3.4  	ThreadPool *tpool;
     3.5  
     3.6  	// render state
     3.7 +	bool rendering;
     3.8  	float inv_gamma;
     3.9  	long cur_time;
    3.10  	int cur_sample;
     4.1 --- a/liberebus/src/scene.cc	Mon Jun 09 23:57:24 2014 +0300
     4.2 +++ b/liberebus/src/scene.cc	Tue Jun 10 10:53:19 2014 +0300
     4.3 @@ -20,13 +20,25 @@
     4.4  
     4.5  Scene::~Scene()
     4.6  {
     4.7 +	clear();
     4.8 +}
     4.9 +
    4.10 +void Scene::clear()
    4.11 +{
    4.12  	for(auto obj : objects) {
    4.13  		delete obj;
    4.14  	}
    4.15 +	objects.clear();
    4.16 +
    4.17  	for(auto node : nodes) {
    4.18  		delete node;
    4.19  	}
    4.20 +	nodes.clear();
    4.21 +
    4.22  	delete root;
    4.23 +	root = 0;
    4.24 +
    4.25 +	active_cam = 0;
    4.26  }
    4.27  
    4.28  void Scene::set_env(const Environment &env)
     5.1 --- a/liberebus/src/scene.h	Mon Jun 09 23:57:24 2014 +0300
     5.2 +++ b/liberebus/src/scene.h	Tue Jun 10 10:53:19 2014 +0300
     5.3 @@ -30,6 +30,8 @@
     5.4  	Scene();
     5.5  	~Scene();
     5.6  
     5.7 +	void clear();
     5.8 +
     5.9  	void set_env(const Environment &env);
    5.10  	Environment &get_env();
    5.11  	const Environment &get_env() const;
     6.1 --- a/src/console.cc	Mon Jun 09 23:57:24 2014 +0300
     6.2 +++ b/src/console.cc	Tue Jun 10 10:53:19 2014 +0300
     6.3 @@ -107,7 +107,7 @@
     6.4  		switch(c) {
     6.5  		case '\n':
     6.6  		case '\r':
     6.7 -			if(cmd_handler) {
     6.8 +			if(!input.empty() && cmd_handler) {
     6.9  				cmd_handler(input.c_str());
    6.10  			}
    6.11  
    6.12 @@ -224,6 +224,7 @@
    6.13  
    6.14  	glPushAttrib(GL_ENABLE_BIT);
    6.15  	glEnable(GL_BLEND);
    6.16 +	glDisable(GL_TEXTURE_2D);
    6.17  	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    6.18  
    6.19  	glBegin(GL_QUADS);
    6.20 @@ -234,8 +235,8 @@
    6.21  	glVertex2f(0, 0);
    6.22  
    6.23  	glColor4f(0.5, 0.2, 0.1, 0.7);
    6.24 -	glVertex2f(0, -(outbox_height + line_height));
    6.25 -	glVertex2f(max_width, -(outbox_height + line_height));
    6.26 +	glVertex2f(0, -(outbox_height + line_height * 1.5));
    6.27 +	glVertex2f(max_width, -(outbox_height + line_height * 1.5));
    6.28  	glVertex2f(max_width, -outbox_height);
    6.29  	glVertex2f(0, -outbox_height);
    6.30  	glEnd();
    6.31 @@ -257,8 +258,8 @@
    6.32  	float cursor_x = dtx_char_pos(inpline.c_str(), cursor + 2);
    6.33  	glBegin(GL_QUADS);
    6.34  	glColor4f(1, 1, 1, 0.7);
    6.35 -	glVertex2f(cursor_x, 2);
    6.36 -	glVertex2f(cursor_x + 2, 2);
    6.37 +	glVertex2f(cursor_x, 0);
    6.38 +	glVertex2f(cursor_x + 2, 0);
    6.39  	glVertex2f(cursor_x + 2, line_height - 2);
    6.40  	glVertex2f(cursor_x, line_height - 2);
    6.41  	glEnd();
     7.1 --- a/src/main.cc	Mon Jun 09 23:57:24 2014 +0300
     7.2 +++ b/src/main.cc	Tue Jun 10 10:53:19 2014 +0300
     7.3 @@ -574,8 +574,11 @@
     7.4  
     7.5  static void con_parse(const char *line)
     7.6  {
     7.7 -	char *buf = (char*)alloca(strlen(line) + 1);
     7.8 -	strcpy(buf, line);
     7.9 +	int len = strlen(line);
    7.10 +	if(!len) return;
    7.11 +
    7.12 +	char *buf = (char*)alloca(len + 1);
    7.13 +	memcpy(buf, line, len + 1);
    7.14  
    7.15  	std::vector<char*> args;
    7.16  	char *tok;
    7.17 @@ -587,8 +590,29 @@
    7.18  	args.push_back(0);
    7.19  	int argc = args.size() - 1;
    7.20  
    7.21 -	if(strcmp(args[0], "exit") == 0) {
    7.22 +	if(!args[0]) return;
    7.23 +
    7.24 +	if(strcmp(args[0], "exit") == 0 || strcmp(args[0], "quit") == 0) {
    7.25  		exit(0);
    7.26 +
    7.27 +	} else if(strcmp(args[0], "clear") == 0) {
    7.28 +		erb_clear(erb);
    7.29 +
    7.30 +	} else if(strcmp(args[0], "stop") == 0) {
    7.31 +		erb_end_frame(erb);
    7.32 +
    7.33 +	} else if(strcmp(args[0], "render") == 0) {
    7.34 +		long tm = 0;
    7.35 +		if(args[1]) {
    7.36 +			char *endp;
    7.37 +			tm = strtol(args[1], &endp, 10);
    7.38 +			if(endp == args[1]) {
    7.39 +				con.printf("the argument to render must be a time value in milliseconds\n");
    7.40 +				return;
    7.41 +			}
    7.42 +		}
    7.43 +		begin_frame(tm);
    7.44 +
    7.45  	} else if(strcmp(args[0], "load") == 0) {
    7.46  		for(int i=1; i<argc; i++) {
    7.47  			if(erb_load_scene(erb, args[i]) == -1) {
    7.48 @@ -597,12 +621,15 @@
    7.49  				begin_frame(0);
    7.50  			}
    7.51  		}
    7.52 -	} else {
    7.53  
    7.54 -		if(erb_proc_cmd(erb, line) == -1) {
    7.55 -			con.puts("invalid command\n");
    7.56 +	} else if(strcmp(args[0], "add") == 0) {
    7.57 +		if(erb_proc_cmd(erb, line + 4) == -1) {
    7.58 +			con.puts("invalid add command\n");
    7.59  		} else {
    7.60  			begin_frame(0);
    7.61  		}
    7.62 +
    7.63 +	} else {
    7.64 +		con.printf("unrecognized command: %s\n", args[0]);
    7.65  	}
    7.66  }