diff --git a/source/shared_lib/include/graphics/FileReader.h b/source/shared_lib/include/graphics/FileReader.h index 05c49ed80..3290f4c1c 100644 --- a/source/shared_lib/include/graphics/FileReader.h +++ b/source/shared_lib/include/graphics/FileReader.h @@ -20,6 +20,7 @@ #include #include #include +#include using std::map; using std::string; @@ -30,6 +31,8 @@ using std::runtime_error; using Shared::Platform::extractExtension; +#define AS_STRING(...) #__VA_ARGS__ + namespace Shared{ // ===================================================== @@ -38,7 +41,7 @@ namespace Shared{ template class FileReader { -protected: +public: string const * extensions; /**Creates a filereader being able to possibly load files @@ -46,6 +49,10 @@ protected: **/ FileReader(string const * extensions); + /**Creates a low-priority filereader + **/ + FileReader(); + public: /*Return the - existing and initialized - fileReadersMap */ @@ -53,7 +60,16 @@ public: static map const * >* > fileReaderByExtension; return fileReaderByExtension; } - static vector const * > fileReaders; + + static vector const * >& getFileReaders() { + static vector const*> fileReaders; + return fileReaders; + }; + + static vector const * >& getLowPriorityFileReaders() { + static vector const*> lowPriorityFileReaders; + return lowPriorityFileReaders; + }; public: @@ -125,14 +141,16 @@ public: template static inline T* readFromFileReaders(vector const *>* readers, const string& filepath) { for (typename vector const *>::const_iterator i = readers->begin(); i != readers->end(); ++i) { + T* ret = NULL; try { FileReader const * reader = *i; - T* ret = reader->read(filepath); //It is guaranteed that at least the filepath matches ... - if (ret != NULL) { - return ret; - } + ret = reader->read(filepath); //It is guaranteed that at least the filepath matches ... } catch (...) { //TODO: Specific exceptions + continue; } + if (ret != NULL) { + return ret; + } } return NULL; } @@ -140,16 +158,17 @@ static inline T* readFromFileReaders(vector const *>* readers, con template static inline T* readFromFileReaders(vector const *>* readers, const string& filepath, T* object) { for (typename vector const *>::const_iterator i = readers->begin(); i != readers->end(); ++i) { + T* ret = NULL; try { FileReader const * reader = *i; - T* ret = reader->read(filepath, object); //It is guaranteed that at least the filepath matches ... - if (ret != NULL) { - return ret; - } + ret = reader->read(filepath, object); //It is guaranteed that at least the filepath matches ... } catch (...) { //TODO: Specific exceptions + continue; } + if (ret != NULL) { + return ret; + } } - std::cerr << "Could not parse filepath: " << filepath << std::endl; return NULL; } @@ -168,7 +187,11 @@ T* FileReader::readPath(const string& filepath) { return ret; } } - T* ret = readFromFileReaders(&fileReaders, filepath); //Try all other + T* ret = readFromFileReaders(&(getFileReaders()), filepath); //Try all other + if (ret == NULL) { + std::cerr << "Could not parse filepath: " << filepath << std::endl; + ret = readFromFileReaders(&(getLowPriorityFileReaders()), filepath); //Try to get dummy file + } return ret; } @@ -187,17 +210,20 @@ T* FileReader::readPath(const string& filepath, T* object) { return ret; } } - T* ret = readFromFileReaders(&fileReaders, filepath, object); //Try all other + T* ret = readFromFileReaders(&(getFileReaders()), filepath, object); //Try all other + if (ret == NULL) { + std::cerr << "Could not parse filepath: " << filepath << std::endl; + ret = readFromFileReaders(&(getLowPriorityFileReaders()), filepath); //Try to get dummy file + if (ret == NULL) { + throw runtime_error(string("Could not parse ") + filepath + " as object of type " + typeid(T).name()); + } + } return ret; } - -template -vector const * > FileReader::fileReaders; - template FileReader::FileReader(string const * extensions): extensions(extensions) { - fileReaders.push_back(this); + getFileReaders().push_back(this); string const * nextExtension = extensions; while (((*nextExtension) != "")) { vector const* >* curPossibleReaders = (getFileReadersMap())[*nextExtension]; diff --git a/source/shared_lib/sources/graphics/BMPReader.cpp b/source/shared_lib/sources/graphics/BMPReader.cpp index d799fb601..f1dda9234 100644 --- a/source/shared_lib/sources/graphics/BMPReader.cpp +++ b/source/shared_lib/sources/graphics/BMPReader.cpp @@ -81,6 +81,7 @@ Pixmap2D* BMPReader::read(ifstream& in, const string& path, Pixmap2D* ret) const int h= infoHeader.height; int w= infoHeader.width; int components= (ret->getComponents() == -1)?3:ret->getComponents(); + std::cout << "BMP-Components: Pic: " << components << " old: " << (ret->getComponents()) << " File: " << 3 << std::endl; ret->init(w,h,components); uint8* pixels = ret->getPixels(); for(int i=0; igetComponents() == -1)?cinfo.num_components:ret->getComponents(); + std::cout << "JPG-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << cinfo.num_components << std::endl; ret->init(cinfo.image_width, cinfo.image_height, picComponents); uint8* pixels = ret->getPixels(); //TODO: Irrlicht has some special CMYK-handling - maybe needed too? diff --git a/source/shared_lib/sources/graphics/PNGReader.cpp b/source/shared_lib/sources/graphics/PNGReader.cpp index 098bd7ec9..6458ef5d8 100644 --- a/source/shared_lib/sources/graphics/PNGReader.cpp +++ b/source/shared_lib/sources/graphics/PNGReader.cpp @@ -108,6 +108,7 @@ Pixmap2D* PNGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const png_read_image(png_ptr, row_pointers); int fileComponents = info_ptr->rowbytes/info_ptr->width; int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents(); + std::cout << "PNG-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << std::endl; //Copy image ret->init(width,height,picComponents); uint8* pixels = ret->getPixels(); diff --git a/source/shared_lib/sources/graphics/TGAReader.cpp b/source/shared_lib/sources/graphics/TGAReader.cpp index 7c74327a1..4a7f24503 100644 --- a/source/shared_lib/sources/graphics/TGAReader.cpp +++ b/source/shared_lib/sources/graphics/TGAReader.cpp @@ -85,6 +85,7 @@ Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const const int w = fileHeader.width; const int fileComponents= fileHeader.bitsPerPixel/8; const int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents(); + std::cout << "BMP-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << std::endl; ret->init(w,h,picComponents); uint8* pixels = ret->getPixels(); //read file diff --git a/source/shared_lib/sources/graphics/gl/texture_gl.cpp b/source/shared_lib/sources/graphics/gl/texture_gl.cpp index cdedf6f14..8e3eac486 100644 --- a/source/shared_lib/sources/graphics/gl/texture_gl.cpp +++ b/source/shared_lib/sources/graphics/gl/texture_gl.cpp @@ -15,6 +15,7 @@ #include "opengl.h" #include "leak_dumper.h" +#include using namespace std; @@ -47,6 +48,7 @@ GLint toFormatGl(Texture::Format format, int components){ case 4: return GL_RGBA; default: + std::cerr << "Components = " << (components) << std::endl; assert(false); return GL_RGBA; }