converted TextureCache to use TextureLoader interface

git-svn-id: svn://anubis/misc/TextureCache@184 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
josh 2009-10-18 16:33:56 +00:00
parent f314a86b77
commit bcbf7759e6
2 changed files with 99 additions and 82 deletions

View File

@ -4,91 +4,104 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>
#include <iostream> #include <iostream>
#include "TextureCache.hh" #include "TextureCache.h"
#include "TextureLoader/TextureLoader.h"
#include "FileLoader/FileLoader.h"
using namespace std; 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<string,GLuint>::iterator it = m_cache.find(filename); string filename = path.shortPath;
if (it != m_cache.end()) if (filename == "")
return it->second; filename = path.fullPath;
GLuint tex = loadTexture(filename.c_str()); if (filename == "")
m_cache[filename] = tex; return 0;
return tex; map<string,GLuint>::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, GLuint TextureCache::loadTexture(const FileLoader::Path & path,
bool mipmaps, FileLoader & fileLoader, bool mipmaps,
int mode, int mode, int quality)
int quality)
{ {
GLuint texture; GLuint texture;
SDL_Surface * temp = IMG_Load(filename); FileLoader::Buffer buff = fileLoader.load(path);
if (!temp) if (buff.size <= 0)
{ return 0;
cerr << "Failed to load image '" << filename << "'!" << endl; SDL_RWops * ops = SDL_RWFromMem(buff.data, buff.size);
return 0; 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; SDL_PixelFormat fmt;
fmt.palette = NULL; fmt.palette = NULL;
fmt.BitsPerPixel = 32; fmt.BitsPerPixel = 32;
fmt.BytesPerPixel = 4; fmt.BytesPerPixel = 4;
fmt.Rmask = 0x000000FF; fmt.Rmask = 0x000000FF;
fmt.Gmask = 0x0000FF00; fmt.Gmask = 0x0000FF00;
fmt.Bmask = 0x00FF0000; fmt.Bmask = 0x00FF0000;
fmt.Amask = 0xFF000000; fmt.Amask = 0xFF000000;
fmt.Rshift = 0; fmt.Rshift = 0;
fmt.Gshift = 8; fmt.Gshift = 8;
fmt.Bshift = 16; fmt.Bshift = 16;
fmt.Ashift = 24; fmt.Ashift = 24;
SDL_Surface * texsurf = SDL_ConvertSurface(temp, &fmt, SDL_SWSURFACE); SDL_Surface * texsurf = SDL_ConvertSurface(temp, &fmt, SDL_SWSURFACE);
SDL_FreeSurface(temp); SDL_FreeSurface(temp);
if (!texsurf) if (!texsurf)
{ {
cerr << '\'' << filename << "' was not converted properly!" << endl; cerr << "Image was not converted properly!" << endl;
return 0; return 0;
} }
unsigned int * pixels = new unsigned int[texsurf->w * texsurf->h]; unsigned int * pixels = new unsigned int[texsurf->w * texsurf->h];
int y; int y;
unsigned int dstOffset = texsurf->w * (texsurf->h - 1); unsigned int dstOffset = texsurf->w * (texsurf->h - 1);
unsigned int srcOffset = 0; unsigned int srcOffset = 0;
for (y = 0; y < texsurf->h; y++) for (y = 0; y < texsurf->h; y++)
{ {
memcpy(pixels + dstOffset, memcpy(pixels + dstOffset,
((unsigned int *)texsurf->pixels) + srcOffset, ((unsigned int *)texsurf->pixels) + srcOffset,
texsurf->w << 2); texsurf->w << 2);
dstOffset -= texsurf->w; dstOffset -= texsurf->w;
srcOffset += texsurf->w; srcOffset += texsurf->w;
} }
glGenTextures(1, &texture); glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 4, texsurf->w, texsurf->h, 0, glTexImage2D(GL_TEXTURE_2D, 0, 4, texsurf->w, texsurf->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, pixels); GL_RGBA, GL_UNSIGNED_BYTE, pixels);
if (quality > 0) if (quality > 0)
{ {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
} }
else else
{ {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); 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) if (mipmaps)
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA,
texsurf->w, texsurf->h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); texsurf->w, texsurf->h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
SDL_FreeSurface(texsurf); SDL_FreeSurface(texsurf);
delete[] pixels; delete[] pixels;
return texture; return texture;
} }

View File

@ -2,21 +2,25 @@
#ifndef TEXTURECACHE_H #ifndef TEXTURECACHE_H
#define TEXTURECACHE_H TEXTURECACHE_H #define TEXTURECACHE_H TEXTURECACHE_H
#include <SDL.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <map> #include <map>
#include <string> #include <string>
#include "TextureLoader/TextureLoader.h"
#include "FileLoader/FileLoader.h"
class TextureCache class TextureCache : public TextureLoader
{ {
public: 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 */ /* methods */
GLuint loadTexture(const char * filename, GLuint loadTexture(const FileLoader::Path & path,
bool mipmaps = false, FileLoader & fileLoader, bool mipmaps,
int mode = GL_DECAL, int mode, int quality);
int quality = 1);
/* data */ /* data */
std::map< std::string, GLuint > m_cache; std::map< std::string, GLuint > m_cache;