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