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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "system/Sound.h"
  10. #include "system/Window.h"
  11. #include "utils/strings.h"
  12. #include "utils/time.h"
  13. #include "RunTime.h"
  14. std::string RunTime::baseDir;
  15. std::string RunTime::pakDir;
  16. std::string RunTime::audioDir;
  17. std::string RunTime::dataDir;
  18. KeyboardButton RunTime::keyBindings[ActionEventCount];
  19. bool RunTime::gameIsRunning = false;
  20. bool RunTime::showFPS = false;
  21. unsigned long RunTime::lastTime = 0;
  22. unsigned long RunTime::lastFrameTime = 0;
  23. unsigned long RunTime::frameCount = 0;
  24. unsigned long RunTime::frameCount2 = 0;
  25. unsigned long RunTime::frameTimeSum = 0;
  26. unsigned long RunTime::frameTimeSum2 = 0;
  27. unsigned long RunTime::fps = 0;
  28. std::vector<float> RunTime::history;
  29. void RunTime::initialize() {
  30. baseDir = expandHomeDirectory("~/.OpenRaider");
  31. pakDir = baseDir + "/paks";
  32. audioDir = baseDir + "/music";
  33. dataDir = baseDir + "/data";
  34. #ifdef DEBUG
  35. showFPS = true;
  36. #endif
  37. for (int i = 0; i < ActionEventCount; i++)
  38. keyBindings[i] = unknownKey;
  39. // Default key bindings
  40. keyBindings[menuAction] = escapeKey;
  41. keyBindings[debugAction] = qKey;
  42. keyBindings[consoleAction] = backquoteKey;
  43. keyBindings[forwardAction] = wKey;
  44. keyBindings[backwardAction] = sKey;
  45. keyBindings[leftAction] = aKey;
  46. keyBindings[rightAction] = dKey;
  47. }
  48. KeyboardButton RunTime::getKeyBinding(ActionEvents event) {
  49. assertLessThan(event, ActionEventCount);
  50. return keyBindings[event];
  51. }
  52. void RunTime::setKeyBinding(ActionEvents event, KeyboardButton button) {
  53. assertLessThan(event, ActionEventCount);
  54. keyBindings[event] = button;
  55. }
  56. void RunTime::updateFPS() {
  57. frameCount++;
  58. frameCount2++;
  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. }
  72. void RunTime::display() {
  73. if (ImGui::CollapsingHeader("RunTime Settings")) {
  74. ImGui::Checkbox("Show FPS##runtime", &showFPS);
  75. ImGui::SameLine();
  76. ImGui::Checkbox("Running (!)##runtime", &gameIsRunning);
  77. ImGui::SameLine();
  78. bool sound = Sound::getEnabled();
  79. if (ImGui::Checkbox("Sound##runtime", &sound)) {
  80. Sound::setEnabled(sound);
  81. }
  82. ImGui::SameLine();
  83. bool fullscreen = Window::getFullscreen();
  84. if (ImGui::Checkbox("Fullscreen##runtime", &fullscreen)) {
  85. Window::setFullscreen(fullscreen);
  86. }
  87. float vol = Sound::getVolume();
  88. if (ImGui::InputFloat("Volume##runtime", &vol, 0.0f, 0.0f, 3,
  89. ImGuiInputTextFlags_EnterReturnsTrue)) {
  90. if (vol < 0.0f)
  91. vol = 0.0f;
  92. if (vol > 1.0f)
  93. vol = 1.0f;
  94. Sound::setVolume(vol);
  95. }
  96. int w = Window::getSize().x;
  97. if (ImGui::InputInt("Width##runtime", &w, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  98. if (w < 1)
  99. w = 1;
  100. Window::setSize(glm::i32vec2(w, Window::getSize().y));
  101. }
  102. int h = Window::getSize().y;
  103. if (ImGui::InputInt("Height##runtime", &h, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  104. if (h < 1)
  105. h = 1;
  106. Window::setSize(glm::i32vec2(Window::getSize().x, h));
  107. }
  108. static int fr = 0;
  109. char buff[1024];
  110. strncpy(buff, getBaseDir().c_str(), 1024);
  111. if (ImGui::InputText("BaseDir##runtime", buff, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  112. setBaseDir(buff);
  113. fr = getFPS();
  114. }
  115. if (fr > 0) {
  116. ImGui::SameLine();
  117. ImGui::Text("Done!##runtime1");
  118. fr--;
  119. }
  120. static int fr2 = 0;
  121. char buff2[1024];
  122. strncpy(buff2, getPakDir().c_str(), 1024);
  123. if (ImGui::InputText("PakDir##runtime", buff2, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  124. setPakDir(buff2);
  125. fr2 = getFPS();
  126. }
  127. if (fr2 > 0) {
  128. ImGui::SameLine();
  129. ImGui::Text("Done!##runtime2");
  130. fr2--;
  131. }
  132. static int fr3 = 0;
  133. char buff3[1024];
  134. strncpy(buff3, getAudioDir().c_str(), 1024);
  135. if (ImGui::InputText("AudioDir##runtime", buff3, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  136. setAudioDir(buff3);
  137. fr3 = getFPS();
  138. }
  139. if (fr3 > 0) {
  140. ImGui::SameLine();
  141. ImGui::Text("Done!##runtime3");
  142. fr3--;
  143. }
  144. static int fr4 = 0;
  145. char buff4[1024];
  146. strncpy(buff4, getDataDir().c_str(), 1024);
  147. if (ImGui::InputText("DataDir##runtime", buff4, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  148. setDataDir(buff4);
  149. fr4 = getFPS();
  150. }
  151. if (fr4 > 0) {
  152. ImGui::SameLine();
  153. ImGui::Text("Done!##runtime4");
  154. fr4--;
  155. }
  156. }
  157. if (ImGui::CollapsingHeader("Performance")) {
  158. ImGui::Text("Uptime: %lums", systemTimerGet());
  159. ImGui::Text("Frames per Second: %luFPS", fps);
  160. int size = history.size() - 1;
  161. if (size > 0) {
  162. static bool scroll = false;
  163. if (scroll) {
  164. int start = 0;
  165. if (size > 10)
  166. start = size - 11;
  167. ImGui::PlotLines("FPS", &history[1 + start], size - start);
  168. } else {
  169. ImGui::PlotLines("FPS", &history[1], size);
  170. }
  171. ImGui::SameLine();
  172. ImGui::Checkbox("Scroll##fpsscroll", &scroll);
  173. }
  174. }
  175. }