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 5.9KB

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