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.

Game.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*!
  2. * \file src/Game.cpp
  3. * \brief Game abstraction
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include <map>
  9. #include <sstream>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include "global.h"
  13. #include "Camera.h"
  14. #include "Game.h"
  15. #include "loader/Loader.h"
  16. #include "Log.h"
  17. #include "Render.h"
  18. #include "RunTime.h"
  19. #include "SoundManager.h"
  20. #include "StaticMesh.h"
  21. #include "system/Font.h"
  22. #include "system/Sound.h"
  23. #include "system/Window.h"
  24. #include "TextureManager.h"
  25. #include "UI.h"
  26. #include "World.h"
  27. #include "utils/strings.h"
  28. Game::Game() {
  29. mLoaded = false;
  30. mLara = -1;
  31. for (int i = 0; i < ActionEventCount; i++) {
  32. activeEvents[i] = false;
  33. }
  34. }
  35. Game::~Game() {
  36. }
  37. int Game::initialize() {
  38. // Enable Renderer
  39. Render::setMode(RenderMode::LoadScreen);
  40. return 0;
  41. }
  42. void Game::display() {
  43. Render::display();
  44. if (getRunTime().getShowFPS()) {
  45. std::ostringstream s;
  46. s << getRunTime().getFPS() << "FPS";
  47. Font::drawText(10, Window::getSize().y - 25, 0.6f, BLUE, s.str());
  48. s.str("");
  49. s << "X: " << Camera::getPosition().x << " (" << Camera::getRotation().x << ")";
  50. Font::drawText(10, Window::getSize().y - 70, 0.6f, BLUE, s.str());
  51. s.str("");
  52. s << "Y: " << Camera::getPosition().y << " (" << Camera::getRotation().y << ")";
  53. Font::drawText(10, Window::getSize().y - 55, 0.6f, BLUE, s.str());
  54. s.str("");
  55. s << "Z: " << Camera::getPosition().z;
  56. Font::drawText(10, Window::getSize().y - 40, 0.6f, BLUE, s.str());
  57. }
  58. }
  59. void Game::destroy() {
  60. mLoaded = false;
  61. mLara = -1;
  62. Render::setMode(RenderMode::LoadScreen);
  63. Camera::reset();
  64. Render::clearRoomList();
  65. SoundManager::clear();
  66. getTextureManager().clear();
  67. getWorld().destroy();
  68. }
  69. bool Game::isLoaded() {
  70. return mLoaded;
  71. }
  72. int Game::loadLevel(const char* level) {
  73. destroy();
  74. levelName = level;
  75. getLog() << "Loading " << levelName << Log::endl;
  76. auto loader = Loader::createLoader(level);
  77. if (loader) {
  78. int error = loader->load(level);
  79. if (error != 0) {
  80. getLog() << "Error loading level (" << error << ")..." << Log::endl;
  81. destroy();
  82. return -2;
  83. }
  84. for (int i = 0; i < getWorld().sizeMesh(); i++) {
  85. getWorld().getMesh(i).prepare();
  86. }
  87. for (int i = 0; i < getWorld().sizeRoom(); i++) {
  88. getWorld().getRoom(i).prepare();
  89. }
  90. SoundManager::prepareSources();
  91. if (mLara == -1) {
  92. getLog() << "Can't find Lara entity in level?!" << Log::endl;
  93. UI::setVisible(true);
  94. } else {
  95. mLoaded = true;
  96. Render::setMode(RenderMode::Texture);
  97. Camera::setPosition(glm::vec3(getLara().getPos(0),
  98. getLara().getPos(1) + 1024.0f,
  99. getLara().getPos(2)));
  100. }
  101. } else {
  102. getLog() << "No suitable loader for this level!" << Log::endl;
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. void Game::handleAction(ActionEvents action, bool isFinished) {
  108. assert(action < ActionEventCount);
  109. if (!mLoaded)
  110. return;
  111. if (isFinished) {
  112. activeEvents[action] = false;
  113. Camera::handleAction(action, isFinished);
  114. } else {
  115. if (!activeEvents[action])
  116. Camera::handleAction(action, isFinished);
  117. activeEvents[action] = true;
  118. }
  119. }
  120. void Game::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  121. if (!mLoaded)
  122. return;
  123. Camera::handleMouseMotion(xrel, yrel);
  124. }
  125. void Game::handleControllerAxis(float value, KeyboardButton axis) {
  126. if (!mLoaded)
  127. return;
  128. Camera::handleControllerAxis(value, axis);
  129. }
  130. void Game::handleControllerButton(KeyboardButton button, bool released) {
  131. if (!mLoaded)
  132. return;
  133. Camera::handleControllerButton(button, released);
  134. }
  135. Entity& Game::getLara() {
  136. assert(mLara >= 0);
  137. assert(mLara < (int)getWorld().sizeEntity());
  138. return getWorld().getEntity(mLara);
  139. }
  140. void Game::setLara(long lara) {
  141. assert(lara >= 0);
  142. assert(lara < getWorld().sizeEntity());
  143. mLara = lara;
  144. }