173 lines
3.3 KiB
C++
173 lines
3.3 KiB
C++
#include "gltexture.h"
|
||
|
||
GLTexture::GLTexture() {
|
||
init();
|
||
alltextures.push_back(this);
|
||
}
|
||
|
||
GLTexture::GLTexture(std::string fname, GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
|
||
init();
|
||
setParameter(_minfilter, _magfilter, _wraps, _wrapt);
|
||
loadImage(fname);
|
||
|
||
alltextures.push_back(this);
|
||
}
|
||
|
||
GLTexture::GLTexture(SDL_Surface *srfc) {
|
||
init();
|
||
loadSurface(srfc);
|
||
}
|
||
|
||
void GLTexture::init() {
|
||
loaded = false;
|
||
texconverted = false;
|
||
keepsurface = false;
|
||
donotreload = false;
|
||
|
||
minfilter = GL_LINEAR;
|
||
magfilter = GL_LINEAR;
|
||
|
||
wraps = GL_REPEAT;
|
||
wrapt = GL_REPEAT;
|
||
|
||
tex = 0;
|
||
width = height = 0;
|
||
}
|
||
|
||
bool GLTexture::loadLocalSurface() {
|
||
if(!texconverted)
|
||
convertLocalSurface();
|
||
|
||
width = tex->w;
|
||
height = tex->h;
|
||
|
||
glGenTextures(1, &texint);
|
||
glBindTexture(GL_TEXTURE_2D, texint);
|
||
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wraps);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapt);
|
||
|
||
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_BGRA, GL_UNSIGNED_BYTE, tex->pixels);
|
||
|
||
loaded = true;
|
||
|
||
return true;
|
||
}
|
||
|
||
void GLTexture::convertLocalSurface() {
|
||
tex = SDL_DisplayFormatAlpha(tex);
|
||
mirrorSurfaceMiddleX(tex);
|
||
|
||
texconverted = true;
|
||
}
|
||
|
||
bool GLTexture::loadImage(std::string fname) {
|
||
if(isLoaded())
|
||
unloadTexture();
|
||
filename = fname;
|
||
tex = IMG_Load(filename.c_str());
|
||
|
||
if(!tex) {
|
||
std::cerr << "Textur konnte nicht geladen werden: " << filename << std::endl;
|
||
return false;
|
||
}
|
||
|
||
bool ret = loadLocalSurface();
|
||
keepsurface = false;
|
||
SDL_FreeSurface(tex);
|
||
tex = 0;
|
||
|
||
return ret;
|
||
}
|
||
|
||
bool GLTexture::loadSurface(SDL_Surface *srfc, bool noreloading) {
|
||
if(isLoaded())
|
||
unloadTexture();
|
||
if(filename!="")
|
||
filename = "";
|
||
|
||
tex = srfc;
|
||
|
||
keepsurface = true;
|
||
donotreload = noreloading;
|
||
|
||
return loadLocalSurface();
|
||
}
|
||
|
||
bool GLTexture::selectTexture() {
|
||
if(!loaded) {
|
||
std::cerr << "Textur wurde nicht geladen: " << filename << std::endl;
|
||
return false;
|
||
}
|
||
|
||
glBindTexture(GL_TEXTURE_2D, texint);
|
||
|
||
return true;
|
||
}
|
||
|
||
void GLTexture::unloadTexture() {
|
||
glDeleteTextures(1, &texint);
|
||
|
||
loaded = false;
|
||
// Entweder tex == image, wurde schon gel<65>scht oder wir m<>ssen es nich l<>schen
|
||
|
||
}
|
||
|
||
bool GLTexture::setParameter(GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
|
||
if(_minfilter!=0)
|
||
minfilter = _minfilter;
|
||
if(_magfilter!=0)
|
||
magfilter = _magfilter;
|
||
if(_wraps!=0)
|
||
wraps = _wraps;
|
||
if(_wrapt!=0)
|
||
wrapt = _wrapt;
|
||
|
||
if(!loaded)
|
||
return true;
|
||
|
||
if(keepsurface&&tex!=0) {
|
||
unloadTexture();
|
||
return loadLocalSurface();
|
||
} else if(filename!="") {
|
||
return loadImage("filename");
|
||
} else {
|
||
// keine datei, kein surface
|
||
unloadTexture();
|
||
std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl;
|
||
return false;
|
||
}
|
||
}
|
||
|
||
int GLTexture::getW() {
|
||
return width;
|
||
}
|
||
|
||
int GLTexture::getH() {
|
||
return height;
|
||
}
|
||
|
||
bool GLTexture::isLoaded() {
|
||
return loaded;
|
||
}
|
||
|
||
void GLTexture::reloadAll() {
|
||
for(unsigned int i=0; i<alltextures.size(); i++)
|
||
alltextures[i]->setParameter();
|
||
}
|
||
|
||
GLTexture::~GLTexture() {
|
||
if(isLoaded())
|
||
unloadTexture();
|
||
|
||
for(unsigned int i=0; i<alltextures.size(); i++) {
|
||
if(alltextures[i]==this) {
|
||
alltextures.erase(alltextures.begin()+i);
|
||
}
|
||
}
|
||
}
|
||
|
||
std::vector<GLTexture*> GLTexture::alltextures;
|