remove GLU/TextureLoader/FileLoader dependance
no longer generating mipmaps
This commit is contained in:
parent
93490cb3d1
commit
4349f3f82c
@ -1,72 +1,26 @@
|
|||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_image.h>
|
#include <SDL_image.h>
|
||||||
#include <GL/gl.h>
|
|
||||||
#include <GL/glu.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "TextureCache.h"
|
#include "TextureCache.h"
|
||||||
#include "TextureLoader/TextureLoader.h"
|
|
||||||
#include "FileLoader/FileLoader.h"
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
//#define DEBUG_GL_ERROR
|
GLuint TextureCache::load(const char *fname)
|
||||||
#ifdef DEBUG_GL_ERROR
|
|
||||||
#define checkGLError() checkGLErrorLine(__FUNCTION__, __LINE__)
|
|
||||||
static void checkGLErrorLine(const char * function, int line)
|
|
||||||
{
|
{
|
||||||
GLenum err = glGetError();
|
string filename = fname;
|
||||||
if (err != 0)
|
|
||||||
{
|
|
||||||
cerr << "gl error in " << function
|
|
||||||
<< ": " << err << " (0x" << hex << err << ") at line "
|
|
||||||
<< dec << line << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
#define checkGLError()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
TextureCache::~TextureCache()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
GLuint TextureCache::load(const FileLoader::Path & path,
|
|
||||||
FileLoader & fileLoader, bool mipmaps,
|
|
||||||
int mode, int quality)
|
|
||||||
{
|
|
||||||
string filename = path.shortPath;
|
|
||||||
if (filename == "")
|
|
||||||
filename = path.fullPath;
|
|
||||||
if (filename == "")
|
|
||||||
return 0;
|
|
||||||
map<string,GLuint>::iterator it = m_cache.find(filename);
|
map<string,GLuint>::iterator it = m_cache.find(filename);
|
||||||
if (it != m_cache.end())
|
if (it != m_cache.end())
|
||||||
{
|
{
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
GLuint tex = loadTexture(path, fileLoader, mipmaps, mode, quality);
|
|
||||||
if (tex > 0)
|
|
||||||
{
|
|
||||||
m_cache[filename] = tex;
|
|
||||||
}
|
|
||||||
return tex;
|
|
||||||
}
|
|
||||||
|
|
||||||
GLuint TextureCache::loadTexture(const FileLoader::Path & path,
|
|
||||||
FileLoader & fileLoader, bool mipmaps,
|
|
||||||
int mode, int quality)
|
|
||||||
{
|
|
||||||
checkGLError();
|
|
||||||
GLuint texture;
|
GLuint texture;
|
||||||
FileLoader::Buffer buff = fileLoader.load(path);
|
SDL_Surface * temp = IMG_Load(filename);
|
||||||
if (buff.size <= 0)
|
|
||||||
return 0;
|
|
||||||
SDL_RWops * ops = SDL_RWFromMem(buff.data, buff.size);
|
|
||||||
SDL_Surface * temp = IMG_Load_RW(ops, true);
|
|
||||||
if (!temp)
|
if (!temp)
|
||||||
{
|
{
|
||||||
cerr << "Failed to load image ('" << path.fullPath << "', '"
|
fprintf(stderr, "Failed to load image '%s'!\n", filename);
|
||||||
<< path.shortPath << "'): " << IMG_GetError() << endl;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,10 +41,11 @@ GLuint TextureCache::loadTexture(const FileLoader::Path & path,
|
|||||||
SDL_FreeSurface(temp);
|
SDL_FreeSurface(temp);
|
||||||
if (!texsurf)
|
if (!texsurf)
|
||||||
{
|
{
|
||||||
cerr << "Image was not converted properly!" << endl;
|
fprintf(stderr, "'%s' was not converted properly!\n", filename);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
unsigned int * pixels = new unsigned int[texsurf->w * texsurf->h];
|
unsigned int * pixels =
|
||||||
|
malloc(sizeof(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;
|
||||||
@ -103,41 +58,13 @@ GLuint TextureCache::loadTexture(const FileLoader::Path & path,
|
|||||||
srcOffset += texsurf->w;
|
srcOffset += texsurf->w;
|
||||||
}
|
}
|
||||||
glGenTextures(1, &texture);
|
glGenTextures(1, &texture);
|
||||||
checkGLError();
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
checkGLError();
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 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)
|
|
||||||
{
|
|
||||||
checkGLError();
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
||||||
mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
|
|
||||||
checkGLError();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
checkGLError();
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
||||||
mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST);
|
|
||||||
checkGLError();
|
|
||||||
}
|
|
||||||
|
|
||||||
checkGLError();
|
|
||||||
|
|
||||||
if (mipmaps)
|
|
||||||
{
|
|
||||||
checkGLError();
|
|
||||||
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA,
|
|
||||||
texsurf->w, texsurf->h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
|
||||||
checkGLError();
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_FreeSurface(texsurf);
|
SDL_FreeSurface(texsurf);
|
||||||
delete[] pixels;
|
free(pixels);
|
||||||
checkGLError();
|
|
||||||
|
m_cache[filename] = texture;
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
@ -3,26 +3,21 @@
|
|||||||
#define TEXTURECACHE_H TEXTURECACHE_H
|
#define TEXTURECACHE_H TEXTURECACHE_H
|
||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
#ifdef GL_INCLUDE_FILE
|
||||||
|
#include GL_INCLUDE_FILE
|
||||||
|
#else
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "TextureLoader/TextureLoader.h"
|
|
||||||
#include "FileLoader/FileLoader.h"
|
|
||||||
|
|
||||||
class TextureCache : public TextureLoader
|
class TextureCache
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~TextureCache();
|
GLuint load(const char *fname);
|
||||||
virtual GLuint load(const FileLoader::Path & path,
|
|
||||||
FileLoader & fileLoader, bool mipmaps = true,
|
|
||||||
int mode = GL_DECAL, int quality = 1);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/* methods */
|
|
||||||
GLuint loadTexture(const FileLoader::Path & path,
|
|
||||||
FileLoader & fileLoader, bool mipmaps,
|
|
||||||
int mode, int quality);
|
|
||||||
|
|
||||||
/* data */
|
/* data */
|
||||||
std::map< std::string, GLuint > m_cache;
|
std::map< std::string, GLuint > m_cache;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user