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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*!
  2. * \file src/RunTime.cpp
  3. * \brief run time configuration storage
  4. *
  5. * \author xythobuz
  6. */
  7. #include "imgui/imgui.h"
  8. #include "global.h"
  9. #include "Camera.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[consoleAction] = backquoteKey;
  45. keyBindings[forwardAction] = wKey;
  46. keyBindings[backwardAction] = sKey;
  47. keyBindings[leftAction] = aKey;
  48. keyBindings[rightAction] = dKey;
  49. }
  50. KeyboardButton RunTime::getKeyBinding(ActionEvents event) {
  51. assert(event < ActionEventCount);
  52. return keyBindings[event];
  53. }
  54. void RunTime::setKeyBinding(ActionEvents event, KeyboardButton button) {
  55. assert(event < ActionEventCount);
  56. keyBindings[event] = button;
  57. }
  58. void RunTime::updateFPS() {
  59. frameCount++;
  60. frameCount2++;
  61. lastFrameTime = systemTimerGet() - lastTime;
  62. frameTimeSum += lastFrameTime;
  63. frameTimeSum2 += lastFrameTime;
  64. lastTime = systemTimerGet();
  65. if (frameTimeSum >= 200) {
  66. fps = frameCount * (1000 / frameTimeSum);
  67. frameCount = frameTimeSum = 0;
  68. }
  69. if (frameTimeSum2 >= 1000) {
  70. history.push_back(frameCount2);
  71. frameCount2 = frameTimeSum2 = 0;
  72. }
  73. }
  74. void RunTime::display() {
  75. if (ImGui::CollapsingHeader("RunTime Settings")) {
  76. ImGui::Checkbox("Show FPS##runtime", &showFPS);
  77. ImGui::SameLine();
  78. ImGui::Checkbox("Running (!)##runtime", &gameIsRunning);
  79. ImGui::SameLine();
  80. bool sound = Sound::getEnabled();
  81. if (ImGui::Checkbox("Sound##runtime", &sound)) {
  82. Sound::setEnabled(sound);
  83. }
  84. ImGui::SameLine();
  85. bool fullscreen = Window::getFullscreen();
  86. if (ImGui::Checkbox("Fullscreen##runtime", &fullscreen)) {
  87. Window::setFullscreen(fullscreen);
  88. }
  89. float vol = Sound::getVolume();
  90. if (ImGui::InputFloat("Volume##runtime", &vol, 0.0f, 0.0f, 3,
  91. ImGuiInputTextFlags_EnterReturnsTrue)) {
  92. if (vol < 0.0f)
  93. vol = 0.0f;
  94. if (vol > 1.0f)
  95. vol = 1.0f;
  96. Sound::setVolume(vol);
  97. }
  98. int w = Window::getSize().x;
  99. if (ImGui::InputInt("Width##runtime", &w, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  100. if (w < 1)
  101. w = 1;
  102. Window::setSize(glm::i32vec2(w, Window::getSize().y));
  103. }
  104. int h = Window::getSize().y;
  105. if (ImGui::InputInt("Height##runtime", &h, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  106. if (h < 1)
  107. h = 1;
  108. Window::setSize(glm::i32vec2(Window::getSize().x, h));
  109. }
  110. static int fr = 0;
  111. char buff[1024];
  112. strncpy(buff, getBaseDir().c_str(), 1024);
  113. if (ImGui::InputText("BaseDir##runtime", buff, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  114. setBaseDir(buff);
  115. fr = getFPS();
  116. }
  117. if (fr > 0) {
  118. ImGui::SameLine();
  119. ImGui::Text("Done!##runtime1");
  120. fr--;
  121. }
  122. static int fr2 = 0;
  123. char buff2[1024];
  124. strncpy(buff2, getPakDir().c_str(), 1024);
  125. if (ImGui::InputText("PakDir##runtime", buff2, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  126. setPakDir(buff2);
  127. fr2 = getFPS();
  128. }
  129. if (fr2 > 0) {
  130. ImGui::SameLine();
  131. ImGui::Text("Done!##runtime2");
  132. fr2--;
  133. }
  134. static int fr3 = 0;
  135. char buff3[1024];
  136. strncpy(buff3, getAudioDir().c_str(), 1024);
  137. if (ImGui::InputText("AudioDir##runtime", buff3, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  138. setAudioDir(buff3);
  139. fr3 = getFPS();
  140. }
  141. if (fr3 > 0) {
  142. ImGui::SameLine();
  143. ImGui::Text("Done!##runtime3");
  144. fr3--;
  145. }
  146. static int fr4 = 0;
  147. char buff4[1024];
  148. strncpy(buff4, getDataDir().c_str(), 1024);
  149. if (ImGui::InputText("DataDir##runtime", buff4, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  150. setDataDir(buff4);
  151. fr4 = getFPS();
  152. }
  153. if (fr4 > 0) {
  154. ImGui::SameLine();
  155. ImGui::Text("Done!##runtime4");
  156. fr4--;
  157. }
  158. }
  159. if (ImGui::CollapsingHeader("Performance")) {
  160. ImGui::Text("Uptime: %lums", systemTimerGet());
  161. ImGui::Text("Frames per Second: %luFPS", fps);
  162. if (history.size() > 1) {
  163. static bool scroll = true;
  164. if (scroll) {
  165. int offset = history.size() - 1;
  166. if (offset > 10)
  167. offset = 10;
  168. ImGui::PlotLines("FPS", &history[1],
  169. history.size() - 1,
  170. history.size() - offset - 1);
  171. } else {
  172. ImGui::PlotLines("FPS", &history[1],
  173. history.size() - 1);
  174. }
  175. ImGui::SameLine();
  176. ImGui::Checkbox("Scroll##fpsscroll", &scroll);
  177. }
  178. }
  179. }