]> git.lizzy.rs Git - minetest.git/blobdiff - src/tile.cpp
Translated using Weblate (German)
[minetest.git] / src / tile.cpp
index e676c56c438c8eb54859e2d4ae1db0d7c22f1aeb..aea9665f5eb69ca3568c12447538a81e7a1b8771 100644 (file)
@@ -1,6 +1,6 @@
 /*
-Minetest-c55
-Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
@@ -206,10 +206,10 @@ class SourceImageCache
        {
                assert(img);
                // Remove old image
-               core::map<std::string, video::IImage*>::Node *n;
+               std::map<std::string, video::IImage*>::iterator n;
                n = m_images.find(name);
-               if(n){
-                       video::IImage *oldimg = n->getValue();
+               if(n != m_images.end()){
+                       video::IImage *oldimg = n->second;
                        if(oldimg)
                                oldimg->drop();
                }
@@ -229,20 +229,20 @@ class SourceImageCache
        }
        video::IImage* get(const std::string &name)
        {
-               core::map<std::string, video::IImage*>::Node *n;
+               std::map<std::string, video::IImage*>::iterator n;
                n = m_images.find(name);
-               if(n)
-                       return n->getValue();
+               if(n != m_images.end())
+                       return n->second;
                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;
+               std::map<std::string, video::IImage*>::iterator n;
                n = m_images.find(name);
-               if(n){
-                       n->getValue()->grab(); // Grab for caller
-                       return n->getValue();
+               if(n != m_images.end()){
+                       n->second->grab(); // Grab for caller
+                       return n->second;
                }
                video::IVideoDriver* driver = device->getVideoDriver();
                std::string path = getTexturePath(name.c_str());
@@ -263,7 +263,7 @@ class SourceImageCache
                return img;
        }
 private:
-       core::map<std::string, video::IImage*> m_images;
+       std::map<std::string, video::IImage*> m_images;
 };
 
 /*
@@ -372,6 +372,18 @@ class TextureSource : public IWritableTextureSource
        // 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.
@@ -400,11 +412,14 @@ class TextureSource : public IWritableTextureSource
        // 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;
+       std::vector<SourceAtlasPointer> m_atlaspointer_cache;
        // Maps a texture name to an index in the former.
-       core::map<std::string, u32> m_name_to_id;
+       std::map<std::string, u32> m_name_to_id;
        // The two former containers are behind this mutex
        JMutex m_atlaspointer_cache_mutex;
        
@@ -450,11 +465,11 @@ u32 TextureSource::getTextureId(const std::string &name)
                        See if texture already exists
                */
                JMutexAutoLock lock(m_atlaspointer_cache_mutex);
-               core::map<std::string, u32>::Node *n;
+               std::map<std::string, u32>::iterator n;
                n = m_name_to_id.find(name);
-               if(n != NULL)
+               if(n != m_name_to_id.end())
                {
-                       return n->getValue();
+                       return n->second;
                }
        }
        
@@ -518,15 +533,6 @@ core::dimension2d<u32> imageTransformDimension(u32 transform, core::dimension2d<
 // Apply transform to image data
 void imageTransform(u32 transform, video::IImage *src, video::IImage *dst);
 
-/*
-       Adds a new texture to the video driver and returns a pointer to it.
-       This pointer should not be dropped. Any texture that was registered
-       with that name before is removed (this may invalidate some ITexture
-       pointers).
-*/
-video::ITexture* register_texture(video::IVideoDriver *driver,
-               std::string name, video::IImage *img);
-
 /*
        Generate image based on a string like "stone.png" or "[crack0".
        if baseimg is NULL, it is created. Otherwise stuff is made on it.
@@ -573,13 +579,13 @@ u32 TextureSource::getTextureIdDirect(const std::string &name)
        {
                JMutexAutoLock lock(m_atlaspointer_cache_mutex);
 
-               core::map<std::string, u32>::Node *n;
+               std::map<std::string, u32>::iterator n;
                n = m_name_to_id.find(name);
-               if(n != NULL)
+               if(n != m_name_to_id.end())
                {
                        /*infostream<<"getTextureIdDirect(): \""<<name
                                        <<"\" found in cache"<<std::endl;*/
-                       return n->getValue();
+                       return n->second;
                }
        }
 
@@ -695,9 +701,11 @@ u32 TextureSource::getTextureIdDirect(const std::string &name)
                                " create texture \""<<name<<"\""<<std::endl;
        }
        
-       // Create texture from resulting image
        if(baseimg != NULL)
-               t = register_texture(driver, name, baseimg);
+       {
+               // Create texture from resulting image
+               t = driver->addTexture(name.c_str(), baseimg);
+       }
        
        /*
                Add texture to caches (add NULL textures too)
@@ -716,7 +724,7 @@ u32 TextureSource::getTextureIdDirect(const std::string &name)
                baseimg_dim = baseimg->getDimension();
        SourceAtlasPointer nap(name, ap, baseimg, v2s32(0,0), baseimg_dim);
        m_atlaspointer_cache.push_back(nap);
-       m_name_to_id.insert(name, id);
+       m_name_to_id[name] = id;
 
        /*infostream<<"getTextureIdDirect(): "
                        <<"Returning id="<<id<<" for name \""<<name<<"\""<<std::endl;*/
@@ -761,7 +769,7 @@ void TextureSource::processQueue()
        /*
                Fetch textures
        */
