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

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