/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER #include "texture.h" #include "debug.h" // INCLUDING SDL_Image #ifdef HAVE_SDL_IMAGE_H #include #else #include #endif /** * Constructor for a Texture */ Texture::Texture(const char* imageName) { this->setClassID(CL_TEXTURE, "Texture"); this->bAlpha = false; this->texture = 0; this->image = NULL; if (imageName != NULL) { this->setName(imageName); this->loadImage(imageName); } } /** * Destructor of a Texture Frees Data, and deletes the textures from GL */ Texture::~Texture() { if (this->texture != 0) glDeleteTextures(1, &this->texture); if (this->image != NULL) SDL_FreeSurface(this->image); } /** * loads an Image from a file to a Texture * @param imageName The image to load */ bool Texture::loadImage(const char* imageName) { if (Texture::texturesEnabled) { if (this->image != NULL) { SDL_FreeSurface(this->image); this->image = NULL; } if (this->texture != 0) { glDeleteTextures(1, &this->texture); this->texture = 0; } if (imageName != NULL) { SDL_Surface* tmpSurf; if (this->texture != 0 && glIsTexture(this->texture)) glDeleteTextures(1, &this->texture); // load the new Image to memory tmpSurf = IMG_Load(imageName); if(tmpSurf != NULL) { PRINTF(4)("loading Image %s\n", imageName); bool hasAlpha; SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha); if (newSurf != NULL) { this->setSurface(newSurf); this->setAlpha(hasAlpha); this->setTexture(Texture::loadTexToGL(newSurf)); } SDL_FreeSurface(tmpSurf); return true; } else { PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); this->texture = 0; return false; } } else { PRINTF(2)("Image-Name not specified\n"); return false; } } return false; } /** * rebuilds the texture. * reloads the Texture from Memory to OpenGL. */ bool Texture::rebuild() { if (this->texture != 0) { if (glIsTexture(this->texture)) glDeleteTextures(1,&this->texture); this->setTexture(0); } if (this->image != NULL) { PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName()); this->setTexture(loadTexToGL(this->image)); } } /** * set the surface this Texture handles * @param newSurface the new Surface to set as the image for this Texture. * * This deletes the old version of the stored Texture, * and sets the newly given Surface as current. */ bool Texture::setSurface(SDL_Surface* newSurface) { if (this->image != NULL) SDL_FreeSurface(this->image); this->image = newSurface; return (this->image != NULL); } bool Texture::texturesEnabled = true; /** * enables, disables textures * @param texturesEnabled true if the textures should be enabled */ void Texture::setTextureEnableState(bool texturesEnabled) { Texture::texturesEnabled = texturesEnabled; } ////////////////////////////////////// // UTILITY FUNCTIONALITY OF TEXTURE // ////////////////////////////////////// /** * converts surface to a new SDL_Surface, that is loadable by openGL * @param surface the Surface to convert * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false. * @returns a !!new!! Surface, that is loadable by openGL. */ SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) { PRINTF(4)("Loading texture to OpenGL-Environment.\n"); SDL_Surface* retSurface; SDL_Rect area; Uint32 saved_flags; Uint8 saved_alpha; hasAlpha = false; retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, 32, #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); if ( retSurface == NULL ) { return NULL; } /* Save the alpha blending attributes */ saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); saved_alpha = surface->format->alpha; if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { SDL_SetAlpha(surface, 0, 0); } /* Copy the surface into the GL texture image */ area.x = 0; area.y = 0; area.w = surface->w; area.h = surface->h; SDL_BlitSurface(surface, &area, retSurface, &area); /* Restore the alpha blending attributes */ if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); hasAlpha = true; } return (retSurface); } /** * Loads a Texture to the openGL-environment. * @param surface the Image to load to openGL * @returns The ID of the texture. */ GLuint Texture::loadTexToGL (const SDL_Surface* surface) { // if (this->texture != 0 && glIsTexture(this->texture)) // glDeleteTextures(1, &this->texture); // this->texture = 0; GLuint texture; if (surface == NULL) return 0; /* Create an OpenGL texture for the image */ glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // build the Texture glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); // build the MipMaps gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, surface->w, surface->h, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); glBindTexture(GL_TEXTURE_2D, 0); return texture; }