| 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 |  | 
|---|
| 23 | #ifdef HAVE_SDL_IMAGE_H | 
|---|
| 24 | #include <SDL_image.h> | 
|---|
| 25 | #else | 
|---|
| 26 | #include <SDL/SDL_image.h> | 
|---|
| 27 | #endif | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 |  *  Constructor for a Texture | 
|---|
| 31 |  */ | 
|---|
| 32 | TextureSequence::TextureSequence(unsigned int count, ...) | 
|---|
| 33 | { | 
|---|
| 34 |   this->setClassID(CL_TEXTURE_SEQUENCE, "TextureSequence"); | 
|---|
| 35 |  | 
|---|
| 36 |   va_list textureNameList; | 
|---|
| 37 |   va_start(textureNameList, count); | 
|---|
| 38 |  | 
|---|
| 39 |   this->loadImageSeries(count, textureNameList); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | /** | 
|---|
| 43 |  * Destructor of a TextureSequence | 
|---|
| 44 |  * | 
|---|
| 45 |  * Frees Data, and deletes the textures from GL | 
|---|
| 46 |  */ | 
|---|
| 47 | TextureSequence::~TextureSequence() | 
|---|
| 48 | { | 
|---|
| 49 |   // delete all images | 
|---|
| 50 |   while(!this->images.empty()) | 
|---|
| 51 |   { | 
|---|
| 52 |     SDL_FreeSurface(this->images.back()); | 
|---|
| 53 |     this->images.pop_back(); | 
|---|
| 54 |   } | 
|---|
| 55 |  | 
|---|
| 56 |   this->setTexture(0); | 
|---|
| 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 |   } | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | /** | 
|---|
| 68 |  * @brief rebuilds all the textures from the Images stored in this FrameSequence | 
|---|
| 69 |  */ | 
|---|
| 70 | bool TextureSequence::rebuild() | 
|---|
| 71 | { | 
|---|
| 72 |   PRINTF(3)("Reloading TextureSequence of %s '%s'\n", this->getClassName(), this->getName()); | 
|---|
| 73 |  | 
|---|
| 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 |   } | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /** | 
|---|
| 88 |  * @brief loads an image Sequence | 
|---|
| 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 |  */ | 
|---|
| 93 | bool TextureSequence::loadImageSeries(unsigned int count, ...) | 
|---|
| 94 | { | 
|---|
| 95 |   va_list textureNameList; | 
|---|
| 96 |   va_start(textureNameList, count); | 
|---|
| 97 |  | 
|---|
| 98 |   return this->loadImageSeries(count, textureNameList); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | /** | 
|---|
| 102 |  * @brief loads an image Sequence | 
|---|
| 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 |  */ | 
|---|
| 107 | bool TextureSequence::loadImageSeries(unsigned int count, va_list textures) | 
|---|
| 108 | { | 
|---|
| 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; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 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 |  */ | 
|---|
| 123 | bool TextureSequence::addFrame(const char* imageName) | 
|---|
| 124 | { | 
|---|
| 125 |   if (imageName == NULL) | 
|---|
| 126 |     return false; | 
|---|
| 127 |  | 
|---|
| 128 |   SDL_Surface* addSurface = IMG_Load(imageName); | 
|---|
| 129 |   bool success = this->addFrame(addSurface); | 
|---|
| 130 |   delete addSurface; | 
|---|
| 131 |  | 
|---|
| 132 |   return success; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | /** | 
|---|
| 136 |  * @brief adds a new Frame at the end of the Sequence. | 
|---|
| 137 |  * @param surface the Surface to add at the end of the Sequence. | 
|---|
| 138 |  */ | 
|---|
| 139 | bool TextureSequence::addFrame(SDL_Surface* surface) | 
|---|
| 140 | { | 
|---|
| 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); | 
|---|
| 151 |   return true; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 |  | 
|---|
| 156 | /** | 
|---|
| 157 |  * @brief moves to the n'th texture which can then be retrieved via the Texture function: this->getTexture() | 
|---|
| 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 | } | 
|---|