1 #ifndef __BT_RESOURCE_MANAGER_H__
2 #define __BT_RESOURCE_MANAGER_H__
3 
4 /* Battle Tanks Game
5  * Copyright (C) 2006-2009 Battle Tanks team
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21 
22 /*
23  * Additional rights can be granted beyond the GNU General Public License
24  * on the terms provided in the Exception. If you modify this file,
25  * you may extend this exception to your version of the file,
26  * but you are not obligated to do so. If you do not wish to provide this
27  * exception without modification, you must delete this exception statement
28  * from your version and license this file solely under the GPL without exception.
29 */
30 
31 #include "export_btanks.h"
32 #include "mrt/singleton.h"
33 #include "mrt/exception.h"
34 #include "notifying_xml_parser.h"
35 #include <map>
36 #include <set>
37 #include <vector>
38 #include <string>
39 
40 namespace sdlx {
41 	class Surface;
42 	class Font;
43 	class CollisionMap;
44 }
45 
46 class Object;
47 class Object;
48 class Animation;
49 class AnimationModel;
50 class Pose;
51 
52 #define AUTOLOAD_SURFACE(surface, filename) if ((surface) == NULL) surface = ResourceManager->load_surface(filename)
53 
54 class BTANKSAPI IResourceManager : public NotifyingXMLParser {
55 public:
56 	IResourceManager();
57 	~IResourceManager();
58 	DECLARE_SINGLETON(IResourceManager);
59 
60 	void init(const std::vector<std::pair<std::string, std::string> > &fname);
61 	void clear();
62 
63 	AnimationModel *get_animation_model(const std::string &id);
64 
65 	void registerObject(const std::string &classname, Object *);
66 	Object *createObject(const std::string &classname) const;
67 	Object *createObject(const std::string &classname, const std::string &animation) const;
68 	const Object *getClass(const std::string &classname) const;
69 	const bool hasClass(const std::string &classname) const;
70 	const Animation *getAnimation(const std::string &id) const;
71 	const bool hasAnimation(const std::string &id) const;
72 
73 	const sdlx::Surface *load_surface(const std::string &id, int scale_to_w = 0, int scale_to_h = 0);
74 	void unload_surface(const std::string &id); //do not use it until you known what you're doing
75 	const sdlx::Surface *get_surface(const std::string &id) const;
76 	const sdlx::CollisionMap *getCollisionMap(const std::string &id) const;
77 	const sdlx::Font *loadFont(const std::string &id, const bool alpha);
78 
79 	void preload(); //preload all animation used on map
80 
81 	void createAlias(const std::string &name, const std::string &classname);
82 
83 	void check_surface(const std::string &animation, const sdlx::Surface *& surface_ptr, const sdlx::CollisionMap *&cmap);
84 
85 	void getAllClasses(std::set<std::string> &classes);
86 
87 	typedef std::map<const std::pair<std::string, std::string>, std::set<std::string> > PreloadMap;
88 
89 private:
90 	void preload(const std::string &animation);
91 	Animation *getAnimation(const std::string &id);
92 
93 	static sdlx::CollisionMap * create_cmap(const sdlx::Surface *s, const std::string &tile);
94 
95 	//xml stuff
96 	std::string _base_dir;
97 	virtual void onFile(const std::string &base, const std::string &file);
98 
99 	virtual void start(const std::string &name, Attrs &attr);
100 	virtual void end(const std::string &name);
101 	virtual void cdata(const std::string &data);
102 
103 	typedef std::map<const std::string, Animation*> AnimationMap;
104 	AnimationMap _animations;
105 
106 	typedef std::map<const std::string, AnimationModel *> AnimationModelMap;
107 	AnimationModelMap _animation_models;
108 
109 	typedef std::map<const std::string, sdlx::Surface *> SurfaceMap;
110 	SurfaceMap _surfaces;
111 
112 	typedef std::map<const std::pair<std::string, bool>, sdlx::Font *> FontMap;
113 	FontMap _fonts;
114 
115 	typedef std::map<const std::string, sdlx::CollisionMap *> CollisionMap;
116 	CollisionMap _cmaps;
117 
118 	//parser specific stuff
119 	AnimationModel *_am;
120 	Pose *_pose;
121 	std::string _data, _pose_id, _am_id;
122 
123 	long _tw, _th;
124 
125 	typedef std::map<const std::string, Object *> ObjectMap;
126 	ObjectMap _objects;
127 
128 	mutable PreloadMap _preload_map, _object_preload_map;
129 
130 	IResourceManager(const IResourceManager &);
131 	const IResourceManager& operator=(const IResourceManager &);
132 };
133 
134 PUBLIC_SINGLETON(BTANKSAPI, ResourceManager, IResourceManager);
135 
136 #endif
137 
138