| 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 "compiler.h" |
|---|
| 22 | |
|---|
| 23 | #include "sdlincl.h" |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * @brief creates a new Texture Data. |
|---|
| 27 | */ |
|---|
| 28 | TextureData::TextureData() |
|---|
| 29 | { |
|---|
| 30 | this->bAlpha = false; |
|---|
| 31 | this->texture = 0; |
|---|
| 32 | this->image = NULL; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 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. |
|---|
| 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. |
|---|
| 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. |
|---|
| 76 | * @returns true on success. |
|---|
| 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 | |
|---|
| 92 | /** |
|---|
| 93 | * @brief sets a new Texture to the Data. |
|---|
| 94 | * @param texture the Texture |
|---|
| 95 | * @returns true on success. |
|---|
| 96 | */ |
|---|
| 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 | } |
|---|