Adaptation du code aux changements
This commit is contained in:
		
							parent
							
								
									3f253fda65
								
							
						
					
					
						commit
						2281924c62
					
				|  | @ -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 | ||||
|  |  | |||
|  | @ -2,7 +2,6 @@ | |||
| #define __SKIZZLE_EDITOR_HPP__ | ||||
| 
 | ||||
| #include "level.hpp" | ||||
| #include "widget_timer.hpp" | ||||
| #include "toolbar.hpp" | ||||
| #include <unordered_map> | ||||
| #include <memory> | ||||
|  | @ -25,7 +24,6 @@ private: | |||
| 
 | ||||
|     DragMode drag_mode; | ||||
| 
 | ||||
|     WidgetTimer widget_timer; | ||||
|     Toolbar toolbar; | ||||
| 
 | ||||
|     /**
 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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<Object::Ptr> pending_kill; | ||||
|     DeathCause death_cause; | ||||
| 
 | ||||
|     WidgetTimer widget_timer; | ||||
|     float time_left; | ||||
| 
 | ||||
|     /**
 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -1,7 +1,6 @@ | |||
| #ifndef __SKIZZLE_MANAGER_HPP__ | ||||
| #define __SKIZZLE_MANAGER_HPP__ | ||||
| 
 | ||||
| #include "resource_manager.hpp" | ||||
| #include <SFGUI/SFGUI.hpp> | ||||
| #include <SFGUI/Widgets.hpp> | ||||
| #include <SFML/Graphics.hpp> | ||||
|  | @ -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<sfg::Widget::Ptr> widgets; | ||||
| 
 | ||||
|     unsigned int framerate; | ||||
|     ResourceManager resource_manager; | ||||
| 
 | ||||
|     sf::Clock clock; | ||||
|     sf::View gui_view; | ||||
|     sf::String title; | ||||
| 
 | ||||
|     State* previous_state; | ||||
|     std::stack<std::unique_ptr<State>> 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<std::unique_ptr<State>> 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); | ||||
| 
 | ||||
|  |  | |||
|  | @ -4,6 +4,7 @@ | |||
| #include "state.hpp" | ||||
| #include <functional> | ||||
| #include <vector> | ||||
| #include <memory> | ||||
| 
 | ||||
| class Manager; | ||||
| 
 | ||||
|  | @ -15,6 +16,7 @@ class Manager; | |||
| class Menu : public State { | ||||
| private: | ||||
|     sf::Sprite background; | ||||
|     std::shared_ptr<sf::Font> font; | ||||
| 
 | ||||
|     std::vector<sf::String> choices; | ||||
|     std::vector<sf::Text> labels; | ||||
|  |  | |||
|  | @ -53,7 +53,7 @@ public: | |||
|     /**
 | ||||
|      * Récupère la liste des fichiers dans le dossier donné | ||||
|      */ | ||||
|     std::vector<boost::filesystem::path> getFiles(boost::filesystem::path dir) const; | ||||
|     std::vector<boost::filesystem::path> getFiles(boost::filesystem::path path) const; | ||||
| 
 | ||||
