OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
CommandSet.cpp
Go to the documentation of this file.
1 
8 #include "global.h"
9 #include "Camera.h"
10 #include "Log.h"
11 #include "RunTime.h"
12 #include "system/Font.h"
13 #include "system/Sound.h"
14 #include "system/Window.h"
15 #include "utils/strings.h"
16 #include "commands/CommandSet.h"
17 
18 std::string CommandSet::name() {
19  return "set";
20 }
21 
22 std::string CommandSet::brief() {
23  return "set a parameter";
24 }
25 
27  Log::get(LOG_USER) << "set-Command Usage:" << Log::endl;
28  Log::get(LOG_USER) << " set VAR VAL" << Log::endl;
29  Log::get(LOG_USER) << "Available Variables:" << Log::endl;
30  Log::get(LOG_USER) << " basedir STRING" << Log::endl;
31  Log::get(LOG_USER) << " pakdir STRING" << Log::endl;
32  Log::get(LOG_USER) << " audiodir STRING" << Log::endl;
33  Log::get(LOG_USER) << " datadir STRING" << Log::endl;
34  Log::get(LOG_USER) << " font STRING" << Log::endl;
35  Log::get(LOG_USER) << " size INT INT" << Log::endl;
36  Log::get(LOG_USER) << " fullscreen BOOL" << Log::endl;
37  Log::get(LOG_USER) << " audio BOOL" << Log::endl;
38  Log::get(LOG_USER) << " volume BOOL" << Log::endl;
39  Log::get(LOG_USER) << " mouse_x FLOAT" << Log::endl;
40  Log::get(LOG_USER) << " mouse_y FLOAT" << Log::endl;
41  Log::get(LOG_USER) << " fps BOOL" << Log::endl;
42  Log::get(LOG_USER) << "Enclose STRINGs with \"\"!" << Log::endl;
43 }
44 
45 static std::string expandNames(std::string s) {
46  // Remove quotes
47  if ((s.length() >= 3) &&
48  (((s[0] == '"') && (s[s.length() - 1] == '"'))
49  || ((s[0] == '\'') && (s[s.length() - 1] == '\'')))) {
50  s.erase(0, 1);
51  s.erase(s.length() - 1, 1);
52  }
53 
54  // Expand Names
55  s = findAndReplace(s, "$(pakdir)", RunTime::getPakDir());
56  s = findAndReplace(s, "$(audiodir)", RunTime::getAudioDir());
57  s = findAndReplace(s, "$(datadir)", RunTime::getDataDir());
58  s = findAndReplace(s, "$(basedir)", RunTime::getBaseDir());
59 
60  // Full path
61  s = expandHomeDirectory(s);
62 
63  return s;
64 }
65 
66 int CommandSet::execute(std::istream& args) {
67  std::string var;
68  args >> var;
69 
70  if (var.compare("size") == 0) {
71  unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
72  if (!(args >> w >> h)) {
73  Log::get(LOG_USER) << "set-size-Error: Invalid value(s)" << Log::endl;
74  return -2;
75  }
76  Window::setSize(glm::i32vec2(w, h));
77  } else if (var.compare("fullscreen") == 0) {
78  bool fullscreen = false;
79  if (!(args >> fullscreen)) {
80  Log::get(LOG_USER) << "set-fullscreen-Error: Invalid value" << Log::endl;
81  return -3;
82  }
83  Window::setFullscreen(fullscreen);
84  } else if (var.compare("audio") == 0) {
85  bool audio = false;
86  if (!(args >> audio)) {
87  Log::get(LOG_USER) << "set-audio-Error: Invalid value" << Log::endl;
88  return -4;
89  }
90  Sound::setEnabled(audio);
91  } else if (var.compare("volume") == 0) {
92  float vol = 1.0f;
93  if (!(args >> vol)) {
94  Log::get(LOG_USER) << "set-volume-Error: Invalid value" << Log::endl;
95  return -5;
96  }
97  Sound::setVolume(vol);
98  } else if (var.compare("mouse_x") == 0) {
99  float sense = 1.0f;
100  if (!(args >> sense)) {
101  Log::get(LOG_USER) << "set-mouse_x-Error: Invalid value" << Log::endl;
102  return -6;
103  }
104  Camera::setSensitivityX(glm::radians(sense));
105  } else if (var.compare("mouse_y") == 0) {
106  float sense = 1.0f;
107  if (!(args >> sense)) {
108  Log::get(LOG_USER) << "set-mouse_y-Error: Invalid value" << Log::endl;
109  return -7;
110  }
111  Camera::setSensitivityY(glm::radians(sense));
112  } else if (var.compare("fps") == 0) {
113  bool fps = false;
114  if (!(args >> fps)) {
115  Log::get(LOG_USER) << "set-fps-Error: Invalid value" << Log::endl;
116  return -8;
117  }
118  RunTime::setShowFPS(fps);
119  } else if (var.compare("basedir") == 0) {
120  std::string temp;
121  args >> temp;
123  } else if (var.compare("pakdir") == 0) {
124  std::string temp;
125  args >> temp;
127  } else if (var.compare("audiodir") == 0) {
128  std::string temp;
129  args >> temp;
131  } else if (var.compare("datadir") == 0) {
132  std::string temp;
133  args >> temp;
135  } else if (var.compare("font") == 0) {
136  std::string temp;
137  args >> temp;
138  int error = Font::initialize(expandNames(temp));
139  if (error != 0)
140  Log::get(LOG_USER) << "Error initializing font: " << expandNames(temp) << "(" << error << ")" <<
141  Log::endl;
142  } else {
143  Log::get(LOG_USER) << "set-Error: Unknown variable (" << var.c_str() << ")" << Log::endl;
144  return -1;
145  }
146 
147  return 0;
148 }
149 
150 std::string CommandGet::name() {
151  return "get";
152 }
153 
154 std::string CommandGet::brief() {
155  return "get a parameter";
156 }
157 
159  Log::get(LOG_USER) << "get-Command Usage:" << Log::endl;
160  Log::get(LOG_USER) << " get VAR" << Log::endl;
161  Log::get(LOG_USER) << "Available Variables:" << Log::endl;
162  Log::get(LOG_USER) << " basedir" << Log::endl;
163  Log::get(LOG_USER) << " pakdir" << Log::endl;
164  Log::get(LOG_USER) << " audiodir" << Log::endl;
165  Log::get(LOG_USER) << " datadir" << Log::endl;
166  Log::get(LOG_USER) << " font" << Log::endl;
167  Log::get(LOG_USER) << " size" << Log::endl;
168  Log::get(LOG_USER) << " fullscreen" << Log::endl;
169  Log::get(LOG_USER) << " audio" << Log::endl;
170  Log::get(LOG_USER) << " volume" << Log::endl;
171  Log::get(LOG_USER) << " mouse_x" << Log::endl;
172  Log::get(LOG_USER) << " mouse_y" << Log::endl;
173  Log::get(LOG_USER) << " fps" << Log::endl;
174 }
175 
176 int CommandGet::execute(std::istream& args) {
177  std::string var;
178  args >> var;
179 
180  if (var.compare("size") == 0) {
182  } else if (var.compare("fullscreen") == 0) {
184  } else if (var.compare("audio") == 0) {
186  } else if (var.compare("volume") == 0) {
188  } else if (var.compare("mouse_x") == 0) {
189  Log::get(LOG_USER) << glm::degrees(Camera::getSensitivityX()) << Log::endl;
190  } else if (var.compare("mouse_y") == 0) {
191  Log::get(LOG_USER) << glm::degrees(Camera::getSensitivityY()) << Log::endl;
192  } else if (var.compare("fps") == 0) {
194  } else if (var.compare("basedir") == 0) {
196  } else if (var.compare("pakdir") == 0) {
198  } else if (var.compare("audiodir") == 0) {
200  } else if (var.compare("datadir") == 0) {
202  } else if (var.compare("font") == 0) {
204  } else {
205  Log::get(LOG_USER) << "get-Error: Unknown variable (" << var << ")" << Log::endl;
206  return -1;
207  }
208 
209  return 0;
210 }
211 
static std::string getAudioDir()
Definition: RunTime.h:29
static std::string expandNames(std::string s)
Definition: CommandSet.cpp:45
Get/Set Commands.
static float getVolume()
Definition: Sound.cpp:119
#define LOG_USER
Definition: Log.h:18
String handling utilities.
virtual int execute(std::istream &args)
Definition: CommandSet.cpp:176
static std::string getPakDir()
Definition: RunTime.h:26
static bool getFullscreen()
Definition: Window.cpp:91
static void setSensitivityX(float sens)
Definition: Camera.h:33
static float getSensitivityX()
Definition: Camera.h:34
virtual std::string brief()
Definition: CommandSet.cpp:154
static void setShowFPS(bool f)
Definition: RunTime.h:39
static std::string getFontName()
Definition: Font.h:22
virtual void printHelp()
Definition: CommandSet.cpp:158
static void setPakDir(std::string dir)
Definition: RunTime.h:27
Included everywhere.
static std::string getBaseDir()
Definition: RunTime.h:23
static LogLevel & get(int level)
Definition: Log.cpp:14
static void setFullscreen(bool f)
Definition: Window.cpp:83
static void setDataDir(std::string dir)
Definition: RunTime.h:33
static int initialize(std::string font="")
Definition: Font.cpp:28
static void setSensitivityY(float sens)
Definition: Camera.h:36
Global Logging Utility.
static float getSensitivityY()
Definition: Camera.h:37
Windowing Interface.
static void setBaseDir(std::string dir)
Definition: RunTime.h:24
static void error(char *msg)
Definition: commander.c:19
static void setEnabled(bool on=true)
Definition: Sound.cpp:99
virtual std::string brief()
Definition: CommandSet.cpp:22
virtual std::string name()
Definition: CommandSet.cpp:150
virtual void printHelp()
Definition: CommandSet.cpp:26
std::string expandHomeDirectory(std::string s)
Definition: strings.cpp:24
Font Interface.
static void setAudioDir(std::string dir)
Definition: RunTime.h:30
static const char endl
Definition: Log.h:35
Runtime Configuration Storage.
static bool getEnabled()
Definition: Sound.cpp:105
virtual int execute(std::istream &args)
Definition: CommandSet.cpp:66
std::string findAndReplace(std::string s, std::string find, std::string replace)
Definition: strings.cpp:14
static void setVolume(float vol=1.0f)
Definition: Sound.cpp:113
Camera, View Frustum.
virtual std::string name()
Definition: CommandSet.cpp:18
static bool getShowFPS()
Definition: RunTime.h:38
static void setSize(glm::i32vec2 s)
Definition: Window.cpp:59
Sound Interface.
static glm::i32vec2 getSize()
Definition: Window.cpp:71
static std::string getDataDir()
Definition: RunTime.h:32