-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cpp
More file actions
105 lines (83 loc) · 2.26 KB
/
Scene.cpp
File metadata and controls
105 lines (83 loc) · 2.26 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
96
97
98
99
100
101
102
103
104
105
#include "Scene.hpp"
#include "Game.hpp"
Scene::Scene(Game* g, sf::RenderWindow* w) :
_game(g),
_window(w),
_killed(false) {
}
Scene::~Scene(){}
void Scene::init(int) {
}
void Scene::run() {
sf::Clock clock;
sf::Time timeSinceLastUpdate;
sf::Time timePerFrame = sf::seconds(1.f/FRAMERATE);
while (_window->isOpen()) {
processInput();
timeSinceLastUpdate = clock.restart();
while (timeSinceLastUpdate > timePerFrame) {
timeSinceLastUpdate -= timePerFrame;
update(timePerFrame.asSeconds());
}
update(timePerFrame.asSeconds());
display();
if (_killed) {
_game->changeScene(_nextSceneName,_players);
_killed = false;
return;
}
}
}
void Scene::killScene() {
_killed = true;
}
sf::View* Scene::getPtrView() {
return &_view;
}
void Scene::processInput() {
sf::Event event;
while (_window->pollEvent(event)) {
if (event.type == sf::Event::Closed) {_window->close(); exit(0);}
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
_window->close(); exit(0);
}
}
}
void Scene::update(float deltaTime) {
(void)deltaTime;
}
void Scene::render() {
render(_window);
}
void Scene::render(sf::RenderTarget* target) {
(void) target;
}
void Scene::display() {
_window->clear();
_window->setView(_view);
render();
_window->setView(_window->getDefaultView());
_window->display();
}
void Scene::initView(sf::Vector2i windowSize) {
int windowX = _window->getSize().x, windowY = _window->getSize().y;
float xr = windowX / float(windowSize.x);
float yr = windowY / float(windowSize.y);
float aux;
if (xr < yr) aux = 1/yr;
else aux = 1/xr;
xr *= aux;
yr *= aux;
sf::Vector2f min,max;
min.x = (1.f - yr) / 2.f;
min.y = (1.f - xr) / 2.f;
max.x = 1.f - min.x*2;
max.y = 1.f - min.y*2;
_view.reset(sf::FloatRect(0,0,windowSize.x*(1+2*min.x),windowSize.y*(1+2*min.y)));
// _view.setViewport(sf::FloatRect(min.x,min.y,max.x,max.y));
}
void Scene::changeScene(std::string nextScene, int players) {
_killed = true;
_nextSceneName = nextScene;
_players = players;
}