#include "texture.h" #include "config.h" #include #include #include #include #include #include #include #include KueTexture::KueTexture(const TQString &filename) { _filename = filename; _texture_id = 0; if (filename.isNull()) { // filename == TQString::null is an alias for the null texture _loaded = true; } else { _loaded = false; } } KueTexture::KueTexture(unsigned int texture_id) { _filename = TQString::null; _texture_id = texture_id; _loaded = true; } KueTexture::KueTexture(const KueTexture &t) { // Is the texture file backed? if (t._filename.isNull()) { // This is easy, copy over the texture id _texture_id = t._texture_id; _loaded = true; } else { // Yes, copy over the filename _filename = t._filename; _loaded = false; } } KueTexture KueTexture::null() { return KueTexture(0); } KueTexture::~KueTexture() { // We only "own" the texture ID if we were created from a filename // Also check that we've allocated a valid texture ID. That means // that the texture is loaded, and it's non-NULL. // We don't use isNull(), because that forces a file load if (_loaded && _texture_id && (!_filename.isNull())) { // Free a texture ID and its associated texture glDeleteTextures(1, &_texture_id); } } bool KueTexture::isNull() { load(); return (_texture_id == 0); } void KueTexture::load() { if (_loaded) { // The texture is already loaded, nothing to do here return; } // Get the full pathname for the texture TQImage raw_image, gl_image; TQString fullname; // Find the real filename fullname = TDEGlobal::dirs()->findResource("appdata", "textures/" + _filename + ".png"); // Try to load the file if (raw_image.load(fullname)) { gl_image = TQGLWidget::convertToGLFormat(raw_image); // Ask OpenGL for a new texture ID glGenTextures(1, &_texture_id); // Make it the current texture (blank right now) glBindTexture(GL_TEXTURE_2D, _texture_id); // Should we filter textures? TDEGlobal::config()->setGroup("Graphics"); if (TDEGlobal::config()->readBoolEntry("Filter Textures", false)) { // Yes, enable smooth scaling glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } else { // No, enable fast scaling glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); } // Load the image data in to the texture glTexImage2D(GL_TEXTURE_2D, 0, 3, gl_image.width(), gl_image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, gl_image.bits()); } else { // Unable to load image, use null texture _texture_id = 0; } _loaded = true; } bool KueTexture::makeCurrent() { load(); // Sets the current 2D texture, where 0 means no texture glBindTexture(GL_TEXTURE_2D, _texture_id); return true; }