From f42921c0597101b5d6f10afa68d0ffc6f6d1a66f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 12 Jul 2012 01:22:46 +0300 Subject: Add to resourcemanager method to load subimage. --- src/resources/resourcemanager.cpp | 35 +++++++++++++++++++++++++++++++++++ src/resources/resourcemanager.h | 3 +++ 2 files changed, 38 insertions(+) (limited to 'src') 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(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(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. -- cgit v1.2.3-60-g2f50