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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*!
  2. * \file src/RunTime.cpp
  3. * \brief Runtime 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. unsigned long RunTime::glCallCount = 0;
  30. void RunTime::initialize() {
  31. baseDir = expandHomeDirectory("~/.OpenRaider");
  32. pakDir = baseDir + "/paks";
  33. audioDir = baseDir + "/music";
  34. dataDir = baseDir + "/data";
  35. #ifdef DEBUG
  36. showFPS = true;
  37. #endif
  38. for (int i = 0; i < ActionEventCount; i++)
  39. keyBindings[i] = unknownKey;
  40. // Default key bindings
  41. keyBindings[menuAction] = escapeKey;
  42. keyBindings[debugAction] = qKey;
  43. keyBindings[consoleAction] = backquoteKey;
  44. keyBindings[forwardAction] = wKey;
  45. keyBindings[backwardAction] = sKey;
  46. keyBindings[leftAction] = aKey;
  47. keyBindings[rightAction] = dKey;
  48. }
  49. KeyboardButton RunTime::getKeyBinding(ActionEvents event) {
  50. orAssertLessThan(event, ActionEventCount);
  51. return keyBindings[event];
  52. }
  53. void RunTime::setKeyBinding(ActionEvents event, KeyboardButton button) {
  54. orAssertLessThan(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::SliderFloat("Volume##runtime", &vol, 0.0f, 1.0f)) {
  90. Sound::setVolume(vol);
  91. }
  92. int w = Window::getSize().x;
  93. if (ImGui::InputInt("Width##runtime", &w, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  94. if (w < 1)
  95. w = 1;
  96. Window::setSize(glm::i32vec2(w, Window::getSize().y));
  97. }
  98. int h = Window::getSize().y;
  99. if (ImGui::InputInt("Height##runtime", &h, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  100. if (h < 1)
  101. h = 1;
  102. Window::setSize(glm::i32vec2(Window::getSize().x, h));
  103. }
  104. static int fr = 0;
  105. char buff[1024];
  106. strncpy(buff, getBaseDir().c_str(), 1024);
  107. if (ImGui::InputText("BaseDir##runtime", buff, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  108. setBaseDir(buff);
  109. fr = getFPS();
  110. }
  111. if (fr > 0) {
  112. ImGui::SameLine();
  113. ImGui::Text("Done!##runtime1");
  114. fr--;
  115. }
  116. static int fr2 = 0;
  117. char buff2[1024];
  118. strncpy(buff2, getPakDir().c_str(), 1024);
  119. if (ImGui::InputText("PakDir##runtime", buff2, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  120. setPakDir(buff2);
  121. fr2 = getFPS();
  122. }
  123. if (fr2 > 0) {
  124. ImGui::SameLine();
  125. ImGui::Text("Done!##runtime2");
  126. fr2--;
  127. }
  128. static int fr3 = 0;
  129. char buff3[1024];
  130. strncpy(buff3, getAudioDir().c_str(), 1024);
  131. if (ImGui::InputText("AudioDir##runtime", buff3, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  132. setAudioDir(buff3);
  133. fr3 = getFPS();
  134. }
  135. if (fr3 > 0) {
  136. ImGui::SameLine();
  137. ImGui::Text("Done!##runtime3");
  138. fr3--;
  139. }
  140. static int fr4 = 0;
  141. char buff4[1024];
  142. strncpy(buff4, getDataDir().c_str(), 1024);
  143. if (ImGui::InputText("DataDir##runtime", buff4, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  144. setDataDir(buff4);
  145. fr4 = getFPS();
  146. }
  147. if (fr4 > 0) {
  148. ImGui::SameLine();
  149. ImGui::Text("Done!##runtime4");
  150. fr4--;
  151. }
  152. }
  153. if (ImGui::CollapsingHeader("Performance")) {
  154. ImGui::Text("Uptime: %lums", systemTimerGet());
  155. ImGui::Text("Frames per Second: %luFPS", fps);
  156. int size = history.size() - 1;
  157. if (size > 0) {
  158. static bool scroll = false;
  159. if (scroll) {
  160. int start = 0;
  161. if (size > 10)
  162. start = size - 11;
  163. ImGui::PlotLines("FPS", &history[1 + start], size - start);
  164. } else {
  165. ImGui::PlotLines("FPS", &history[1], size);
  166. }
  167. ImGui::SameLine();
  168. ImGui::Checkbox("Scroll##fpsscroll", &scroll);
  169. }
  170. }
  171. }