Use canonical path names for resource lookup (Font, TextureResource).

Fixes "./../art/file.png" causing duplicates when used in multiple systems, even if it refers to the same file.
Remove debug messages for SVGResource and ImageComponent.
This commit is contained in:
Aloshi 2014-05-23 17:21:01 -05:00
parent 1e7ced2c66
commit b15f3aeec0
6 changed files with 22 additions and 14 deletions

View file

@ -1,4 +1,5 @@
#include "Util.h"
#include <boost/filesystem.hpp>
std::string strToUpper(const char* from)
{
@ -59,3 +60,11 @@ Eigen::Vector2f roundVector(const Eigen::Vector2f& vec)
ret[1] = round(ret[1]);
return ret;
}
std::string getCanonicalPath(const std::string& path)
{
if(boost::filesystem::exists(path))
return boost::filesystem::canonical(path).generic_string();
else
return "";
}

View file

@ -12,3 +12,5 @@ Eigen::Vector3f roundVector(const Eigen::Vector3f& vec);
Eigen::Vector2f roundVector(const Eigen::Vector2f& vec);
float round(float num);
std::string getCanonicalPath(const std::string& str);

View file

@ -283,14 +283,11 @@ bool ImageComponent::hasImage()
void ImageComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties)
{
LOG(LogInfo) << " req image [" << view << "." << element << "] (flags: " << properties << ")";
using namespace ThemeFlags;
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "image");
if(!elem)
{
LOG(LogInfo) << " (missing)";
return;
}

View file

@ -73,7 +73,9 @@ void Font::unload(std::shared_ptr<ResourceManager>& rm)
std::shared_ptr<Font> Font::get(int size, const std::string& path)
{
std::pair<std::string, int> def(path.empty() ? getDefaultPath() : path, size);
const std::string canonicalPath = getCanonicalPath(path);
std::pair<std::string, int> def(canonicalPath.empty() ? getDefaultPath() : canonicalPath, size);
auto foundFont = sFontMap.find(def);
if(foundFont != sFontMap.end())
{

View file

@ -69,10 +69,6 @@ void SVGResource::rasterizeAt(size_t width, size_t height)
mLastHeight = height;
}
LOG(LogInfo) << "Rasterizing \"" << mPath << "\"...";
LOG(LogInfo) << " Original width: " << mSVGImage->width << ", original height: " << mSVGImage->height;
LOG(LogInfo) << " width: " << width << ", height: " << height << ", scale: " << height / mSVGImage->height;
unsigned char* imagePx = (unsigned char*)malloc(width * height * 4);
NSVGrasterizer* rast = nsvgCreateRasterizer();

View file

@ -4,7 +4,7 @@
#include GLHEADER
#include "../ImageIO.h"
#include "../Renderer.h"
#include "../Util.h"
#include "SVGResource.h"
std::map< TextureResource::TextureKeyType, std::weak_ptr<TextureResource> > TextureResource::sTextureMap;
@ -102,14 +102,16 @@ std::shared_ptr<TextureResource> TextureResource::get(const std::string& path, b
{
std::shared_ptr<ResourceManager>& rm = ResourceManager::getInstance();
if(path.empty())
const std::string canonicalPath = getCanonicalPath(path);
if(canonicalPath.empty())
{
std::shared_ptr<TextureResource> tex(new TextureResource("", tile));
rm->addReloadable(tex); //make sure we get properly deinitialized even though we do nothing on reinitialization
return tex;
}
TextureKeyType key(path, tile);
TextureKeyType key(canonicalPath, tile);
auto foundTexture = sTextureMap.find(key);
if(foundTexture != sTextureMap.end())
{
@ -121,18 +123,18 @@ std::shared_ptr<TextureResource> TextureResource::get(const std::string& path, b
std::shared_ptr<TextureResource> tex;
// is it an SVG?
if(path.substr(path.size() - 4, std::string::npos) == ".svg")
if(key.first.substr(key.first.size() - 4, std::string::npos) == ".svg")
{
// probably
// don't add it to our map because 2 svgs might be rasterized at different sizes
tex = std::shared_ptr<SVGResource>(new SVGResource(path, tile));
tex = std::shared_ptr<SVGResource>(new SVGResource(key.first, tile));
sTextureList.push_back(tex); // add it to our list though
rm->addReloadable(tex);
tex->reload(rm);
return tex;
}else{
// normal texture
tex = std::shared_ptr<TextureResource>(new TextureResource(path, tile));
tex = std::shared_ptr<TextureResource>(new TextureResource(key.first, tile));
sTextureMap[key] = std::weak_ptr<TextureResource>(tex);
sTextureList.push_back(tex);
rm->addReloadable(tex);