[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" |
---|
[6139] | 21 | #include "compiler.h" |
---|
[3622] | 22 | |
---|
[8363] | 23 | #include "sdlincl.h" |
---|
[8293] | 24 | |
---|
[8363] | 25 | /** |
---|
| 26 | * @brief creates a new Texture Data. |
---|
[8293] | 27 | */ |
---|
[7785] | 28 | TextureData::TextureData() |
---|
[3655] | 29 | { |
---|
[5212] | 30 | this->bAlpha = false; |
---|
[3655] | 31 | this->texture = 0; |
---|
[5753] | 32 | this->image = NULL; |
---|
[7785] | 33 | } |
---|
[5754] | 34 | |
---|
[7785] | 35 | |
---|
| 36 | /** |
---|
| 37 | * @brief Destructor of a Texture |
---|
| 38 | * |
---|
| 39 | * Frees Data, and deletes the textures from GL |
---|
| 40 | */ |
---|
| 41 | TextureData::~TextureData() |
---|
| 42 | { |
---|
| 43 | if (this->texture != 0) |
---|
| 44 | glDeleteTextures(1, &this->texture); |
---|
| 45 | if (this->image != NULL) |
---|
| 46 | SDL_FreeSurface(this->image); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * @brief Loads an SDL_Surface. |
---|
[8363] | 52 | * @param surface the Surface to Load. |
---|
| 53 | * @param target the GL-Target to load the Surface to default GL_TEXTURE_2D |
---|
| 54 | * @returns true on success, false otherwise. |
---|
[7785] | 55 | */ |
---|
| 56 | bool TextureData::loadSurface(SDL_Surface* surface, GLenum target) |
---|
| 57 | { |
---|
| 58 | if (Texture::getTextureEnableState()) |
---|
| 59 | { |
---|
| 60 | SDL_Surface* newSurf = Texture::prepareSurface(surface, this->bAlpha); |
---|
| 61 | if (newSurf != NULL) |
---|
| 62 | { |
---|
| 63 | this->setSurface(newSurf); |
---|
| 64 | this->setTexture(Texture::loadTexToGL(newSurf, target)); |
---|
| 65 | return true; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | return false; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * @brief set the surface this Texture handles |
---|
| 75 | * @param newSurface the new Surface to set as the image for this Texture. |
---|
[8363] | 76 | * @returns true on success. |
---|
[7785] | 77 | * |
---|
| 78 | * This deletes the old version of the stored Texture, |
---|
| 79 | * and sets the newly given Surface as current. |
---|
| 80 | */ |
---|
| 81 | bool TextureData::setSurface(SDL_Surface* newSurface) |
---|
| 82 | { |
---|
| 83 | if (this->image != NULL) |
---|
| 84 | SDL_FreeSurface(this->image); |
---|
| 85 | |
---|
| 86 | this->image = newSurface; |
---|
| 87 | |
---|
| 88 | return (this->image != NULL); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
[8363] | 92 | /** |
---|
| 93 | * @brief sets a new Texture to the Data. |
---|
| 94 | * @param texture the Texture |
---|
| 95 | * @returns true on success. |
---|
| 96 | */ |
---|
[7785] | 97 | bool TextureData::setTexture(GLuint texture) |
---|
| 98 | { |
---|
| 99 | // unload the old Texture. |
---|
| 100 | if (this->texture != 0 && glIsTexture(this->getTexture())) |
---|
| 101 | { |
---|
| 102 | glDeleteTextures(1, &this->texture); |
---|
| 103 | } |
---|
| 104 | this->texture = texture; |
---|
| 105 | return (texture != 0); |
---|
| 106 | } |
---|