[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" |
---|
[8309] | 22 | #include <stdarg.h> |
---|
[3622] | 23 | |
---|
[4662] | 24 | #ifdef HAVE_SDL_IMAGE_H |
---|
[4357] | 25 | #include <SDL_image.h> |
---|
[4662] | 26 | #else |
---|
| 27 | #include <SDL/SDL_image.h> |
---|
| 28 | #endif |
---|
[4357] | 29 | |
---|
[3341] | 30 | /** |
---|
[8310] | 31 | * @brief Constructor for a Texture |
---|
[5865] | 32 | */ |
---|
[5861] | 33 | TextureSequence::TextureSequence(unsigned int count, ...) |
---|
[3655] | 34 | { |
---|
[5861] | 35 | this->setClassID(CL_TEXTURE_SEQUENCE, "TextureSequence"); |
---|
[5304] | 36 | |
---|
[5860] | 37 | va_list textureNameList; |
---|
| 38 | va_start(textureNameList, count); |
---|
| 39 | |
---|
[8310] | 40 | for (unsigned int i = 0; i < count; i++) |
---|
| 41 | { |
---|
| 42 | this->addFrame(va_arg(textureNameList, char*)); |
---|
| 43 | } |
---|
| 44 | va_end(textureNameList); |
---|
| 45 | |
---|
[5860] | 46 | this->loadImageSeries(count, textureNameList); |
---|
[4662] | 47 | } |
---|
[3655] | 48 | |
---|
[8310] | 49 | /** |
---|
| 50 | * @brief Creates an ImageSeries out of TextureNames. |
---|
| 51 | * @param textureNames The Names of the Textures |
---|
| 52 | * @param prependFolder Optional you can prepend a Folder of Textures. |
---|
| 53 | */ |
---|
| 54 | TextureSequence::TextureSequence(const std::vector<std::string>& textureNames, const std::string& prependFolder) |
---|
| 55 | { |
---|
| 56 | this->setClassID(CL_TEXTURE_SEQUENCE, "TextureSequence"); |
---|
| 57 | this->loadImageSeries(textureNames, prependFolder); |
---|
| 58 | } |
---|
[8309] | 59 | |
---|
[8310] | 60 | |
---|
[3655] | 61 | /** |
---|
[8310] | 62 | * @brief Destructor of a TextureSequence |
---|
[5865] | 63 | * |
---|
| 64 | * Frees Data, and deletes the textures from GL |
---|
| 65 | */ |
---|
[5861] | 66 | TextureSequence::~TextureSequence() |
---|
[3344] | 67 | { |
---|
[6532] | 68 | this->clearLists(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void TextureSequence::clearLists() |
---|
| 72 | { |
---|
[5863] | 73 | // delete all images |
---|
| 74 | while(!this->images.empty()) |
---|
| 75 | { |
---|
| 76 | SDL_FreeSurface(this->images.back()); |
---|
| 77 | this->images.pop_back(); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | // delete all textures. |
---|
| 81 | while(!this->textures.empty()) |
---|
| 82 | { |
---|
| 83 | if (glIsTexture(this->textures.back())) |
---|
| 84 | glDeleteTextures(1, &this->textures.back()); |
---|
| 85 | this->textures.pop_back(); |
---|
| 86 | } |
---|
[3344] | 87 | } |
---|
| 88 | |
---|
[5860] | 89 | /** |
---|
[5865] | 90 | * @brief loads an image Sequence |
---|
[5863] | 91 | * @param count how many images to load to the TextureSequence |
---|
| 92 | * @param ... the names of the Images to load |
---|
| 93 | * @returns true on success, false otherwise |
---|
| 94 | */ |
---|
[5861] | 95 | bool TextureSequence::loadImageSeries(unsigned int count, ...) |
---|
[5860] | 96 | { |
---|
[8309] | 97 | bool retVal = true; |
---|
[5860] | 98 | va_list textureNameList; |
---|
| 99 | va_start(textureNameList, count); |
---|
| 100 | |
---|
[5863] | 101 | for (unsigned int i = 0; i < count; i++) |
---|
| 102 | { |
---|
[8309] | 103 | if( !this->addFrame(va_arg(textureNameList, char*))) |
---|
[5863] | 104 | retVal = false; |
---|
| 105 | } |
---|
[8310] | 106 | va_end(textureNameList); |
---|
[5863] | 107 | return retVal; |
---|
[5860] | 108 | } |
---|
| 109 | |
---|
[8310] | 110 | /** |
---|
| 111 | * @brief Load an TextureSeries from TextureNames |
---|
| 112 | * @param textureNames The Names of the Textures. |
---|
| 113 | * @param prependFolder (optional) the Folder to prepend |
---|
| 114 | * @return true on success. |
---|
| 115 | */ |
---|
| 116 | bool TextureSequence::loadImageSeries(const std::vector<std::string>& textureNames, const std::string& prependFolder) |
---|
| 117 | { |
---|
| 118 | bool retVal = true; |
---|
| 119 | for (unsigned int i = 0; i < textureNames.size(); i++) |
---|
| 120 | { |
---|
| 121 | if( !this->addFrame(prependFolder + textureNames[i])) |
---|
| 122 | retVal = false; |
---|
| 123 | } |
---|
| 124 | return retVal; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
[8309] | 128 | /** |
---|
| 129 | * @brief Loads an Image-Series into the TextureSequence. |
---|
[8324] | 130 | * @param imageNameSubstitue The Prefix of the Image |
---|
[8309] | 131 | * @param from From which image |
---|
| 132 | * @param to To which image |
---|
| 133 | * @return true on success. |
---|
| 134 | * |
---|
| 135 | * @example to load the Files image_001.jpg, image_002.jpg, ... image_099.jpg |
---|
| 136 | * use loadImageSeries("image_###.jpg", 0, 99); |
---|
| 137 | * @note important is, that the count of ###'s is correct. |
---|
| 138 | */ |
---|
[8324] | 139 | bool TextureSequence::loadImageSeries(const std::string& imageNameSubstitue, unsigned int from, unsigned int to) |
---|
[8297] | 140 | { |
---|
| 141 | unsigned int index = 0; |
---|
| 142 | unsigned int frameSize = 0; |
---|
| 143 | // search for the special character # in the LoadParam |
---|
[8324] | 144 | if ((index = imageNameSubstitue.find("_#")) != std::string::npos) |
---|
[8297] | 145 | { |
---|
[8324] | 146 | std::string _imageNameSubstitue = imageNameSubstitue; |
---|
[8297] | 147 | index++; // start at # |
---|
[8324] | 148 | while(imageNameSubstitue[index+frameSize] == '#') |
---|
[8297] | 149 | { |
---|
[8324] | 150 | _imageNameSubstitue[index+frameSize] = '0'; |
---|
[8297] | 151 | frameSize++; |
---|
| 152 | } |
---|
| 153 | |
---|
[8324] | 154 | PRINTF(4)("Found %d '#'s in %s... searching for LOD's\n", frameSize, imageNameSubstitue.c_str()); |
---|
[8297] | 155 | char tmpString[32]; |
---|
| 156 | for (unsigned int i = from; i < to; i++) |
---|
| 157 | { |
---|
| 158 | sprintf(tmpString, "%d", i); |
---|
[8324] | 159 | _imageNameSubstitue.replace(index+frameSize -strlen(tmpString), strlen(tmpString), tmpString); |
---|
| 160 | this->addFrame(_imageNameSubstitue); |
---|
[8297] | 161 | } |
---|
| 162 | return true; |
---|
| 163 | } |
---|
| 164 | return false; |
---|
| 165 | } |
---|
| 166 | |
---|
[8309] | 167 | |
---|
[5865] | 168 | /** |
---|
| 169 | * @brief adds a new Frame to this Sequence (at the end) |
---|
| 170 | * @param imageName the Name of the Image to add |
---|
| 171 | * @returns true on success |
---|
| 172 | */ |
---|
[7221] | 173 | bool TextureSequence::addFrame(const std::string& imageName) |
---|
[5860] | 174 | { |
---|
[7221] | 175 | SDL_Surface* addSurface = IMG_Load(imageName.c_str()); |
---|
[8311] | 176 | if (addSurface == NULL) |
---|
| 177 | { |
---|
| 178 | PRINTF(2)("Unable to load Image %s\n", imageName.c_str()); |
---|
| 179 | return false; |
---|
| 180 | } |
---|
[5863] | 181 | bool success = this->addFrame(addSurface); |
---|
[8619] | 182 | SDL_FreeSurface(addSurface); |
---|
[5860] | 183 | |
---|
[5863] | 184 | return success; |
---|
[5860] | 185 | } |
---|
| 186 | |
---|
[5863] | 187 | /** |
---|
[5865] | 188 | * @brief adds a new Frame at the end of the Sequence. |
---|
[5863] | 189 | * @param surface the Surface to add at the end of the Sequence. |
---|
| 190 | */ |
---|
[5861] | 191 | bool TextureSequence::addFrame(SDL_Surface* surface) |
---|
[5860] | 192 | { |
---|
[5863] | 193 | if (surface == NULL) |
---|
| 194 | return false; |
---|
| 195 | bool hasAlpha; |
---|
| 196 | SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha); |
---|
| 197 | if (newSurf != NULL) |
---|
| 198 | { |
---|
| 199 | this->images.push_back(newSurf); |
---|
| 200 | this->textures.push_back(Texture::loadTexToGL(newSurf)); |
---|
| 201 | } |
---|
| 202 | this->setAlpha(hasAlpha); |
---|
[6532] | 203 | |
---|
[5865] | 204 | return true; |
---|
[5860] | 205 | } |
---|
[5865] | 206 | |
---|
[6532] | 207 | /** |
---|
| 208 | * @brief adds a new Frame at the end of the Sequence. |
---|
| 209 | * @param texture the texture to add at the end of the Sequence. |
---|
| 210 | */ |
---|
| 211 | bool TextureSequence::addFrame(GLuint texture) |
---|
| 212 | { |
---|
| 213 | if (texture == 0) |
---|
| 214 | return false; |
---|
| 215 | this->textures.push_back(texture); |
---|
[5865] | 216 | |
---|
[6532] | 217 | return true; |
---|
| 218 | } |
---|
[5865] | 219 | |
---|
[8309] | 220 | |
---|
| 221 | |
---|
[5865] | 222 | /** |
---|
[8309] | 223 | * @brief rebuilds all the textures from the Images stored in this FrameSequence |
---|
[5865] | 224 | */ |
---|
[8309] | 225 | bool TextureSequence::rebuild() |
---|
[5865] | 226 | { |
---|
[8309] | 227 | PRINTF(3)("Reloading TextureSequence of %s '%s'\n", this->getClassName(), this->getName()); |
---|
| 228 | |
---|
| 229 | for (unsigned int i = 0; i < this->textures.size(); i++) |
---|
| 230 | { |
---|
| 231 | if (glIsTexture(this->textures[i])) |
---|
| 232 | { |
---|
| 233 | glDeleteTextures(1, &this->textures[i]); |
---|
| 234 | this->textures[i] = 0; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | if (this->images[i] != NULL) |
---|
| 238 | this->textures[i] = loadTexToGL(this->images[i]); |
---|
| 239 | } |
---|
| 240 | return true; |
---|
[5865] | 241 | } |
---|