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