summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2022-12-12 23:55:47 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2022-12-12 23:55:47 +0100
commit4e43958f84336e3bcc4104257f51ba84e57fc4b7 (patch)
tree99ad6039c001c30982b75ca3f71f6c11717a477e
parentd0ee939a32fc7cec65a77d028d72881ee8ea1495 (diff)
Add TextureFonts to the ResourceManager
-rw-r--r--src/app.cpp2
-rw-r--r--src/app.h1
-rw-r--r--src/resourceIds.h11
-rw-r--r--src/resourceManager.h22
-rw-r--r--src/states.h1
5 files changed, 34 insertions, 3 deletions
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;
};
@@ -32,6 +41,17 @@ ResourceManager<Resource, Identifier>::load(Identifier id,
}
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);