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

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