/*
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
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "tile.h"
#include <ICameraSceneNode.h>
#include "util/string.h"
#include "util/container.h"
#include "util/thread.h"
#include "util/numeric.h"
#include "irrlichttypes_extrabloated.h"
#include "debug.h"
#include "filesys.h"
#include "settings.h"
#include "mesh.h"
#include "log.h"
#include "gamedef.h"
#include "util/strfnd.h"
#include "util/string.h" // for parseColorString()
#include "imagefilters.h"
#include "guiscalingfilter.h"
#include "nodedef.h"
#ifdef __ANDROID__
#include <GLES/gl.h>
#endif
/*
A cache from texture name to texture path
*/
MutexedMap<std::string, std::string> g_texturename_to_path_cache;
/*
Replaces the filename extension.
eg:
std::string image = "a/image.png"
replace_ext(image, "jpg")
-> image = "a/image.jpg"
Returns true on success.
*/
static bool replace_ext(std::string &path, const char *ext)
{
if (ext == NULL)
return false;
// Find place of last dot, fail if \ or / found.
s32 last_dot_i = -1;
for (s32 i=path.size()-1; i>=0; i--)
{
if (path[i] == '.')
{
last_dot_i = i;
break;
}
if (path[i] == '\\' || path[i] == '/')
break;
}
// If not found, return an empty string
if (last_dot_i == -1)
return false;
// Else make the new path
path = path.substr(0, last_dot_i+1) + ext;
return true;
}
/*
Find out the full path of an image by trying different filename
extensions.
If failed, return "".
*/
std::string getImagePath(std::string path)
{
// A NULL-ended list of possible image extensions
const char *extensions[] = {
"png", "jpg", "bmp", "tga",
"pcx", "ppm", "psd", "wal", "rgb",
NULL
};
// If there is no extension, add one
if (removeStringEnd(path, extensions) == "")
path = path + ".png";
// Check paths until something is found to exist
const char **ext = extensions;
do{
bool r = replace_ext(path, *ext);
if (r == false)
return "";
if (fs::PathExists(path))
return path;
}
while((++ext) != NULL);
return "";
}
/*
Gets the path to a texture by first checking if the texture exists
in texture_path and if not, using the data path.
Checks all supported extensions by replacing the original extension.
If not found, returns "".
Utilizes a thread-safe cache.
*/
std::string getTexturePath(const std::string &filename)
{
std::string fullpath = "";
/*
Check from cache
*/
bool incache = g_texturename_to_path_cache.get(filename, &fullpath);
if (incache)
return fullpath;
/*
Check from texture_path
*/
std::string texture_path = g_settings->get("texture_path");
if (texture_path != "")
{
std::string testpath = texture_path + DIR_DELIM + filename;
// Check all filename extensions. Returns "" if not found.
fullpath = getImagePath(testpath);
}
/*
Check from default data directory
*/
if (fullpath == "")
{
std::string base_path = porting::path_share + DIR_DELIM + "textures"
+ DIR_DELIM + "base" + DIR_DELIM + "pack";
std::string testpath = base_path + DIR_DELIM + filename;
// Check all filename extensions. Returns "" if not found.
fullpath = getImagePath(testpath);
}
// Add to cache (also an empty result is cached)
g_texturename_to_path_cache.set(filename, fullpath);
// Finally return it
return fullpath;
}
void clearTextureNameCache()
{
g_texturename_to_path_cache.clear();
}
/*
Stores internal information about a texture.
*/
struct TextureInfo
{
std::string name;
video::ITexture *texture;
TextureInfo(
const std::string &name_,
video::ITexture *texture_=NULL
):
name(name_),
texture(texture_)
{
}
};
/*
SourceImageCache: A cache used for storing source images.
*/
class SourceImageCache
{
public:
~SourceImageCache() {
for (std::map<std::string, video::IImage*>::iterator iter = m_images.begin();
iter != m_images.end(); ++iter) {
iter->second->drop();
}
m_images.clear();
}
void insert(const std::string &name, video::IImage *img,
bool prefer_local, video::IVideoDriver *driver)
{
assert(img); // Pre-condition
// Remove old image
std::map<std::string, video::IImage*>::iterator n;
n = m_images.find(name);
if (n != m_images.end()){
if (n->second)
n->second->drop();
}
video::IImage* toadd = img;
bool need_to_grab = true;
// Try to use local texture instead if asked to
if (prefer_local){
std::string path = getTexturePath(name);
if (path != ""){
video::IImage *img2 = driver->createImageFromFile(path.c_str());
if (img2){
toadd = img2;
need_to_grab = false;
}
}
}
if (need_to_grab)
toadd->grab();
m_images[name] = toadd;
}
video::IImage* get(const std::string &name)
{
std::map<std::string, video::IImage*>::iterator n;
n = m_images.find(name);
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)
{
std::map<std::string, video::IImage*>::iterator n;
n = m_images.find(name);
if (n != m_images.end()){
n->second->grab(); // Grab for caller
return n->second;
}
video::IVideoDriver* driver = device->getVideoDriver();
std::string path = getTexturePath(name);
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());
if (img){
m_images[name] = img;
img->grab(); // Grab for caller
}
return img;
}
private:
std::map<std::string, video::IImage*> m_images;
};
/*
TextureSource
*/
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:
|