[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 | |
---|
[5861] | 18 | #include "texture_sequence.h" |
---|
[3341] | 19 | |
---|
[4357] | 20 | #include "debug.h" |
---|
[3622] | 21 | #include "graphics_engine.h" |
---|
| 22 | |
---|
[4662] | 23 | #ifdef HAVE_SDL_IMAGE_H |
---|
[4357] | 24 | #include <SDL_image.h> |
---|
[4662] | 25 | #else |
---|
| 26 | #include <SDL/SDL_image.h> |
---|
| 27 | #endif |
---|
[4357] | 28 | |
---|
[3341] | 29 | /** |
---|
[4836] | 30 | * Constructor for a Texture |
---|
[5865] | 31 | */ |
---|
[5861] | 32 | TextureSequence::TextureSequence(unsigned int count, ...) |
---|
[3655] | 33 | { |
---|
[5861] | 34 | this->setClassID(CL_TEXTURE_SEQUENCE, "TextureSequence"); |
---|
[5304] | 35 | |
---|
[5860] | 36 | va_list textureNameList; |
---|
| 37 | va_start(textureNameList, count); |
---|
| 38 | |
---|
| 39 | this->loadImageSeries(count, textureNameList); |
---|
[4662] | 40 | } |
---|
[3655] | 41 | |
---|
| 42 | /** |
---|
[5865] | 43 | * Destructor of a TextureSequence |
---|
| 44 | * |
---|
| 45 | * Frees Data, and deletes the textures from GL |
---|
| 46 | */ |
---|
[5861] | 47 | TextureSequence::~TextureSequence() |
---|
[3344] | 48 | { |
---|
[5863] | 49 | // delete all images |
---|
| 50 | while(!this->images.empty()) |
---|
| 51 | { |
---|
| 52 | SDL_FreeSurface(this->images.back()); |
---|
| 53 | this->images.pop_back(); |
---|
| 54 | } |
---|
| 55 | |
---|
[5865] | 56 | this->setTexture(0); |
---|
[5863] | 57 | // delete all textures. |
---|
| 58 | while(!this->textures.empty()) |
---|
| 59 | { |
---|
| 60 | if (glIsTexture(this->textures.back())) |
---|
| 61 | glDeleteTextures(1, &this->textures.back()); |
---|
| 62 | this->textures.pop_back(); |
---|
| 63 | } |
---|
[3344] | 64 | } |
---|
| 65 | |
---|
[3905] | 66 | |
---|
[5860] | 67 | /** |
---|
[5865] | 68 | * @brief rebuilds all the textures from the Images stored in this FrameSequence |
---|
[5860] | 69 | */ |
---|
[5861] | 70 | bool TextureSequence::rebuild() |
---|
[5754] | 71 | { |
---|
[5863] | 72 | PRINTF(3)("Reloading TextureSequence of %s '%s'\n", this->getClassName(), this->getName()); |
---|
[5754] | 73 | |
---|
[5863] | 74 | for (unsigned int i = 0; i < this->textures.size(); i++) |
---|
| 75 | { |
---|
| 76 | if (glIsTexture(this->textures[i])) |
---|
| 77 | { |
---|
| 78 | glDeleteTextures(1, &this->textures[i]); |
---|
| 79 | this->textures[i] = 0; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if (this->images[i] != NULL) |
---|
| 83 | this->textures[i] = loadTexToGL(this->images[i]); |
---|
| 84 | } |
---|
[5754] | 85 | } |
---|
[5860] | 86 | |
---|
[5863] | 87 | /** |
---|
[5865] | 88 | * @brief loads an image Sequence |
---|
[5863] | 89 | * @param count how many images to load to the TextureSequence |
---|
| 90 | * @param ... the names of the Images to load |
---|
| 91 | * @returns true on success, false otherwise |
---|
| 92 | */ |
---|
[5861] | 93 | bool TextureSequence::loadImageSeries(unsigned int count, ...) |
---|
[5860] | 94 | { |
---|
| 95 | va_list textureNameList; |
---|
| 96 | va_start(textureNameList, count); |
---|
| 97 | |
---|
[5863] | 98 | return this->loadImageSeries(count, textureNameList); |
---|
[5860] | 99 | } |
---|
| 100 | |
---|
[5863] | 101 | /** |
---|
[5865] | 102 | * @brief loads an image Sequence |
---|
[5863] | 103 | * @param count how many images to load to the TextureSequence |
---|
| 104 | * @param textures the names of the Images to load |
---|
| 105 | * @returns true on success, false otherwise |
---|
| 106 | */ |
---|
[5861] | 107 | bool TextureSequence::loadImageSeries(unsigned int count, va_list textures) |
---|
[5860] | 108 | { |
---|
[5863] | 109 | bool retVal = true; |
---|
| 110 | for (unsigned int i = 0; i < count; i++) |
---|
| 111 | { |
---|
| 112 | if( !this->addFrame(va_arg(textures, char*))) |
---|
| 113 | retVal = false; |
---|
| 114 | } |
---|
| 115 | return retVal; |
---|
[5860] | 116 | } |
---|
| 117 | |
---|
[5865] | 118 | /** |
---|
| 119 | * @brief adds a new Frame to this Sequence (at the end) |
---|
| 120 | * @param imageName the Name of the Image to add |
---|
| 121 | * @returns true on success |
---|
| 122 | */ |
---|
[5861] | 123 | bool TextureSequence::addFrame(const char* imageName) |
---|
[5860] | 124 | { |
---|
[5863] | 125 | if (imageName == NULL) |
---|
| 126 | return false; |
---|
[5860] | 127 | |
---|
[5863] | 128 | SDL_Surface* addSurface = IMG_Load(imageName); |
---|
| 129 | bool success = this->addFrame(addSurface); |
---|
| 130 | delete addSurface; |
---|
[5860] | 131 | |
---|
[5863] | 132 | return success; |
---|
[5860] | 133 | } |
---|
| 134 | |
---|
[5863] | 135 | /** |
---|
[5865] | 136 | * @brief adds a new Frame at the end of the Sequence. |
---|
[5863] | 137 | * @param surface the Surface to add at the end of the Sequence. |
---|
| 138 | */ |
---|
[5861] | 139 | bool TextureSequence::addFrame(SDL_Surface* surface) |
---|
[5860] | 140 | { |
---|
[5863] | 141 | if (surface == NULL) |
---|
| 142 | return false; |
---|
| 143 | bool hasAlpha; |
---|
| 144 | SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha); |
---|
| 145 | if (newSurf != NULL) |
---|
| 146 | { |
---|
| 147 | this->images.push_back(newSurf); |
---|
| 148 | this->textures.push_back(Texture::loadTexToGL(newSurf)); |
---|
| 149 | } |
---|
| 150 | this->setAlpha(hasAlpha); |
---|
[5865] | 151 | return true; |
---|
[5860] | 152 | } |
---|
[5865] | 153 | |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | /** |
---|
[5885] | 157 | * @brief moves to the n'th texture which can then be retrieved via the Texture function: this->getTexture() |
---|
[5865] | 158 | * @param frameNumber the n-th frame |
---|
| 159 | */ |
---|
| 160 | void TextureSequence::gotoFrame(unsigned int frameNumber) |
---|
| 161 | { |
---|
| 162 | if (this->textures.size() > frameNumber) |
---|
| 163 | this->setTexture(this->textures[frameNumber]); |
---|
| 164 | } |
---|