[4662] | 1 | /* |
---|
[3341] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Grauer |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[3590] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
| 17 | |
---|
[3341] | 18 | #include "texture.h" |
---|
| 19 | |
---|
[4357] | 20 | #include "debug.h" |
---|
[3622] | 21 | |
---|
[5858] | 22 | // INCLUDING SDL_Image |
---|
[4662] | 23 | #ifdef HAVE_SDL_IMAGE_H |
---|
[4357] | 24 | #include <SDL_image.h> |
---|
[4662] | 25 | #else |
---|
| 26 | #include <SDL/SDL_image.h> |
---|
| 27 | #endif |
---|
[4357] | 28 | |
---|
[3341] | 29 | /** |
---|
[4836] | 30 | * Constructor for a Texture |
---|
[3344] | 31 | */ |
---|
[3655] | 32 | Texture::Texture(const char* imageName) |
---|
| 33 | { |
---|
[5304] | 34 | this->setClassID(CL_TEXTURE, "Texture"); |
---|
| 35 | |
---|
[5212] | 36 | this->bAlpha = false; |
---|
[3655] | 37 | this->texture = 0; |
---|
[5753] | 38 | this->image = NULL; |
---|
[5754] | 39 | |
---|
[5305] | 40 | if (imageName != NULL) |
---|
[5769] | 41 | { |
---|
| 42 | this->setName(imageName); |
---|
[3905] | 43 | this->loadImage(imageName); |
---|
[5769] | 44 | } |
---|
[4662] | 45 | } |
---|
[3655] | 46 | |
---|
| 47 | /** |
---|
[4836] | 48 | * Destructor of a Texture |
---|
[4662] | 49 | |
---|
[3344] | 50 | Frees Data, and deletes the textures from GL |
---|
| 51 | */ |
---|
[4746] | 52 | Texture::~Texture() |
---|
[3344] | 53 | { |
---|
[5211] | 54 | if (this->texture != 0) |
---|
[3344] | 55 | glDeleteTextures(1, &this->texture); |
---|
[5753] | 56 | if (this->image != NULL) |
---|
| 57 | SDL_FreeSurface(this->image); |
---|
[3344] | 58 | } |
---|
| 59 | |
---|
[3863] | 60 | /** |
---|
[4836] | 61 | * loads an Image from a file to a Texture |
---|
| 62 | * @param imageName The image to load |
---|
[3863] | 63 | */ |
---|
[3655] | 64 | bool Texture::loadImage(const char* imageName) |
---|
[3341] | 65 | { |
---|
[5858] | 66 | if (Texture::texturesEnabled) |
---|
[3341] | 67 | { |
---|
[5754] | 68 | if (this->image != NULL) |
---|
[5768] | 69 | { |
---|
| 70 | SDL_FreeSurface(this->image); |
---|
| 71 | this->image = NULL; |
---|
| 72 | } |
---|
[5754] | 73 | if (this->texture != 0) |
---|
[5768] | 74 | { |
---|
| 75 | glDeleteTextures(1, &this->texture); |
---|
| 76 | this->texture = 0; |
---|
| 77 | } |
---|
[5305] | 78 | if (imageName != NULL) |
---|
[4662] | 79 | { |
---|
| 80 | SDL_Surface* tmpSurf; |
---|
[5856] | 81 | if (this->texture != 0 && glIsTexture(this->texture)) |
---|
[4662] | 82 | glDeleteTextures(1, &this->texture); |
---|
| 83 | // load the new Image to memory |
---|
| 84 | tmpSurf = IMG_Load(imageName); |
---|
[5753] | 85 | if(tmpSurf != NULL) |
---|
| 86 | { |
---|
[5754] | 87 | PRINTF(4)("loading Image %s\n", imageName); |
---|
[5859] | 88 | bool hasAlpha; |
---|
| 89 | SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha); |
---|
| 90 | if (newSurf != NULL) |
---|
| 91 | { |
---|
| 92 | this->setSurface(newSurf); |
---|
| 93 | this->setAlpha(hasAlpha); |
---|
| 94 | this->setTexture(Texture::loadTexToGL(newSurf)); |
---|
| 95 | } |
---|
[5856] | 96 | |
---|
[5753] | 97 | SDL_FreeSurface(tmpSurf); |
---|
| 98 | return true; |
---|
| 99 | } |
---|
| 100 | else |
---|
[4662] | 101 | { |
---|
| 102 | PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); |
---|
[5305] | 103 | this->texture = 0; |
---|
[4662] | 104 | return false; |
---|
| 105 | } |
---|
| 106 | } |
---|
[3622] | 107 | else |
---|
[4662] | 108 | { |
---|
[5768] | 109 | PRINTF(2)("Image-Name not specified\n"); |
---|
[4662] | 110 | return false; |
---|
| 111 | } |
---|
[3341] | 112 | } |
---|
[5754] | 113 | return false; |
---|
[3341] | 114 | } |
---|
[5753] | 115 | |
---|
[5755] | 116 | bool Texture::rebuild() |
---|
[5754] | 117 | { |
---|
[5860] | 118 | if (this->texture != 0) |
---|
[5754] | 119 | { |
---|
[5860] | 120 | if (glIsTexture(this->texture)) |
---|
| 121 | glDeleteTextures(1,&this->texture); |
---|
[5859] | 122 | this->setTexture(0); |
---|
[5754] | 123 | } |
---|
| 124 | |
---|
| 125 | if (this->image != NULL) |
---|
[5755] | 126 | { |
---|
[5790] | 127 | PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName()); |
---|
[5859] | 128 | this->setTexture(loadTexToGL(this->image)); |
---|
[5755] | 129 | } |
---|
[5768] | 130 | |
---|
[5754] | 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * converts surface to a new SDL_Surface, that is loadable by openGL |
---|
| 135 | * @param surface the Surface to convert |
---|
[5859] | 136 | * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false. |
---|
[5754] | 137 | * @returns a !!new!! Surface, that is loadable by openGL. |
---|
| 138 | */ |
---|
[5859] | 139 | SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) |
---|
[5753] | 140 | { |
---|
| 141 | PRINTF(4)("Loading texture to OpenGL-Environment.\n"); |
---|
[5768] | 142 | |
---|
[5859] | 143 | SDL_Surface* retSurface; |
---|
[5753] | 144 | SDL_Rect area; |
---|
| 145 | Uint32 saved_flags; |
---|
| 146 | Uint8 saved_alpha; |
---|
[5768] | 147 | |
---|
[5859] | 148 | hasAlpha = false; |
---|
| 149 | retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
[5768] | 150 | surface->w, surface->h, |
---|
| 151 | 32, |
---|
[5753] | 152 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
[5768] | 153 | 0x000000FF, |
---|
| 154 | 0x0000FF00, |
---|
| 155 | 0x00FF0000, |
---|
| 156 | 0xFF000000 |
---|
[5753] | 157 | #else |
---|
[5768] | 158 | 0xFF000000, |
---|
| 159 | 0x00FF0000, |
---|
| 160 | 0x0000FF00, |
---|
| 161 | 0x000000FF |
---|
[5753] | 162 | #endif |
---|
[5768] | 163 | ); |
---|
[5859] | 164 | if ( retSurface == NULL ) |
---|
[5768] | 165 | { |
---|
[5859] | 166 | return NULL; |
---|
[5768] | 167 | } |
---|
| 168 | |
---|
[5753] | 169 | /* Save the alpha blending attributes */ |
---|
| 170 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
---|
| 171 | saved_alpha = surface->format->alpha; |
---|
| 172 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
| 173 | SDL_SetAlpha(surface, 0, 0); |
---|
| 174 | } |
---|
[5768] | 175 | |
---|
[5753] | 176 | /* Copy the surface into the GL texture image */ |
---|
| 177 | area.x = 0; |
---|
| 178 | area.y = 0; |
---|
| 179 | area.w = surface->w; |
---|
| 180 | area.h = surface->h; |
---|
[5859] | 181 | SDL_BlitSurface(surface, &area, retSurface, &area); |
---|
[5768] | 182 | |
---|
[5753] | 183 | /* Restore the alpha blending attributes */ |
---|
[5768] | 184 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) |
---|
| 185 | { |
---|
| 186 | SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); |
---|
[5859] | 187 | hasAlpha = true; |
---|
[5768] | 188 | } |
---|
| 189 | |
---|
[5859] | 190 | return (retSurface); |
---|
[5753] | 191 | } |
---|
| 192 | |
---|
[5859] | 193 | /** |
---|
| 194 | * set the surface this Texture handles |
---|
| 195 | * @param newSurface the new Surface to set as the image for this Texture. |
---|
| 196 | * |
---|
| 197 | * This deletes the old version of the stored Texture, |
---|
| 198 | * and sets the newly given Surface as current. |
---|
| 199 | */ |
---|
[5768] | 200 | bool Texture::setSurface(SDL_Surface* newSurface) |
---|
| 201 | { |
---|
| 202 | if (this->image != NULL) |
---|
| 203 | SDL_FreeSurface(this->image); |
---|
[5753] | 204 | |
---|
[5768] | 205 | this->image = newSurface; |
---|
| 206 | |
---|
| 207 | return (this->image != NULL); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
[5858] | 211 | bool Texture::texturesEnabled = true; |
---|
| 212 | |
---|
[5753] | 213 | /** |
---|
[5858] | 214 | * enables, disables textures |
---|
| 215 | * @param texturesEnabled true if the textures should be enabled |
---|
| 216 | */ |
---|
| 217 | void Texture::setTextureEnableState(bool texturesEnabled) |
---|
| 218 | { |
---|
| 219 | Texture::texturesEnabled = texturesEnabled; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | ////////////////////////////////////// |
---|
| 224 | // UTILITY FUNCTIONALITY OF TEXTURE // |
---|
| 225 | ////////////////////////////////////// |
---|
| 226 | /** |
---|
[5753] | 227 | * Loads a Texture to the openGL-environment. |
---|
| 228 | * @param surface the Image to load to openGL |
---|
| 229 | * @returns The ID of the texture. |
---|
| 230 | */ |
---|
[5856] | 231 | GLuint Texture::loadTexToGL (const SDL_Surface* surface) |
---|
[5753] | 232 | { |
---|
[5856] | 233 | // if (this->texture != 0 && glIsTexture(this->texture)) |
---|
| 234 | // glDeleteTextures(1, &this->texture); |
---|
| 235 | // this->texture = 0; |
---|
[5768] | 236 | |
---|
[5856] | 237 | GLuint texture; |
---|
| 238 | |
---|
| 239 | if (surface == NULL) |
---|
[5768] | 240 | return 0; |
---|
| 241 | |
---|
[5753] | 242 | /* Create an OpenGL texture for the image */ |
---|
[5856] | 243 | glGenTextures(1, &texture); |
---|
| 244 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
[5753] | 245 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
| 246 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
| 247 | // build the Texture |
---|
| 248 | glTexImage2D(GL_TEXTURE_2D, |
---|
[5768] | 249 | 0, |
---|
| 250 | GL_RGBA, |
---|
[5856] | 251 | surface->w, surface->h, |
---|
[5768] | 252 | 0, |
---|
| 253 | GL_RGBA, |
---|
| 254 | GL_UNSIGNED_BYTE, |
---|
[5856] | 255 | surface->pixels); |
---|
[5753] | 256 | // build the MipMaps |
---|
| 257 | gluBuild2DMipmaps(GL_TEXTURE_2D, |
---|
[5768] | 258 | GL_RGBA, |
---|
[5856] | 259 | surface->w, |
---|
| 260 | surface->h, |
---|
[5768] | 261 | GL_RGBA, |
---|
| 262 | GL_UNSIGNED_BYTE, |
---|
[5856] | 263 | surface->pixels); |
---|
[5753] | 264 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
[5856] | 265 | return texture; |
---|
[5753] | 266 | } |
---|