-       if(m_get_texture_queue.size() > 0)
+       if(!m_get_texture_queue.empty())
        {
                GetRequest<std::string, u32, u8, u8>
                                request = m_get_texture_queue.pop();
@@ -788,6 +796,7 @@ void TextureSource::insertSourceImage(const std::string &name, video::IImage *im
        assert(get_current_thread_id() == m_main_thread);
        
        m_sourcecache.insert(name, img, true, m_device->getVideoDriver());
+       m_source_image_existence.set(name, true);
 }
        
 void TextureSource::rebuildImagesAndTextures()
@@ -816,7 +825,7 @@ void TextureSource::rebuildImagesAndTextures()
                // Create texture from resulting image
                video::ITexture *t = NULL;
                if(img)
-                       t = register_texture(driver, sap->name, img);
+                       t = driver->addTexture(sap->name.c_str(), img);
                
                // Replace texture
                sap->a.atlas = t;
@@ -863,7 +872,7 @@ void TextureSource::buildMainAtlas(class IGameDef *gamedef)
                main content features
        */
 
-       core::map<std::string, bool> sourcelist;
+       std::set<std::string> sourcelist;
 
        for(u16 j=0; j<MAX_CONTENT+1; j++)
        {
@@ -873,16 +882,16 @@ void TextureSource::buildMainAtlas(class IGameDef *gamedef)
                for(u32 i=0; i<6; i++)
                {
                        std::string name = f.tiledef[i].name;
-                       sourcelist[name] = true;
+                       sourcelist.insert(name);
                }
        }
        
        infostream<<"Creating texture atlas out of textures: ";
-       for(core::map<std::string, bool>::Iterator
-                       i = sourcelist.getIterator();
-                       i.atEnd() == false; i++)
+       for(std::set<std::string>::iterator
+                       i = sourcelist.begin();
+                       i != sourcelist.end(); ++i)
        {
-               std::string name = i.getNode()->getKey();
+               std::string name = *i;
                infostream<<"\""<<name<<"\" ";
        }
        infostream<<std::endl;
@@ -901,11 +910,11 @@ void TextureSource::buildMainAtlas(class IGameDef *gamedef)
        pos_in_atlas.X = column_padding;
        pos_in_atlas.Y = padding;
 
-       for(core::map<std::string, bool>::Iterator
-                       i = sourcelist.getIterator();
-                       i.atEnd() == false; i++)
+       for(std::set<std::string>::iterator
+                       i = sourcelist.begin();
+                       i != sourcelist.end(); ++i)
        {
-               std::string name = i.getNode()->getKey();
+               std::string name = *i;
 
                // Generate image by name
                video::IImage *img2 = generate_image_from_scratch(name, m_device,
@@ -1017,11 +1026,11 @@ void TextureSource::buildMainAtlas(class IGameDef *gamedef)
                bool reuse_old_id = false;
                u32 id = m_atlaspointer_cache.size();
                // Check old id without fetching a texture
-               core::map<std::string, u32>::Node *n;
+               std::map<std::string, u32>::iterator n;
                n = m_name_to_id.find(name);
                // If it exists, we will replace the old definition
-               if(n){
-                       id = n->getValue();
+               if(n != m_name_to_id.end()){
+                       id = n->second;
                        reuse_old_id = true;
                        /*infostream<<"TextureSource::buildMainAtlas(): "
                                        <<"Replacing old AtlasPointer"<<std::endl;*/
@@ -1051,18 +1060,18 @@ void TextureSource::buildMainAtlas(class IGameDef *gamedef)
        /*
                Make texture
        */
-       video::ITexture *t = register_texture(driver, "__main_atlas__", atlas_img);
+       video::ITexture *t = driver->addTexture("__main_atlas__", atlas_img);
        assert(t);
 
        /*
                Second pass: set texture pointer in generated AtlasPointers
        */
-       for(core::map<std::string, bool>::Iterator
-                       i = sourcelist.getIterator();
-                       i.atEnd() == false; i++)
+       for(std::set<std::string>::iterator
+                       i = sourcelist.begin();
+                       i != sourcelist.end(); ++i)
        {
-               std::string name = i.getNode()->getKey();
-               if(m_name_to_id.find(name) == NULL)
+               std::string name = *i;
+               if(m_name_to_id.find(name) == m_name_to_id.end())
                        continue;
                u32 id = m_name_to_id[name];
                //infostream<<"id of name "<<name<<" is "<<id<<std::endl;
@@ -1142,15 +1151,6 @@ video::IImage* generate_image_from_scratch(std::string name,
        return baseimg;
 }
 
-video::ITexture* register_texture(video::IVideoDriver *driver,
-               std::string name, video::IImage *img)
-{
-       video::ITexture *old_texture = driver->findTexture(name.c_str());
-       if(old_texture)
-               driver->removeTexture(old_texture);
-       return driver->addTexture(name.c_str(), img);
-}
-
 bool generate_image(std::string part_of_name, video::IImage *& baseimg,
                IrrlichtDevice *device, SourceImageCache *sourcecache)
 {
@@ -1557,12 +1557,12 @@ bool generate_image(std::string part_of_name, video::IImage *& baseimg,
                        assert(img_top && img_left && img_right);
 
                        // Create textures from images
-                       video::ITexture *texture_top = register_texture(driver,
-                                       imagename_top + "__temp1__", img_top);
-                       video::ITexture *texture_left = register_texture(driver,
-                                       imagename_left + "__temp2__", img_left);
-                       video::ITexture *texture_right = register_texture(driver,
-                                       imagename_right + "__temp3__", img_right);
+                       video::ITexture *texture_top = driver->addTexture(
+                                       (imagename_top + "__temp__").c_str(), img_top);
+                       video::ITexture *texture_left = driver->addTexture(
+                                       (imagename_left + "__temp__").c_str(), img_left);
+                       video::ITexture *texture_right = driver->addTexture(
+                                       (imagename_right + "__temp__").c_str(), img_right);
                        assert(texture_top && texture_left && texture_right);
 
                        // Drop images