2016-04-10 10:27:06 +00:00
|
|
|
|
#include <iostream>
|
2016-03-28 15:23:47 +00:00
|
|
|
|
#include "manager.hpp"
|
|
|
|
|
|
2016-04-10 02:47:02 +00:00
|
|
|
|
const unsigned int Manager::FPS = 60;
|
|
|
|
|
const sf::Time Manager::FRAME_TIME = sf::seconds(1.f / Manager::FPS);
|
|
|
|
|
|
2016-04-10 10:27:06 +00:00
|
|
|
|
Manager::Manager() : default_view(window.getDefaultView()),
|
|
|
|
|
title(sf::String(L"")), state(NULL), next_state(NULL), running(false) {
|
|
|
|
|
// préchargement des textures
|
|
|
|
|
resource_manager.preload();
|
|
|
|
|
|
|
|
|
|
// création de la fenêtre (après avoir préchargé les ressources,
|
|
|
|
|
// on évite ainsi tout lag pendant le traitement des événements)
|
|
|
|
|
window.create(
|
|
|
|
|
sf::VideoMode(704, 480), "Skizzle", sf::Style::Default,
|
|
|
|
|
sf::ContextSettings(0, 0, 2)
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-03-28 15:23:47 +00:00
|
|
|
|
|
|
|
|
|
void Manager::start() {
|
2016-04-08 00:35:17 +00:00
|
|
|
|
running = true;
|
|
|
|
|
|
|
|
|
|
while (running) {
|
2016-03-28 15:23:47 +00:00
|
|
|
|
sf::Event event;
|
|
|
|
|
|
2016-04-09 13:53:12 +00:00
|
|
|
|
// si un changement d'état a été demandé, on l'effectue maintenant
|
|
|
|
|
if (next_state != nullptr) {
|
|
|
|
|
state = next_state;
|
|
|
|
|
next_state->begin();
|
|
|
|
|
next_state = nullptr;
|
2016-04-08 00:35:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-09 21:57:23 +00:00
|
|
|
|
// vérification de sécurité
|
|
|
|
|
if (state == NULL) {
|
|
|
|
|
throw std::runtime_error("Aucun état à afficher pour le jeu");
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
|
// traitement des évènements reçus
|
|
|
|
|
while (window.pollEvent(event)) {
|
|
|
|
|
// fermeture de la fenêtre
|
|
|
|
|
if (event.type == sf::Event::Closed) {
|
2016-04-08 00:35:17 +00:00
|
|
|
|
quit();
|
2016-03-28 15:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
|
// redimensionnement de la vue par défaut
|
2016-04-08 00:21:19 +00:00
|
|
|
|
if (event.type == sf::Event::Resized) {
|
|
|
|
|
default_view = sf::View(sf::FloatRect(
|
|
|
|
|
0, 0, event.size.width, event.size.height
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-09 21:57:23 +00:00
|
|
|
|
state->processEvent(event);
|
2016-03-28 15:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-09 21:57:23 +00:00
|
|
|
|
// affichage de la prochaine frame
|
|
|
|
|
state->frame();
|
2016-04-05 15:50:28 +00:00
|
|
|
|
window.display();
|
2016-03-28 15:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-08 00:35:17 +00:00
|
|
|
|
void Manager::quit() {
|
|
|
|
|
running = false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-09 13:53:12 +00:00
|
|
|
|
std::shared_ptr<State> Manager::getState() {
|
|
|
|
|
return state;
|
2016-04-05 23:13:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-09 13:53:12 +00:00
|
|
|
|
void Manager::setState(std::shared_ptr<State> set_state) {
|
|
|
|
|
// on ne change pas immédiatement l'état, on attend
|
2016-04-08 00:35:17 +00:00
|
|
|
|
// la prochaine frame pour éviter toute erreur de segmentation
|
2016-04-09 13:53:12 +00:00
|
|
|
|
next_state = set_state;
|
2016-03-28 17:57:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
|
sf::RenderWindow& Manager::getWindow() {
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-03 18:19:48 +00:00
|
|
|
|
sf::Time Manager::getCurrentTime() const {
|
|
|
|
|
return clock.getElapsedTime();
|
2016-03-28 15:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResourceManager& Manager::getResourceManager() {
|
|
|
|
|
return resource_manager;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-08 00:21:19 +00:00
|
|
|
|
void Manager::resetDefaultView() {
|
|
|
|
|
window.setView(default_view);
|
2016-04-05 15:50:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 11:41:12 +00:00
|
|
|
|
sf::String Manager::getTitle() {
|
2016-04-05 18:07:58 +00:00
|
|
|
|
return title;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 11:41:12 +00:00
|
|
|
|
void Manager::setTitle(sf::String set_title) {
|
2016-04-09 00:32:11 +00:00
|
|
|
|
if (title == set_title) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 18:07:58 +00:00
|
|
|
|
title = set_title;
|
|
|
|
|
|
2016-04-06 11:41:12 +00:00
|
|
|
|
if (title.isEmpty()) {
|
|
|
|
|
window.setTitle(sf::String(L"Skizzle"));
|
2016-04-05 18:07:58 +00:00
|
|
|
|
} else {
|
2016-04-06 11:41:12 +00:00
|
|
|
|
window.setTitle(sf::String(L"Skizzle ‒ ") + title);
|
2016-04-05 18:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
|
bool Manager::isKeyPressed(sf::Keyboard::Key key) const {
|
2016-04-04 17:53:54 +00:00
|
|
|
|
return sf::Keyboard::isKeyPressed(key) && window.hasFocus();
|
2016-03-28 15:23:47 +00:00
|
|
|
|
}
|
2016-04-09 23:29:42 +00:00
|
|
|
|
|
|
|
|
|
bool Manager::isKeyPressed(Manager::Modifier modifier) const {
|
|
|
|
|
switch (modifier) {
|
|
|
|
|
case Manager::Modifier::CONTROL:
|
|
|
|
|
return isKeyPressed(sf::Keyboard::LControl) || isKeyPressed(sf::Keyboard::RControl);
|
|
|
|
|
|
|
|
|
|
case Manager::Modifier::ALT:
|
|
|
|
|
return isKeyPressed(sf::Keyboard::LAlt) || isKeyPressed(sf::Keyboard::RAlt);
|
|
|
|
|
|
|
|
|
|
case Manager::Modifier::SYSTEM:
|
|
|
|
|
return isKeyPressed(sf::Keyboard::LSystem) || isKeyPressed(sf::Keyboard::RSystem);
|
|
|
|
|
|
|
|
|
|
case Manager::Modifier::SHIFT:
|
|
|
|
|
return isKeyPressed(sf::Keyboard::LShift) || isKeyPressed(sf::Keyboard::RShift);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|