Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RunTime.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. 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. orAssertLessThan(event, ActionEventCount);
  50. return keyBindings[event];
  51. }
  52. void RunTime::setKeyBinding(ActionEvents event, KeyboardButton button) {
  53. orAssertLessThan(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::SliderFloat("Volume##runtime", &vol, 0.0f, 1.0f)) {
  89. Sound::setVolume(vol);
  90. }
  91. int w = Window::getSize().x;
  92. if (ImGui::InputInt("Width##runtime", &w, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  93. if (w < 1)
  94. w = 1;
  95. Window::setSize(glm::i32vec2(w, Window::getSize().y));
  96. }
  97. int h = Window::getSize().y;
  98. if (ImGui::InputInt("Height##runtime", &h, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
  99. if (h < 1)
  100. h = 1;
  101. Window::setSize(glm::i32vec2(Window::getSize().x, h));
  102. }
  103. static int fr = 0;
  104. char buff[1024];
  105. strncpy(buff, getBaseDir().c_str(), 1024);
  106. if (ImGui::InputText("BaseDir##runtime", buff, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  107. setBaseDir(buff);
  108. fr = getFPS();
  109. }
  110. if (fr > 0) {
  111. ImGui::SameLine();
  112. ImGui::Text("Done!##runtime1");
  113. fr--;
  114. }
  115. static int fr2 = 0;
  116. char buff2[1024];
  117. strncpy(buff2, getPakDir().c_str(), 1024);
  118. if (ImGui::InputText("PakDir##runtime", buff2, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  119. setPakDir(buff2);
  120. fr2 = getFPS();
  121. }
  122. if (fr2 > 0) {
  123. ImGui::SameLine();
  124. ImGui::Text("Done!##runtime2");
  125. fr2--;
  126. }
  127. static int fr3 = 0;
  128. char buff3[1024];
  129. strncpy(buff3, getAudioDir().c_str(), 1024);
  130. if (ImGui::InputText("AudioDir##runtime", buff3, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  131. setAudioDir(buff3);
  132. fr3 = getFPS();
  133. }
  134. if (fr3 > 0) {
  135. ImGui::SameLine();
  136. ImGui::Text("Done!##runtime3");
  137. fr3--;
  138. }
  139. static int fr4 = 0;
  140. char buff4[1024];
  141. strncpy(buff4, getDataDir().c_str(), 1024);
  142. if (ImGui::InputText("DataDir##runtime", buff4, 1024, ImGuiInputTextFlags_EnterReturnsTrue)) {
  143. setDataDir(buff4);
  144. fr4 = getFPS();
  145. }
  146. if (fr4 > 0) {
  147. ImGui::SameLine();
  148. ImGui::Text("Done!##runtime4");
  149. fr4--;
  150. }
  151. }
  152. if (ImGui::CollapsingHeader("Performance")) {
  153. ImGui::Text("Uptime: %lums", systemTimerGet());
  154. ImGui::Text("Frames per Second: %luFPS", fps);
  155. int size = history.size() - 1;
  156. if (size > 0) {
  157. static bool scroll = false;
  158. if (scroll) {
  159. int start = 0;
  160. if (size > 10)
  161. start = size - 11;
  162. ImGui::PlotLines("FPS", &history[1 + start], size - start);
  163. } else {
  164. ImGui::PlotLines("FPS", &history[1], size);
  165. }
  166. ImGui::SameLine();
  167. ImGui::Checkbox("Scroll##fpsscroll", &scroll);
  168. }
  169. }
  170. }