OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
RunTime.cpp
Go to the documentation of this file.
1 
8 #include "imgui/imgui.h"
9 
10 #include "global.h"
11 #include "system/Sound.h"
12 #include "system/Window.h"
13 #include "utils/strings.h"
14 #include "utils/time.h"
15 #include "RunTime.h"
16 
17 std::string RunTime::baseDir;
18 std::string RunTime::pakDir;
19 std::string RunTime::audioDir;
20 std::string RunTime::dataDir;
22 bool RunTime::gameIsRunning = false;
23 bool RunTime::showFPS = false;
24 unsigned long RunTime::lastTime = 0;
25 unsigned long RunTime::lastFrameTime = 0;
26 unsigned long RunTime::frameCount = 0;
27 unsigned long RunTime::frameCount2 = 0;
28 unsigned long RunTime::frameTimeSum = 0;
29 unsigned long RunTime::frameTimeSum2 = 0;
30 unsigned long RunTime::fps = 0;
31 std::vector<float> RunTime::history;
32 
34  baseDir = expandHomeDirectory("~/.OpenRaider");
35  pakDir = baseDir + "/paks";
36  audioDir = baseDir + "/music";
37  dataDir = baseDir + "/data";
38 
39 #ifdef DEBUG
40  showFPS = true;
41 #endif
42 
43  for (int i = 0; i < ActionEventCount; i++)
45 
46  // Default key bindings
54 }
55 
58  return keyBindings[event];
59 }
60 
63  keyBindings[event] = button;
64 }
65 
67  frameCount++;
68  frameCount2++;
69 
74 
75  if (frameTimeSum >= 200) {
76  fps = frameCount * (1000 / frameTimeSum);
78  }
79 
80  if (frameTimeSum2 >= 1000) {
81  history.push_back(frameCount2);
83  }
84 }
85 
87  if (ImGui::CollapsingHeader("RunTime Settings")) {
88  ImGui::Checkbox("Show FPS##runtime", &showFPS);
90  ImGui::Checkbox("Running (!)##runtime", &gameIsRunning);
92  bool sound = Sound::getEnabled();
93  if (ImGui::Checkbox("Sound##runtime", &sound)) {
94  Sound::setEnabled(sound);
95  }
97  bool fullscreen = Window::getFullscreen();
98  if (ImGui::Checkbox("Fullscreen##runtime", &fullscreen)) {
99  Window::setFullscreen(fullscreen);
100  }
101 
102  float vol = Sound::getVolume();
103  if (ImGui::SliderFloat("Volume##runtime", &vol, 0.0f, 1.0f)) {
104  Sound::setVolume(vol);
105  }
106 
107  int w = Window::getSize().x;
108  if (ImGui::InputInt("Width##runtime", &w, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
109  if (w < 1)
110  w = 1;
111  Window::setSize(glm::i32vec2(w, Window::getSize().y));
112  }
113  int h = Window::getSize().y;
114  if (ImGui::InputInt("Height##runtime", &h, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
115  if (h < 1)
116  h = 1;
117  Window::setSize(glm::i32vec2(Window::getSize().x, h));
118  }
119 
120  static int fr = 0;
121  char buff[1024];
122  strncpy(buff, getBaseDir().c_str(), 1024);
123  if (ImGui::InputText("BaseDir##runtime", buff, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
124  setBaseDir(buff);
125  fr = getFPS();
126  }
127  if (fr > 0) {
128  ImGui::SameLine();
129  ImGui::Text("Done!##runtime1");
130  fr--;
131  }
132 
133  static int fr2 = 0;
134  char buff2[1024];
135  strncpy(buff2, getPakDir().c_str(), 1024);
136  if (ImGui::InputText("PakDir##runtime", buff2, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
137  setPakDir(buff2);
138  fr2 = getFPS();
139  }
140  if (fr2 > 0) {
141  ImGui::SameLine();
142  ImGui::Text("Done!##runtime2");
143  fr2--;
144  }
145 
146  static int fr3 = 0;
147  char buff3[1024];
148  strncpy(buff3, getAudioDir().c_str(), 1024);
149  if (ImGui::InputText("AudioDir##runtime", buff3, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
150  setAudioDir(buff3);
151  fr3 = getFPS();
152  }
153  if (fr3 > 0) {
154  ImGui::SameLine();
155  ImGui::Text("Done!##runtime3");
156  fr3--;
157  }
158 
159  static int fr4 = 0;
160  char buff4[1024];
161  strncpy(buff4, getDataDir().c_str(), 1024);
162  if (ImGui::InputText("DataDir##runtime", buff4, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
163  setDataDir(buff4);
164  fr4 = getFPS();
165  }
166  if (fr4 > 0) {
167  ImGui::SameLine();
168  ImGui::Text("Done!##runtime4");
169  fr4--;
170  }
171  }
172 
173  if (ImGui::CollapsingHeader("Performance")) {
174  ImGui::Text("Uptime: %lums", systemTimerGet());
175  ImGui::Text("Frames per Second: %luFPS", fps);
176  int size = history.size() - 1;
177  if (size > 0) {
178  static bool scroll = false;
179  if (scroll) {
180  int start = 0;
181  if (size > 10)
182  start = size - 11;
183  ImGui::PlotLines("FPS", &history[1 + start], size - start);
184  } else {
185  ImGui::PlotLines("FPS", &history[1], size);
186  }
187  ImGui::SameLine();
188  ImGui::Checkbox("Scroll##fpsscroll", &scroll);
189  }
190  }
191 }
192 
static std::string getAudioDir()
Definition: RunTime.h:29
static std::string audioDir
Definition: RunTime.h:48
static unsigned long fps
Definition: RunTime.h:58
static bool showFPS
Definition: RunTime.h:53
static unsigned long frameTimeSum
Definition: RunTime.h:57
static float getVolume()
Definition: Sound.cpp:119
static std::vector< float > history
Definition: RunTime.h:59
IMGUI_API bool Checkbox(const char *label, bool *v)
Definition: imgui.cpp:5128
IMGUI_API bool CollapsingHeader(const char *label, const char *str_id=NULL, bool display_frame=true, bool default_open=false)
Definition: imgui.cpp:4332
String handling utilities.
Definition: global.h:43
static std::string pakDir
Definition: RunTime.h:47
Time handling utilities.
static std::string getPakDir()
Definition: RunTime.h:26
static bool getFullscreen()
Definition: Window.cpp:91
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *display_format="%.3f", float power=1.0f)
Definition: imgui.cpp:4654
static KeyboardButton keyBindings[ActionEventCount]
Definition: RunTime.h:51
KeyboardButton
Definition: global.h:34
static unsigned long lastTime
Definition: RunTime.h:55
static void setPakDir(std::string dir)
Definition: RunTime.h:27
Included everywhere.
static std::string getBaseDir()
Definition: RunTime.h:23
static void setFullscreen(bool f)
Definition: Window.cpp:83
static void setDataDir(std::string dir)
Definition: RunTime.h:33
static unsigned long getFPS()
Definition: RunTime.h:41
Definition: global.h:39
Windowing Interface.
static std::string dataDir
Definition: RunTime.h:49
static void setBaseDir(std::string dir)
Definition: RunTime.h:24
IMGUI_API bool InputText(const char *label, char *buf, size_t buf_size, ImGuiInputTextFlags flags=0, ImGuiTextEditCallback callback=NULL, void *user_data=NULL)
Definition: imgui.cpp:5577
ActionEvents
Definition: global.h:15
static unsigned long frameTimeSum2
Definition: RunTime.h:57
Definition: global.h:45
static unsigned long frameCount
Definition: RunTime.h:56
static void setEnabled(bool on=true)
Definition: Sound.cpp:99
static void initialize()
Definition: RunTime.cpp:33
static void setKeyBinding(ActionEvents event, KeyboardButton button)
Definition: RunTime.cpp:61
std::string expandHomeDirectory(std::string s)
Definition: strings.cpp:24
static void setAudioDir(std::string dir)
Definition: RunTime.h:30
Runtime Configuration Storage.
static std::string baseDir
Definition: RunTime.h:46
IMGUI_API void SameLine(int column_x=0, int spacing_w=-1)
Definition: imgui.cpp:6551
static void display()
Definition: RunTime.cpp:86
static KeyboardButton getKeyBinding(ActionEvents event)
Definition: RunTime.cpp:56
static bool getEnabled()
Definition: Sound.cpp:105
static void setVolume(float vol=1.0f)
Definition: Sound.cpp:113
IMGUI_API void Text(const char *fmt,...)
Definition: imgui.cpp:3802
#define assertLessThan(x, y)
Definition: global.h:146
static unsigned long lastFrameTime
Definition: RunTime.h:55
static unsigned long frameCount2
Definition: RunTime.h:56
static bool gameIsRunning
Definition: RunTime.h:52
IMGUI_API void PlotLines(const char *label, const float *values, int values_count, int values_offset=0, const char *overlay_text=NULL, float scale_min=FLT_MAX, float scale_max=FLT_MAX, ImVec2 graph_size=ImVec2(0, 0), size_t stride=sizeof(float))
Definition: imgui.cpp:5106
static void setSize(glm::i32vec2 s)
Definition: Window.cpp:59
IMGUI_API bool InputInt(const char *label, int *v, int step=1, int step_fast=100, ImGuiInputTextFlags extra_flags=0)
Definition: imgui.cpp:5479
Definition: global.h:40
Sound Interface.
Definition: global.h:44
or_time_t systemTimerGet()
Read the system timer.
Definition: time.cpp:15
static glm::i32vec2 getSize()
Definition: Window.cpp:71
static void updateFPS()
Definition: RunTime.cpp:66
static std::string getDataDir()
Definition: RunTime.h:32