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