| 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_sequence.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "debug.h" | 
|---|
| 21 | #include "graphics_engine.h" | 
|---|
| 22 | #include <stdarg.h> | 
|---|
| 23 |  | 
|---|
| 24 | #ifdef HAVE_SDL_IMAGE_H | 
|---|
| 25 | #include <SDL_image.h> | 
|---|
| 26 | #else | 
|---|
| 27 | #include <SDL/SDL_image.h> | 
|---|
| 28 | #endif | 
|---|
| 29 |  | 
|---|
| 30 | ObjectListDefinition(TextureSequence); | 
|---|
| 31 |  | 
|---|
| 32 | /** | 
|---|
| 33 | * @brief Constructor for a Texture | 
|---|
| 34 | */ | 
|---|
| 35 | TextureSequence::TextureSequence(unsigned int count, ...) | 
|---|
| 36 | { | 
|---|
| 37 | this->registerObject(this, TextureSequence::_objectList); | 
|---|
| 38 |  | 
|---|
| 39 | va_list textureNameList; | 
|---|
| 40 | va_start(textureNameList, count); | 
|---|
| 41 |  | 
|---|
| 42 | for (unsigned int i = 0; i < count; i++) | 
|---|
| 43 | { | 
|---|
| 44 | this->addFrame(va_arg(textureNameList, char*)); | 
|---|
| 45 | } | 
|---|
| 46 | va_end(textureNameList); | 
|---|
| 47 |  | 
|---|
| 48 | this->loadImageSeries(count, textureNameList); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 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 | { | 
|---|
| 58 | this->registerObject(this, TextureSequence::_objectList); | 
|---|
| 59 | this->loadImageSeries(textureNames, prependFolder); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | /** | 
|---|
| 64 | * @brief Destructor of a TextureSequence | 
|---|
| 65 | * | 
|---|
| 66 | * Frees Data, and deletes the textures from GL | 
|---|
| 67 | */ | 
|---|
| 68 | TextureSequence::~TextureSequence() | 
|---|
| 69 | { | 
|---|
| 70 | this->clearLists(); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | void TextureSequence::clearLists() | 
|---|
| 74 | { | 
|---|
| 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 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /** | 
|---|
| 92 | * @brief loads an image Sequence | 
|---|
| 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 | */ | 
|---|
| 97 | bool TextureSequence::loadImageSeries(unsigned int count, ...) | 
|---|
| 98 | { | 
|---|
| 99 | bool retVal = true; | 
|---|
| 100 | va_list textureNameList; | 
|---|
| 101 | va_start(textureNameList, count); | 
|---|
| 102 |  | 
|---|
| 103 | for (unsigned int i = 0; i < count; i++) | 
|---|
| 104 | { | 
|---|
| 105 | if( !this->addFrame(va_arg(textureNameList, char*))) | 
|---|
| 106 | retVal = false; | 
|---|
| 107 | } | 
|---|
| 108 | va_end(textureNameList); | 
|---|
| 109 | return retVal; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 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 |  | 
|---|
| 130 | /** | 
|---|
| 131 | * @brief Loads an Image-Series into the TextureSequence. | 
|---|
| 132 | * @param imageNameSubstitue The Prefix of the Image | 
|---|
| 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 | */ | 
|---|
| 141 | bool TextureSequence::loadImageSeries(const std::string& imageNameSubstitue, unsigned int from, unsigned int to) | 
|---|
| 142 | { | 
|---|
| 143 | unsigned int index = 0; | 
|---|
| 144 | unsigned int frameSize = 0; | 
|---|
| 145 | // search for the special character # in the LoadParam | 
|---|
| 146 | if ((index = imageNameSubstitue.find("_#")) != std::string::npos) | 
|---|
| 147 | { | 
|---|
| 148 | std::string _imageNameSubstitue = imageNameSubstitue; | 
|---|
| 149 | index++; // start at # | 
|---|
| 150 | while(imageNameSubstitue[index+frameSize] == '#') | 
|---|
| 151 | { | 
|---|
| 152 | _imageNameSubstitue[index+frameSize] = '0'; | 
|---|
| 153 | frameSize++; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | PRINTF(4)("Found %d '#'s in %s... searching for LOD's\n", frameSize, imageNameSubstitue.c_str()); | 
|---|
| 157 | char tmpString[32]; | 
|---|
| 158 | for (unsigned int i = from; i < to; i++) | 
|---|
| 159 | { | 
|---|
| 160 | sprintf(tmpString, "%d", i); | 
|---|
| 161 | _imageNameSubstitue.replace(index+frameSize -strlen(tmpString), strlen(tmpString), tmpString); | 
|---|
| 162 | this->addFrame(_imageNameSubstitue); | 
|---|
| 163 | } | 
|---|
| 164 | return true; | 
|---|
| 165 | } | 
|---|
| 166 | return false; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 |  | 
|---|
| 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 | */ | 
|---|
| 175 | bool TextureSequence::addFrame(const std::string& imageName) | 
|---|
| 176 | { | 
|---|
| 177 | SDL_Surface* addSurface = IMG_Load(imageName.c_str()); | 
|---|
| 178 | if (addSurface == NULL) | 
|---|
| 179 | { | 
|---|
| 180 | PRINTF(2)("Unable to load Image %s\n", imageName.c_str()); | 
|---|
| 181 | return false; | 
|---|
| 182 | } | 
|---|
| 183 | bool success = this->addFrame(addSurface); | 
|---|
| 184 | SDL_FreeSurface(addSurface); | 
|---|
| 185 |  | 
|---|
| 186 | return success; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | /** | 
|---|
| 190 | * @brief adds a new Frame at the end of the Sequence. | 
|---|
| 191 | * @param surface the Surface to add at the end of the Sequence. | 
|---|
| 192 | */ | 
|---|
| 193 | bool TextureSequence::addFrame(SDL_Surface* surface) | 
|---|
| 194 | { | 
|---|
| 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); | 
|---|
| 205 |  | 
|---|
| 206 | return true; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 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); | 
|---|
| 218 |  | 
|---|
| 219 | return true; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 |  | 
|---|
| 223 |  | 
|---|
| 224 | /** | 
|---|
| 225 | * @brief rebuilds all the textures from the Images stored in this FrameSequence | 
|---|
| 226 | */ | 
|---|
| 227 | bool TextureSequence::rebuild() | 
|---|
| 228 | { | 
|---|
| 229 | PRINTF(3)("Reloading TextureSequence of %s '%s'\n", this->getClassCName(), this->getCName()); | 
|---|
| 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; | 
|---|
| 243 | } | 
|---|