[3341] | 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 | |
---|
[3590] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
| 17 | |
---|
[3341] | 18 | #include "texture.h" |
---|
| 19 | |
---|
[3622] | 20 | #include "graphics_engine.h" |
---|
| 21 | |
---|
[3341] | 22 | /** |
---|
[3344] | 23 | \brief Constructor for a Texture |
---|
| 24 | */ |
---|
[3655] | 25 | Texture::Texture(const char* imageName) |
---|
| 26 | { |
---|
| 27 | this->texture = 0; |
---|
[3905] | 28 | if (imageName) |
---|
| 29 | this->loadImage(imageName); |
---|
[3655] | 30 | } |
---|
| 31 | |
---|
| 32 | /** |
---|
[3344] | 33 | \brief Destructor of a Texture |
---|
| 34 | |
---|
| 35 | Frees Data, and deletes the textures from GL |
---|
| 36 | */ |
---|
| 37 | Texture::~Texture(void) |
---|
| 38 | { |
---|
| 39 | if (this->texture) |
---|
| 40 | glDeleteTextures(1, &this->texture); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | \brief a Simple function that switches two char values |
---|
| 45 | \param a The first value |
---|
| 46 | \param b The second value |
---|
| 47 | */ |
---|
[3863] | 48 | void Texture::swap (unsigned char &a, unsigned char &b) |
---|
[3343] | 49 | { |
---|
| 50 | unsigned char temp; |
---|
| 51 | temp = a; |
---|
| 52 | a = b; |
---|
| 53 | b = temp; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
[3341] | 57 | /** |
---|
| 58 | \brief Loads a Texture to the openGL-environment. |
---|
[3905] | 59 | \param surface the Image to load to openGL |
---|
| 60 | \returns The ID of the texture. |
---|
[3341] | 61 | */ |
---|
[3905] | 62 | GLuint Texture::loadTexToGL (SDL_Surface* surface) |
---|
[3341] | 63 | { |
---|
[3622] | 64 | if (GraphicsEngine::texturesEnabled) |
---|
| 65 | { |
---|
| 66 | PRINTF(4)("Loading texture to OpenGL-Environment.\n"); |
---|
[3905] | 67 | |
---|
| 68 | GLuint texture; |
---|
| 69 | int w, h; |
---|
| 70 | SDL_Surface *image; |
---|
| 71 | SDL_Rect area; |
---|
| 72 | Uint32 saved_flags; |
---|
| 73 | Uint8 saved_alpha; |
---|
[3622] | 74 | |
---|
[3905] | 75 | w = surface->w; |
---|
| 76 | h = surface->h; |
---|
| 77 | |
---|
| 78 | image = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
| 79 | w, h, |
---|
| 80 | 32, |
---|
| 81 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
| 82 | 0x000000FF, |
---|
| 83 | 0x0000FF00, |
---|
| 84 | 0x00FF0000, |
---|
| 85 | 0xFF000000 |
---|
| 86 | #else |
---|
| 87 | 0xFF000000, |
---|
| 88 | 0x00FF0000, |
---|
| 89 | 0x0000FF00, |
---|
| 90 | 0x000000FF |
---|
| 91 | #endif |
---|
| 92 | ); |
---|
| 93 | if ( image == NULL ) { |
---|
| 94 | return 0; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /* Save the alpha blending attributes */ |
---|
| 98 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
---|
| 99 | saved_alpha = surface->format->alpha; |
---|
| 100 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
| 101 | SDL_SetAlpha(surface, 0, 0); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /* Copy the surface into the GL texture image */ |
---|
| 105 | area.x = 0; |
---|
| 106 | area.y = 0; |
---|
| 107 | area.w = surface->w; |
---|
| 108 | area.h = surface->h; |
---|
| 109 | SDL_BlitSurface(surface, &area, image, &area); |
---|
| 110 | |
---|
| 111 | /* Restore the alpha blending attributes */ |
---|
| 112 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
| 113 | SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /* Create an OpenGL texture for the image */ |
---|
| 117 | glGenTextures(1, &texture); |
---|
| 118 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
| 119 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
| 120 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
| 121 | /* glTexImage2D(GL_TEXTURE_2D, |
---|
| 122 | 0, |
---|
| 123 | GL_RGBA, |
---|
| 124 | w, h, |
---|
| 125 | 0, |
---|
| 126 | GL_RGBA, |
---|
| 127 | GL_UNSIGNED_BYTE, |
---|
| 128 | image->pixels); |
---|
| 129 | */ |
---|
| 130 | gluBuild2DMipmaps(GL_TEXTURE_2D, |
---|
| 131 | 3, |
---|
| 132 | w, |
---|
| 133 | h, |
---|
| 134 | GL_RGBA, |
---|
| 135 | GL_UNSIGNED_BYTE, |
---|
| 136 | image->pixels); |
---|
| 137 | |
---|
| 138 | SDL_FreeSurface(image); /* No longer needed */ |
---|
| 139 | |
---|
| 140 | return texture; |
---|
[3622] | 141 | } |
---|
[3341] | 142 | } |
---|
| 143 | |
---|
[3863] | 144 | /** |
---|
| 145 | \brief loads an Image from a file to a Texture |
---|
| 146 | \param imageName The image to load |
---|
| 147 | */ |
---|
[3655] | 148 | bool Texture::loadImage(const char* imageName) |
---|
[3341] | 149 | { |
---|
[3622] | 150 | if (GraphicsEngine::texturesEnabled) |
---|
[3341] | 151 | { |
---|
[3660] | 152 | if (imageName) |
---|
[3341] | 153 | { |
---|
[3905] | 154 | SDL_Surface* tmpSurf; |
---|
| 155 | if (this->texture) |
---|
| 156 | glDeleteTextures(1, &this->texture); |
---|
| 157 | // load the new Image to memory |
---|
| 158 | tmpSurf = IMG_Load(imageName); |
---|
| 159 | if(!tmpSurf) |
---|
[3622] | 160 | { |
---|
| 161 | PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); |
---|
| 162 | return false; |
---|
| 163 | } |
---|
[3790] | 164 | |
---|
[3905] | 165 | GLubyte* pixels = (GLubyte*)tmpSurf->pixels; |
---|
[3622] | 166 | |
---|
[3905] | 167 | /* this swaps the Mapping so lowel left will be upper left */ |
---|
| 168 | for( int i = 0 ; i < (tmpSurf->h / 2) ; ++i ) |
---|
| 169 | for( int j = 0 ; j < tmpSurf->w * tmpSurf->format->BytesPerPixel; j += tmpSurf->format->BytesPerPixel ) |
---|
| 170 | for(int k = 0; k < tmpSurf->format->BytesPerPixel; ++k) |
---|
| 171 | swap( pixels[(i * tmpSurf->w * tmpSurf->format->BytesPerPixel) + j + k], |
---|
| 172 | pixels[ ( (tmpSurf->h - i - 1) * tmpSurf->w * tmpSurf->format->BytesPerPixel) + j + k]); |
---|
| 173 | |
---|
| 174 | PRINTF(3)("loading Image %s\n", imageName); |
---|
| 175 | if (tmpSurf) |
---|
| 176 | this->texture = loadTexToGL(tmpSurf); |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | SDL_FreeSurface(tmpSurf); |
---|
| 180 | return true; |
---|
[3622] | 181 | } |
---|
| 182 | else |
---|
| 183 | { |
---|
[3660] | 184 | PRINTF(2)("Image not Found: %s\n", imageName); |
---|
[3341] | 185 | return false; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | } |
---|