scripts load from world
[Tsunagari.git] / src / resourcer.h
blob5e1c71052fe08197b7b97d712e3e3b7741c718ec
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** resourcer.h **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #ifndef RESOURCER_H
8 #define RESOURCER_H
10 #include <deque>
11 #include <string>
13 #include <boost/shared_ptr.hpp>
14 #include <boost/unordered_map.hpp>
15 #include <libxml/parser.h>
16 #include <lua.hpp>
18 #include "common.h"
20 struct zip;
22 namespace Gosu {
23 class Bitmap;
24 class Buffer;
25 class Image;
26 class Sample;
29 class GameWindow;
31 // We hand out and manage Gosu resources in these forms:
32 typedef boost::shared_ptr<Gosu::Image> ImageRef;
33 typedef boost::shared_ptr<Gosu::Sample> SampleRef;
34 typedef std::deque<ImageRef> TiledImage;
35 // libxml2 resources
36 typedef boost::shared_ptr<xmlDoc> XMLDocRef;
38 //! Resourcer Class
39 /*!
40 This class provides the engine's global resource handling and caching.
42 class Resourcer
44 public:
45 Resourcer(GameWindow* window, ClientValues* conf);
46 ~Resourcer();
47 bool init();
49 //! Expunge old stuff from the cache.
50 void garbageCollect();
52 //! Requests an image resource from cache.
53 ImageRef getImage(const std::string& name);
55 //! Requests an image resource from cache and splits it into a number
56 // of tiles each with width and height w by x. Returns false if the
57 // source image wasn't found.
58 bool getTiledImage(TiledImage& img, const std::string& name,
59 unsigned w, unsigned h, bool tileable);
61 //! Returns a music stream from disk or cache.
62 SampleRef getSample(const std::string& name);
64 //! Requests an XML resource from cache.
65 XMLDocRef getXMLDoc(const std::string& name,
66 const std::string& dtdFile);
68 //! Requests a Lua script from cache. Lua state L will parse the script.
69 bool getLuaScript(const std::string& name, lua_State* L);
71 private:
72 template<class Res>
73 struct CachedItem
75 Res resource;
76 int lastUsed;
77 int memoryUsed;
80 //! Resource maps.
81 typedef boost::unordered_map<std::string, CachedItem<ImageRef> >
82 ImageRefMap;
83 typedef boost::unordered_map<std::string, CachedItem<SampleRef> >
84 SampleRefMap;
85 typedef boost::unordered_map<std::string, CachedItem<XMLDocRef> >
86 XMLMap;
87 typedef boost::unordered_map<std::string, CachedItem<
88 boost::shared_ptr<TiledImage> > > TiledImageMap;
90 //! Garbage collect a map.
91 template<class Map, class MapValue>
92 void reclaim(Map& map);
94 //! Reads an XML document from disk and parses it.
95 xmlDoc* readXMLDocFromDisk(const std::string& name,
96 const std::string& dtdFile);
98 //! Read a string resource from disk.
99 std::string readStringFromDisk(const std::string& name);
101 //! Read a generic resource from disk.
102 Gosu::Buffer* read(const std::string& name);
104 //! Helper function
105 std::string path(const std::string& entryName) const;
107 GameWindow* window;
108 zip* z;
109 ClientValues* conf;
111 //! Resource Cache
113 The key is the cached file's name, and the value is a pair of a
114 "tally" and a pointer to the data. The "tally" is increased each
115 time something requests the resource, and decreased each time
116 something is finished with that resource. When the tally reaches
117 zero, nothing is using the resource, and it is dropped after a
118 few minutes. The cache drop timer is in a thread.
120 ImageRefMap images;
121 SampleRefMap samples;
122 XMLMap xmls;
123 TiledImageMap tiles;
126 #endif