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

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