change Gosu::Bitmap API; Tileset::source a pointer
[Tsunagari.git] / src / resourcer.h
blob8f08a74669e3464b05446a3d9d906a8fda643bae
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** resourcer.h **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #ifndef RESOURCER_H
8 #define RESOURCER_H
10 #include <map>
11 #include <string>
12 #include <utility>
14 #include <boost/unordered_map.hpp>
15 #include <libxml/parser.h>
16 #include <libxml/tree.h>
17 #include <zip.h>
19 namespace Gosu {
20 class Bitmap;
21 class Buffer;
22 class Image;
23 class Sample;
26 class GameWindow;
28 //! Resourcer Class
29 /*!
30 This class provides the engine's global resource handling and caching.
32 class Resourcer
34 public:
35 //! Resourcer Constructor
36 Resourcer(GameWindow* window, const std::string& filename);
38 //! Resourcer Destructor
39 ~Resourcer();
41 //! Resourcer Initializer
42 bool init();
44 //! Requests an image resource from cache.
45 Gosu::Image* getImage(const std::string& name);
47 //! Requests a bitmap that can be used to construct subimages from cache.
48 void getBitmap(Gosu::Bitmap& bitmap, const std::string& name);
50 //! Converts a subrectangle of a Bitmap into an Image.
51 Gosu::Image* bitmapSection(const Gosu::Bitmap& src,
52 unsigned x, unsigned y, unsigned w, unsigned h, bool tileable);
54 //! Requests an XML resource from cache.
55 xmlDoc* getXMLDoc(const std::string& name);
57 //! Close resource request and drop from cache. IMPORTANT!!!
58 void drop(const std::string& name);
60 //! Returns a music stream from disk or cache.
61 Gosu::Sample* getSample(const std::string& name);
63 private:
64 //! Requests a string resource from cache.
65 std::string getString(const std::string& name);
67 //! Requests a string resource from cache.
68 std::string getStringFromZip(const std::string& name);
70 //! Read a resource from disk into memory. Returns NULL on error.
71 Gosu::Buffer* read(const std::string& name);
73 //! Helper function
74 std::string path(const std::string& entry_name) const;
76 //! Resource Cache
77 /*!
78 The key is the cached file's name, and the value is a pair of a
79 "tally" and a pointer to the data. The "tally" is increased each
80 time something requests the resource, and decreased each time
81 something is finished with that resource. When the tally reaches
82 zero, nothing is using the resource, and it is dropped after a
83 few minutes. The cache drop timer is in a thread.
85 std::map<std::string, std::pair<int, void*> > cache;
87 //! Modify the cache tally. Logs a Developer warning on tally underrun.
88 void cacheTally(const std::string& key, const int mod);
90 GameWindow* window;
91 zip* z;
92 const std::string zip_filename;
94 typedef boost::unordered_map<std::string, std::string> strMap;
95 strMap strings;
98 #endif