diff options
-rw-r--r-- | src/resources/resourcemanager.cpp | 35 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 3 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 452bc9942..2c4bc0f95 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -552,6 +552,41 @@ ImageSet *ResourceManager::getImageSet(const std::string &imagePath, return static_cast<ImageSet*>(get(ss.str(), ImageSetLoader::load, &rl)); } +struct SubImageLoader +{ + ResourceManager *manager; + Image *parent; + int x, y; + int width, height; + static Resource *load(void *v) + { + if (!v) + return nullptr; + + SubImageLoader *rl = static_cast< SubImageLoader * >(v); + if (!rl->manager || !rl->parent) + return nullptr; + + Image *res = rl->parent->getSubImage(rl->x, rl->y, + rl->width, rl->height); + return res; + } +}; + +SubImage *ResourceManager::getSubImage(Image *parent, int x, int y, + int width, int height) +{ + if (!parent) + return nullptr; + + SubImageLoader rl = { this, parent, x, y, width, height}; + + std::stringstream ss; + ss << parent->getIdPath() << ",[" << x << "," << y << "," + << width << "x" << height << "]"; + return reinterpret_cast<SubImage*>(get(ss.str(), SubImageLoader::load, &rl)); +} + struct SpriteDefLoader { std::string path; diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index b07b23431..b4892227e 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -38,6 +38,7 @@ class ImageSet; class Music; class Resource; class SoundEffect; +class SubImage; class SpriteDef; struct SDL_Surface; @@ -202,6 +203,8 @@ class ResourceManager */ ImageSet *getImageSet(const std::string &imagePath, int w, int h); + SubImage *getSubImage(Image *parent, int x, int y, int width, int height); + /** * Creates a sprite definition based on a given path and the supplied * variant. |