Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RunTime.cpp 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*!
  2. * \file src/RunTime.cpp
  3. * \brief run time configuration storage
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Camera.h"
  9. #include "Render.h"
  10. #include "UI.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. std::string RunTime::baseDir;
  17. std::string RunTime::pakDir;
  18. std::string RunTime::audioDir;
  19. std::string RunTime::dataDir;
  20. KeyboardButton RunTime::keyBindings[ActionEventCount];
  21. bool RunTime::gameIsRunning = false;
  22. bool RunTime::showFPS = false;
  23. unsigned long RunTime::lastTime = 0;
  24. unsigned long RunTime::lastFrameTime = 0;
  25. unsigned long RunTime::frameCount = 0;
  26. unsigned long RunTime::frameCount2 = 0;
  27. unsigned long RunTime::frameTimeSum = 0;
  28. unsigned long RunTime::frameTimeSum2 = 0;
  29. unsigned long RunTime::fps = 0;
  30. std::vector<float> RunTime::history;
  31. void RunTime::initialize() {
  32. baseDir = expandHomeDirectory("~/.OpenRaider");
  33. pakDir = baseDir + "/paks";
  34. audioDir = baseDir + "/music";
  35. dataDir = baseDir + "/data";
  36. #ifdef DEBUG
  37. showFPS = true;
  38. #endif
  39. for (int i = 0; i < ActionEventCount; i++)
  40. keyBindings[i] = unknownKey;
  41. // Default key bindings
  42. keyBindings[menuAction] = escapeKey;
  43. keyBindings[debugAction] = qKey;
  44. keyBindings[forwardAction] = wKey;
  45. keyBindings[backwardAction] = sKey;
  46. keyBindings[leftAction] = aKey;
  47. keyBindings[rightAction] = dKey;
  48. }
  49. KeyboardButton RunTime::getKeyBinding(ActionEvents event) {
  50. assert(event < ActionEventCount);
  51. return keyBindings[event];
  52. }
  53. void RunTime::setKeyBinding(ActionEvents event, KeyboardButton button) {
  54. assert(event < ActionEventCount);
  55. keyBindings[event] = button;
  56. }
  57. void RunTime::updateFPS() {
  58. frameCount++;
  59. lastFrameTime = systemTimerGet() - lastTime;
  60. frameTimeSum += lastFrameTime;
  61. frameTimeSum2 += lastFrameTime;
  62. lastTime = systemTimerGet();
  63. if (frameTimeSum >= 200) {
  64. fps = frameCount * (1000 / frameTimeSum);
  65. frameCount = frameTimeSum = 0;
  66. }
  67. if (frameTimeSum2 >= 1000) {
  68. history.push_back(frameCount2);
  69. frameCount2 = frameTimeSum2 = 0;
  70. }
  71. frameCount2++;
  72. }
  73. void RunTime::display() {
  74. if (ImGui::CollapsingHeader("RunTime Settings")) {
  75. bool showFPS = getShowFPS();
  76. if (ImGui::Checkbox("Show FPS##runtime", &showFPS)) {
  77. setShowFPS(showFPS);
  78. }
  79. ImGui::SameLine();
  80. bool running = isRunning();
  81. if (ImGui::Checkbox("Running (!)##runtime", &running)) {
  82. setRunning(running);
  83. }
  84. ImGui::SameLine();
  85. bool sound = Sound::getEnabled();
  86. if (ImGui::Checkbox("Sound##runtime", &sound)) {
  87. Sound::setEnabled(sound);
  88. }
  89. ImGui::SameLine();
  90. bool fullscreen = Window::getFullscreen();
  91. if (ImGui::Checkbox("Fullscreen##runtime", &fullscreen)) {
  92. Window::setFullscreen(fullscreen);
  93. }
  94. bool updateViewFrustum = Camera::getUpdateViewFrustum();
  95. if (ImGui::Checkbox("Update Frustum##runtime", &updateViewFrustum)) {
  96. Camera::setUpdateViewFrustum(updateViewFrustum);
  97. }
  98. ImGui::SameLine();
  99. bool displayViewFrustum = Render::getDisplayViewFrustum();
  100. if (ImGui::Checkbox("Show Frustum##runtime", &displayViewFrustum)) {
  101. Render::setDisplayViewFrustum(displayViewFrustum);
  102. }
  103. float vol = Sound::getVolume();
  104. if (ImGui::InputFloat("Volume##runtime", &vol, 0.0f, 0.0f, 3,
  105. ImGuiInputTextFlags_EnterReturnsTrue)) {
  106. if (vol < 0.0f)
  107. vol = 0.0f;
  108. if (vol > 1.0f)
  109. vol = 1.0f;
  110. Sound::setVolume(vol);
  111. }
  112. int w = Window::getSize().x;
  113. if (ImGui::InputInt("Width##runtime", &w, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  114. if (w < 1)
  115. w = 1;
  116. Window::setSize(glm::vec2(w, Window::getSize().y));
  117. }
  118. int h = Window::getSize().y;
  119. if (ImGui::InputInt("Height##runtime", &h, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  120. if (h < 1)
  121. h = 1;
  122. Window::setSize(glm::vec2(Window::getSize().x, h));
  123. }
  124. static int fr = 0;
  125. char buff[1024];
  126. strncpy(buff, getBaseDir().c_str(), 1024);
  127. if (ImGui::InputText("BaseDir##runtime", buff, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  128. setBaseDir(buff);
  129. fr = getFPS();
  130. }
  131. if (fr > 0) {
  132. ImGui::SameLine();
  133. ImGui::Text("Done!##runtime1");
  134. fr--;
  135. }
  136. static int fr2 = 0;
  137. char buff2[1024];
  138. strncpy(buff2, getPakDir().c_str(), 1024);
  139. if (ImGui::InputText("PakDir##runtime", buff2, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  140. setPakDir(buff2);
  141. fr2 = getFPS();
  142. }
  143. if (fr2 > 0) {
  144. ImGui::SameLine();
  145. ImGui::Text("Done!##runtime2");
  146. fr2--;
  147. }
  148. static int fr3 = 0;
  149. char buff3[1024];
  150. strncpy(buff3, getAudioDir().c_str(), 1024);
  151. if (ImGui::InputText("AudioDir##runtime", buff3, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  152. setAudioDir(buff3);
  153. fr3 = getFPS();
  154. }
  155. if (fr3 > 0) {
  156. ImGui::SameLine();
  157. ImGui::Text("Done!##runtime3");
  158. fr3--;
  159. }
  160. static int fr4 = 0;
  161. char buff4[1024];
  162. strncpy(buff4, getDataDir().c_str(), 1024);
  163. if (ImGui::InputText("DataDir##runtime", buff4, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  164. setDataDir(buff4);
  165. fr4 = getFPS();
  166. }
  167. if (fr4 > 0) {
  168. ImGui::SameLine();
  169. ImGui::Text("Done!##runtime4");
  170. fr4--;
  171. }
  172. }
  173. if (ImGui::CollapsingHeader("Performance")) {
  174. ImGui::Text("Uptime: %lums", systemTimerGet());
  175. ImGui::Text("Frames per Second: %luFPS", getFPS());
  176. if (getHistoryFPS().size() > 1) {
  177. static bool scroll = true;
  178. if (scroll) {
  179. int offset = getHistoryFPS().size() - 1;
  180. if (offset > 10)
  181. offset = 10;
  182. ImGui::PlotLines("FPS", &getHistoryFPS()[1],
  183. getHistoryFPS().size() - 1,
  184. getHistoryFPS().size() - offset - 1);
  185. } else {
  186. ImGui::PlotLines("FPS", &getHistoryFPS()[1],
  187. getHistoryFPS().size() - 1);
  188. }
  189. ImGui::SameLine();
  190. ImGui::Checkbox("Scroll##fpsscroll", &scroll);
  191. }
  192. }
  193. }