From bcbf7759e65c5089d5857f6aee9de335ddfbc4b9 Mon Sep 17 00:00:00 2001 From: josh Date: Sun, 18 Oct 2009 16:33:56 +0000 Subject: [PATCH] converted TextureCache to use TextureLoader interface git-svn-id: svn://anubis/misc/TextureCache@184 bd8a9e45-a331-0410-811e-c64571078777 --- TextureCache.cc | 163 ++++++++++++++++++++++++++---------------------- TextureCache.h | 18 +++--- 2 files changed, 99 insertions(+), 82 deletions(-) diff --git a/TextureCache.cc b/TextureCache.cc index 8a8ab29..40ceaa2 100644 --- a/TextureCache.cc +++ b/TextureCache.cc @@ -4,91 +4,104 @@ #include #include #include -#include "TextureCache.hh" +#include "TextureCache.h" +#include "TextureLoader/TextureLoader.h" +#include "FileLoader/FileLoader.h" using namespace std; -GLuint TextureCache::load(const string & filename) +GLuint TextureCache::load(const FileLoader::Path & path, + FileLoader & fileLoader, bool mipmaps, + int mode, int quality) { - map::iterator it = m_cache.find(filename); - if (it != m_cache.end()) - return it->second; - GLuint tex = loadTexture(filename.c_str()); - m_cache[filename] = tex; - return tex; + string filename = path.shortPath; + if (filename == "") + filename = path.fullPath; + if (filename == "") + return 0; + map::iterator it = m_cache.find(filename); + if (it != m_cache.end()) + return it->second; + GLuint tex = loadTexture(path, fileLoader, mipmaps, mode, quality); + m_cache[filename] = tex; + return tex; } -GLuint TextureCache::loadTexture(const char * filename, - bool mipmaps, - int mode, - int quality) +GLuint TextureCache::loadTexture(const FileLoader::Path & path, + FileLoader & fileLoader, bool mipmaps, + int mode, int quality) { - GLuint texture; - SDL_Surface * temp = IMG_Load(filename); - if (!temp) - { - cerr << "Failed to load image '" << filename << "'!" << endl; - return 0; - } + GLuint texture; + FileLoader::Buffer buff = fileLoader.load(path); + if (buff.size <= 0) + return 0; + SDL_RWops * ops = SDL_RWFromMem(buff.data, buff.size); + SDL_Surface * temp = IMG_Load_RW(ops, /* TODO: figure out param */ true); + if (!temp) + { + cerr << "Failed to load image ('" << path.fullPath + << ", " << path.shortPath << ")!" << endl; + return 0; + } - SDL_PixelFormat fmt; - fmt.palette = NULL; - fmt.BitsPerPixel = 32; - fmt.BytesPerPixel = 4; - fmt.Rmask = 0x000000FF; - fmt.Gmask = 0x0000FF00; - fmt.Bmask = 0x00FF0000; - fmt.Amask = 0xFF000000; - fmt.Rshift = 0; - fmt.Gshift = 8; - fmt.Bshift = 16; - fmt.Ashift = 24; + SDL_PixelFormat fmt; + fmt.palette = NULL; + fmt.BitsPerPixel = 32; + fmt.BytesPerPixel = 4; + fmt.Rmask = 0x000000FF; + fmt.Gmask = 0x0000FF00; + fmt.Bmask = 0x00FF0000; + fmt.Amask = 0xFF000000; + fmt.Rshift = 0; + fmt.Gshift = 8; + fmt.Bshift = 16; + fmt.Ashift = 24; - SDL_Surface * texsurf = SDL_ConvertSurface(temp, &fmt, SDL_SWSURFACE); - SDL_FreeSurface(temp); - if (!texsurf) - { - cerr << '\'' << filename << "' was not converted properly!" << endl; - return 0; - } - unsigned int * pixels = new unsigned int[texsurf->w * texsurf->h]; - int y; - unsigned int dstOffset = texsurf->w * (texsurf->h - 1); - unsigned int srcOffset = 0; - for (y = 0; y < texsurf->h; y++) - { - memcpy(pixels + dstOffset, - ((unsigned int *)texsurf->pixels) + srcOffset, - texsurf->w << 2); - dstOffset -= texsurf->w; - srcOffset += texsurf->w; - } - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D(GL_TEXTURE_2D, 0, 4, texsurf->w, texsurf->h, 0, - GL_RGBA, GL_UNSIGNED_BYTE, pixels); + SDL_Surface * texsurf = SDL_ConvertSurface(temp, &fmt, SDL_SWSURFACE); + SDL_FreeSurface(temp); + if (!texsurf) + { + cerr << "Image was not converted properly!" << endl; + return 0; + } + unsigned int * pixels = new unsigned int[texsurf->w * texsurf->h]; + int y; + unsigned int dstOffset = texsurf->w * (texsurf->h - 1); + unsigned int srcOffset = 0; + for (y = 0; y < texsurf->h; y++) + { + memcpy(pixels + dstOffset, + ((unsigned int *)texsurf->pixels) + srcOffset, + texsurf->w << 2); + dstOffset -= texsurf->w; + srcOffset += texsurf->w; + } + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, 4, texsurf->w, texsurf->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, pixels); - if (quality > 0) - { - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, - mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, - mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); - } - else - { - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, - mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, - mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); - } + if (quality > 0) + { + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, + mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); + } + else + { + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, + mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); + } - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode); - if (mipmaps) - gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, - texsurf->w, texsurf->h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + if (mipmaps) + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, + texsurf->w, texsurf->h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - SDL_FreeSurface(texsurf); - delete[] pixels; - return texture; + SDL_FreeSurface(texsurf); + delete[] pixels; + return texture; } diff --git a/TextureCache.h b/TextureCache.h index f2c18e5..597a232 100644 --- a/TextureCache.h +++ b/TextureCache.h @@ -2,21 +2,25 @@ #ifndef TEXTURECACHE_H #define TEXTURECACHE_H TEXTURECACHE_H +#include #include #include #include +#include "TextureLoader/TextureLoader.h" +#include "FileLoader/FileLoader.h" -class TextureCache +class TextureCache : public TextureLoader { public: - GLuint load(const std::string & filename); + virtual GLuint load(const FileLoader::Path & path, + FileLoader & fileLoader, bool mipmaps = true, + int mode = GL_DECAL, int quality = 1); -private: +protected: /* methods */ - GLuint loadTexture(const char * filename, - bool mipmaps = false, - int mode = GL_DECAL, - int quality = 1); + GLuint loadTexture(const FileLoader::Path & path, + FileLoader & fileLoader, bool mipmaps, + int mode, int quality); /* data */ std::map< std::string, GLuint > m_cache;