48 Log::info(
"Initializing resource manager...");
53 auto cleanupResources = [&](
auto match)
61 if (match(iter->second))
77 cleanupResources([](
Resource *res) {
return dynamic_cast<ImageSet *
>(res); });
80 cleanupResources([](
Resource *res) {
return true; });
89 Log::info(
"ResourceManager::~ResourceManager() cleaning up %d "
102 time_t oldest = time(
nullptr);
103 const time_t threshold = oldest - 30;
137 Log::info(
"Added search path: %s", path.c_str());
142 const std::string &ext,
149 const size_t len = strlen(fileName);
151 if (len > ext.length() && ext != (fileName + (len - ext.length())))
153 std::string file = path + fileName;
156 std::string archive = std::string(*realDir) + dirSep + file;
171 path = std::string(*realDir) +
"/" + file;
183 const std::function<
Resource *()> &generator)
189 return resIter->second;
235 std::string path = idPath;
236 std::string::size_type p = path.find(
'|');
237 std::unique_ptr<Dye> d;
238 if (p != std::string::npos)
240 d = std::make_unique<Dye>(path.substr(p + 1));
241 path = path.substr(0, p);
256 std::stringstream ss;
257 ss << imagePath <<
"[" << w <<
"x" << h <<
"]";
260 auto img = getImage(imagePath);
264 return new ImageSet(img, w, h);
270 std::stringstream ss;
271 ss << path <<
"[" << variant <<
"]";
274 return SpriteDef::load(path, variant);
283 assert(resIter !=
mResources.end() && resIter->second == res);
285 const time_t timestamp = time(
nullptr);
static const std::string & getPackageDirectory()
Stores a set of subimages originating from a single image.
Defines a class for loading and storing images.
static Resource * load(SDL_RWops *rw)
Loads an image from an SDL_RWops structure.
Defines a class for loading and storing music.
static Music * load(SDL_RWops *rw)
Loads a music from a buffer in memory.
A class for loading and managing resources.
ResourceRef< SpriteDef > getSprite(const std::string &path, int variant=0)
Loads a SpriteDef based on a given path and the supplied variant.
void remove(Resource *)
Removes a resource from the list of resources managed by the resource manager.
static ResourceManager * getInstance()
Returns an instance of the class, creating one if it does not already exist.
Resource * get(const std::string &idPath, const std::function< Resource *()> &generator)
Looks up a resource, creating it with the generator function if it does not exist.
ResourceRef< Music > getMusic(const std::string &path)
Loads the Music resource found at the given path.
std::map< std::string, Resource * > mResources
static ResourceManager * instance
~ResourceManager()
Destructor.
ResourceRef< SoundEffect > getSoundEffect(const std::string &path)
Loads the SoundEffect resource found at the given path.
static std::string getPath(const std::string &file)
Returns the real path to a file.
ResourceRef< Image > getImage(const std::string &idPath)
Loads the Image resource found at the given identifier path.
static void deleteInstance()
Deletes the class instance if it exists.
static void cleanUp(Resource *resource)
Deletes the resource after logging a cleanup message.
static void searchAndAddArchives(const std::string &path, const std::string &ext, bool append)
Searches for zip files and adds them to the search path.
void release(Resource *)
Releases a resource, placing it in the set of orphaned resources.
static bool addToSearchPath(const std::string &path, bool append)
Adds a directory or archive to the search path.
std::map< std::string, Resource * > mOrphanedResources
ResourceRef< ImageSet > getImageSet(const std::string &imagePath, int w, int h)
Loads a image set based on the image referenced by the given path and the supplied sprite sizes.
Automatically counting Resource reference.
A generic reference counted resource object.
std::string mIdPath
Path identifying this resource.
time_t mTimeStamp
Time at which the resource was orphaned.
unsigned mRefCount
Reference count.
Defines a class for loading and storing sound effects.
static SoundEffect * load(SDL_RWops *rw)
Loads a sample from a buffer in memory.
Defines a class to load an animation.
const char * getDirSeparator()
SDL_RWops * openBufferedRWops(const std::string &path, PHYSFS_uint64 bufferSize=2048)
Creates a buffered SDL_RWops.
bool addToSearchPath(const std::string &path, bool append)
Adds a directory or archive to the search path.
const char * getLastError()
SDL_RWops * openRWops(const std::string &path)
std::optional< const char * > getRealDir(const std::string &path)
Files enumerateFiles(const std::string &dir)
Returns a list of files in the given directory.
void info(const char *log_text,...) LOG_PRINTF_ATTR
void error(const char *log_text,...) LOG_PRINTF_ATTR