FEATURE: Entity "stance" phase (fallback)
[Tsunagari.git] / src / resourcer.h
blobd5273a2d47c86d5831b550eec0fc06f687702d3d
1 /*********************************
2 ** Tsunagari Tile Engine **
3 ** resourcer.h **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
7 #ifndef RESOURCER_H
8 #define RESOURCER_H
10 #include <deque>
11 #include <string>
13 #include <boost/shared_ptr.hpp>
14 #include <libxml/parser.h>
15 #include <Python.h>
17 #include "cache.h"
18 #include "sound.h"
19 #include "xml.h"
21 namespace Gosu {
22 class Bitmap;
23 class Buffer;
24 class Image;
25 class Sample;
26 class Song;
29 using boost::shared_ptr;
31 // We hand out and manage resources in these forms:
32 typedef boost::shared_ptr<Gosu::Image> ImageRef;
33 typedef boost::shared_ptr<Sound> SampleRef;
34 typedef boost::shared_ptr<Gosu::Song> SongRef;
35 typedef boost::shared_ptr<XMLDoc> XMLRef;
36 typedef std::deque<ImageRef> TiledImage;
37 typedef boost::shared_ptr<TiledImage> TiledImageRef;
38 typedef boost::shared_ptr<std::string> StringRef;
40 /**
41 * Provides data and resource extraction for a World.
42 * Each World comes bundled with associated data in the form of a Zip file.
43 * A Resourcer object knows how to navigate the data, extract individual
44 * requested files, and process the files into data structures. The final data
45 * structures are kept in memory for future requests.
47 class Resourcer
49 public:
50 //! Get the Resourcer for the current World.
51 static Resourcer* instance();
53 Resourcer();
54 ~Resourcer();
55 bool init(char* argv0);
56 bool prependPath(const std::string& path);
57 bool appendPath(const std::string& path);
58 bool rmPath(const std::string& path);
60 //! Returns true if the World contains a resource by that name.
61 bool resourceExists(const std::string& name) const;
63 //! Request an image from the World.
64 ImageRef getImage(const std::string& name);
66 //! Request an image resource from the World and splits it into a
67 //! number of tiles that each have width and height w by h. Returns
68 //! false if the source image is not found.
69 bool getTiledImage(TiledImage& img, const std::string& name,
70 int w, int h, bool tileable);
72 //! Request a sound object from the World. The sound will be
73 //! completely loaded into memory at once.
74 SampleRef getSample(const std::string& name);
76 //! Request a music stream from the World. The stream will be
77 //! loaded from disk as it is being played.
78 SongRef getSong(const std::string& name);
80 //! Request an XML document from the World.
81 XMLRef getXMLDoc(const std::string& name,
82 const std::string& dtdFile);
84 //! Request a Python script from the World be run.
85 bool runPythonScript(const std::string& name);
87 //! Request a text file from the World.
88 std::string getText(const std::string& name);
90 //! Expunge old resources cached in memory. Decisions on which are
91 //! removed and which are kept are based on the global Conf struct.
92 void garbageCollect();
94 private:
95 //! Reads a file from the game archive.
96 template <class T>
97 bool readFromDisk(const std::string& name, T& buf);
99 //! Read an XML document from the game archive and parse it.
100 XMLDoc* readXMLDocFromDisk(const std::string& name,
101 const std::string& dtdFile);
103 //! Read a string resource from the game archive.
104 std::string readStringFromDisk(const std::string& name);
106 //! Read a generic resource from the game archive.
107 Gosu::Buffer* read(const std::string& name);
109 //! Helper function.
110 std::string path(const std::string& entryName) const;
112 private:
113 // Caches that store processed, game-ready objects. Garbage collected.
114 Cache<ImageRef> images;
115 Cache<TiledImageRef> tiles;
116 Cache<SampleRef> sounds;
117 Cache<SongRef> songs;
118 Cache<XMLRef> xmls;
119 Cache<PyCodeObject*> codes;
120 Cache<StringRef> texts;
123 void exportResourcer();
125 #endif