| 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 | #include "graphics_engine.h" | 
|---|
| 22 |  | 
|---|
| 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 |   this->setName(imageName); | 
|---|
| 36 |  | 
|---|
| 37 |   this->bAlpha = false; | 
|---|
| 38 |   this->texture = 0; | 
|---|
| 39 |   if (imageName != NULL) | 
|---|
| 40 |     this->loadImage(imageName); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | /** | 
|---|
| 44 |  *  Destructor of a Texture | 
|---|
| 45 |  | 
|---|
| 46 |    Frees Data, and deletes the textures from GL | 
|---|
| 47 | */ | 
|---|
| 48 | Texture::~Texture() | 
|---|
| 49 | { | 
|---|
| 50 |   if (this->texture != 0) | 
|---|
| 51 |     glDeleteTextures(1, &this->texture); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | /** | 
|---|
| 55 |  *  Loads a Texture to the openGL-environment. | 
|---|
| 56 |  * @param surface the Image to load to openGL | 
|---|
| 57 |  * @returns The ID of the texture. | 
|---|
| 58 | */ | 
|---|
| 59 | GLuint Texture::loadTexToGL (SDL_Surface* surface) | 
|---|
| 60 | { | 
|---|
| 61 |   if (GraphicsEngine::texturesEnabled) | 
|---|
| 62 |     { | 
|---|
| 63 |       PRINTF(4)("Loading texture to OpenGL-Environment.\n"); | 
|---|
| 64 |  | 
|---|
| 65 |       int w, h; | 
|---|
| 66 |       SDL_Surface *image; | 
|---|
| 67 |       SDL_Rect area; | 
|---|
| 68 |       Uint32 saved_flags; | 
|---|
| 69 |       Uint8  saved_alpha; | 
|---|
| 70 |  | 
|---|
| 71 |       w = surface->w; | 
|---|
| 72 |       h = surface->h; | 
|---|
| 73 |  | 
|---|
| 74 |       image = SDL_CreateRGBSurface(SDL_SWSURFACE, | 
|---|
| 75 |                                    w, h, | 
|---|
| 76 |                                    32, | 
|---|
| 77 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ | 
|---|
| 78 |                                    0x000000FF, | 
|---|
| 79 |                                    0x0000FF00, | 
|---|
| 80 |                                    0x00FF0000, | 
|---|
| 81 |                                    0xFF000000 | 
|---|
| 82 | #else | 
|---|
| 83 |                                    0xFF000000, | 
|---|
| 84 |                                    0x00FF0000, | 
|---|
| 85 |                                    0x0000FF00, | 
|---|
| 86 |                                    0x000000FF | 
|---|
| 87 | #endif | 
|---|
| 88 |                                    ); | 
|---|
| 89 |       if ( image == NULL ) { | 
|---|
| 90 |         return 0; | 
|---|
| 91 |       } | 
|---|
| 92 |  | 
|---|
| 93 |       /* Save the alpha blending attributes */ | 
|---|
| 94 |       saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); | 
|---|
| 95 |       saved_alpha = surface->format->alpha; | 
|---|
| 96 |       if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { | 
|---|
| 97 |         SDL_SetAlpha(surface, 0, 0); | 
|---|
| 98 |       } | 
|---|
| 99 |  | 
|---|
| 100 |       /* Copy the surface into the GL texture image */ | 
|---|
| 101 |       area.x = 0; | 
|---|
| 102 |       area.y = 0; | 
|---|
| 103 |       area.w = surface->w; | 
|---|
| 104 |       area.h = surface->h; | 
|---|
| 105 |       SDL_BlitSurface(surface, &area, image, &area); | 
|---|
| 106 |  | 
|---|
| 107 |       /* Restore the alpha blending attributes */ | 
|---|
| 108 |       if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { | 
|---|
| 109 |         SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); | 
|---|
| 110 |         this->bAlpha = true; | 
|---|
| 111 |       } | 
|---|
| 112 |  | 
|---|
| 113 |       /* Create an OpenGL texture for the image */ | 
|---|
| 114 |       glGenTextures(1, &this->texture); | 
|---|
| 115 |       glBindTexture(GL_TEXTURE_2D, this->texture); | 
|---|
| 116 |       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 
|---|
| 117 |       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 
|---|
| 118 |       // build the Texture | 
|---|
| 119 |       glTexImage2D(GL_TEXTURE_2D, | 
|---|
| 120 |                    0, | 
|---|
| 121 |                    GL_RGBA, | 
|---|
| 122 |                    w, h, | 
|---|
| 123 |                    0, | 
|---|
| 124 |                    GL_RGBA, | 
|---|
| 125 |                    GL_UNSIGNED_BYTE, | 
|---|
| 126 |                    image->pixels); | 
|---|
| 127 |       // build the MipMaps | 
|---|
| 128 |       gluBuild2DMipmaps(GL_TEXTURE_2D, | 
|---|
| 129 |                         GL_RGBA, | 
|---|
| 130 |                         w, | 
|---|
| 131 |                         h, | 
|---|
| 132 |                         GL_RGBA, | 
|---|
| 133 |                         GL_UNSIGNED_BYTE, | 
|---|
| 134 |                         image->pixels); | 
|---|
| 135 |  | 
|---|
| 136 |       SDL_FreeSurface(image); /* No longer needed */ | 
|---|
| 137 |       glBindTexture(GL_TEXTURE_2D, 0); | 
|---|
| 138 |     } | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | /** | 
|---|
| 142 |  *  loads an Image from a file to a Texture | 
|---|
| 143 |  * @param imageName The image to load | 
|---|
| 144 | */ | 
|---|
| 145 | bool Texture::loadImage(const char* imageName) | 
|---|
| 146 | { | 
|---|
| 147 |   if (GraphicsEngine::texturesEnabled) | 
|---|
| 148 |     { | 
|---|
| 149 |       if (imageName != NULL) | 
|---|
| 150 |         { | 
|---|
| 151 |           SDL_Surface* tmpSurf; | 
|---|
| 152 |           if (this->texture) | 
|---|
| 153 |             glDeleteTextures(1, &this->texture); | 
|---|
| 154 |           // load the new Image to memory | 
|---|
| 155 |           tmpSurf = IMG_Load(imageName); | 
|---|
| 156 |           if(tmpSurf == NULL) | 
|---|
| 157 |             { | 
|---|
| 158 |               PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); | 
|---|
| 159 |               this->texture = 0; | 
|---|
| 160 |               return false; | 
|---|
| 161 |             } | 
|---|
| 162 |           else | 
|---|
| 163 |           { | 
|---|
| 164 |             PRINTF(3)("loading Image %s\n", imageName); | 
|---|
| 165 |             loadTexToGL(tmpSurf); | 
|---|
| 166 |             SDL_FreeSurface(tmpSurf); | 
|---|
| 167 |             return true; | 
|---|
| 168 |           } | 
|---|
| 169 |         } | 
|---|
| 170 |       else | 
|---|
| 171 |         { | 
|---|
| 172 |           return false; | 
|---|
| 173 |         } | 
|---|
| 174 |     } | 
|---|
| 175 |     return false; | 
|---|
| 176 | } | 
|---|