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.

OpenRaider.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*!
  2. * \file src/OpenRaider.cpp
  3. * \brief Main Game Object
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdio>
  8. #include <cstring>
  9. #include "global.h"
  10. #include "Console.h"
  11. #include "Game.h"
  12. #include "Menu.h"
  13. #include "Render.h"
  14. #include "Sound.h"
  15. #include "TextureManager.h"
  16. #include "TombRaider.h"
  17. #include "utils/strings.h"
  18. #include "utils/time.h"
  19. #include "Window.h"
  20. #include "OpenRaider.h"
  21. OpenRaider::OpenRaider() {
  22. mRunning = false;
  23. mFPS = false;
  24. mBaseDir = NULL;
  25. mPakDir = NULL;
  26. mAudioDir = NULL;
  27. mDataDir = NULL;
  28. for (int i = 0; i < ActionEventCount; i++)
  29. keyBindings[i] = unknownKey;
  30. }
  31. OpenRaider::~OpenRaider() {
  32. delete mBaseDir;
  33. mBaseDir = NULL;
  34. delete mPakDir;
  35. mPakDir = NULL;
  36. delete mAudioDir;
  37. mAudioDir = NULL;
  38. delete mDataDir;
  39. mDataDir = NULL;
  40. }
  41. int OpenRaider::initialize() {
  42. // Initialize Windowing
  43. int error = getWindow().initialize();
  44. if (error != 0) {
  45. printf("Could not initialize Window (%d)!\n", error);
  46. return -1;
  47. }
  48. // Initialize OpenGL
  49. error = getWindow().initializeGL();
  50. if (error != 0) {
  51. printf("Could not initialize OpenGL (%d)!\n", error);
  52. return -2;
  53. }
  54. // Initialize Font
  55. error = getFont().initialize();
  56. if (error != 0) {
  57. printf("Could not initialize Font (%d)!\n", error);
  58. return -3;
  59. }
  60. // Initialize Sound
  61. error = getSound().initialize();
  62. if (error != 0) {
  63. printf("Could not initialize Sound (%d)!\n", error);
  64. return -4;
  65. }
  66. // Initialize Texture Manager
  67. error = getTextureManager().initialize();
  68. if (error != 0) {
  69. printf("Could not initialize Textures (%d)!\n", error);
  70. return -5;
  71. }
  72. // Initialize game engine
  73. error = getGame().initialize();
  74. if (error != 0) {
  75. printf("Could not initialize Game (%d)!\n", error);
  76. return -6;
  77. }
  78. #ifdef DEBUG
  79. mFPS = true;
  80. #endif
  81. getMenu().setVisible(true);
  82. systemTimerReset();
  83. return 0;
  84. }
  85. void OpenRaider::run() {
  86. assert(mRunning == false);
  87. mRunning = true;
  88. while (mRunning)
  89. frame();
  90. }
  91. void OpenRaider::frame() {
  92. assert(mRunning == true);
  93. static clock_t fpsSum = 0, fpsCount = 0;
  94. static int fps = 0;
  95. clock_t startTime = systemTimerGet();
  96. // Get keyboard and mouse input
  97. getWindow().eventHandling();
  98. // Draw game scene
  99. getRender().display();
  100. // Draw 2D overlays (console and menu)
  101. getWindow().glEnter2D();
  102. getConsole().display();
  103. getMenu().display();
  104. // Draw FPS counter
  105. if (mFPS)
  106. getFont().drawText(10, getWindow().getHeight() - 20, 0.5f, BLUE, "%dFPS", fps);
  107. #ifdef DEBUG
  108. // Draw debug infos
  109. if (getGame().isLoaded() && (!getMenu().isVisible())) {
  110. for (int i = 0; i < 3; i++) {
  111. getFont().drawText(10, getWindow().getHeight() - ((4 - i) * 20), 0.5f, BLUE, "%.2f (%.2f)",
  112. getGame().getLara().getPos(i) / 256.0f, getGame().getLara().getAngle(i));
  113. }
  114. }
  115. #endif
  116. getWindow().glExit2D();
  117. // Put new frame on screen
  118. getWindow().swapBuffersGL();
  119. // Calculate FPS display value
  120. fpsCount++;
  121. fpsSum += (systemTimerGet() - startTime);
  122. if (fpsSum >= 500) {
  123. // Update every 500ms
  124. fps = (int)((float)fpsCount * (1000.0f / (float)fpsSum));
  125. fpsCount = fpsSum = 0;
  126. }
  127. }
  128. void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
  129. assert(key < unknownKey);
  130. assert(mRunning == true);
  131. if ((keyBindings[menuAction] == key) && pressed) {
  132. getMenu().setVisible(!getMenu().isVisible());
  133. } else if (!getMenu().isVisible()) {
  134. if ((keyBindings[consoleAction] == key) && pressed) {
  135. getConsole().setVisible(!getConsole().isVisible());
  136. } else if (!getConsole().isVisible()) {
  137. for (int i = forwardAction; i < ActionEventCount; i++) {
  138. if (keyBindings[i] == key) {
  139. getGame().handleAction((ActionEvents)i, !pressed);
  140. }
  141. }
  142. } else {
  143. getConsole().handleKeyboard(key, pressed);
  144. }
  145. } else {
  146. getMenu().handleKeyboard(key, pressed);
  147. }
  148. //! \fixme Menu/Console visibility could also change in other ways,
  149. // that should still result in the correct mousegrab state
  150. getWindow().setMousegrab(!(getMenu().isVisible() || getConsole().isVisible()));
  151. }
  152. void OpenRaider::handleText(char *text, bool notFinished) {
  153. assert(text != NULL);
  154. assert(text[0] != '\0');
  155. assert(mRunning == true);
  156. if ((getConsole().isVisible()) && (!getMenu().isVisible())) {
  157. getConsole().handleText(text, notFinished);
  158. }
  159. }
  160. void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  161. assert(button < unknownKey);
  162. assert(mRunning == true);
  163. if (getMenu().isVisible()) {
  164. getMenu().handleMouseClick(x, y, button, released);
  165. } else if (!getConsole().isVisible()) {
  166. for (int i = forwardAction; i < ActionEventCount; i++) {
  167. if (keyBindings[i] == button) {
  168. getGame().handleAction((ActionEvents)i, released);
  169. }
  170. }
  171. }
  172. }
  173. void OpenRaider::handleMouseMotion(int xrel, int yrel) {
  174. assert((xrel != 0) || (yrel != 0));
  175. assert(mRunning == true);
  176. if ((!getConsole().isVisible()) && (!getMenu().isVisible())) {
  177. getGame().handleMouseMotion(xrel, yrel);
  178. }
  179. }
  180. void OpenRaider::handleMouseScroll(int xrel, int yrel) {
  181. assert((xrel != 0) || (yrel != 0));
  182. assert(mRunning == true);
  183. if ((getConsole().isVisible()) && (!getMenu().isVisible())) {
  184. getConsole().handleMouseScroll(xrel, yrel);
  185. }
  186. }