vrheights

view src/game_var.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
children 3f221bdc9bab
line source
1 #include <map>
2 #include "game_var.h"
4 static std::map<std::string, GameVariable> gvars;
6 GameVariable::GameVariable()
7 {
8 num_val = 0.0f;
9 bool_val = false;
10 }
12 std::string GameVariable::to_str() const
13 {
14 char buf[128];
16 switch(type) {
17 case GameVariable::STR:
18 return val;
19 case GameVariable::BOOL:
20 return bool_val ? "true" : "false";
21 case GameVariable::NUMBER:
22 sprintf(buf, "%g", num_val);
23 return buf;
24 }
25 return "<unknown>";
26 }
28 void set_gvar(const GameVariable &var)
29 {
30 gvars[var.name] = var;
31 }
33 void set_gvar_str(const char *name, const char *val)
34 {
35 GameVariable v;
36 v.name = name;
37 v.val = val;
38 v.type = GameVariable::STR;
39 set_gvar(v);
40 }
42 void set_gvar_num(const char *name, float val)
43 {
44 GameVariable v;
45 v.name = name;
46 v.num_val = val;
47 v.type = GameVariable::NUMBER;
48 set_gvar(v);
49 }
51 void set_gvar_bool(const char *name, bool val)
52 {
53 GameVariable v;
54 v.name = name;
55 v.bool_val = val;
56 v.type = GameVariable::BOOL;
57 set_gvar(v);
58 }
60 static char *strip_whitespace(char *s)
61 {
62 while(*s && isspace(*s)) ++s;
63 if(*s == 0) return s;
65 char *endp = s + strlen(s) - 1;
66 while(endp > s && isspace(*endp)) {
67 *endp-- = 0;
68 }
69 return s;
70 }
72 static int strccmp(const char *a, const char *b)
73 {
74 while(*a && *b) {
75 int diff = tolower(*a) - tolower(*b);
76 if(diff != 0) return diff;
77 ++a;
78 ++b;
79 }
80 if(*a == 0 && *b == 0) return 0;
81 return *a == 0 ? -1 : 1;
82 }
84 void set_gvar_parse(const char *name, const char *val)
85 {
86 char *endp, *valstr = (char*)alloca(strlen(val) + 1);
87 strcpy(valstr, val);
88 valstr = strip_whitespace(valstr);
90 float fval = strtod(valstr, &endp);
91 if(endp != valstr && *endp == 0) {
92 set_gvar_num(name, fval);
93 return;
94 }
96 if(strccmp(valstr, "true") == 0 || strccmp(valstr, "yes") == 0) {
97 set_gvar_bool(name, true);
98 return;
99 }
100 if(strccmp(valstr, "false") == 0 || strccmp(valstr, "no") == 0) {
101 set_gvar_bool(name, false);
102 return;
103 }
104 return set_gvar_str(name, val);
105 }
107 GameVariable &get_gvar(const char *name)
108 {
109 return gvars[name];
110 }
112 const char *get_gvar_str(const char *name)
113 {
114 return gvars[name].val.c_str();
115 }
117 float get_gvar_num(const char *name)
118 {
119 return gvars[name].num_val;
120 }
122 bool get_gvar_bool(const char *name)
123 {
124 return gvars[name].bool_val;
125 }
127 bool have_gvar(const char *name)
128 {
129 return gvars.find(std::string(name)) != gvars.end();
130 }
132 std::list<std::string> get_gvar_list()
133 {
134 std::list<std::string> res;
136 std::map<std::string, GameVariable>::const_iterator it = gvars.begin();
137 while(it != gvars.end()) {
138 res.push_back((it++)->first);
139 }
140 return res;