area.cpp: fix pointer dereference
[Tsunagari.git] / src / resourcer.h
blob12139c3677f70f34abdb3b9925cdd513fd6b80a2
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 <Gosu/Gosu.hpp>
15 #include <libxml/parser.h>
16 #include <libxml/tree.h>
17 #include <zip.h>
19 // Time to live for empty cache members. Defaults to 5 minutes.
20 #define CACHE_EMPTY_TTL "300"
22 class GameWindow;
24 //! Resourcer Class
25 /*!
26 This class provides the engine's global resource handling and caching.
28 class Resourcer
30 public:
31 //! Resourcer Constructor
32 Resourcer(GameWindow* window, const std::string& filename);
34 //! Resourcer Destructor
35 ~Resourcer();
37 //! Resourcer Initializer
38 bool init();
40 //! Requests an image resource from cache.
41 Gosu::Image* getImage(const std::string& name);
43 //! Requests a bitmap that can be used to construct subimages from cache.
44 Gosu::Bitmap getBitmap(const std::string& name);
46 //! Converts a subrectangle of a Bitmap into an Image.
47 Gosu::Image* bitmapSection(const Gosu::Bitmap& src,
48 unsigned x, unsigned y, unsigned w, unsigned h, bool tileable);
50 //! Requests a string resource from cache.
51 std::string getString(const std::string& name);
53 //! Requests an XML resource from cache.
54 xmlDoc* getXMLDoc(const std::string& name);
56 //! Close resource request and drop from cache. IMPORTANT!!!
57 void drop(const std::string& name);
59 //! Returns a music stream from disk or cache.
60 Gosu::Sample* getSample(const std::string& name);
62 private:
63 //! Read a resource from disk into memory. Returns NULL on error.
64 Gosu::Buffer* read(const std::string& name);
66 //! Helper function
67 std::string path(const std::string& entry_name) const;
69 GameWindow* window;
70 zip* z;
71 const std::string zip_filename;
73 //! Resource Cache
74 /*!
75 The key is the cached file's name, and the value is a pair of a
76 "tally" and a pointer to the data. The "tally" is increased each
77 time something requests the resource, and decreased each time
78 something is finished with that resource. When the tally reaches
79 zero, nothing is using the resource, and it is dropped after a
80 few minutes. The cache drop timer is in a thread.
82 std::map<std::string, std::pair<int, void*> > cache;
84 //! Modify the cache tally. Logs a Developer warning on tally underrun.
85 void cacheTally(const std::string& key, const int mod);
88 #endif