hx3d  1
2D/3D Simple Game Framework
game.cpp
1 /*
2  Game management.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/window/game.hpp"
22 
23 #include "hx3d/core/core.hpp"
24 #include "hx3d/window/application.hpp"
25 
26 #include "hx3d/utils/log.hpp"
27 #include "hx3d/utils/timer_manager.hpp"
28 #include "hx3d/tweens/tween_manager.hpp"
29 #include "hx3d/window/event_manager.hpp"
30 
31 namespace hx3d {
32 namespace window {
33 
34 Game::Game():
35 _running(true), _screen(nullptr), _showStats(false)
36 {
37  _deltaText.transform.position.x = Core::App()->getWidth() / 2;
38  _deltaText.transform.position.y = 100;
39  _deltaText.transform.position.z = 0.95f;
40  _deltaText.setCharacterSize(20);
41 
42  _fpsText.transform.position.x = Core::App()->getWidth() / 2;
43  _fpsText.transform.position.y = _deltaText.transform.position.y + _deltaText.getCharacterSize();
44  _fpsText.transform.position.z = 0.95f;
45  _fpsText.setCharacterSize(20);
46 
47  _batch.setCamera(_camera);
48 }
49 
50 void Game::create() {}
51 
52 void Game::dispose() {
53  if (_screen)
54  _screen->hide();
55 }
56 
57 void Game::pause() {
58  if (_screen)
59  _screen->pause();
60 }
61 
62 void Game::resume() {
63  if (_screen)
64  _screen->resume();
65 }
66 
68  _currentTransition = transition;
69 }
70 
72  if (viewport) {
73  auto world_size = viewport->getWorldSize();
74  _currentFB.resize(world_size.x, world_size.y);
75  _nextFB.resize(world_size.x, world_size.y);
76  }
77 
78  _currentViewport = viewport;
79  _currentViewport->apply(_camera);
80 
81  updateStats();
82 }
83 
85  return _currentViewport;
86 }
87 
88 glm::vec2 Game::getSize() {
89  if (_currentViewport)
90  return _currentViewport->getWorldSize();
91  else
92  return Core::App()->getSize();
93 }
94 
95 void Game::updateStats() {
96  _deltaText.transform.position.x = this->getSize().x / 2;
97  _deltaText.transform.position.y = 100;
98  _deltaText.transform.position.z = 0.95f;
99  _deltaText.setCharacterSize(20);
100 
101  _fpsText.transform.position.x = this->getSize().x / 2;
102  _fpsText.transform.position.y = _deltaText.transform.position.y + _deltaText.getCharacterSize();
103  _fpsText.transform.position.z = 0.95f;
104  _fpsText.setCharacterSize(20);
105 }
106 
107 void Game::render() {
108 
109  if (!_nextScreen) {
110  if (_screen) {
111  _screen->render();
112  }
113  }
114 
115  else {
116  if (_currentTransition) {
117  if (_currentTransition->isFinished()) {
118  _screen->hide();
119  _screen->dispose();
120  _screen = _nextScreen;
121  _screen->resume();
122  _currentTransition->reset();
123  _nextScreen = nullptr;
124 
125  _screen->render();
126  }
127 
128  else {
129  graphics::Framebuffer::use(_currentFB);
131  _screen->render();
132 
135  _nextScreen->render();
136 
138  if (_currentViewport) _currentViewport->apply(_camera);
139  _currentTransition->render(_batch, _currentFB, _nextFB);
140  }
141  }
142 
143  else {
144  _screen->hide();
145  _screen = _nextScreen;
146  _screen->resume();
147  _nextScreen = nullptr;
148 
149  _screen->render();
150  }
151  }
152 
153  if (_showStats) {
154  _batch.begin();
155  _batch.draw(_deltaText);
156  _batch.draw(_fpsText);
157  _batch.end();
158  }
159 }
160 
162  return _session;
163 }
164 
165 void Game::update(float delta) {
166 
167  if (!_nextScreen) {
168  _screen->update(delta);
169  }
170 
171  else {
172  if (_currentTransition) {
173  if (_currentTransition->isRunning()) {
174  _currentTransition->update(delta);
175  }
176  }
177  }
178 
179  if (_showStats) {
180  _deltaText.setContent(format("D: %2.0f", delta * 1000.f));
181  _fpsText.setContent(format("FPS: %2.0f", 1/delta));
182  }
183 }
184 
185 void Game::resize(int width, int height) {
186  if (_screen)
187  _screen->resize(width, height);
188 }
189 
191  return _running;
192 }
193 
194 void Game::stop() {
195  dispose();
196  _running = false;
197 }
198 
200 
201  Core::Events()->setInputHandler(nullptr);
202  auto size = Core::App()->getSize();
203 
204  if (screen) {
205  screen->show();
206  screen->resize(size.x, size.y);
207 
208  if (!_screen) {
209  _screen = screen;
210  } else {
211  _nextScreen = screen;
212  _screen->pause();
213  _nextScreen->pause();
214 
215  if (_currentTransition) {
216  _currentTransition->start();
217  }
218  }
219  }
220 }
221 
222 void Game::activateStats(bool enabled) {
223  _showStats = enabled;
224 }
225 
226 } /* window */
227 } /* hx3d */
virtual void resume()
Resume the current screen.
Definition: game.cpp:62
virtual void update(float delta)
Update the current screen.
Definition: game.cpp:165
std::string format(const std::string fmt,...)
Format a string using printf notation.
Definition: string.cpp:27
virtual void render()
Render the current scren.
Definition: game.cpp:107
static window::Application * App()
Get the application instance.
Definition: core.cpp:78
hx3d framework namespace
Definition: audio.hpp:26
virtual void dispose()
Clean the current screen.
Definition: game.cpp:52
glm::vec2 getSize()
Get the current game size.
Definition: game.cpp:88
virtual void stop()
Stop the game.
Definition: game.cpp:194
glm::ivec2 getSize()
Get the window size (ivec2)
Definition: application.cpp:43
bool isRunning()
Test if the game is running.
Definition: game.cpp:190
Map of whatever you want. Useful for user data.
Definition: object_map.hpp:34
virtual void pause()
Pause the current screen.
Definition: game.cpp:57
void setScreen(Ptr< Screen > screen)
Set the current screen.
Definition: game.cpp:199
void setTransition(const Ptr< graphics::Transition > &transition)
Set the current transition.
Definition: game.cpp:67
const Ptr< graphics::viewports::Viewport > & getViewport()
Get the current viewport.
Definition: game.cpp:84
void setInputHandler(InputHandler *handler)
Define an input handler.
virtual void create()
Initialize the game.
Definition: game.cpp:50
void setViewport(const Ptr< graphics::viewports::Viewport > &viewport)
Set the current viewport.
Definition: game.cpp:71
static Color Black
Black color.
Definition: color.hpp:109
static window::EventManager * Events()
Get the event manager.
Definition: core.cpp:90
virtual void resize(int width, int height)
Resize the current screen.
Definition: game.cpp:185
void activateStats(bool enabled)
Activate the stats.
Definition: game.cpp:222
static void useDefault()
Use the default framebuffer.
ObjectMap & getSession()
Get the session.
Definition: game.cpp:161
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
static void use(Framebuffer &buf)
Use the framebuffer as current framebuffer.
Definition: framebuffer.cpp:95
static void clear(Color color)
Clear the framebuffer.
int getWidth()
Get the window width.
Definition: application.cpp:35