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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  32. Game::~Game() {
  33. }
  34. int Game::initialize() {
  35. // Enable Renderer
  36. Render::setMode(RenderMode::LoadScreen);
  37. return 0;
  38. }
  39. void Game::display() {
  40. Render::display();
  41. if (getRunTime().getShowFPS()) {
  42. std::ostringstream s;
  43. s << getRunTime().getFPS() << "FPS";
  44. Font::drawText(10, getWindow().getHeight() - 25, 0.6f, BLUE, s.str());
  45. s.str("");
  46. s << "X: " << Camera::getPosition().x << " (" << Camera::getRotation().x << ")";
  47. Font::drawText(10, getWindow().getHeight() - 70, 0.6f, BLUE, s.str());
  48. s.str("");
  49. s << "Y: " << Camera::getPosition().y << " (" << Camera::getRotation().y << ")";
  50. Font::drawText(10, getWindow().getHeight() - 55, 0.6f, BLUE, s.str());
  51. s.str("");
  52. s << "Z: " << Camera::getPosition().z;
  53. Font::drawText(10, getWindow().getHeight() - 40, 0.6f, BLUE, s.str());
  54. }
  55. }
  56. void Game::destroy() {
  57. mLoaded = false;
  58. mLara = -1;
  59. Render::setMode(RenderMode::LoadScreen);
  60. Camera::reset();
  61. Render::clearRoomList();
  62. SoundManager::clear();
  63. getTextureManager().clear();
  64. getWorld().destroy();
  65. }
  66. bool Game::isLoaded() {
  67. return mLoaded;
  68. }
  69. int Game::loadLevel(const char* level) {
  70. destroy();
  71. levelName = level;
  72. getLog() << "Loading " << levelName << Log::endl;
  73. auto loader = Loader::createLoader(level);
  74. if (loader) {
  75. int error = loader->load(level);
  76. if (error != 0) {
  77. getLog() << "Error loading level (" << error << ")..." << Log::endl;
  78. destroy();
  79. return -2;
  80. }
  81. for (int i = 0; i < getWorld().sizeMesh(); i++) {
  82. getWorld().getMesh(i).prepare();
  83. }
  84. for (int i = 0; i < getWorld().sizeRoom(); i++) {
  85. getWorld().getRoom(i).prepare();
  86. }
  87. SoundManager::prepareSources();
  88. if (mLara == -1) {
  89. getLog() << "Can't find Lara entity in level?!" << Log::endl;
  90. UI::setVisible(true);
  91. } else {
  92. mLoaded = true;
  93. Render::setMode(RenderMode::Texture);
  94. Camera::setPosition(glm::vec3(getLara().getPos(0),
  95. getLara().getPos(1) + 1024.0f,
  96. getLara().getPos(2)));
  97. }
  98. } else {
  99. getLog() << "No suitable loader for this level!" << Log::endl;
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. void Game::handleAction(ActionEvents action, bool isFinished) {
  105. if (isFinished || (!mLoaded))
  106. return;
  107. Camera::handleAction(action, isFinished);
  108. }
  109. void Game::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  110. if (!mLoaded)
  111. return;
  112. Camera::handleMouseMotion(xrel, yrel);
  113. }
  114. Entity& Game::getLara() {
  115. assert(mLara >= 0);
  116. assert(mLara < (int)getWorld().sizeEntity());
  117. return getWorld().getEntity(mLara);
  118. }
  119. void Game::setLara(long lara) {
  120. assert(lara >= 0);
  121. assert(lara < getWorld().sizeEntity());
  122. mLara = lara;
  123. }