[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 | #include "graphics_engine.h" |
---|
| 22 | |
---|
[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 | |
---|
[3905] | 60 | |
---|
[3863] | 61 | /** |
---|
[4836] | 62 | * loads an Image from a file to a Texture |
---|
| 63 | * @param imageName The image to load |
---|
[3863] | 64 | */ |
---|
[3655] | 65 | bool Texture::loadImage(const char* imageName) |
---|
[3341] | 66 | { |
---|
[3622] | 67 | if (GraphicsEngine::texturesEnabled) |
---|
[3341] | 68 | { |
---|
[5754] | 69 | if (this->image != NULL) |
---|
[5768] | 70 | { |
---|
| 71 | SDL_FreeSurface(this->image); |
---|
| 72 | this->image = NULL; |
---|
| 73 | } |
---|
[5754] | 74 | if (this->texture != 0) |
---|
[5768] | 75 | { |
---|
| 76 | glDeleteTextures(1, &this->texture); |
---|
| 77 | this->texture = 0; |
---|
| 78 | } |
---|
[5305] | 79 | if (imageName != NULL) |
---|
[4662] | 80 | { |
---|
| 81 | SDL_Surface* tmpSurf; |
---|
[5856] | 82 | if (this->texture != 0 && glIsTexture(this->texture)) |
---|
[4662] | 83 | glDeleteTextures(1, &this->texture); |
---|
| 84 | // load the new Image to memory |
---|
| 85 | tmpSurf = IMG_Load(imageName); |
---|
[5753] | 86 | if(tmpSurf != NULL) |
---|
| 87 | { |
---|
[5754] | 88 | PRINTF(4)("loading Image %s\n", imageName); |
---|
[5768] | 89 | if (this->prepareSurface(tmpSurf)) |
---|
[5856] | 90 | this->texture = Texture::loadTexToGL(this->image); |
---|
| 91 | |
---|
[5753] | 92 | SDL_FreeSurface(tmpSurf); |
---|
| 93 | return true; |
---|
| 94 | } |
---|
| 95 | else |
---|
[4662] | 96 | { |
---|
| 97 | PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); |
---|
[5305] | 98 | this->texture = 0; |
---|
[4662] | 99 | return false; |
---|
| 100 | } |
---|
| 101 | } |
---|
[3622] | 102 | else |
---|
[4662] | 103 | { |
---|
[5768] | 104 | PRINTF(2)("Image-Name not specified\n"); |
---|
[4662] | 105 | return false; |
---|
| 106 | } |
---|
[3341] | 107 | } |
---|
[5754] | 108 | return false; |
---|
[3341] | 109 | } |
---|
[5753] | 110 | |
---|
[5755] | 111 | bool Texture::rebuild() |
---|
[5754] | 112 | { |
---|
[5856] | 113 | if (this->texture != 0 && glIsTexture(this->texture)) |
---|
[5754] | 114 | { |
---|
| 115 | glDeleteTextures(1,&this->texture); |
---|
| 116 | this->texture = 0; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | if (this->image != NULL) |
---|
[5755] | 120 | { |
---|
[5790] | 121 | PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName()); |
---|
[5856] | 122 | this->texture = loadTexToGL(this->image); |
---|
[5755] | 123 | } |
---|
[5768] | 124 | |
---|
[5754] | 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * converts surface to a new SDL_Surface, that is loadable by openGL |
---|
| 129 | * @param surface the Surface to convert |
---|
| 130 | * @returns a !!new!! Surface, that is loadable by openGL. |
---|
| 131 | */ |
---|
[5768] | 132 | bool Texture::prepareSurface(SDL_Surface* surface) |
---|
[5753] | 133 | { |
---|
| 134 | PRINTF(4)("Loading texture to OpenGL-Environment.\n"); |
---|
[5768] | 135 | |
---|
| 136 | SDL_Surface* putSurface; |
---|
[5753] | 137 | SDL_Rect area; |
---|
| 138 | Uint32 saved_flags; |
---|
| 139 | Uint8 saved_alpha; |
---|
[5768] | 140 | |
---|
| 141 | putSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
| 142 | surface->w, surface->h, |
---|
| 143 | 32, |
---|
[5753] | 144 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
[5768] | 145 | 0x000000FF, |
---|
| 146 | 0x0000FF00, |
---|
| 147 | 0x00FF0000, |
---|
| 148 | 0xFF000000 |
---|
[5753] | 149 | #else |
---|
[5768] | 150 | 0xFF000000, |
---|
| 151 | 0x00FF0000, |
---|
| 152 | 0x0000FF00, |
---|
| 153 | 0x000000FF |
---|
[5753] | 154 | #endif |
---|
[5768] | 155 | ); |
---|
| 156 | if ( putSurface == NULL ) |
---|
| 157 | { |
---|
| 158 | this->setSurface(NULL); |
---|
| 159 | return false; |
---|
| 160 | } |
---|
| 161 | |
---|
[5753] | 162 | /* Save the alpha blending attributes */ |
---|
| 163 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
---|
| 164 | saved_alpha = surface->format->alpha; |
---|
| 165 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
| 166 | SDL_SetAlpha(surface, 0, 0); |
---|
| 167 | } |
---|
[5768] | 168 | |
---|
[5753] | 169 | /* Copy the surface into the GL texture image */ |
---|
| 170 | area.x = 0; |
---|
| 171 | area.y = 0; |
---|
| 172 | area.w = surface->w; |
---|
| 173 | area.h = surface->h; |
---|
[5768] | 174 | SDL_BlitSurface(surface, &area, putSurface, &area); |
---|
| 175 | |
---|
[5753] | 176 | /* Restore the alpha blending attributes */ |
---|
[5768] | 177 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) |
---|
| 178 | { |
---|
| 179 | SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); |
---|
| 180 | this->bAlpha = true; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | return (this->setSurface(putSurface)); |
---|
[5753] | 184 | } |
---|
| 185 | |
---|
[5768] | 186 | bool Texture::setSurface(SDL_Surface* newSurface) |
---|
| 187 | { |
---|
| 188 | if (this->image != NULL) |
---|
| 189 | SDL_FreeSurface(this->image); |
---|
[5753] | 190 | |
---|
[5768] | 191 | this->image = newSurface; |
---|
| 192 | |
---|
| 193 | return (this->image != NULL); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | |
---|
[5753] | 197 | /** |
---|
| 198 | * Loads a Texture to the openGL-environment. |
---|
| 199 | * @param surface the Image to load to openGL |
---|
| 200 | * @returns The ID of the texture. |
---|
| 201 | */ |
---|
[5856] | 202 | GLuint Texture::loadTexToGL (const SDL_Surface* surface) |
---|
[5753] | 203 | { |
---|
[5856] | 204 | // if (this->texture != 0 && glIsTexture(this->texture)) |
---|
| 205 | // glDeleteTextures(1, &this->texture); |
---|
| 206 | // this->texture = 0; |
---|
[5768] | 207 | |
---|
[5856] | 208 | GLuint texture; |
---|
| 209 | |
---|
| 210 | if (surface == NULL) |
---|
[5768] | 211 | return 0; |
---|
| 212 | |
---|
[5753] | 213 | /* Create an OpenGL texture for the image */ |
---|
[5856] | 214 | glGenTextures(1, &texture); |
---|
| 215 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
[5753] | 216 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
| 217 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
| 218 | // build the Texture |
---|
| 219 | glTexImage2D(GL_TEXTURE_2D, |
---|
[5768] | 220 | 0, |
---|
| 221 | GL_RGBA, |
---|
[5856] | 222 | surface->w, surface->h, |
---|
[5768] | 223 | 0, |
---|
| 224 | GL_RGBA, |
---|
| 225 | GL_UNSIGNED_BYTE, |
---|
[5856] | 226 | surface->pixels); |
---|
[5753] | 227 | // build the MipMaps |
---|
| 228 | gluBuild2DMipmaps(GL_TEXTURE_2D, |
---|
[5768] | 229 | GL_RGBA, |
---|
[5856] | 230 | surface->w, |
---|
| 231 | surface->h, |
---|
[5768] | 232 | GL_RGBA, |
---|
| 233 | GL_UNSIGNED_BYTE, |
---|
[5856] | 234 | surface->pixels); |
---|
[5753] | 235 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
[5856] | 236 | return texture; |
---|
[5753] | 237 | } |
---|