vrheights

annotate src/game_var.cc @ 8:3f221bdc9bab

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