diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 21 | ||||
-rw-r--r-- | src/resourceManager.h | 54 |
2 files changed, 72 insertions, 3 deletions
diff --git a/src/main.cpp b/src/main.cpp index 94bf125..bd330fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "graphics/animation.h" #include "entity.h" +#include "resourceManager.h" namespace gph = Graphics; @@ -34,8 +35,8 @@ class Unit: Entity{ sf::Vector2f speed; - Unit(){ - spritesheet_.loadFromFile("assets/img/Player/Player Angle 1 Sheet.png"); + Unit(sf::Texture& spritesheet){ + spritesheet_ = spritesheet; standing_animation = gph::Animation(spritesheet_, 6, 60, 44, 48, 0); walking_animation = gph::Animation(spritesheet_, 8, 60, 44, 48, 1); shooting_animation = gph::FlippedAnimation<gph::OneShotAnimation>(spritesheet_, 6, 60, 44, 48, 2); @@ -66,16 +67,30 @@ class Unit: Entity{ sprite.setScale(8,8); sprite.setTextureRect(current_animation->next(dt)); sprite.setPosition(position.x, position.y); + sprite = sprite; } }; + +enum class TextureId{ + Angle1, + Angle2, + Front, + Back, + Side +}; + int main() { sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Demo Game"); sf::Event event; sf::Clock clock; - Unit unit; + + ResourceManager<sf::Texture, TextureId> TextureManager; + TextureManager.load( TextureId::Angle1, "assets/img/Player/Player Angle 1 Sheet.png"); + + Unit unit = Unit(TextureManager.get(TextureId::Angle1)); renderWindow.setFramerateLimit(60); diff --git a/src/resourceManager.h b/src/resourceManager.h new file mode 100644 index 0000000..be6e250 --- /dev/null +++ b/src/resourceManager.h @@ -0,0 +1,54 @@ +#ifndef RESOURCE_MANAGER_H +#define RESOURCE_MANAGER_H + +#include <map> +#include <string> +#include <memory> +#include <stdexcept> +#include <cassert> + +template <typename Resource, typename Identifier> +class ResourceManager{ + private: + std::map<Identifier,std::unique_ptr<Resource>> resourceMap; + public: + void load(Identifier id, const std::string& path); + Resource& get(Identifier id); + const Resource& get(Identifier id) const; +}; + +template <typename Resource, typename Identifier> +void +ResourceManager<Resource, Identifier>::load(Identifier id, + const std::string& path){ + auto resource = std::make_unique<Resource>(); + if( !resource->loadFromFile(path) ){ + throw("ResoureManager::load - Failed to load filename: " + path); + } + auto inserted = resourceMap.insert( + std::make_pair(id, std::move(resource)) + ); + assert(inserted.second); +} + +template <typename Resource, typename Identifier> +Resource& +ResourceManager<Resource, Identifier>::get(Identifier id) +{ + auto found = resourceMap.find(id); + assert(found != resourceMap.end()); + + return *found->second; +} + +template <typename Resource, typename Identifier> +const Resource& +ResourceManager<Resource, Identifier>::get(Identifier id) const +{ + auto found = resourceMap.find(id); + assert(found != resourceMap.end()); + + return *found->second; +} + +#endif // RESOURCE_MANAGER_H |