vrheights

diff src/game.cc @ 7:0eca023ed909

- fixed the hmd-tracking / mouselook interaction - added game variable system
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 01 Oct 2014 01:06:55 +0300
parents 608e03b688f8
children 3f221bdc9bab
line diff
     1.1 --- a/src/game.cc	Tue Sep 30 18:57:20 2014 +0300
     1.2 +++ b/src/game.cc	Wed Oct 01 01:06:55 2014 +0300
     1.3 @@ -9,6 +9,7 @@
     1.4  #include "teapot.h"
     1.5  #include "console.h"
     1.6  #include "drawtext.h"
     1.7 +#include "game_var.h"
     1.8  
     1.9  static void draw_scene();
    1.10  static void material(float r, float g, float b, float roughness);
    1.11 @@ -17,8 +18,6 @@
    1.12  static int next_pow2(int x);
    1.13  static void con_handle(const char *cmd);
    1.14  
    1.15 -bool opt_separate_walk_look;
    1.16 -
    1.17  static int win_width, win_height;
    1.18  static unsigned int fb_tex;
    1.19  static unsigned int fbo, fb_depth;
    1.20 @@ -34,6 +33,8 @@
    1.21  static Console con;
    1.22  static dtx_font *con_font;
    1.23  
    1.24 +#define OPT_LOOK_WALK	"look-walk"
    1.25 +
    1.26  bool game_init()
    1.27  {
    1.28  	init_opengl();
    1.29 @@ -61,9 +62,13 @@
    1.30  		return false;
    1.31  	}
    1.32  	con.set_font(con_font, 14);
    1.33 -	con.set_size(7, 40);
    1.34 +	con.set_size(7, 52);
    1.35  	con.set_position(0, 0, Console::CENTER);
    1.36  	con.set_command_func(con_handle);
    1.37 +	con.set_echo(false);
    1.38 +
    1.39 +	/* initialize all option variables */
    1.40 +	set_gvar_bool(OPT_LOOK_WALK, true);
    1.41  
    1.42  	return true;
    1.43  }
    1.44 @@ -100,18 +105,19 @@
    1.45  		dir += Vector3(0, 0, offs);
    1.46  	}
    1.47  
    1.48 +	if(get_gvar_bool(OPT_LOOK_WALK)) {
    1.49 +		float rot[4];
    1.50 +		vr_view_rotation(0, rot);
    1.51 +		Quaternion q(rot[3], rot[0], rot[1], rot[2]);
    1.52 +
    1.53 +		dir.transform(q);
    1.54 +	}
    1.55 +
    1.56  	float cos_theta = cos(DEG_TO_RAD(cam_theta));
    1.57  	float sin_theta = sin(DEG_TO_RAD(cam_theta));
    1.58  	cam_pos.x += dir.x * cos_theta - dir.z * sin_theta;
    1.59  	cam_pos.z += dir.x * sin_theta + dir.z * cos_theta;
    1.60  
    1.61 -	if(!opt_separate_walk_look) {
    1.62 -		float rot[4];
    1.63 -		vr_view_rotation(0, rot);
    1.64 -		Quaternion q(rot[3], rot[0], rot[1], rot[2]);
    1.65 -
    1.66 -		cam_pos.transform(q);
    1.67 -	}
    1.68  }
    1.69  
    1.70  void game_display()
    1.71 @@ -377,6 +383,13 @@
    1.72  	return str;
    1.73  }
    1.74  
    1.75 +struct Variable {
    1.76 +	const char *var;
    1.77 +	enum { BOOL, NUM } type;
    1.78 +	bool bool_val;
    1.79 +	float num_val;
    1.80 +};
    1.81 +
    1.82  static void con_handle(const char *cmd)
    1.83  {
    1.84  	std::vector<char*> argv;
    1.85 @@ -396,20 +409,45 @@
    1.86  
    1.87  	if(strcmp(argv[0], "set") == 0) {
    1.88  		if(argv.size() < 3) {
    1.89 -			fprintf(stderr, "set must be followed by 2 arguments\n");
    1.90 +			con.printf("set must be followed by 2 arguments\n");
    1.91  			return;
    1.92  		}
    1.93 -		if(strcmp(argv[1], "separate-walk") == 0) {
    1.94 -			if(strcmp(argv[2], "true") == 0) {
    1.95 -				opt_separate_walk_look = true;
    1.96 -			} else if(strcmp(argv[2], "false") == 0) {
    1.97 -				opt_separate_walk_look = false;
    1.98 -			}
    1.99 +		if(have_gvar(argv[1])) {
   1.100 +			GameVariable &gv = get_gvar(argv[1]);
   1.101 +			con.printf("%s->%s (prev: %s)\n", argv[1], argv[2], gv.to_str().c_str());
   1.102  		} else {
   1.103 -			con.printf("unknown option: %s\n", argv[1]);
   1.104 +			con.printf("%s->%s\n", argv[1], argv[2]);
   1.105  		}
   1.106 +		set_gvar_parse(argv[1], argv[2]);
   1.107 +
   1.108 +	} else if(strcmp(argv[0], "get") == 0) {
   1.109 +		if(argv.size() < 2) {
   1.110 +			con.printf("get must be followed by the variable name\n");
   1.111 +			return;
   1.112 +		}
   1.113 +		if(have_gvar(argv[1])) {
   1.114 +			GameVariable &gv = get_gvar(argv[1]);
   1.115 +			con.printf("%s: %s\n", argv[1], gv.to_str().c_str());
   1.116 +		}
   1.117 +
   1.118 +	} else if(strcmp(argv[0], "vars") == 0) {
   1.119 +		std::list<std::string> vars = get_gvar_list();
   1.120 +		std::list<std::string>::const_iterator it = vars.begin();
   1.121 +		while(it != vars.end()) {
   1.122 +			con.printf("  %s\n", it++->c_str());
   1.123 +		}
   1.124 +
   1.125  	} else if(strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
   1.126  		exit_game();
   1.127 +
   1.128 +	} else if(strcmp(argv[0], "help") == 0) {
   1.129 +		con.printf("available commands:\n");
   1.130 +		con.printf(" set <var> <val>  set value to a variable\n");
   1.131 +		con.printf(" get <var>        print the value of a variable\n");
   1.132 +		con.printf(" vars             print a list of all variables\n");
   1.133 +		con.printf(" help             print this help screen\n");
   1.134 +		con.printf(" exit or quit     quit the program\n");
   1.135 +
   1.136  	} else {
   1.137  		con.printf("invalid command: %s\n", argv[0]);
   1.138  	}