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