-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.cpp
More file actions
95 lines (81 loc) · 2.86 KB
/
Copy pathGameEngine.cpp
File metadata and controls
95 lines (81 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "GameEngine.h"
#include "BitmapStore.h"
#include "FontStore.h"
using namespace std;
using namespace sf;
GameEngine::GameEngine()
{
const VideoMode desktop = VideoMode::getDesktopMode();
#ifdef SPACEINVADERS_FULLSCREEN
m_Resolution.x = static_cast<float>(desktop.width);
m_Resolution.y = static_cast<float>(desktop.height);
const Uint32 style = Style::Fullscreen;
#else
// Windowed by default. A crash while fullscreen on macOS can leave you
// with no visible way back to the desktop; -DSPACEINVADERS_FULLSCREEN=ON
// opts back in.
m_Resolution.x = static_cast<float>(desktop.width) * 0.8f;
m_Resolution.y = static_cast<float>(desktop.height) * 0.8f;
const Uint32 style = Style::Default;
#endif
m_Window.create(
VideoMode(static_cast<unsigned int>(m_Resolution.x), static_cast<unsigned int>(m_Resolution.y)),
"Space Invaders",
style
);
// Without a cap the loop runs as fast as the machine allows. On a 120Hz
// display that is twice the delta-time resolution the original was tuned
// against, and on an idle menu it spins a core for nothing.
m_Window.setFramerateLimit(60);
m_ScreenManager = make_unique<ScreenManager>(
Vector2i(static_cast<int>(m_Resolution.x), static_cast<int>(m_Resolution.y)),
m_SoundEngine
);
}
GameEngine::~GameEngine()
{
// Runs before any member is destroyed, so m_Window -- and the OpenGL
// context it owns -- is still alive here. The texture and font caches are
// function-local statics that would otherwise be torn down after main()
// returns, with no context left to release their GPU handles against.
BitmapStore::clear();
FontStore::clear();
}
void GameEngine::run()
{
// The largest step the simulation will take in one frame. A stall -- the
// first frame after loading a level, or dragging the window -- otherwise
// produces a single huge dt that teleports every bullet straight through
// the invaders it should have hit.
const float maxDeltaTimeSeconds = 0.1f;
while (m_Window.isOpen())
{
m_DT = m_Clock.restart();
// This variable was named m_FPS but has always held seconds elapsed
// since the previous frame -- delta time, the reciprocal of a rate.
// The misnomer propagated into every update(float fps) signature in
// the codebase.
m_DeltaTimeSeconds = m_DT.asSeconds();
if (m_DeltaTimeSeconds > maxDeltaTimeSeconds)
{
m_DeltaTimeSeconds = maxDeltaTimeSeconds;
}
handleInput();
update();
draw();
}
}
void GameEngine::handleInput()
{
m_ScreenManager->handleInput(m_Window);
}
void GameEngine::update()
{
m_ScreenManager->update(m_DeltaTimeSeconds);
}
void GameEngine::draw()
{
m_Window.clear(Color::Black);
m_ScreenManager->draw(m_Window);
m_Window.display();
}