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.

OpenRaider.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*!
  2. * \file src/OpenRaider.cpp
  3. * \brief Main Game Object
  4. *
  5. * \author xythobuz
  6. */
  7. #include <fstream>
  8. #include <sstream>
  9. #include "global.h"
  10. #include "commands/Command.h"
  11. #include "Console.h"
  12. #include "Font.h"
  13. #include "Game.h"
  14. #include "Menu.h"
  15. #include "Render.h"
  16. #include "Sound.h"
  17. #include "TextureManager.h"
  18. #include "TombRaider.h"
  19. #include "utils/strings.h"
  20. #include "utils/time.h"
  21. #include "Window.h"
  22. #include "OpenRaider.h"
  23. OpenRaider::OpenRaider() {
  24. mRunning = false;
  25. mFPS = false;
  26. for (int i = 0; i < ActionEventCount; i++)
  27. keyBindings[i] = unknownKey;
  28. }
  29. std::string OpenRaider::getBaseDir() {
  30. return baseDir;
  31. }
  32. void OpenRaider::setBaseDir(std::string dir) {
  33. baseDir = dir;
  34. }
  35. std::string OpenRaider::getPakDir() {
  36. return pakDir;
  37. }
  38. void OpenRaider::setPakDir(std::string dir) {
  39. pakDir = dir;
  40. }
  41. std::string OpenRaider::getAudioDir() {
  42. return audioDir;
  43. }
  44. void OpenRaider::setAudioDir(std::string dir) {
  45. audioDir = dir;
  46. }
  47. std::string OpenRaider::getDataDir() {
  48. return dataDir;
  49. }
  50. void OpenRaider::setDataDir(std::string dir) {
  51. dataDir = dir;
  52. }
  53. int OpenRaider::loadConfig(std::string config) {
  54. std::string configFile = expandHomeDirectory(config);
  55. getConsole() << "Loading config from \"" << configFile << "\"..." << Console::endl;
  56. std::ifstream file(configFile);
  57. if (!file) {
  58. getConsole() << "Could not open file!" << Console::endl;
  59. return -1;
  60. }
  61. for (std::string line; std::getline(file, line);) {
  62. if (line.length() == 0)
  63. continue;
  64. int error = Command::command(line);
  65. if (error != 0)
  66. getConsole() << "Error Code: " << error << Console::endl;
  67. }
  68. file.close();
  69. return 0;
  70. }
  71. int OpenRaider::initialize() {
  72. // Initialize Windowing
  73. int error = getWindow().initialize();
  74. if (error != 0) {
  75. printf("Could not initialize Window (%d)!\n", error);
  76. return -1;
  77. }
  78. // Initialize OpenGL
  79. error = getWindow().initializeGL();
  80. if (error != 0) {
  81. printf("Could not initialize OpenGL (%d)!\n", error);
  82. return -2;
  83. }
  84. // Initialize Font
  85. error = getFont().initialize();
  86. if (error != 0) {
  87. printf("Could not initialize Font (%d)!\n", error);
  88. return -3;
  89. }
  90. // Initialize Sound
  91. error = getSound().initialize();
  92. if (error != 0) {
  93. printf("Could not initialize Sound (%d)!\n", error);
  94. return -4;
  95. }
  96. // Initialize Texture Manager
  97. error = getTextureManager().initialize();
  98. if (error != 0) {
  99. printf("Could not initialize Textures (%d)!\n", error);
  100. return -5;
  101. }
  102. // Initialize UIs
  103. error = UI::passInitialize();
  104. if (error != 0) {
  105. printf("Could not initialize UIs (%d)!\n", error);
  106. return -6;
  107. }
  108. #ifdef DEBUG
  109. mFPS = true;
  110. #endif
  111. getMenu().moveToTop();
  112. systemTimerReset();
  113. return 0;
  114. }
  115. void OpenRaider::run() {
  116. assert(mRunning == false);
  117. mRunning = true;
  118. while (mRunning)
  119. frame();
  120. }
  121. void OpenRaider::frame() {
  122. assert(mRunning == true);
  123. static clock_t fpsSum = 0, fpsCount = 0;
  124. static int fps = 0;
  125. clock_t startTime = systemTimerGet();
  126. // Get keyboard and mouse input
  127. getWindow().eventHandling();
  128. ImGui::SetNewWindowDefaultPos(ImVec2(50, 50));
  129. bool show_test_window = true;
  130. ImGui::ShowTestWindow(&show_test_window);
  131. UI::passDisplay();
  132. getWindow().glEnter2D();
  133. // Draw FPS counter
  134. if (mFPS) {
  135. std::ostringstream fpsText;
  136. fpsText << fps << "FPS";
  137. getFont().drawText(10, getWindow().getHeight() - 20, 0.5f, BLUE, fpsText.str());
  138. }
  139. #ifdef DEBUG
  140. // Draw debug infos
  141. if (getGame().isLoaded() && (!getMenu().isOnTop())) {
  142. for (int i = 0; i < 3; i++) {
  143. std::ostringstream axis;
  144. axis << getGame().getLara().getPos(i) / 256.0f << " (" << getGame().getLara().getAngle(i) << ")";
  145. getFont().drawText(10, getWindow().getHeight() - ((4 - i) * 20), 0.5f, BLUE, axis.str());
  146. }
  147. }
  148. #endif
  149. getWindow().glExit2D();
  150. // Put new frame on screen
  151. getWindow().swapBuffersGL();
  152. // Calculate FPS display value
  153. fpsCount++;
  154. fpsSum += (systemTimerGet() - startTime);
  155. if (fpsSum >= 250) {
  156. // Update every 250ms
  157. fps = (int)((float)fpsCount * (1000.0f / (float)fpsSum));
  158. fpsCount = fpsSum = 0;
  159. }
  160. }