-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
136 lines (109 loc) · 2.6 KB
/
game.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "game.h"
#include "config.h"
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <assert.h>
#include <memory>
using namespace std;
Game::Game(const char *name, int width, int height, int bpp) : sf::RenderWindow(sf::VideoMode(width,height,bpp),name)
{
clear(sf::Color::Black);
fps = false;
}
void Game::run() {
sf::Event event;
int frames = 0;
if(game_DEFAULT_FRAMELIMIT != -1) {
this->setFramerateLimit(game_DEFAULT_FRAMELIMIT);
}
sf::Text text;
sf::Font font;
if (!font.loadFromFile("./assets/arial.ttf"))
{
// error...
}
text.setColor(sf::Color::White);
text.setFont(font);
text.setCharacterSize(20);
running = true;
render = true;
while(this->isOpen() && running) {
while(pollEvent(event)) {
switch(event.type) {
case sf::Event::Closed:
quit();
break;
case sf::Event::Resized:
setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
break;
}
onEvent(event);
}
if(render) {
clear();
m_Tick = m_Clock.restart();
onUpdate(m_Tick);
if(m_Scene) {
m_Scene->update(m_Tick);
}
std::lock_guard<std::mutex> lock(m_Mutex);
if(m_Scene) {
draw(*m_Scene);
}
if(fps) {
frames++;
if (m_FPSClock.getElapsedTime().asMilliseconds() >= 1000) {
m_FPSClock.restart();
std::stringstream out;
out << frames;
text.setString(out.str());
frames = 0;
}
draw(text);
}
display();
}
}
if(m_Scene)
m_Scene.reset();
}
void Game::setScene(std::shared_ptr<Scene> &s)
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_Scene = s;
}
void Game::onUpdate(const sf::Time &time)
{
}
void Game::onEvent(const sf::Event &event)
{
}
void Game::toggleFPS() {
fps = !fps;
}
void Game::quit() {
running = false;
render = false;
close();
}
bool Game::isRendering() {
return render;
}
bool Game::isRunning() {
return running;
}
bool Game::getShowFPS() {
return fps;
}
void Game::setShowFPS(const bool &v) {
fps = true;
}
void Game::onQuit() {
}
bool Game::hasScene() const {
return (bool)m_Scene;
}
void Game::sendEventToScene(const sf::Event &e) {
m_Scene->handleEvent(e);
}