rework on resources system

This commit is contained in:
Eduardo Bart
2011-05-19 14:11:05 -03:00
parent e1af35f061
commit ab6c52a3ee
24 changed files with 322 additions and 226 deletions

View File

@@ -68,13 +68,12 @@ void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
bool Font::load(const std::string& file)
{
std::string fileContents = g_resources.loadTextFile(file);
if(!fileContents.size()) {
std::stringstream fin;
if(!g_resources.loadFile(file, fin)) {
flogError("ERROR: Coult not load font file \"%s", file.c_str());
return false;
}
std::istringstream fin(fileContents);
std::string textureName;
Size glyphSize;
@@ -94,7 +93,7 @@ bool Font::load(const std::string& file)
m_glyphSpacing = yamlRead(doc, "glyph spacing", Size(0,0));
// load texture
m_texture = g_textures.get("fonts/" + textureName);
m_texture = g_textures.get(textureName);
if(!m_texture) {
flogError("ERROR: Failed to load image for font file \"%s\"", file.c_str());
return false;

View File

@@ -30,17 +30,21 @@ Fonts g_fonts;
void Fonts::init()
{
g_resources.pushCurrentPath("fonts");
// load all fonts
std::list<std::string> files = g_resources.getDirectoryFiles("fonts");
std::list<std::string> files = g_resources.listDirectoryFiles();
foreach(const std::string& file, files) {
if(boost::ends_with(file, ".yml")) {
std::string name = file;
boost::erase_first(name, ".yml");
FontPtr font(new Font(name));
if(font->load("fonts/" + file))
if(font->load(file))
m_fonts.push_back(font);
}
}
g_resources.popCurrentPath();
}
FontPtr Fonts::get(const std::string& fontName)

View File

@@ -28,12 +28,12 @@
#include <util/apngloader.h>
#include "animatedtexture.h"
TexturePtr TextureLoader::loadPNG(uchar *fileData, uint fileSize)
TexturePtr TextureLoader::loadPNG(std::stringstream& file)
{
TexturePtr texture;
apng_data apng;
if(load_apng(fileData, fileSize, &apng) == 0) {
if(load_apng(file, &apng) == 0) {
if(apng.num_frames > 1) { // animated texture
uchar *framesdata = apng.pdata + (apng.first_frame * apng.width * apng.height * apng.bpp);
texture = TexturePtr(new AnimatedTexture(apng.width, apng.height, apng.bpp, apng.num_frames, framesdata, (int*)apng.frames_delay));

View File

@@ -32,7 +32,7 @@ class TextureLoader
{
public:
/// Load a png textures
static TexturePtr loadPNG(uchar *fileData, uint fileSize);
static TexturePtr loadPNG(std::stringstream& file);
};
#endif // TEXTURELOADER_H

View File

@@ -52,19 +52,17 @@ TexturePtr Textures::get(const std::string& textureFile)
}
// load texture file data
uint fileSize;
uchar *textureFileData = g_resources.loadFile(textureFile, &fileSize);
if(!textureFileData) {
std::stringstream fin;
if(!g_resources.loadFile(textureFile, fin)) {
flogError("ERROR: Unable to load texture %s, file could not be read.", textureFile.c_str());
return texture;
}
// load the texture
texture = TexturePtr(TextureLoader::loadPNG(textureFileData, fileSize));
texture = TexturePtr(TextureLoader::loadPNG(fin));
if(!texture)
flogError("ERROR: Unable to load texture %s", textureFile.c_str());
delete[] textureFileData;
}
return texture;
}