+/*
+ An internal variant of AtlasPointer with more data.
+ (well, more like a wrapper)
+*/
+
+struct SourceAtlasPointer
+{
+ std::string name;
+ AtlasPointer a;
+ video::IImage *atlas_img; // The source image of the atlas
+ // Integer variants of position and size
+ v2s32 intpos;
+ v2u32 intsize;
+
+ SourceAtlasPointer(
+ const std::string &name_,
+ AtlasPointer a_=AtlasPointer(0, NULL),
+ video::IImage *atlas_img_=NULL,
+ v2s32 intpos_=v2s32(0,0),
+ v2u32 intsize_=v2u32(0,0)
+ ):
+ name(name_),
+ a(a_),
+ atlas_img(atlas_img_),
+ intpos(intpos_),
+ intsize(intsize_)
+ {
+ }
+};
+
+/*
+ SourceImageCache: A cache used for storing source images.
+*/
+
+class SourceImageCache
+{
+public:
+ void insert(const std::string &name, video::IImage *img,
+ bool prefer_local, video::IVideoDriver *driver)
+ {
+ assert(img);
+ // Remove old image
+ core::map<std::string, video::IImage*>::Node *n;
+ n = m_images.find(name);
+ if(n){
+ video::IImage *oldimg = n->getValue();
+ if(oldimg)
+ oldimg->drop();
+ }
+ // Try to use local texture instead if asked to
+ if(prefer_local){
+ std::string path = getTexturePath(name.c_str());
+ if(path != ""){
+ video::IImage *img2 = driver->createImageFromFile(path.c_str());
+ if(img2){
+ m_images[name] = img2;
+ return;
+ }
+ }
+ }
+ img->grab();
+ m_images[name] = img;
+ }
+ video::IImage* get(const std::string &name)
+ {
+ core::map<std::string, video::IImage*>::Node *n;
+ n = m_images.find(name);
+ if(n)
+ return n->getValue();
+ return NULL;
+ }
+ // Primarily fetches from cache, secondarily tries to read from filesystem
+ video::IImage* getOrLoad(const std::string &name, IrrlichtDevice *device)
+ {
+ core::map<std::string, video::IImage*>::Node *n;
+ n = m_images.find(name);
+ if(n){
+ n->getValue()->grab(); // Grab for caller
+ return n->getValue();
+ }
+ video::IVideoDriver* driver = device->getVideoDriver();
+ std::string path = getTexturePath(name.c_str());
+ if(path == ""){
+ infostream<<"SourceImageCache::getOrLoad(): No path found for \""
+ <<name<<"\""<<std::endl;
+ return NULL;
+ }
+ infostream<<"SourceImageCache::getOrLoad(): Loading path \""<<path
+ <<"\""<<std::endl;
+ video::IImage *img = driver->createImageFromFile(path.c_str());
+ // Even if could not be loaded, put as NULL
+ //m_images[name] = img;
+ if(img){
+ m_images[name] = img;
+ img->grab(); // Grab for caller
+ }
+ return img;
+ }
+private:
+ core::map<std::string, video::IImage*> m_images;
+};
+
+/*
+ TextureSource
+*/
+
+class TextureSource : public IWritableTextureSource
+{
+public:
+ TextureSource(IrrlichtDevice *device);
+ ~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^mineral1" and is specified as a part of some atlas.
+ - 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^mineral1^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.
+ */
+ AtlasPointer getTexture(u32 id);
+
+ AtlasPointer getTexture(const std::string &name)
+ {
+ return getTexture(getTextureId(name));
+ }
+
+ // Gets a separate texture
+ video::ITexture* getTextureRaw(const std::string &name)
+ {
+ AtlasPointer ap = getTexture(name + "^[forcesingle");
+ return ap.atlas;
+ }
+
+ // Gets a separate texture atlas pointer
+ AtlasPointer getTextureRawAP(const AtlasPointer &ap)
+ {
+ return getTexture(getTextureName(ap.id) + "^[forcesingle");
+ }
+
+ // Returns a pointer to the irrlicht device
+ virtual IrrlichtDevice* getDevice()
+ {
+ return m_device;
+ }
+
+ // Update new texture pointer and texture coordinates to an
+ // AtlasPointer based on it's texture id
+ void updateAP(AtlasPointer &ap);
+
+ 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();
+
+ // Build the main texture atlas which contains most of the
+ // textures.
+ void buildMainAtlas(class IGameDef *gamedef);
+
+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.
+ core::array<SourceAtlasPointer> m_atlaspointer_cache;
+ // Maps a texture name to an index in the former.
+ core::map<std::string, u32> m_name_to_id;
+ // The two former containers are behind this mutex
+ JMutex m_atlaspointer_cache_mutex;
+
+ // Main texture atlas. This is filled at startup and is then not touched.
+ video::IImage *m_main_atlas_image;
+ video::ITexture *m_main_atlas_texture;
+
+ // Queued texture fetches (to be processed by the main thread)
+ RequestQueue<std::string, u32, u8, u8> m_get_texture_queue;
+};
+
+IWritableTextureSource* createTextureSource(IrrlichtDevice *device)
+{
+ return new TextureSource(device);
+}
+