Mana
Loading...
Searching...
No Matches
resourcemanager.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include "resources/resource.h"
25
26#include <ctime>
27#include <functional>
28#include <map>
29#include <string>
30
31class Image;
32class ImageSet;
33class Music;
34class SoundEffect;
35class SpriteDef;
36
41{
42 friend class Resource;
43
44 public:
46
52
60 static bool addToSearchPath(const std::string &path, bool append);
61
65 static void searchAndAddArchives(const std::string &path,
66 const std::string &ext,
67 bool append);
68
76 static std::string getPath(const std::string &file);
77
82 ResourceRef<Image> getImage(const std::string &idPath);
83
87 ResourceRef<Music> getMusic(const std::string &path);
88
92 ResourceRef<SoundEffect> getSoundEffect(const std::string &path);
93
98 ResourceRef<ImageSet> getImageSet(const std::string &imagePath, int w, int h);
99
103 ResourceRef<SpriteDef> getSprite(const std::string &path, int variant = 0);
104
110
114 static void deleteInstance();
115
116 private:
127 Resource *get(const std::string &idPath,
128 const std::function<Resource *()> &generator);
129
134 void release(Resource *);
135
140 void remove(Resource *);
141
145 static void cleanUp(Resource *resource);
146
147 void cleanOrphans();
148
150 std::map<std::string, Resource *> mResources;
151 std::map<std::string, Resource *> mOrphanedResources;
152 time_t mOldestOrphan = 0;
153};
Stores a set of subimages originating from a single image.
Definition imageset.h:34
Defines a class for loading and storing images.
Definition image.h:45
Defines a class for loading and storing music.
Definition music.h:32
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.
Definition resource.h:74
A generic reference counted resource object.
Definition resource.h:31
Defines a class for loading and storing sound effects.
Definition soundeffect.h:32
Defines a class to load an animation.
Definition spritedef.h:87