From 49bbaaca99b03a78b805d26c2fd296990f3fbaf8 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Fri, 29 Nov 2024 10:41:50 +0100 Subject: [PATCH] Syntaxe fixes --- ECS/ECS.hpp | 42 ++++++++++++++++++++++++++++++------------ TileMap.cpp | 18 +++++++++++++++--- TileMap.hpp | 3 +++ 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/ECS/ECS.hpp b/ECS/ECS.hpp index 34c7aab..5c8131b 100644 --- a/ECS/ECS.hpp +++ b/ECS/ECS.hpp @@ -13,21 +13,23 @@ #define ECS_HPP #include -#include -#include -#include -#include -#include -#include -#include "../ChannelManager.hpp" +#include // array +#include // bitset +#include // runtime_error +#include // unique_ptr, make_unique +#include // vector +#include "../ChannelManager.hpp" // Class ChannelManager using std::bitset, std::array, std::vector, std::unique_ptr, std::make_unique, std::move, std::forward, std::runtime_error, std::sort, std::remove_if; +// Prédefinition des class Component et Entity class Component; class Entity; +// Type pour l'ID des Components using ComponentID = std::size_t; +// Donne un ID différent en fonction du Component donné inline ComponentID getComponentTypeID() { static ComponentID lastID = 0; @@ -40,11 +42,14 @@ template inline ComponentID getComponentTypeID() noexcept return typeID; } +// Nombre maximum de Components dans une Entity constexpr std::size_t maxComponents = 32; +// Definition de types pour la contenance des Components using ComponentBitSet = bitset; using ComponentArray = array; +// Definition de la class Components struct Component { Entity *entity; @@ -55,18 +60,22 @@ struct Component { virtual ~Component() {} }; +// Definition de la class Entity class Entity { public: + // Met à jours les components void update() { for(auto &c : m_components) c->update(); } + // Dessine les components void draw() { for(auto &c : m_components) c->draw(); } + // Ajouter un composant à l'entité bool isActive() const { return m_active; } void destroy() { m_active = false; } @@ -96,6 +105,7 @@ class Entity { return *c; } + // Vérifier si l'entité à un composant donné template bool hasComponent() const { @@ -117,6 +127,7 @@ class Entity { return false; } + // Prendre un argument en tant qu'objet template T& getComponent() const { @@ -139,6 +150,7 @@ class Entity { throw runtime_error("Composant non trouvé.\n"); } + // Priorité de dessin int draw_priority; private: @@ -154,7 +166,10 @@ class Manager { public: void update() { - for(auto &e : m_entities) e->update(); + for(auto &e : m_entities) + { + e->update(); + } } void draw() @@ -163,9 +178,12 @@ class Manager { for(int i {0}; i < static_cast(m_entities.size()); i++) drawOrder[i] = i; - sort(drawOrder.begin(), drawOrder.end(), [this](const int &a, const int &b) { - return m_entities.at(a)->draw_priority < m_entities.at(b)->draw_priority; - }); + sort(drawOrder.begin(), drawOrder.end(), + [this](const int &a, const int &b) + { + return m_entities.at(a)->draw_priority < m_entities.at(b)->draw_priority; + } + ); for(int i {0}; i < static_cast(m_entities.size()); i++) { @@ -190,7 +208,7 @@ class Manager { return *e; } - std::size_t getNumberOfEntity() const + size_t getNumberOfEntities() const { return m_entities.size(); } diff --git a/TileMap.cpp b/TileMap.cpp index 81d38ae..6a9ba5d 100644 --- a/TileMap.cpp +++ b/TileMap.cpp @@ -14,7 +14,6 @@ #include "Camera.hpp" #include "Game.hpp" #include "MapManager.hpp" -#include "externLibs/nlohmann/json.hpp" #include "TextureManager.hpp" #include "TileMap.hpp" #include "Vector2D.hpp" @@ -27,8 +26,7 @@ using json = nlohmann::json; void TileMap::LoadTileMap(const string &path) { // Load the map data from memory - json *mapData; - mapData = Game::mapManager.LoadMap(path); + json *mapData = Game::mapManager.LoadMap(path); // Get tilemap width and height same for world tileMapWidth = mapData->at("TileMapWidth").get(); @@ -81,6 +79,8 @@ void TileMap::LoadTileMap(const string &path) } } + LoadEntities(mapData); + if(mapData->contains("PlayerInitPos")) { m_playerInitPos.x = mapData->at("PlayerInitPos").at("x").get() * TILE_SIZE * TILEMAP_SCALE; @@ -98,6 +98,18 @@ void TileMap::LoadTileMap(const string &path) m_nextMaps.emplace(it->at("Number").get(), nextMap); } + // Texture renderer creating + if(Game::textureRenderer != NULL) SDL_DestroyTexture(Game::textureRenderer); + Game::textureRenderer = SDL_CreateTexture(Game::renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, worldWidth, worldHeight); + if(Game::textureRenderer == NULL) + { + cerr << "Erreur SDL_CreateTexture : " << SDL_GetError() << '\n'; + throw runtime_error("Impossible de creer la texture de rendu.\n"); + } +} + +void TileMap::LoadEntities(json *mapData) +{ // Erase lasts entities and load news if there are some std::size_t numberOfEntity {Game::entityManager.getNumberOfEntity()}; for(std::size_t i {1}; i < numberOfEntity; i++) Game::entityManager.destroy(i); diff --git a/TileMap.hpp b/TileMap.hpp index 04a545c..d2e7b00 100644 --- a/TileMap.hpp +++ b/TileMap.hpp @@ -17,12 +17,14 @@ #include #include #include +#include "externLibs/nlohmann/json.hpp" #include "Vector2D.hpp" #define TILE_SIZE 16 #define TILEMAP_SCALE 2 using std::string, std::vector, std::unordered_map; +using json = nlohmann::json; using TileID = int; @@ -36,6 +38,7 @@ class TileMap { TileMap() = default; ~TileMap(); + void LoadEntities(json *); void LoadTileMap(const string &); void LoadNextMap(int); void LoadTileset(const string &);