diff --git a/include/game.hpp b/include/game.hpp index 5d0b87e..c6ef18a 100644 --- a/include/game.hpp +++ b/include/game.hpp @@ -32,6 +32,22 @@ private: public: Game(); + virtual ~Game(); + + /** + * Charge un niveau de jeu depuis le fichier donné + */ + void load(); + + /** + * Sauvegarde la configuration actuelle comme un niveau + */ + void save(); + + /** + * Libère les ressources du niveau + */ + void clear(); /** * Demande le passage à la frame suivante sur diff --git a/include/manager.hpp b/include/manager.hpp index 4bb92b8..eb0377c 100644 --- a/include/manager.hpp +++ b/include/manager.hpp @@ -3,6 +3,7 @@ #include "resource_manager.hpp" #include "view.hpp" +#include /** * Gestionnaire principal de toutes les vues et @@ -17,10 +18,9 @@ private: ResourceManager resource_manager; std::array keys; -public: - // FIXME: devrait être privé - View* view; + std::shared_ptr view; +public: Manager(); /** @@ -28,6 +28,11 @@ public: */ void start(); + /** + * Charge la vue donnée dans le jeu + */ + void setView(std::shared_ptr set_view); + /** * Renvoie la fenêtre actuellement utilisée pour le dessin */ diff --git a/include/view.hpp b/include/view.hpp index fd3b384..1fd02ea 100644 --- a/include/view.hpp +++ b/include/view.hpp @@ -11,13 +11,13 @@ class Object; */ class View { public: + virtual ~View() {} + /** * Demande le passage à la frame suivante sur * cette vue */ virtual void frame(Manager& manager) = 0; - - virtual void addObject(Object& object) = 0; }; #endif diff --git a/src/game.cpp b/src/game.cpp index e451b01..9551e80 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -3,6 +3,8 @@ #include #include +#include "player.hpp" + Game::Game() : accumulator(0.f) { if (!music.openFromFile("./res/music_lvl1.wav")) { // erreur @@ -11,10 +13,36 @@ Game::Game() : accumulator(0.f) { music.play(); music.setVolume(15); music.setLoop(true); + + load(); } -void Game::addObject(Object& object) { - objects.push_back(&object); +Game::~Game() { + clear(); +} + +void Game::load() { + // vide le niveau précédent s'il y a lieu + if (objects.size()) { + clear(); + } + + // TODO: faire une vraie fonction de chargement + Player* player1 = new Player(3.5f * Constants::GRID, 10 * Constants::GRID); + objects.push_back(player1); +} + +void Game::save() { + // TODO: faire une fonction d'enregistrement + // TODO: migrer sur une classe commune Game <-> Editor +} + +void Game::clear() { + for (unsigned int i = 0; i < objects.size(); i++) { + delete objects[i]; + } + + objects.clear(); } void Game::frame(Manager& manager) { diff --git a/src/main.cpp b/src/main.cpp index 02d0bc8..1e2492f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,69 +5,13 @@ #include "constants.hpp" #include #include +#include int main() { Manager manager; + std::shared_ptr game = std::shared_ptr(new Game); - Player player1(3.5f * Constants::GRID, 10 * Constants::GRID); - player1.setPlayerNumber(1); - Player player2(18.5f * Constants::GRID, 10 * Constants::GRID); - player2.setPlayerNumber(2); - Block block01(2 * Constants::GRID, 10 * Constants::GRID); - Block block02(2 * Constants::GRID, 11 * Constants::GRID); - Block block03(3 * Constants::GRID, 11 * Constants::GRID); - Block block04(4 * Constants::GRID, 11 * Constants::GRID); - Block block05(5 * Constants::GRID, 11 * Constants::GRID); - Block block06(6 * Constants::GRID, 11 * Constants::GRID); - Block block07(7 * Constants::GRID, 11 * Constants::GRID); - Block block08(8 * Constants::GRID, 11 * Constants::GRID); - Block block09(9 * Constants::GRID, 11 * Constants::GRID); - Block block10(10 * Constants::GRID, 11 * Constants::GRID); - Block block11(11 * Constants::GRID, 11 * Constants::GRID); - Block block12(12 * Constants::GRID, 11 * Constants::GRID); - Block block13(13 * Constants::GRID, 11 * Constants::GRID); - Block block14(14 * Constants::GRID, 11 * Constants::GRID); - Block block15(15 * Constants::GRID, 11 * Constants::GRID); - Block block16(16 * Constants::GRID, 11 * Constants::GRID); - Block block17(17 * Constants::GRID, 11 * Constants::GRID); - Block block18(18 * Constants::GRID, 11 * Constants::GRID); - Block block19(19 * Constants::GRID, 11 * Constants::GRID); - Block block20(20 * Constants::GRID, 10 * Constants::GRID); - Block block21(20 * Constants::GRID, 11 * Constants::GRID); - Block block22(11 * Constants::GRID, 10 * Constants::GRID); - - player1.setCharge(-.01f); - player2.setCharge(-.01f); - block22.setCharge(1.f); - block22.setMass(2); - - Game game_view; - manager.view = &game_view; - - manager.view->addObject(player1); - manager.view->addObject(player2); - manager.view->addObject(block01); - manager.view->addObject(block02); - manager.view->addObject(block03); - manager.view->addObject(block04); - manager.view->addObject(block05); - manager.view->addObject(block06); - manager.view->addObject(block07); - manager.view->addObject(block08); - manager.view->addObject(block09); - manager.view->addObject(block10); - manager.view->addObject(block11); - manager.view->addObject(block12); - manager.view->addObject(block13); - manager.view->addObject(block14); - manager.view->addObject(block15); - manager.view->addObject(block16); - manager.view->addObject(block17); - manager.view->addObject(block18); - manager.view->addObject(block19); - manager.view->addObject(block20); - manager.view->addObject(block21); - manager.view->addObject(block22); + manager.setView(game); try { manager.start(); diff --git a/src/manager.cpp b/src/manager.cpp index 7836b76..11f85ed 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -40,6 +40,10 @@ void Manager::start() { } } +void Manager::setView(std::shared_ptr set_view) { + view = set_view; +} + sf::RenderWindow& Manager::getWindow() { return window; }