forked from VivianMochi/Crescendo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
186 lines (160 loc) · 4.27 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "game.hpp"
#include "state.hpp"
#include "menu_state.hpp"
#include <iostream>
#include <algorithm>
Game::Game() {
// Create game window
//window = new sf::RenderWindow(sf::VideoMode(1920, 1080), "Crescendo", sf::Style::Fullscreen);
window = new sf::RenderWindow(sf::VideoMode(960, 540), "Crescendo");
window->setFramerateLimit(60);
fullscreen = false;
scale = std::min(window->getSize().x / 240, window->getSize().y / 135);
// Initialize game-wide variables
// Initial state
changeState(new MenuState());
// Start the clock
sf::Clock clock;
while (window->isOpen()) {
// State
if (nextState) {
// Load up the new state
if (state)
delete state;
state = nextState;
nextState = nullptr;
state->setGame(this);
state->init();
// Restart the clock to account for load time
clock.restart();
}
// Event
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed) {
exit();
}
else if (event.type == sf::Event::Resized) {
window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
}
else if (event.type == sf::Event::KeyReleased) {
if (event.key.code == sf::Keyboard::F11) {
if (fullscreen) {
delete window;
window = new sf::RenderWindow(sf::VideoMode(960, 540), "Crescendo");
window->setFramerateLimit(60);
fullscreen = false;
}
else {
delete window;
window = new sf::RenderWindow(sf::VideoMode(1920, 1080), "Crescendo", sf::Style::Fullscreen);
window->setFramerateLimit(60);
fullscreen = true;
}
}
}
if (state) {
state->gotEvent(event);
}
}
// Update
sf::Time elapsed = clock.restart();
// If the game freezes, make sure no time passes
if (elapsed.asMilliseconds() >= 250) {
elapsed = sf::Time::Zero;
}
if (state)
state->update(elapsed);
// Render
window->clear();
if (state) {
state->render(*window);
}
// Pixelate
sf::Texture postTexture;
postTexture.create(window->getSize().x, window->getSize().y);
postTexture.update(*window);
sf::Sprite postSprite(postTexture, sf::IntRect(0, 0, 240, 135));
scale = std::min(window->getSize().x / 240, window->getSize().y / 135);
postSprite.setScale(scale, scale);
window->draw(postSprite);
window->display();
// Exit
if (exiting) {
window->close();
}
}
}
Game::~Game() {
if (state)
delete state;
if (nextState)
delete nextState;
delete window;
}
sf::RenderWindow *Game::getWindow() {
return window;
}
void Game::changeState(State *newState) {
if (nextState)
delete nextState;
nextState = newState;
}
void Game::exit() {
exiting = true;
}
sf::Texture& Game::loadTexture(std::string filename) {
if (textures.count(filename) == 0) {
sf::Texture newTexture;
if (newTexture.loadFromFile(filename)) {
textures.insert(std::pair<std::string, sf::Texture>(filename, newTexture));
// Debug
//std::cout << "New texture created (" << filename << ")\n";
}
else {
if (textures.count("BadTexture") == 0) {
sf::Image badImage;
badImage.create(2, 2, sf::Color::Black);
badImage.setPixel(0, 0, sf::Color::Cyan);
badImage.setPixel(1, 1, sf::Color::Cyan);
newTexture.loadFromImage(badImage);
newTexture.setRepeated(true);
textures.insert(std::pair<std::string, sf::Texture>("BadTexture", newTexture));
// Debug
//std::cout << "Bad texture created (" << filename << ")\n";
}
else {
// Debug
//std::cout << "Bad texture loaded (" << filename << ")\n";
}
return textures["BadTexture"];
}
}
else {
// Debug
//std::cout << "Existing texture loaded (" << filename << ")\n";
}
return textures[filename];
}
sf::SoundBuffer& Game::loadSoundBuffer(std::string filename) {
if (soundBuffers.count(filename) == 0) {
sf::SoundBuffer newSoundBuffer;
if (newSoundBuffer.loadFromFile(filename)) {
soundBuffers.insert(std::pair<std::string, sf::SoundBuffer>(filename, newSoundBuffer));
// Debug
//std::cout << "New sound buffer created (" << filename << ")\n";
}
else {
// Debug
//std::cout << "Cannot create sound buffer (" << filename << ")\n";
}
}
else {
// Debug
//std::cout << "Existing sound buffer loaded (" << filename << ")\n";
}
return soundBuffers[filename];
}
sf::Vector2u Game::screenSize() {
return window->getSize();
}