|     /**
 | ||||
|      * Récupère le chemin vers le dossier des textures | ||||
|  |  | |||
|  | @ -3,7 +3,6 @@ | |||
| 
 | ||||
| #include <SFML/Graphics.hpp> | ||||
| 
 | ||||
| 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 | ||||
|  |  | |||
|  | @ -1,42 +0,0 @@ | |||
| #ifndef __SKIZZLE_UTIL_WIDGET_BUTTON_HPP__ | ||||
| #define __SKIZZLE_UTIL_WIDGET_BUTTON_HPP__ | ||||
| 
 | ||||
| #include <SFML/Graphics.hpp> | ||||
| #include <functional> | ||||
| 
 | ||||
| class Manager; | ||||
| 
 | ||||
| /**
 | ||||
|  * Affiche un bouton pouvant être cliqué | ||||
|  */ | ||||
| class WidgetButton { | ||||
| private: | ||||
|     Manager& manager; | ||||
|     std::function<void(void)> 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<void(void)> 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 | ||||
|  | @ -1,59 +0,0 @@ | |||
| #ifndef __SKIZZLE_UTIL_WIDGET_TIMER_HPP__ | ||||
| #define __SKIZZLE_UTIL_WIDGET_TIMER_HPP__ | ||||
| 
 | ||||
| #include <SFML/Graphics.hpp> | ||||
| #include <functional> | ||||
| #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<void(int)> 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<void(int)> time_left_cb = std::function<void(int)>()); | ||||
| 
 | ||||
|     /**
 | ||||
|      * 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 | ||||
|  | @ -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) { | ||||
|  |  | |||
|  | @ -1,4 +1,5 @@ | |||
| #include "manager.hpp" | ||||
| #include "resource_manager.hpp" | ||||
| #include "editor.hpp" | ||||
| #include "game.hpp" | ||||
| #include <cmath> | ||||
|  | @ -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, | ||||
|  |  | |||
|  | @ -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) { | ||||
|  |  | |||
							
								
								
									
										23
									
								
								src/game.cpp
								
								
								
								
							
							
						
						
									
										23
									
								
								src/game.cpp
								
								
								
								
							|  | @ -2,6 +2,7 @@ | |||
| #include <cmath> | ||||
| #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() { | ||||
|  |  | |||
|  | @ -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" | ||||
|     )); | ||||
| } | ||||
|  |  | |||
|  | @ -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) { | ||||
|  |  | |||
|  | @ -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); | ||||
|  |  | |||
|  | @ -1,35 +1,36 @@ | |||
| #include "state.hpp" | ||||
| #include "manager.hpp" | ||||
| #include "resource_manager.hpp" | ||||
| #include <algorithm> | ||||
| 
 | ||||
| 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); | ||||
|  |  | |||
							
								
								
									
										29
									
								
								src/menu.cpp
								
								
								
								
							
							
						
						
									
										29
									
								
								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 <boost/filesystem.hpp> | ||||
| #include <cmath> | ||||
| 
 | ||||
| /**
 | ||||
|  | @ -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<sf::Font> 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<std::string> path_list = getResourceManager().getLevelList(); | ||||
|     std::vector<std::string> name_list; | ||||
|     std::vector<boost::filesystem::path> 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<std::string> path_list = getResourceManager().getLevelList(); | ||||
|     std::vector<std::string> name_list; | ||||
|     std::vector<boost::filesystem::path> 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"); | ||||
|  |  | |||
|  | @ -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) { | ||||
|  |  | |||
|  | @ -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<fs::path> ResourceManager::getFiles(fs::path dir) const { | ||||
| std::vector<fs::path> ResourceManager::getFiles(fs::path path) const { | ||||
|     fs::recursive_directory_iterator dir(path), end; | ||||
|     std::vector<fs::path> result; | ||||
| 
 | ||||
|  | @ -42,19 +40,19 @@ std::vector<fs::path> 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); | ||||
| } | ||||
|  |  | |||
|  | @ -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)
 | ||||
|  |  | |||
|  | @ -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(); | ||||
| } | ||||
|  |  | |||
|  | @ -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) { | ||||
|  |  | |||
|  | @ -110,7 +110,7 @@ void Toolbar::addCreator(std::string path, std::function<Object::Ptr()> 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 | ||||
|     ); | ||||
| 
 | ||||
|  |  | |||
|  | @ -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<void(void)> 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); | ||||
| } | ||||
|  | @ -1,113 +0,0 @@ | |||
| #include "manager.hpp" | ||||
| #include "widget_timer.hpp" | ||||
| 
 | ||||
| WidgetTimer::WidgetTimer(Manager& manager, bool can_change, std::function<void(int)> 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; | ||||
| } | ||||
		Loading…
	
		Reference in New Issue