1 /******************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
13 #include <boost/shared_ptr.hpp>
14 #include <boost/unordered_map.hpp>
15 #include <libxml/parser.h>
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
;
36 typedef boost::shared_ptr
<xmlDoc
> XMLDocRef
;
40 This class provides the engine's global resource handling and caching.
45 Resourcer(GameWindow
* window
, ClientValues
* conf
);
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
);
81 typedef boost::unordered_map
<std::string
, CachedItem
<ImageRef
> >
83 typedef boost::unordered_map
<std::string
, CachedItem
<SampleRef
> >
85 typedef boost::unordered_map
<std::string
, CachedItem
<XMLDocRef
> >
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
);
105 std::string
path(const std::string
& entryName
) const;
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.
121 SampleRefMap samples
;