Changeset 5858 in orxonox.OLD for trunk/src/lib/graphics/importer/multi_frame_texture.cc
- Timestamp:
- Dec 1, 2005, 8:03:35 PM (18 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/importer/multi_frame_texture.cc
r5856 r5858 16 16 #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER 17 17 18 #include " texture.h"18 #include "multi_frame_texture.h" 19 19 20 20 #include "debug.h" 21 21 #include "graphics_engine.h" 22 23 #include <stdarg.h> 22 24 23 25 #ifdef HAVE_SDL_IMAGE_H … … 30 32 * Constructor for a Texture 31 33 */ 32 Texture::Texture(const char* imageName)34 MultiFrameTexture::MultiFrameTexture(unsigned int count, ...) 33 35 { 34 36 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 * Destructor of a Texture49 50 Frees Data, and deletes the textures from GL51 */52 Texture::~Texture()53 {54 if (this->texture != 0)55 glDeleteTextures(1, &this->texture);56 if (this->image != NULL)57 SDL_FreeSurface(this->image);58 }59 60 61 /**62 * loads an Image from a file to a Texture63 * @param imageName The image to load64 */65 bool Texture::loadImage(const char* imageName)66 {67 if (GraphicsEngine::texturesEnabled)68 {69 if (this->image != NULL)70 {71 SDL_FreeSurface(this->image);72 this->image = NULL;73 }74 if (this->texture != 0)75 {76 glDeleteTextures(1, &this->texture);77 this->texture = 0;78 }79 if (imageName != NULL)80 {81 SDL_Surface* tmpSurf;82 if (this->texture != 0 && glIsTexture(this->texture))83 glDeleteTextures(1, &this->texture);84 // load the new Image to memory85 tmpSurf = IMG_Load(imageName);86 if(tmpSurf != NULL)87 {88 PRINTF(4)("loading Image %s\n", imageName);89 if (this->prepareSurface(tmpSurf))90 this->texture = Texture::loadTexToGL(this->image);91 92 SDL_FreeSurface(tmpSurf);93 return true;94 }95 else96 {97 PRINTF(1)("IMG_Load: %s\n", IMG_GetError());98 this->texture = 0;99 return false;100 }101 }102 else103 {104 PRINTF(2)("Image-Name not specified\n");105 return false;106 }107 }108 return false;109 }110 111 bool Texture::rebuild()112 {113 if (this->texture != 0 && glIsTexture(this->texture))114 {115 glDeleteTextures(1,&this->texture);116 this->texture = 0;117 }118 119 if (this->image != NULL)120 {121 PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());122 this->texture = loadTexToGL(this->image);123 }124 37 125 38 } 126 39 127 40 /** 128 * converts surface to a new SDL_Surface, that is loadable by openGL129 * @param surface the Surface to convert 130 * @returns a !!new!! Surface, that is loadable by openGL.131 132 bool Texture::prepareSurface(SDL_Surface* surface)41 * Destructor of a MultiFrameTexture 42 43 Frees Data, and deletes the textures from GL 44 */ 45 MultiFrameTexture::~MultiFrameTexture() 133 46 { 134 PRINTF(4)("Loading texture to OpenGL-Environment.\n");135 136 SDL_Surface* putSurface;137 SDL_Rect area;138 Uint32 saved_flags;139 Uint8 saved_alpha;140 141 putSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,142 surface->w, surface->h,143 32,144 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */145 0x000000FF,146 0x0000FF00,147 0x00FF0000,148 0xFF000000149 #else150 0xFF000000,151 0x00FF0000,152 0x0000FF00,153 0x000000FF154 #endif155 );156 if ( putSurface == NULL )157 {158 this->setSurface(NULL);159 return false;160 }161 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 }168 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;174 SDL_BlitSurface(surface, &area, putSurface, &area);175 176 /* Restore the alpha blending attributes */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));184 }185 186 bool Texture::setSurface(SDL_Surface* newSurface)187 {188 if (this->image != NULL)189 SDL_FreeSurface(this->image);190 191 this->image = newSurface;192 193 return (this->image != NULL);194 47 } 195 48 196 49 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 */ 202 GLuint Texture::loadTexToGL (const SDL_Surface* surface) 50 51 bool MultiFrameTexture::rebuild() 203 52 { 204 // if (this->texture != 0 && glIsTexture(this->texture)) 205 // glDeleteTextures(1, &this->texture); 206 // this->texture = 0; 53 // if (this->texture != 0 && glIsMultiFrameTexture(this->texture)) 54 // { 55 // glDeleteMultiFrameTextures(1,&this->texture); 56 // this->texture = 0; 57 // } 58 // 59 // if (this->image != NULL) 60 // { 61 // PRINTF(3)("Reloading MultiFrameTexture of %s '%s'\n", this->getClassName(), this->getName()); 62 // this->texture = loadTexToGL(this->image); 63 // } 207 64 208 GLuint texture;209 210 if (surface == NULL)211 return 0;212 213 /* Create an OpenGL texture for the image */214 glGenTextures(1, &texture);215 glBindTexture(GL_TEXTURE_2D, texture);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 Texture219 glTexImage2D(GL_TEXTURE_2D,220 0,221 GL_RGBA,222 surface->w, surface->h,223 0,224 GL_RGBA,225 GL_UNSIGNED_BYTE,226 surface->pixels);227 // build the MipMaps228 gluBuild2DMipmaps(GL_TEXTURE_2D,229 GL_RGBA,230 surface->w,231 surface->h,232 GL_RGBA,233 GL_UNSIGNED_BYTE,234 surface->pixels);235 glBindTexture(GL_TEXTURE_2D, 0);236 return texture;237 65 }
Note: See TracChangeset
for help on using the changeset viewer.