From 2281924c62d98fbe76c1bdbfc062a4d9a0988535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Wed, 20 Apr 2016 17:31:13 +0200 Subject: [PATCH] Adaptation du code aux changements --- include/block.hpp | 3 +- include/editor.hpp | 2 - include/finish_block.hpp | 2 +- include/game.hpp | 2 - include/kill_block.hpp | 2 +- include/manager.hpp | 54 +++++++---------- include/menu.hpp | 2 + include/resource_manager.hpp | 2 +- include/state.hpp | 21 ------- include/widget_button.hpp | 42 ------------- include/widget_timer.hpp | 59 ------------------ src/block.cpp | 11 ++-- src/editor.cpp | 17 ++---- src/finish_block.cpp | 6 +- src/game.cpp | 23 ++----- src/gravity_block.cpp | 53 ++++++++-------- src/kill_block.cpp | 6 +- src/level.cpp | 13 ++-- src/manager.cpp | 85 ++++++++++++-------------- src/menu.cpp | 29 +++++---- src/player.cpp | 9 +-- src/resource_manager.cpp | 30 +++++----- src/rules.cpp | 10 ++-- src/state.cpp | 16 ----- src/switch_block.cpp | 10 ++-- src/toolbar.cpp | 2 +- src/widget_button.cpp | 82 ------------------------- src/widget_timer.cpp | 113 ----------------------------------- 28 files changed, 164 insertions(+), 542 deletions(-) delete mode 100644 include/widget_button.hpp delete mode 100644 include/widget_timer.hpp delete mode 100644 src/widget_button.cpp delete mode 100644 src/widget_timer.cpp diff --git a/include/block.hpp b/include/block.hpp index f08cb82..020539f 100644 --- a/include/block.hpp +++ b/include/block.hpp @@ -6,7 +6,6 @@ #include "object.hpp" class Game; -class ResourceManager; class Level; /** @@ -59,7 +58,7 @@ public: /** * Prépare les textures avant le dessin du bloc */ - virtual void prepareDraw(ResourceManager& resources); + virtual void prepareDraw(); /** * Dessin du bloc dans la fenêtre donnée diff --git a/include/editor.hpp b/include/editor.hpp index 109c463..707559a 100644 --- a/include/editor.hpp +++ b/include/editor.hpp @@ -2,7 +2,6 @@ #define __SKIZZLE_EDITOR_HPP__ #include "level.hpp" -#include "widget_timer.hpp" #include "toolbar.hpp" #include #include @@ -25,7 +24,6 @@ private: DragMode drag_mode; - WidgetTimer widget_timer; Toolbar toolbar; /** diff --git a/include/finish_block.hpp b/include/finish_block.hpp index d36f73b..62a9a09 100644 --- a/include/finish_block.hpp +++ b/include/finish_block.hpp @@ -40,7 +40,7 @@ public: /** * Prépare les textures avant le dessin du bloc */ - virtual void prepareDraw(ResourceManager& resources); + virtual void prepareDraw(); /** * Appelé lorsque le bloc d'arrivée est activé par un objet diff --git a/include/game.hpp b/include/game.hpp index 40ab0a2..363a3b8 100644 --- a/include/game.hpp +++ b/include/game.hpp @@ -2,7 +2,6 @@ #define __SKIZZLE_GAME_HPP__ #include "level.hpp" -#include "widget_timer.hpp" /** * La classe Game gère l'affichage et les objets @@ -29,7 +28,6 @@ private: std::vector pending_kill; DeathCause death_cause; - WidgetTimer widget_timer; float time_left; /** diff --git a/include/kill_block.hpp b/include/kill_block.hpp index 41dda6a..1ca3177 100644 --- a/include/kill_block.hpp +++ b/include/kill_block.hpp @@ -39,7 +39,7 @@ public: /** * Prépare les textures avant le dessin du bloc */ - virtual void prepareDraw(ResourceManager& resources); + virtual void prepareDraw(); /** * Appelé lorsque le bloc tueur est activé par un objet diff --git a/include/manager.hpp b/include/manager.hpp index 445fd6c..36126d1 100644 --- a/include/manager.hpp +++ b/include/manager.hpp @@ -1,7 +1,6 @@ #ifndef __SKIZZLE_MANAGER_HPP__ #define __SKIZZLE_MANAGER_HPP__ -#include "resource_manager.hpp" #include #include #include @@ -16,29 +15,29 @@ class State; */ class Manager { private: + sf::String title; sf::RenderWindow window; + + sf::Clock clock; sf::Time previous_time; sfg::SFGUI sfgui; sfg::Desktop desktop; std::vector widgets; - unsigned int framerate; - ResourceManager resource_manager; - - sf::Clock clock; - sf::View gui_view; - sf::String title; - - State* previous_state; - std::stack> states; - /** * Détermine si l'événement donné s'est passé à l'intérieur * de l'interface et doit être ignoré pour la suite ou non */ bool isInsideGUI(const sf::Event& event); + // FIXME: après avoir supprimé ::useGUIView(), supprimer ceci + sf::View gui_view; + ////////////////////////////// + + State* previous_state; + std::stack> states; + public: /** * Énumération des modificateurs @@ -79,19 +78,13 @@ public: void popState(); /** - * Renvoie la fenêtre actuellement utilisée pour le dessin + * /!\ DÉPRÉCIÉ : UTILISER LA LIBRAIRIE SFGUI À LA PLACE + * @deprecated + * + * Passage en vue de l'interface + * (coin en haut à gauche, zoom 1:1) */ - sf::RenderWindow& getWindow(); - - /** - * Récupère le framerate (maximal) actuel - */ - unsigned int getFramerate(); - - /** - * Modifie le framerate (maximal) actuel - */ - void setFramerate(unsigned int set_framerate); + void useGUIView(); /** * Renvoie le temps actuel du jeu @@ -99,16 +92,9 @@ public: sf::Time getCurrentTime() const; /** - * Renvoie le gestionnaire de ressources + * Renvoie la fenêtre actuellement utilisée pour le dessin */ - ResourceManager& getResourceManager(); - - /** - * Passage en vue de l'interface - * (coin en haut à gauche, zoom 1:1) - * @deprecated - */ - void useGUIView(); + sf::RenderWindow& getWindow(); /** * Ajoute un nouveau widget à l'interface @@ -121,12 +107,12 @@ public: void removeWidget(sfg::Widget::Ptr widget); /** - * Renvoie le titre actuel de la fenêtre + * Récupère le titre du jeu */ sf::String getTitle(); /** - * Modifie le titre actuel de la fenêtre + * Modifie le titre du jeu */ void setTitle(sf::String set_title); diff --git a/include/menu.hpp b/include/menu.hpp index b12a599..e305c25 100644 --- a/include/menu.hpp +++ b/include/menu.hpp @@ -4,6 +4,7 @@ #include "state.hpp" #include #include +#include class Manager; @@ -15,6 +16,7 @@ class Manager; class Menu : public State { private: sf::Sprite background; + std::shared_ptr font; std::vector choices; std::vector labels; diff --git a/include/resource_manager.hpp b/include/resource_manager.hpp index b669c9f..66e69f8 100644 --- a/include/resource_manager.hpp +++ b/include/resource_manager.hpp @@ -53,7 +53,7 @@ public: /** * Récupère la liste des fichiers dans le dossier donné */ - std::vector getFiles(boost::filesystem::path dir) const; + std::vector getFiles(boost::filesystem::path path) const; /** * Récupère le chemin vers le dossier des textures diff --git a/include/state.hpp b/include/state.hpp index debf787..bf4ae04 100644 --- a/include/state.hpp +++ b/include/state.hpp @@ -3,7 +3,6 @@ #include -class ResourceManager; class Manager; /** @@ -41,26 +40,6 @@ public: * Récupère le gestionnaire (version constante) */ const Manager& getManager() const; - - /** - * Récupère le gestionnaire de ressources - */ - ResourceManager& getResourceManager(); - - /** - * Récupère le gestionnaire de ressources (version constante) - */ - const ResourceManager& getResourceManager() const; - - /** - * Récupère la fenêtre - */ - sf::RenderWindow& getWindow(); - - /** - * Récupère la fenêtre (version constante) - */ - const sf::RenderWindow& getWindow() const; }; #endif diff --git a/include/widget_button.hpp b/include/widget_button.hpp deleted file mode 100644 index 766be17..0000000 --- a/include/widget_button.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef __SKIZZLE_UTIL_WIDGET_BUTTON_HPP__ -#define __SKIZZLE_UTIL_WIDGET_BUTTON_HPP__ - -#include -#include - -class Manager; - -/** - * Affiche un bouton pouvant être cliqué - */ -class WidgetButton { -private: - Manager& manager; - std::function click_cb; - - unsigned int shape; - sf::RectangleShape button; - sf::VertexArray button_shape; - -public: - static const unsigned int ARROW_UP; - static const unsigned int ARROW_DOWN; - - WidgetButton( - Manager& manager, std::function click_cb, - sf::Vector2f size, unsigned int shape - ); - - /** - * Process l'événement et renvoie true si - * on s'en est servi - */ - bool processEvent(const sf::Event& event); - - /** - * Dessine le widget à la position (haut-gauche) donnée - */ - void draw(sf::Vector2f position); -}; - -#endif diff --git a/include/widget_timer.hpp b/include/widget_timer.hpp deleted file mode 100644 index 1d78169..0000000 --- a/include/widget_timer.hpp +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef __SKIZZLE_UTIL_WIDGET_TIMER_HPP__ -#define __SKIZZLE_UTIL_WIDGET_TIMER_HPP__ - -#include -#include -#include "widget_button.hpp" - -class Manager; - -/** - * Affiche le compteur de temps pouvant (ou non) - * être modifié - */ -class WidgetTimer { -private: - Manager& manager; - bool can_change; - std::function time_left_cb; - int time_left; - - sf::RectangleShape timer_zone; - sf::Text timer_seconds_text; - sf::Text timer_sep_text; - sf::Text timer_minutes_text; - - WidgetButton timer_up; - WidgetButton timer_down; - -public: - WidgetTimer(Manager& manager, bool can_change, std::function time_left_cb = std::function()); - - /** - * Process l'événement et renvoie true si - * on s'en est servi - */ - bool processEvent(const sf::Event& event); - - /** - * Dessine le widget à la position (haut-gauche) donnée - */ - void draw(sf::Vector2f position); - - /** - * Augmente le temps de 5 secondes - */ - void addTime(); - - /** - * Diminue le temps de 5 secondes - */ - void subtractTime(); - - /** - * Modifie le temps restant - */ - void setTimeLeft(int set_time_left); -}; - -#endif diff --git a/src/block.cpp b/src/block.cpp index 7b5d135..3a4fa6b 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -3,6 +3,7 @@ #include "game.hpp" #include "player.hpp" #include "manager.hpp" +#include "resource_manager.hpp" const unsigned int Block::TYPE_ID = 2; @@ -40,21 +41,21 @@ void Block::save(std::ofstream& file) const { Object::save(file); } -void Block::prepareDraw(ResourceManager& resources) { +void Block::prepareDraw() { std::string texture_name = "movable_block.tga"; if (getMass() == 0) { texture_name = "block.tga"; } - sprite.setTexture(*resources.getTexture(texture_name)); - select_sprite.setTexture(*resources.getTexture("block_select.tga")); + sprite.setTexture(*ResourceManager::get().getTexture("objects/" + texture_name)); + select_sprite.setTexture(*ResourceManager::get().getTexture("objects/block_select.tga")); } void Block::draw(Level& level) { // utilisation de la texture - sf::RenderWindow& window = level.getWindow(); - prepareDraw(level.getResourceManager()); + sf::RenderWindow& window = level.getManager().getWindow(); + prepareDraw(); // coloration du bloc selon sa charge if (getCharge() > 0) { diff --git a/src/editor.cpp b/src/editor.cpp index 32a4aa1..416d7b4 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -1,4 +1,5 @@ #include "manager.hpp" +#include "resource_manager.hpp" #include "editor.hpp" #include "game.hpp" #include @@ -33,7 +34,6 @@ inline sf::Vector2f roundVectorToGrid(sf::Vector2f input) { Editor::Editor(Manager& manager) : Level(manager), drag_control_point(nullptr), drag_mode(Editor::DragMode::NONE), - widget_timer(manager, true, std::bind(&Editor::setTotalTime, this, std::placeholders::_1)), toolbar(*this) { getManager().addWidget(toolbar.getWindow()); } @@ -47,10 +47,10 @@ void Editor::enable() { // attributs de la fenêtre getManager().setTitle(sf::String(L"Édition de ") + getName()); - getManager().setFramerate(Manager::FPS); + getManager().getWindow().setFramerateLimit(Manager::FPS); // joue la musique de l'éditeur - getResourceManager().playMusic("editor.ogg"); + ResourceManager::get().playMusic("editor.ogg"); // on affiche la toolbar de l'éditeur toolbar.getWindow()->Show(true); @@ -59,11 +59,6 @@ void Editor::enable() { void Editor::processEvent(const sf::Event& event) { Level::processEvent(event); - // traitement des événements du widget chronomètre - if (widget_timer.processEvent(event)) { - return; - } - // lorsque l'on clique dans l'éditeur if (event.type == sf::Event::MouseButtonPressed) { sf::Vector2i mouse_position(event.mouseButton.x, event.mouseButton.y); @@ -258,7 +253,7 @@ void Editor::frame() { } void Editor::draw() { - sf::RenderWindow& window = getWindow(); + sf::RenderWindow& window = getManager().getWindow(); sf::Vector2i window_size = (sf::Vector2i) window.getSize(); // scroll de la caméra lorsque la souris se situe sur les bords @@ -325,10 +320,6 @@ void Editor::draw() { window.draw(selection_rect); } - // dessin du widget timer - widget_timer.setTimeLeft(getTotalTime()); - widget_timer.draw(sf::Vector2f(window_size.x / 2 - 50, 0)); - // on redimensionne la toolbar pour qu'elle occupe l'espace droite toolbar.getWindow()->SetAllocation(sf::FloatRect( window_size.x - toolbar.getWidth(), 0, diff --git a/src/finish_block.cpp b/src/finish_block.cpp index 9c24a33..e5f8d43 100644 --- a/src/finish_block.cpp +++ b/src/finish_block.cpp @@ -11,10 +11,10 @@ Object::Ptr FinishBlock::clone() const { return Object::Ptr(new FinishBlock(*this)); } -void FinishBlock::prepareDraw(ResourceManager& resources) { - Block::prepareDraw(resources); +void FinishBlock::prepareDraw() { + Block::prepareDraw(); sprite.setOrigin(sf::Vector2f(23, 41)); - sprite.setTexture(*resources.getTexture("finish_block.tga"), true); + sprite.setTexture(*ResourceManager::get().getTexture("objects/finish_block.tga"), true); } void FinishBlock::activate(Game& game, Object::Ptr object) { diff --git a/src/game.cpp b/src/game.cpp index 8ad6eac..a465984 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,6 +2,7 @@ #include #include "utility.hpp" #include "manager.hpp" +#include "resource_manager.hpp" #include "game.hpp" #include "player.hpp" @@ -19,8 +20,7 @@ Game::Game(Manager& manager) : Level(manager), mode(Game::Mode::NORMAL), next_frame_time(manager.getCurrentTime()), skipped_frames(0), - death_cause(Game::DeathCause::NONE), - widget_timer(manager, false) {} + death_cause(Game::DeathCause::NONE) {} Game::~Game() {} @@ -29,13 +29,13 @@ void Game::enable() { // attributs de la fenêtre getManager().setTitle(getName()); - getManager().setFramerate(0); + getManager().getWindow().setFramerateLimit(0); // si musique il y a, on la joue if (getMusic() != "") { - getResourceManager().playMusic(getMusic()); + ResourceManager::get().playMusic("levels/" + getMusic()); } else { - getResourceManager().stopMusic(); + ResourceManager::get().stopMusic(); } } @@ -130,21 +130,8 @@ void Game::frame() { } void Game::draw() { - sf::Vector2i window_size = (sf::Vector2i) getWindow().getSize(); - // dessin des objets du niveau Level::draw(); - - // on passe au dessin d'éléments d'interface. - // Changement de vue sur la vue par défaut - getManager().useGUIView(); - - // dessin du timer - widget_timer.setTimeLeft( - std::max(std::ceil(time_left), 0.f) - ); - - widget_timer.draw(sf::Vector2f(window_size.x / 2 - 50, 0)); } void Game::ensureCentered() { diff --git a/src/gravity_block.cpp b/src/gravity_block.cpp index 8ba9dde..0f44120 100644 --- a/src/gravity_block.cpp +++ b/src/gravity_block.cpp @@ -1,4 +1,5 @@ #include "manager.hpp" +#include "resource_manager.hpp" #include "utility.hpp" #include "gravity_block.hpp" #include "game.hpp" @@ -16,27 +17,6 @@ Object::Ptr GravityBlock::clone() const { } void GravityBlock::draw(Level& level) { - // sélectionne le sprite d'icône - std::string texture_name = "gravity_block_"; - - switch (gravity_direction) { - case GravityDirection::NORTH: - texture_name += "north"; - break; - - case GravityDirection::EAST: - texture_name += "east"; - break; - - case GravityDirection::SOUTH: - texture_name += "south"; - break; - - case GravityDirection::WEST: - texture_name += "west"; - break; - } - // on dessine le bloc normal Block::draw(level); @@ -45,12 +25,8 @@ void GravityBlock::draw(Level& level) { icon_sprite.setColor(sf::Color(255, 255, 255, opacity)); // on dessine l'icône - icon_sprite.setTexture(*level.getResourceManager().getTexture( - texture_name + ".tga" - )); - icon_sprite.setPosition(getPosition()); - level.getWindow().draw(icon_sprite); + level.getManager().getWindow().draw(icon_sprite); } void GravityBlock::activate(Game& game, Object::Ptr object) { @@ -104,4 +80,29 @@ GravityDirection GravityBlock::getGravityDirection() const { void GravityBlock::setGravityDirection(GravityDirection set_gravity_direction) { gravity_direction = set_gravity_direction; + + // sélectionne le sprite d'icône selon la direction + std::string texture; + + switch (gravity_direction) { + case GravityDirection::NORTH: + texture = "north"; + break; + + case GravityDirection::EAST: + texture = "east"; + break; + + case GravityDirection::SOUTH: + texture = "south"; + break; + + case GravityDirection::WEST: + texture = "west"; + break; + } + + icon_sprite.setTexture(*ResourceManager::get().getTexture( + "objects/gravity_block_" + texture + ".tga" + )); } diff --git a/src/kill_block.cpp b/src/kill_block.cpp index 3643915..33945ea 100644 --- a/src/kill_block.cpp +++ b/src/kill_block.cpp @@ -12,9 +12,9 @@ Object::Ptr KillBlock::clone() const { return Object::Ptr(new KillBlock(*this)); } -void KillBlock::prepareDraw(ResourceManager& resources) { - Block::prepareDraw(resources); - sprite.setTexture(*resources.getTexture("kill_block.tga")); +void KillBlock::prepareDraw() { + Block::prepareDraw(); + sprite.setTexture(*ResourceManager::get().getTexture("objects/kill_block.tga")); } void KillBlock::activate(Game& game, Object::Ptr object) { diff --git a/src/level.cpp b/src/level.cpp index 701cbb2..02df932 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -1,4 +1,5 @@ #include "manager.hpp" +#include "resource_manager.hpp" #include "level.hpp" #include "player.hpp" #include "block.hpp" @@ -53,7 +54,7 @@ Level::Level(Manager& manager) : State(manager) { // métadonnées par défaut setName(sf::String("Nouveau niveau")); - setPath(getResourceManager().getLevelPath("new_level.dat")); + setPath((ResourceManager::get().getLevelsPath() / "new_level.dat").string()); setTotalTime(30); // zone de jeu par défaut @@ -73,7 +74,7 @@ Level::~Level() {} void Level::enable() { // positionnement par défaut de la caméra - sf::Vector2u window_size = getWindow().getSize(); + sf::Vector2u window_size = getManager().getWindow().getSize(); camera.setSize(window_size.x, window_size.y); camera.setCenter(0, 0); @@ -252,7 +253,7 @@ void Level::processEvent(const sf::Event& event) { } void Level::draw() { - sf::RenderWindow& window = getWindow(); + sf::RenderWindow& window = getManager().getWindow(); sf::Vector2u window_size = window.getSize(); // animation de la rotation de la caméra @@ -288,7 +289,7 @@ void Level::draw() { // on dessine le fond s'il y en a un if (background != "") { - auto bg_texture = getResourceManager().getTexture(background); + auto bg_texture = ResourceManager::get().getTexture("levels/" + background); sf::Vector2f bg_size = (sf::Vector2f) bg_texture->getSize(); background_sprite.setTexture(*bg_texture); @@ -484,7 +485,7 @@ sf::View Level::getCamera() const { } sf::Vector2f Level::pixelToCoords(sf::Vector2i pixel) { - sf::RenderWindow& window = getWindow(); + sf::RenderWindow& window = getManager().getWindow(); sf::View old_view = window.getView(); window.setView(camera); @@ -495,7 +496,7 @@ sf::Vector2f Level::pixelToCoords(sf::Vector2i pixel) { } sf::Vector2i Level::coordsToPixel(sf::Vector2f coords) { - sf::RenderWindow& window = getWindow(); + sf::RenderWindow& window = getManager().getWindow(); sf::View old_view = window.getView(); window.setView(camera); diff --git a/src/manager.cpp b/src/manager.cpp index 9f65ce8..959ffa4 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -1,35 +1,36 @@ #include "state.hpp" #include "manager.hpp" +#include "resource_manager.hpp" #include const unsigned int Manager::FPS = 60; const sf::Time Manager::FRAME_TIME = sf::seconds(1.f / Manager::FPS); const float Manager::GRID = 32; -Manager::Manager() : previous_time(sf::seconds(0)), title(sf::String(L"")), +Manager::Manager() : title(sf::String(L"")), previous_time(sf::seconds(0)), previous_state(nullptr) { - // préchargement des textures - resource_manager.preload(); - - // ajout des polices - desktop.GetEngine().GetResourceManager(). - AddFont("overpass", resource_manager.getFont("overpass.ttf")); - desktop.GetEngine().GetResourceManager(). - AddFont("overpass-bold", resource_manager.getFont("overpass-bold.ttf")); - desktop.GetEngine().GetResourceManager(). - AddFont("monoid", resource_manager.getFont("monoid.ttf")); - - // chargement du thème du desktop - desktop.LoadThemeFromFile("res/gui.theme"); - - // 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) + // ajout des polices dans le gestionnaire de ressources + // de la librairie pour l'interface + desktop.GetEngine().GetResourceManager().AddFont( + "overpass", ResourceManager::get().getFont("overpass.ttf") ); + desktop.GetEngine().GetResourceManager().AddFont( + "overpass-bold", ResourceManager::get().getFont("overpass-bold.ttf") + ); + + desktop.GetEngine().GetResourceManager().AddFont( + "monoid", ResourceManager::get().getFont("monoid.ttf") + ); + + // chargement du thème de l'interface + desktop.LoadThemeFromFile("res/gui.theme"); + + // création de la fenêtre du jeu + window.create(sf::VideoMode(704, 480), "Skizzle", sf::Style::Default); + + // FIXME: après avoir supprimé ::useGUIView(), supprimer ceci // récupération de la vue par défaut comme vue du gui gui_view = window.getDefaultView(); } @@ -52,12 +53,14 @@ void Manager::start() { } } + // FIXME: après avoir supprimé ::useGUIView(), supprimer ceci // redimensionnement de la vue par défaut if (event.type == sf::Event::Resized) { gui_view = sf::View(sf::FloatRect( 0, 0, event.size.width, event.size.height )); } + /////////////////////////// // événements de l'interface desktop.HandleEvent(event); @@ -119,34 +122,6 @@ void Manager::popState() { states.pop(); } -sf::RenderWindow& Manager::getWindow() { - return window; -} - -unsigned int Manager::getFramerate() { - return framerate; -} - -void Manager::setFramerate(unsigned int set_framerate) { - // on ne modifie le framerate maximal que s'il a changé - if (set_framerate != framerate) { - window.setFramerateLimit(set_framerate); - framerate = set_framerate; - } -} - -sf::Time Manager::getCurrentTime() const { - return clock.getElapsedTime(); -} - -ResourceManager& Manager::getResourceManager() { - return resource_manager; -} - -void Manager::useGUIView() { - window.setView(gui_view); -} - bool Manager::isInsideGUI(const sf::Event& event) { sf::Vector2f check_point; bool should_check_point = false; @@ -183,6 +158,20 @@ bool Manager::isInsideGUI(const sf::Event& event) { return false; } +sf::Time Manager::getCurrentTime() const { + return clock.getElapsedTime(); +} + +sf::RenderWindow& Manager::getWindow() { + return window; +} + +// FIXME: à supprimer après avoir supprimé ::useGUIView() +void Manager::useGUIView() { + window.setView(gui_view); +} +/////////////////////////// + void Manager::addWidget(sfg::Widget::Ptr widget) { widgets.push_back(widget); desktop.Add(widget); diff --git a/src/menu.cpp b/src/menu.cpp index d28cfad..fa6cec3 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,8 +1,10 @@ #include "manager.hpp" +#include "resource_manager.hpp" #include "menu.hpp" #include "rules.hpp" #include "editor.hpp" #include "game.hpp" +#include #include /** @@ -27,7 +29,9 @@ namespace { } Menu::Menu(Manager& manager) : State(manager) { - background.setTexture(*getResourceManager().getTexture("bg_menu.tga")); + background.setTexture(*ResourceManager::get().getTexture("menu.tga")); + font = ResourceManager::get().getFont("overpass.ttf"); + loadMainMenu(); } @@ -36,10 +40,10 @@ Menu::~Menu() {} void Menu::enable() { // attributs de la fenêtre getManager().setTitle(""); - getManager().setFramerate(Manager::FPS); + getManager().getWindow().setFramerateLimit(Manager::FPS); // joue la musique du menu - getResourceManager().playMusic("menu.ogg"); + ResourceManager::get().playMusic("menu.ogg"); } void Menu::processEvent(const sf::Event& event) { @@ -102,9 +106,8 @@ void Menu::processEvent(const sf::Event& event) { void Menu::frame() { // affichage du menu - sf::RenderWindow& window = getWindow(); + sf::RenderWindow& window = getManager().getWindow(); sf::Vector2f size = (sf::Vector2f) window.getSize(); - std::shared_ptr font = getResourceManager().getFont("raleway.ttf"); // on s'assure d'être dans la vue par défaut (pas de zoom, 0x0 en haut gauche) getManager().useGUIView(); @@ -191,12 +194,12 @@ void Menu::loadLevelMenu() { actions.clear(); selection = 0; - std::vector path_list = getResourceManager().getLevelList(); - std::vector name_list; + std::vector path_list = + ResourceManager::get().getFiles(ResourceManager::get().getLevelsPath()); for (auto it = path_list.begin(); it != path_list.end(); it++) { - choices.push_back(getLevelName(getManager(), *it)); - actions.push_back(std::bind(&Menu::launchGame, this, *it)); + choices.push_back(getLevelName(getManager(), boost::filesystem::canonical(*it).string())); + actions.push_back(std::bind(&Menu::launchGame, this, boost::filesystem::canonical(*it).string())); } choices.push_back(sf::String(L"Retour")); @@ -208,15 +211,15 @@ void Menu::loadEditorMenu() { actions.clear(); selection = 0; - std::vector path_list = getResourceManager().getLevelList(); - std::vector name_list; + std::vector path_list = + ResourceManager::get().getFiles(ResourceManager::get().getLevelsPath()); choices.push_back(L"Créer un nouveau niveau"); actions.push_back(std::bind(&Menu::launchEditor, this, "")); for (auto it = path_list.begin(); it != path_list.end(); it++) { - choices.push_back(getLevelName(getManager(), *it)); - actions.push_back(std::bind(&Menu::launchEditor, this, *it)); + choices.push_back(getLevelName(getManager(), boost::filesystem::canonical(*it).string())); + actions.push_back(std::bind(&Menu::launchEditor, this, boost::filesystem::canonical(*it).string())); } choices.push_back("Retour"); diff --git a/src/player.cpp b/src/player.cpp index a9f5ac1..e2c938e 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1,4 +1,5 @@ #include "manager.hpp" +#include "resource_manager.hpp" #include "player.hpp" #include "game.hpp" @@ -70,12 +71,12 @@ void Player::draw(Level& level) { texture_name = "player_2.tga"; } - sprite.setTexture(level.getResourceManager().getTexture(texture_name).get()); + sprite.setTexture(ResourceManager::get().getTexture("objects/" + texture_name).get()); sprite.setRadius(getRadius()); sprite.setOrigin(sf::Vector2f(getRadius(), getRadius())); sprite.setPosition(getPosition()); - shadow_sprite.setTexture(level.getResourceManager().getTexture("player_shadow.tga").get()); + shadow_sprite.setTexture(ResourceManager::get().getTexture("objects/player_shadow.tga").get()); shadow_sprite.setRadius(getRadius() - 1); shadow_sprite.setOrigin(sf::Vector2f(getRadius() - 1, getRadius() - 1)); shadow_sprite.setPosition(getPosition()); @@ -108,8 +109,8 @@ void Player::draw(Level& level) { } // dessin des sprites - level.getWindow().draw(sprite); - level.getWindow().draw(shadow_sprite); + level.getManager().getWindow().draw(sprite); + level.getManager().getWindow().draw(shadow_sprite); } void Player::activate(Game& game, Object::Ptr object) { diff --git a/src/resource_manager.cpp b/src/resource_manager.cpp index ded1fde..2b74aaf 100644 --- a/src/resource_manager.cpp +++ b/src/resource_manager.cpp @@ -4,14 +4,12 @@ namespace fs = boost::filesystem; -ResourceManager::get() { +ResourceManager& ResourceManager::get() { static ResourceManager manager; return manager; } -ResourceManager::ResourceManager() : preloaded(false), - music_volume(20), playing_state(false), current_music("") { - +ResourceManager::ResourceManager() : is_playing(false), music_volume(20) { // mise en mémoire des chemins vers les dossiers de ressources fs::path res_path = fs::current_path() / "res"; @@ -21,11 +19,11 @@ ResourceManager::ResourceManager() : preloaded(false), musics_path = res_path / "musics"; // initialisation de la musique en bouclage et au volume par défaut - music.setLoop(true); - music.setVolume(music_volume); + current_music.setLoop(true); + current_music.setVolume(music_volume); } -std::vector ResourceManager::getFiles(fs::path dir) const { +std::vector ResourceManager::getFiles(fs::path path) const { fs::recursive_directory_iterator dir(path), end; std::vector result; @@ -42,19 +40,19 @@ std::vector ResourceManager::getFiles(fs::path dir) const { return result; } -const fs::path& getTexturesPath() const { +const fs::path& ResourceManager::getTexturesPath() const { return textures_path; } -const fs::path& getFontsPath() const { +const fs::path& ResourceManager::getFontsPath() const { return fonts_path; } -const fs::path& getLevelsPath() const { +const fs::path& ResourceManager::getLevelsPath() const { return levels_path; } -const fs::path& getMusicsPath() const { +const fs::path& ResourceManager::getMusicsPath() const { return musics_path; } @@ -136,7 +134,7 @@ void ResourceManager::playMusic(std::string name) { if (current_music_path == music_path) { if (!is_playing) { is_playing = true; - music.play(); + current_music.play(); } return; @@ -146,7 +144,7 @@ void ResourceManager::playMusic(std::string name) { std::string full_path = fs::canonical(music_path).string(); std::cout << "Lecture de la musique " << name << "... "; - if (music.openFromFile(full_path)) { + if (current_music.openFromFile(full_path)) { std::cout << "OK!" << std::endl; } else { std::cerr << "ERR!" << std::endl; @@ -154,12 +152,12 @@ void ResourceManager::playMusic(std::string name) { current_music_path = music_path; is_playing = true; - music.play(); + current_music.play(); } void ResourceManager::stopMusic() { is_playing = false; - music.stop(); + current_music.stop(); } float ResourceManager::getMusicVolume() const { @@ -168,5 +166,5 @@ float ResourceManager::getMusicVolume() const { void ResourceManager::setMusicVolume(float set_music_volume) { music_volume = set_music_volume; - music.setVolume(music_volume); + current_music.setVolume(music_volume); } diff --git a/src/rules.cpp b/src/rules.cpp index e679ec6..02cc1e5 100644 --- a/src/rules.cpp +++ b/src/rules.cpp @@ -1,9 +1,9 @@ #include "rules.hpp" #include "manager.hpp" +#include "resource_manager.hpp" Rules::Rules(Manager& manager) : State(manager) { - background.setTexture(*getResourceManager().getTexture("bg_rules.png")); - getWindow().setFramerateLimit(Manager::FPS); + background.setTexture(*ResourceManager::get().getTexture("rules.png")); } Rules::~Rules() {} @@ -11,10 +11,10 @@ Rules::~Rules() {} void Rules::enable() { // attributs de la fenêtre getManager().setTitle(L"Règles"); - getManager().setFramerate(Manager::FPS); + getManager().getWindow().setFramerateLimit(Manager::FPS); // joue la musique du menu - getResourceManager().playMusic("menu.ogg"); + ResourceManager::get().playMusic("menu.ogg"); } void Rules::processEvent(const sf::Event& event) { @@ -30,7 +30,7 @@ void Rules::processEvent(const sf::Event& event) { } void Rules::frame() { - sf::RenderWindow& window = getWindow(); + sf::RenderWindow& window = getManager().getWindow(); sf::Vector2f size = (sf::Vector2f) window.getSize(); // on s'assure d'être dans la vue par défaut (pas de zoom, 0x0 en haut gauche) diff --git a/src/state.cpp b/src/state.cpp index 8b31fc3..686dbff 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -11,19 +11,3 @@ Manager& State::getManager() { const Manager& State::getManager() const { return manager; } - -ResourceManager& State::getResourceManager() { - return manager.getResourceManager(); -} - -const ResourceManager& State::getResourceManager() const { - return manager.getResourceManager(); -} - -sf::RenderWindow& State::getWindow() { - return manager.getWindow(); -} - -const sf::RenderWindow& State::getWindow() const { - return manager.getWindow(); -} diff --git a/src/switch_block.cpp b/src/switch_block.cpp index 7045732..47300c0 100644 --- a/src/switch_block.cpp +++ b/src/switch_block.cpp @@ -1,4 +1,5 @@ #include "manager.hpp" +#include "resource_manager.hpp" #include "utility.hpp" #include "switch_block.hpp" #include "game.hpp" @@ -7,6 +8,9 @@ const unsigned int SwitchBlock::TYPE_ID = 6; SwitchBlock::SwitchBlock() : Block(), opacity(255), used(false) { icon_sprite.setOrigin(sf::Vector2f(23, 23)); + icon_sprite.setTexture(*ResourceManager::get().getTexture( + "objects/switch_block.tga" + )); } SwitchBlock::~SwitchBlock() {} @@ -24,12 +28,8 @@ void SwitchBlock::draw(Level& level) { icon_sprite.setColor(sf::Color(255, 255, 255, opacity)); // on dessine l'icône - icon_sprite.setTexture(*level.getResourceManager().getTexture( - "switch_block.tga" - )); - icon_sprite.setPosition(getPosition()); - level.getWindow().draw(icon_sprite); + level.getManager().getWindow().draw(icon_sprite); } void SwitchBlock::activate(Game& game, Object::Ptr object) { diff --git a/src/toolbar.cpp b/src/toolbar.cpp index e925fdd..e0fcfbd 100644 --- a/src/toolbar.cpp +++ b/src/toolbar.cpp @@ -110,7 +110,7 @@ void Toolbar::addCreator(std::string path, std::function creator) // on crée un bouton d'objet correspondant au créateur donné ObjectButton::Ptr button = ObjectButton::Create( sfg::Image::Create( - *editor.getResourceManager().getImage("toolbar_" + path + ".tga") + *ResourceManager::get().getImage("toolbar/" + path + ".tga") ), creators_group ); diff --git a/src/widget_button.cpp b/src/widget_button.cpp deleted file mode 100644 index 1d7174d..0000000 --- a/src/widget_button.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include "manager.hpp" -#include "widget_button.hpp" - -const unsigned int WidgetButton::ARROW_UP = 0; -const unsigned int WidgetButton::ARROW_DOWN = 1; - -/** - * Définition des variables et fonctions globales internes - * (accessibles uniquement dans ce fichier) - */ -namespace { - const sf::Color ARROW_COLOR = sf::Color(33, 33, 33); - const sf::Color NORMAL_COLOR = sf::Color(230, 230, 230); - const sf::Color HOVER_COLOR = sf::Color(220, 220, 220); - const sf::Color ACTIVE_COLOR = sf::Color(190, 190, 190); -} - -WidgetButton::WidgetButton( - Manager& manager, std::function click_cb, - sf::Vector2f size, unsigned int shape -) : manager(manager), click_cb(click_cb), shape(shape), button(size) { - if (shape == WidgetButton::ARROW_UP || shape == WidgetButton::ARROW_DOWN) { - button_shape.setPrimitiveType(sf::Triangles); - button_shape.resize(3); - - button_shape[0].color = ARROW_COLOR; - button_shape[1].color = ARROW_COLOR; - button_shape[2].color = ARROW_COLOR; - } -} - -bool WidgetButton::processEvent(const sf::Event& event) { - if (event.type == sf::Event::MouseButtonPressed) { - sf::Vector2f position(event.mouseButton.x, event.mouseButton.y); - - if (event.mouseButton.button == sf::Mouse::Left) { - // clic gauche sur le bouton : appel de la callback - if (button.getGlobalBounds().contains(position)) { - click_cb(); - return true; - } - } - } - - return false; -} - -void WidgetButton::draw(sf::Vector2f position) { - sf::RenderWindow& window = manager.getWindow(); - - // positionnement du bouton - button.setPosition(position); - sf::FloatRect box = button.getGlobalBounds(); - sf::Vector2f center(box.left + box.width / 2, box.top + box.height / 2); - - if (shape == WidgetButton::ARROW_UP) { - button_shape[0].position = center + sf::Vector2f(-5, 2); - button_shape[1].position = center + sf::Vector2f(5, 2); - button_shape[2].position = center + sf::Vector2f(0, -2); - } - - if (shape == WidgetButton::ARROW_DOWN) { - button_shape[0].position = center + sf::Vector2f(-5, -2); - button_shape[1].position = center + sf::Vector2f(5, -2); - button_shape[2].position = center + sf::Vector2f(0, 2); - } - - // coloration des boutons si enfoncement - sf::Vector2f mouse_position = (sf::Vector2f) sf::Mouse::getPosition(window); - button.setFillColor(NORMAL_COLOR); - - if (button.getGlobalBounds().contains(mouse_position)) { - if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) { - button.setFillColor(ACTIVE_COLOR); - } else { - button.setFillColor(HOVER_COLOR); - } - } - - window.draw(button); - window.draw(button_shape); -} diff --git a/src/widget_timer.cpp b/src/widget_timer.cpp deleted file mode 100644 index 86ca44b..0000000 --- a/src/widget_timer.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "manager.hpp" -#include "widget_timer.hpp" - -WidgetTimer::WidgetTimer(Manager& manager, bool can_change, std::function time_left_cb) : - manager(manager), can_change(can_change), time_left_cb(time_left_cb), - timer_zone(sf::Vector2f(100, 32)), - timer_up(manager, std::bind(&WidgetTimer::addTime, this), sf::Vector2f(30, 16), WidgetButton::ARROW_UP), - timer_down(manager, std::bind(&WidgetTimer::subtractTime, this), sf::Vector2f(30, 16), WidgetButton::ARROW_DOWN) { - - // initialisation des formes - timer_seconds_text.setFont(*manager.getResourceManager().getFont("monoid.ttf")); - timer_seconds_text.setCharacterSize(18); - timer_seconds_text.setColor(sf::Color::Black); - - timer_sep_text.setString(":"); - timer_sep_text.setFont(*manager.getResourceManager().getFont("monoid.ttf")); - timer_sep_text.setCharacterSize(18); - timer_sep_text.setColor(sf::Color::Black); - - timer_minutes_text.setFont(*manager.getResourceManager().getFont("monoid.ttf")); - timer_minutes_text.setCharacterSize(18); - timer_minutes_text.setColor(sf::Color::Black); -} - -bool WidgetTimer::processEvent(const sf::Event& event) { - // si le timer n'est pas modifiable, pas d'évent à gérer - if (!can_change) { - return false; - } - - // gestion des boutons - if (timer_up.processEvent(event)) { - return true; - } - - if (timer_down.processEvent(event)) { - return true; - } - - if (event.type == sf::Event::MouseButtonPressed) { - sf::Vector2f position(event.mouseButton.x, event.mouseButton.y); - - // clic dans le widget : ne rien faire, mais empêcher le traversement - if (timer_zone.getGlobalBounds().contains(position)) { - return true; - } - } - - if (event.type == sf::Event::MouseWheelScrolled && event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) { - // scroll sur le timer : modification du temps alloué au niveau - sf::Vector2f position(event.mouseWheelScroll.x, event.mouseWheelScroll.y); - - if (timer_zone.getGlobalBounds().contains(position)) { - time_left_cb(time_left + round(event.mouseWheelScroll.delta * 10)); - return true; - } - } - - return false; -} - -void WidgetTimer::draw(sf::Vector2f position) { - sf::RenderWindow& window = manager.getWindow(); - - // zone de fond du timer - timer_zone.setPosition(position); - window.draw(timer_zone); - - // affichage du temps du niveau - sf::String minutes = std::to_string(time_left / 60); - sf::String seconds = std::to_string(time_left % 60); - - // ajout d'un zéro devant les secondes si nécessaire - if (minutes.getSize() == 1) { - minutes = "0" + minutes; - } - - if (seconds.getSize() == 1) { - seconds = "0" + seconds; - } - - timer_minutes_text.setString(minutes); - timer_seconds_text.setString(seconds); - - float base_x = can_change ? 30 : 45; - timer_sep_text.setPosition(position + sf::Vector2f(base_x, 6)); - timer_seconds_text.setPosition(position + sf::Vector2f(base_x + 8, 6)); - timer_minutes_text.setPosition(position + sf::Vector2f( - base_x - 3 - floor(timer_minutes_text.getGlobalBounds().width), 6 - )); - - window.draw(timer_sep_text); - window.draw(timer_seconds_text); - window.draw(timer_minutes_text); - - // interface de modification du temps - if (can_change) { - timer_up.draw(position + sf::Vector2f(70, 0)); - timer_down.draw(position + sf::Vector2f(70, 16)); - } -} - -void WidgetTimer::addTime() { - time_left_cb(time_left + 1); -} - -void WidgetTimer::subtractTime() { - time_left_cb(time_left - 1); -} - -void WidgetTimer::setTimeLeft(int set_time_left) { - time_left = set_time_left; -}