aboutsummaryrefslogtreecommitdiff
path: root/src/tile.cpp
diff options
context:
space:
mode:
authorPerttu Ahola <celeron55@gmail.com>2011-05-21 11:07:03 +0300
committerPerttu Ahola <celeron55@gmail.com>2011-05-21 11:07:03 +0300
commit969fbb189d16d87d26ceab29795644588fb90a32 (patch)
treea67a53a3ec9bd1afffda372587a9205d3ba8ef96 /src/tile.cpp
parenta8acf3c391b4dbdc4f4fd12b7a491a1c38dab337 (diff)
downloadminetest-969fbb189d16d87d26ceab29795644588fb90a32.tar.gz
minetest-969fbb189d16d87d26ceab29795644588fb90a32.tar.bz2
minetest-969fbb189d16d87d26ceab29795644588fb90a32.zip
All textures are are now searched first from the directory specified by the texture_path setting.
Diffstat (limited to 'src/tile.cpp')
-rw-r--r--src/tile.cpp62
1 files changed, 49 insertions, 13 deletions
diff --git a/src/tile.cpp b/src/tile.cpp
index 5b89c6932..dabc1dcf3 100644
--- a/src/tile.cpp
+++ b/src/tile.cpp
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 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 General Public License as published by
@@ -21,6 +21,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "debug.h"
#include "main.h" // for g_settings
#include "filesys.h"
+#include "utility.h"
+
+/*
+ A cache from texture name to texture path
+*/
+MutexedMap<std::string, std::string> g_texturename_to_path_cache;
/*
Replaces the filename extension.
@@ -30,7 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
-> image = "a/image.jpg"
Returns true on success.
*/
-inline bool replace_ext(std::string &path, const char *ext)
+static bool replace_ext(std::string &path, const char *ext)
{
if(ext == NULL)
return false;
@@ -61,7 +67,7 @@ inline bool replace_ext(std::string &path, const char *ext)
If failed, return "".
*/
-inline std::string getImagePath(std::string path)
+static std::string getImagePath(std::string path)
{
// A NULL-ended list of possible image extensions
const char *extensions[] = {
@@ -86,25 +92,55 @@ inline std::string getImagePath(std::string path)
/*
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.
*/
-inline std::string getTexturePath(std::string filename)
+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 fullpath = texture_path + '/' + filename;
- // Check all filename extensions
- fullpath = getImagePath(fullpath);
- // If found, return it
- if(fullpath != "")
- return fullpath;
+ std::string testpath = texture_path + '/' + filename;
+ // Check all filename extensions. Returns "" if not found.
+ fullpath = getImagePath(testpath);
}
- std::string fullpath = porting::getDataPath(filename.c_str());
- // Check all filename extensions
- fullpath = getImagePath(fullpath);
+
+ /*
+ Check from default data directory
+ */
+ if(fullpath == "")
+ {
+ std::string testpath = porting::getDataPath(filename.c_str());
+ // 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;
}
+/*
+ TextureSource
+*/
+
TextureSource::TextureSource(IrrlichtDevice *device):
m_device(device),
m_main_atlas_image(NULL),