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

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