From 4e43958f84336e3bcc4104257f51ba84e57fc4b7 Mon Sep 17 00:00:00 2001
From: Ekaitz Zarraga <ekaitz@elenq.tech>
Date: Mon, 12 Dec 2022 23:55:47 +0100
Subject: Add TextureFonts to the ResourceManager

---
 src/app.cpp           |  2 +-
 src/app.h             |  1 +
 src/resourceIds.h     | 11 ++++++++++-
 src/resourceManager.h | 22 +++++++++++++++++++++-
 src/states.h          |  1 +
 5 files changed, 34 insertions(+), 3 deletions(-)

(limited to 'src')

diff --git a/src/app.cpp b/src/app.cpp
index 30bccdf..a8cc661 100644
--- a/src/app.cpp
+++ b/src/app.cpp
@@ -8,7 +8,7 @@ const sf::Time App::TIME_PER_FRAME = sf::seconds(1.f/60.f);
 App::App()
     : textures_()
     , window_(sf::VideoMode(640, 480), "Demo game" )
-    , stateStack_( State::Context{&window_, &textures_} )
+    , stateStack_( State::Context{&window_, &textures_, &textureFonts_} )
     //, player_()
 {
     window_.setFramerateLimit(60);
diff --git a/src/app.h b/src/app.h
index 7d667f9..2c7e21e 100644
--- a/src/app.h
+++ b/src/app.h
@@ -9,6 +9,7 @@
 class App {
         static const sf::Time TIME_PER_FRAME;
     private:
+        TextureFontManager textureFonts_;
         TextureManager textures_;
         sf::RenderWindow window_;
         StateStack stateStack_;
diff --git a/src/resourceIds.h b/src/resourceIds.h
index 9236fb6..6e84582 100644
--- a/src/resourceIds.h
+++ b/src/resourceIds.h
@@ -2,14 +2,23 @@
 #define RESOURCE_IDS_H
 
 #include "resourceManager.h"
+#include "graphics/textureFont.h"
 
 namespace Textures{
     enum class Id{
+        Base_Font,
         Player_NE_Shooting,
         Player_SE_Shooting,
     };
 }
 
-using TextureManager = ResourceManager<sf::Texture, Textures::Id>;
+namespace Fonts{
+    enum class Id{
+        Base_Font,
+    };
+}
+
+using TextureManager     = ResourceManager<sf::Texture, Textures::Id>;
+using TextureFontManager = ResourceManager<Graphics::TextureFont, Fonts::Id>;
 
 #endif // RESOURCE_IDS_H
diff --git a/src/resourceManager.h b/src/resourceManager.h
index be6e250..b69ce1e 100644
--- a/src/resourceManager.h
+++ b/src/resourceManager.h
@@ -7,12 +7,21 @@
 #include <stdexcept>
 #include <cassert>
 
+
+// TODO: I don't like the `load` function here as it forces the `Resource` to
+// have a loadFromFile function that they may not have (see TextureFont) or
+// they may need to call with several arguments (see SFML shaders). I think
+// it's better to load them by hand and capture them in the unique pointer
+// inside.
+// Maybe make a `manage` function that gets a pointer and captures.
+
 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);
+        void load(Identifier id, const std::string &path);
+        void manage(Identifier id, Resource *resource);
         Resource& get(Identifier id);
         const Resource& get(Identifier id) const;
 };
@@ -31,6 +40,17 @@ ResourceManager<Resource, Identifier>::load(Identifier id,
     assert(inserted.second);
 }
 
+template <typename Resource, typename Identifier>
+void
+ResourceManager<Resource, Identifier>::manage(Identifier id,
+        Resource *resource){
+    std::unique_ptr<Resource> resource_ptr (resource);
+    auto inserted = resourceMap.insert(
+            std::make_pair(id, std::move(resource_ptr))
+    );
+    assert(inserted.second);
+}
+
 template <typename Resource, typename Identifier>
 Resource&
 ResourceManager<Resource, Identifier>::get(Identifier id)
diff --git a/src/states.h b/src/states.h
index 57898a6..d8a63f6 100644
--- a/src/states.h
+++ b/src/states.h
@@ -16,6 +16,7 @@ class State {
         struct Context{
             sf::RenderWindow *window;
             TextureManager *textures;
+            TextureFontManager *textureFonts;
             // add IO controller
         };
         State(StateStack &stack, State::Context context);
-- 
cgit v1.2.3