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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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::getRadianPitch() << ")";
  47. Font::drawText(10, getWindow().getHeight() - 70, 0.6f, BLUE, s.str());
  48. s.str("");
  49. s << "Y: " << Camera::getPosition().y << " (" << Camera::getRadianYaw() << ")";
  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. SoundManager::clear();
  62. getTextureManager().clear();
  63. getWorld().destroy();
  64. }
  65. bool Game::isLoaded() {
  66. return mLoaded;
  67. }
  68. int Game::loadLevel(const char* level) {
  69. destroy();
  70. levelName = level;
  71. getLog() << "Loading " << levelName << Log::endl;
  72. auto loader = Loader::createLoader(level);
  73. if (loader) {
  74. int error = loader->load(level);
  75. if (error != 0) {
  76. getLog() << "Error loading level (" << error << ")..." << Log::endl;
  77. destroy();
  78. return -2;
  79. }
  80. for (int i = 0; i < getWorld().sizeRoom(); i++) {
  81. getWorld().getRoom(i).prepare();
  82. }
  83. SoundManager::prepareSources();
  84. if (mLara == -1) {
  85. getLog() << "Can't find Lara entity in level?!" << Log::endl;
  86. UI::setVisible(true);
  87. } else {
  88. mLoaded = true;
  89. Render::setMode(RenderMode::Texture);
  90. }
  91. } else {
  92. getLog() << "No suitable loader for this level!" << Log::endl;
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. void Game::handleAction(ActionEvents action, bool isFinished) {
  98. if (isFinished || (!mLoaded))
  99. return;
  100. Camera::handleAction(action, isFinished);
  101. }
  102. void Game::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  103. if (!mLoaded)
  104. return;
  105. Camera::handleMouseMotion(xrel, yrel);
  106. /* TODO
  107. float angles[3] = { 0.0f, getCamera().getRadianYaw(), getCamera().getRadianPitch() };
  108. getLara().setAngles(angles);
  109. */
  110. }
  111. Entity& Game::getLara() {
  112. assert(mLara >= 0);
  113. assert(mLara < (int)getWorld().sizeEntity());
  114. return getWorld().getEntity(mLara);
  115. }
  116. void Game::setLara(long lara) {
  117. assert(lara >= 0);
  118. assert(lara < getWorld().sizeEntity());
  119. mLara = lara;
  120. }