tile animations!
[Tsunagari.git] / src / resourcer.h
blob9e98b50553115cf8539238f010ea6ed4f44a564c
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>
12 #include <utility>
14 #include <boost/scoped_ptr.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <boost/unordered_map.hpp>
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
20 #include "common.h"
22 struct zip;
24 namespace Gosu {
25 class Bitmap;
26 class Buffer;
27 class Image;
28 class Sample;
31 class GameWindow;
33 // We hand out and manage Gosu resources in these forms:
34 typedef boost::scoped_ptr<Gosu::Buffer> BufferPtr;
35 typedef boost::shared_ptr<Gosu::Image> ImageRef;
36 typedef boost::shared_ptr<Gosu::Sample> SampleRef;
37 typedef std::deque<ImageRef> TiledImage;
38 // libxml2 resources
39 typedef boost::shared_ptr<xmlDoc> XMLDocRef;
41 //! Resourcer Class
42 /*!
43 This class provides the engine's global resource handling and caching.
45 class Resourcer
47 public:
48 //! Resourcer Constructor
49 Resourcer(GameWindow* window, ClientValues* conf);
51 //! Resourcer Destructor
52 ~Resourcer();
54 //! Resourcer Initializer
55 bool init();
57 //! Requests an image resource from cache.
58 ImageRef getImage(const std::string& name);
60 //! Requests an image resource from cache and splits it into a number
61 // of tiles each with with and height w by x. Returns false if the
62 // source image wasn't found.
63 bool getTiledImage(TiledImage& img, const std::string& name,
64 unsigned w, unsigned h, bool tileable);
66 //! Returns a music stream from disk or cache.
67 SampleRef getSample(const std::string& name);
69 //! Requests an XML resource from cache.
70 XMLDocRef getXMLDoc(const std::string& name,
71 const std::string& dtdPath);
73 private:
74 typedef boost::unordered_map<std::string, ImageRef> ImageRefMap;
75 typedef boost::unordered_map<std::string, SampleRef> SampleRefMap;
76 typedef boost::unordered_map<std::string, XMLDocRef> XMLMap;
77 typedef boost::unordered_map<std::string, TiledImage> TiledImageMap;
79 //! Requests an XML document from disk.
80 xmlDoc* readXMLDocFromDisk(const std::string& name,
81 const std::string& dtdPath);
83 //! Requests a string resource from disk.
84 std::string readStringFromDisk(const std::string& name);
86 //! Read a generic resource from disk.
87 Gosu::Buffer* read(const std::string& name);
89 //! Helper function
90 std::string path(const std::string& entry_name) const;
92 GameWindow* window;
93 zip* z;
94 ClientValues* conf;
96 //! Resource Cache
97 /*!
98 The key is the cached file's name, and the value is a pair of a
99 "tally" and a pointer to the data. The "tally" is increased each
100 time something requests the resource, and decreased each time
101 something is finished with that resource. When the tally reaches
102 zero, nothing is using the resource, and it is dropped after a
103 few minutes. The cache drop timer is in a thread.
105 ImageRefMap images;
106 SampleRefMap samples;
107 XMLMap xmls;
108 TiledImageMap tiles;
111 #endif