erebus

diff liberebus/src/erebus.cc @ 37:db8a90307386

implemented console and rudimentary commandline parser
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Jun 2014 16:01:00 +0300
parents d15ee526daa6
children 5e27c85e79ca
line diff
     1.1 --- a/liberebus/src/erebus.cc	Mon Jun 09 07:28:03 2014 +0300
     1.2 +++ b/liberebus/src/erebus.cc	Mon Jun 09 16:01:00 2014 +0300
     1.3 @@ -3,6 +3,11 @@
     1.4  #include <algorithm>
     1.5  #include <chrono>
     1.6  #include <random>
     1.7 +#ifndef _MSC_VER
     1.8 +#include <alloca.h>
     1.9 +#else
    1.10 +#include <malloc.h>
    1.11 +#endif
    1.12  #include "erebus.h"
    1.13  #include "erebus_impl.h"
    1.14  #include "scene.h"
    1.15 @@ -248,8 +253,9 @@
    1.16  
    1.17  int erb_load_scene(struct erebus *ctx, const char *fname)
    1.18  {
    1.19 -	delete ctx->scn;
    1.20 -	ctx->scn = new Scene;
    1.21 +	if(!ctx->scn) {
    1.22 +		ctx->scn = new Scene;
    1.23 +	}
    1.24  
    1.25  	if(!ctx->scn->load(fname)) {
    1.26  		return -1;
    1.27 @@ -257,6 +263,29 @@
    1.28  	return 0;
    1.29  }
    1.30  
    1.31 +int erb_proc_cmd(struct erebus *ctx, const char *cmd)
    1.32 +{
    1.33 +	if(!ctx->scn) {
    1.34 +		ctx->scn = new Scene;
    1.35 +	}
    1.36 +
    1.37 +	char *buf = (char*)alloca(strlen(cmd) + 1);
    1.38 +	strcpy(buf, cmd);
    1.39 +
    1.40 +	std::vector<char*> args;
    1.41 +	char *tok;
    1.42 +	while((tok = strtok(buf, " \t\v\n\r"))) {
    1.43 +		buf = 0;
    1.44 +		args.push_back(tok);
    1.45 +	}
    1.46 +	args.push_back(0);
    1.47 +
    1.48 +	if(!ctx->scn->proc_cmd(args.size() - 1, &args[0])) {
    1.49 +		return -1;
    1.50 +	}
    1.51 +	return 0;
    1.52 +}
    1.53 +
    1.54  bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed)
    1.55  {
    1.56  	if(!ctx) return false;