+class TextureSource : public IWritableTextureSource
+{
+public:
+ TextureSource(IrrlichtDevice *device);
+ virtual ~TextureSource();
+
+ /*
+ Example case:
+ Now, assume a texture with the id 1 exists, and has the name
+ "stone.png^mineral1".
+ Then a random thread calls getTextureId for a texture called
+ "stone.png^mineral1^crack0".
+ ...Now, WTF should happen? Well:
+ - getTextureId strips off stuff recursively from the end until
+ the remaining part is found, or nothing is left when
+ something is stripped out
+
+ But it is slow to search for textures by names and modify them
+ like that?
+ - ContentFeatures is made to contain ids for the basic plain
+ textures
+ - Crack textures can be slow by themselves, but the framework
+ must be fast.
+
+ Example case #2:
+ - Assume a texture with the id 1 exists, and has the name
+ "stone.png^mineral_coal.png".
+ - Now getNodeTile() stumbles upon a node which uses
+ texture id 1, and determines that MATERIAL_FLAG_CRACK
+ must be applied to the tile
+ - MapBlockMesh::animate() finds the MATERIAL_FLAG_CRACK and
+ has received the current crack level 0 from the client. It
+ finds out the name of the texture with getTextureName(1),
+ appends "^crack0" to it and gets a new texture id with
+ getTextureId("stone.png^mineral_coal.png^crack0").
+
+ */
+
+ /*
+ Gets a texture id from cache or
+ - if main thread, from getTextureIdDirect
+ - if other thread, adds to request queue and waits for main thread
+ */
+ u32 getTextureId(const std::string &name);
+
+ /*
+ Example names:
+ "stone.png"
+ "stone.png^crack2"
+ "stone.png^mineral_coal.png"
+ "stone.png^mineral_coal.png^crack1"
+
+ - If texture specified by name is found from cache, return the
+ cached id.
+ - Otherwise generate the texture, add to cache and return id.
+ Recursion is used to find out the largest found part of the
+ texture and continue based on it.
+
+ The id 0 points to a NULL texture. It is returned in case of error.
+ */
+ u32 getTextureIdDirect(const std::string &name);
+
+ // Finds out the name of a cached texture.
+ std::string getTextureName(u32 id);
+
+ /*
+ If texture specified by the name pointed by the id doesn't
+ exist, create it, then return the cached texture.
+
+ Can be called from any thread. If called from some other thread
+ and not found in cache, the call is queued to the main thread
+ for processing.
+ */
+ video::ITexture* getTexture(u32 id);
+
+ video::ITexture* getTexture(const std::string &name, u32 *id);
+
+ // Returns a pointer to the irrlicht device
+ virtual IrrlichtDevice* getDevice()
+ {
+ return m_device;
+ }
+
+ bool isKnownSourceImage(const std::string &name)
+ {
+ bool is_known = false;
+ bool cache_found = m_source_image_existence.get(name, &is_known);
+ if(cache_found)
+ return is_known;
+ // Not found in cache; find out if a local file exists
+ is_known = (getTexturePath(name) != "");
+ m_source_image_existence.set(name, is_known);
+ return is_known;
+ }
+
+ // Processes queued texture requests from other threads.
+ // Shall be called from the main thread.
+ void processQueue();
+
+ // Insert an image into the cache without touching the filesystem.
+ // Shall be called from the main thread.
+ void insertSourceImage(const std::string &name, video::IImage *img);
+
+ // Rebuild images and textures from the current set of source images
+ // Shall be called from the main thread.
+ void rebuildImagesAndTextures();
+
+ // Render a mesh to a texture.
+ // Returns NULL if render-to-texture failed.
+ // Shall be called from the main thread.
+ video::ITexture* generateTextureFromMesh(
+ const TextureFromMeshParams ¶ms);
+
+ // Generates an image from a full string like
+ // "stone.png^mineral_coal.png^[crack:1:0".
+ // Shall be called from the main thread.
+ video::IImage* generateImageFromScratch(std::string name);
+
+ // Generate image based on a string like "stone.png" or "[crack:1:0".
+ // if baseimg is NULL, it is created. Otherwise stuff is made on it.
+ // Shall be called from the main thread.
+ bool generateImage(std::string part_of_name, video::IImage *& baseimg);
+
+private:
+
+ // The id of the thread that is allowed to use irrlicht directly
+ threadid_t m_main_thread;
+ // The irrlicht device
+ IrrlichtDevice *m_device;
+
+ // Cache of source images
+ // This should be only accessed from the main thread
+ SourceImageCache m_sourcecache;
+
+ // Thread-safe cache of what source images are known (true = known)
+ MutexedMap<std::string, bool> m_source_image_existence;
+
+ // A texture id is index in this array.
+ // The first position contains a NULL texture.
+ std::vector<TextureInfo> m_textureinfo_cache;
+ // Maps a texture name to an index in the former.
+ std::map<std::string, u32> m_name_to_id;
+ // The two former containers are behind this mutex
+ JMutex m_textureinfo_cache_mutex;
+
+ // Queued texture fetches (to be processed by the main thread)
+ RequestQueue<std::string, u32, u8, u8> m_get_texture_queue;
+
+ // Textures that have been overwritten with other ones
+ // but can't be deleted because the ITexture* might still be used
+ std::list<video::ITexture*> m_texture_trash;
+
+ // Cached settings needed for making textures from meshes
+ bool m_setting_trilinear_filter;
+ bool m_setting_bilinear_filter;
+ bool m_setting_anisotropic_filter;
+};
+
+IWritableTextureSource* createTextureSource(IrrlichtDevice *device)
+{
+ return new TextureSource(device);
+}
+