summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-03-21 12:19:06 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-03-21 21:15:42 +0100
commit5333d8554be855af7b78cdd47d19e8d31abd47c3 (patch)
tree394b869a472fa19a354c7392b5cbbc0d6969aa0a
parent7a110c36b58478b633a694ba74a1cb17d237c3df (diff)
downloadmana-5333d8554be855af7b78cdd47d19e8d31abd47c3.tar.gz
mana-5333d8554be855af7b78cdd47d19e8d31abd47c3.tar.bz2
mana-5333d8554be855af7b78cdd47d19e8d31abd47c3.tar.xz
mana-5333d8554be855af7b78cdd47d19e8d31abd47c3.zip
Use std::function in ResourceManager
Simplifies the code a little. Also use ResourceRef in SubImage to avoid manual reference counting.
-rw-r--r--src/resources/image.cpp8
-rw-r--r--src/resources/image.h2
-rw-r--r--src/resources/resourcemanager.cpp105
-rw-r--r--src/resources/resourcemanager.h5
4 files changed, 35 insertions, 85 deletions
diff --git a/src/resources/image.cpp b/src/resources/image.cpp
index e643143b..82a928b4 100644
--- a/src/resources/image.cpp
+++ b/src/resources/image.cpp
@@ -372,11 +372,6 @@ SubImage::SubImage(Image *parent, SDL_Texture *texture,
Image(texture, width, height),
mParent(parent)
{
- if (mParent)
- {
- mParent->incRef();
- }
-
// Set up the rectangle.
mBounds.x = x;
mBounds.y = y;
@@ -391,8 +386,6 @@ SubImage::SubImage(Image *parent, GLuint image,
Image(image, width, height, texWidth, texHeight),
mParent(parent)
{
- mParent->incRef();
-
// Set up the rectangle.
mBounds.x = x;
mBounds.y = y;
@@ -408,5 +401,4 @@ SubImage::~SubImage()
#ifdef USE_OPENGL
mGLImage = 0;
#endif
- mParent->decRef();
}
diff --git a/src/resources/image.h b/src/resources/image.h
index 20a85433..de759612 100644
--- a/src/resources/image.h
+++ b/src/resources/image.h
@@ -226,7 +226,7 @@ class SubImage : public Image
~SubImage() override;
private:
- Image *mParent;
+ ResourceRef<Image> mParent;
};
#endif
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp
index 65cc4d50..e215cfd7 100644
--- a/src/resources/resourcemanager.cpp
+++ b/src/resources/resourcemanager.cpp
@@ -40,6 +40,7 @@
#include <cassert>
#include <sstream>
+#include <memory>
#include <sys/time.h>
@@ -230,7 +231,7 @@ std::string ResourceManager::getPath(const std::string &file)
}
bool ResourceManager::addResource(const std::string &idPath,
- Resource* resource)
+ Resource *resource)
{
if (resource)
{
@@ -253,8 +254,8 @@ Resource *ResourceManager::get(const std::string &idPath)
return nullptr;
}
-Resource *ResourceManager::get(const std::string &idPath, generator fun,
- void *data)
+Resource *ResourceManager::get(const std::string &idPath,
+ const std::function<Resource *()> &generator)
{
// Check if the id exists, and return the value if it does.
auto resIter = mResources.find(idPath);
@@ -274,7 +275,7 @@ Resource *ResourceManager::get(const std::string &idPath, generator fun,
return res;
}
- Resource *resource = fun(data);
+ Resource *resource = generator();
if (resource)
{
@@ -288,27 +289,13 @@ Resource *ResourceManager::get(const std::string &idPath, generator fun,
return resource;
}
-struct ResourceLoader
-{
- ResourceManager *manager;
- std::string path;
- ResourceManager::loader fun;
-
- static Resource *load(void *v)
- {
- auto *l = static_cast< ResourceLoader * >(v);
- SDL_RWops *rw = PHYSFSRWOPS_openRead(l->path.c_str());
- if (!rw)
- return nullptr;
- Resource *res = l->fun(rw);
- return res;
- }
-};
-
Resource *ResourceManager::load(const std::string &path, loader fun)
{
- ResourceLoader l = { this, path, fun };
- return get(path, ResourceLoader::load, &l);
+ return get(path, [&] () -> Resource * {
+ if (SDL_RWops *rw = PHYSFSRWOPS_openRead(path.c_str()))
+ return fun(rw);
+ return nullptr;
+ });
}
Music *ResourceManager::getMusic(const std::string &idPath)
@@ -321,82 +308,52 @@ SoundEffect *ResourceManager::getSoundEffect(const std::string &idPath)
return static_cast<SoundEffect*>(load(idPath, SoundEffect::load));
}
-struct DyedImageLoader
+Image *ResourceManager::getImage(const std::string &idPath)
{
- ResourceManager *manager;
- std::string path;
- static Resource *load(void *v)
- {
- auto *l = static_cast< DyedImageLoader * >(v);
- std::string path = l->path;
+ return static_cast<Image*>(get(idPath, [&] () -> Resource * {
+ std::string path = idPath;
std::string::size_type p = path.find('|');
- Dye *d = nullptr;
+ std::unique_ptr<Dye> d;
if (p != std::string::npos)
{
- d = new Dye(path.substr(p + 1));
+ d = std::make_unique<Dye>(path.substr(p + 1));
path = path.substr(0, p);
}
SDL_RWops *rw = PHYSFSRWOPS_openRead(path.c_str());
if (!rw)
- {
- delete d;
return nullptr;
- }
+
Resource *res = d ? Image::load(rw, *d)
: Image::load(rw);
- delete d;
return res;
- }
-};
-
-Image *ResourceManager::getImage(const std::string &idPath)
-{
- DyedImageLoader l = { this, idPath };
- return static_cast<Image*>(get(idPath, DyedImageLoader::load, &l));
+ }));
}
-struct ImageSetLoader
-{
- ResourceManager *manager;
- std::string path;
- int w, h;
- static Resource *load(void *v)
- {
- auto *l = static_cast< ImageSetLoader * >(v);
- Image *img = l->manager->getImage(l->path);
- if (!img) return nullptr;
- auto *res = new ImageSet(img, l->w, l->h);
- img->decRef();
- return res;
- }
-};
-
ImageSet *ResourceManager::getImageSet(const std::string &imagePath,
int w, int h)
{
- ImageSetLoader l = { this, imagePath, w, h };
std::stringstream ss;
ss << imagePath << "[" << w << "x" << h << "]";
- return static_cast<ImageSet*>(get(ss.str(), ImageSetLoader::load, &l));
-}
-struct SpriteDefLoader
-{
- std::string path;
- int variant;
- static Resource *load(void *v)
- {
- auto *l = static_cast< SpriteDefLoader * >(v);
- return SpriteDef::load(l->path, l->variant);
- }
-};
+ return static_cast<ImageSet*>(get(ss.str(), [&] () -> Resource * {
+ Image *img = getImage(imagePath);
+ if (!img)
+ return nullptr;
+
+ auto *res = new ImageSet(img, w, h);
+ img->decRef();
+ return res;
+ }));
+}
SpriteDef *ResourceManager::getSprite(const std::string &path, int variant)
{
- SpriteDefLoader l = { path, variant };
std::stringstream ss;
ss << path << "[" << variant << "]";
- return static_cast<SpriteDef*>(get(ss.str(), SpriteDefLoader::load, &l));
+
+ return static_cast<SpriteDef*>(get(ss.str(), [&] () -> Resource * {
+ return SpriteDef::load(path, variant);
+ }));
}
void ResourceManager::release(Resource *res)
diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h
index 2377eea1..b207585f 100644
--- a/src/resources/resourcemanager.h
+++ b/src/resources/resourcemanager.h
@@ -23,6 +23,7 @@
#define RESOURCE_MANAGER_H
#include <ctime>
+#include <functional>
#include <map>
#include <string>
#include <vector>
@@ -46,7 +47,6 @@ class ResourceManager
public:
using loader = Resource *(*)(SDL_RWops *);
- using generator = Resource *(*)(void *);
ResourceManager();
@@ -123,7 +123,8 @@ class ResourceManager
* @return A valid resource or <code>NULL</code> if the resource could
* not be generated.
*/
- Resource *get(const std::string &idPath, generator fun, void *data);
+ Resource *get(const std::string &idPath,
+ const std::function<Resource *()> &generator);
/**
* Loads a resource from a file and adds it to the resource map.