OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
Game.cpp
Go to the documentation of this file.
1 
8 #include "global.h"
9 #include "Camera.h"
10 #include "Console.h"
11 #include "Game.h"
12 #include "loader/Loader.h"
13 #include "Log.h"
14 #include "Menu.h"
15 #include "Render.h"
16 #include "SoundManager.h"
17 #include "TextureManager.h"
18 #include "UI.h"
19 #include "World.h"
20 
21 bool Game::mLoaded = false;
22 long Game::mLara = -1;
24 
25 void Game::destroy() {
26  mLoaded = false;
27  mLara = -1;
28 
29  for (int i = 0; i < ActionEventCount; i++) {
30  activeEvents[i] = false;
31  }
32 
34  Camera::reset();
38  getWorld().destroy();
39 }
40 
41 int Game::loadLevel(std::string level) {
42  destroy();
43 
44  Log::get(LOG_INFO) << "Loading " << level << Log::endl;
45  auto loader = Loader::createLoader(level);
46  if (loader) {
47  int error = loader->load(level);
48  if (error != 0) {
49  Log::get(LOG_ERROR) << "Error loading level (" << error << ")..." << Log::endl;
50  destroy();
51  return -2;
52  }
53 
54  for (int i = 0; i < getWorld().sizeMesh(); i++) {
55  getWorld().getMesh(i).prepare();
56  }
57 
58  for (int i = 0; i < getWorld().sizeRoom(); i++) {
59  getWorld().getRoom(i).prepare();
60  }
61 
64 
65  mLoaded = true;
67  Menu::setVisible(false);
68 
69  if (mLara == -1) {
70  Log::get(LOG_WARNING) << "Can't find Lara entity in level?!" << Log::endl;
71  } else {
72  Camera::setPosition(glm::vec3(getLara().getPosition().x,
73  getLara().getPosition().y - 1024.0f,
74  getLara().getPosition().z));
75  }
76  } else {
77  Log::get(LOG_ERROR) << "No suitable loader for this level!" << Log::endl;
78  return -1;
79  }
80 
81  return 0;
82 }
83 
84 void Game::handleAction(ActionEvents action, bool isFinished) {
86 
87  if (!mLoaded)
88  return;
89 
90  if (isFinished) {
91  if (activeEvents[action])
92  Camera::handleAction(action, isFinished);
93  activeEvents[action] = false;
94  } else {
95  if (!activeEvents[action])
96  Camera::handleAction(action, isFinished);
97  activeEvents[action] = true;
98  }
99 }
100 
101 void Game::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
102  if (!mLoaded)
103  return;
104 
105  Camera::handleMouseMotion(xrel, yrel);
106 }
107 
109  if (!mLoaded)
110  return;
111 
112  Camera::handleControllerAxis(value, axis);
113 }
114 
117  assertLessThan(mLara, getWorld().sizeEntity());
118  return getWorld().getEntity(mLara);
119 }
120 
121 void Game::setLara(long lara) {
122  assertGreaterThanEqual(lara, 0);
123  assertLessThan(lara, getWorld().sizeEntity());
124  mLara = lara;
125 }
126 
#define LOG_WARNING
Definition: Log.h:20
static void setMode(RenderMode m)
Definition: Render.h:40
static void handleMouseMotion(int xrel, int yrel, int xabs, int yabs)
Definition: Game.cpp:101
Room & getRoom(unsigned long index)
Definition: World.cpp:33
unsigned long sizeMesh()
Definition: World.cpp:107
static void handleControllerAxis(float value, KeyboardButton axis)
Definition: Game.cpp:108
Level file loader.
static void reset()
Definition: Camera.cpp:67
World Model.
static bool mLoaded
Definition: Game.h:30
Main Menu.
KeyboardButton
Definition: global.h:34
Sound Source Manager.
void destroy()
Clears all data in world.
Definition: World.cpp:15
World & getWorld()
Definition: main.cpp:32
Included everywhere.
static LogLevel & get(int level)
Definition: Log.cpp:14
static void handleMouseMotion(int x, int y)
Definition: Camera.cpp:111
static long mLara
Definition: Game.h:31
Global Logging Utility.
static int prepareSources()
Entity & getEntity(unsigned long index)
Definition: World.cpp:72
ActionEvents
Definition: global.h:15
Console Window.
static void error(char *msg)
Definition: commander.c:19
Definition: Entity.h:11
World Renderer.
static void prepare()
#define LOG_INFO
Definition: Log.h:21
static void handleAction(ActionEvents action, bool isFinished)
Definition: Game.cpp:84
static const char endl
Definition: Log.h:35
static bool activeEvents[ActionEventCount]
Definition: Game.h:32
static void handleControllerAxis(float value, KeyboardButton axis)
Definition: Camera.cpp:140
#define assertGreaterThanEqual(x, y)
Definition: global.h:170
void prepare()
Definition: Room.h:27
#define LOG_ERROR
Definition: Log.h:19
static void handleAction(ActionEvents action, bool isFinished)
Definition: Camera.cpp:85
static void setLara(long lara)
Definition: Game.cpp:121
static void setPosition(glm::vec3 p)
Definition: Camera.h:26
static std::unique_ptr< Loader > createLoader(std::string f)
Definition: Loader.cpp:39
UI/Event Manager.
Texture Registry.
static void clear()
static void clearRoomList()
Definition: Render.h:29
static void clear()
static Entity & getLara()
Definition: Game.cpp:115
Camera, View Frustum.
#define assertLessThan(x, y)
Definition: global.h:146
unsigned long sizeRoom()
Definition: World.cpp:29
static int loadLevel(std::string level)
Definition: Game.cpp:41
Mesh & getMesh(unsigned long index)
Definition: World.cpp:111
static void setVisible(bool v)
Definition: Menu.h:23
static void destroy()
Definition: Game.cpp:25
Gameplay Handler.
void prepare()
Definition: Mesh.cpp:52