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 | this->clearLists(); |
---|
50 | } |
---|
51 | |
---|
52 | void TextureSequence::clearLists() |
---|
53 | { |
---|
54 | // delete all images |
---|
55 | while(!this->images.empty()) |
---|
56 | { |
---|
57 | SDL_FreeSurface(this->images.back()); |
---|
58 | this->images.pop_back(); |
---|
59 | } |
---|
60 | |
---|
61 | // delete all textures. |
---|
62 | while(!this->textures.empty()) |
---|
63 | { |
---|
64 | if (glIsTexture(this->textures.back())) |
---|
65 | glDeleteTextures(1, &this->textures.back()); |
---|
66 | this->textures.pop_back(); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief rebuilds all the textures from the Images stored in this FrameSequence |
---|
72 | */ |
---|
73 | bool TextureSequence::rebuild() |
---|
74 | { |
---|
75 | PRINTF(3)("Reloading TextureSequence of %s '%s'\n", this->getClassName(), this->getName()); |
---|
76 | |
---|
77 | for (unsigned int i = 0; i < this->textures.size(); i++) |
---|
78 | { |
---|
79 | if (glIsTexture(this->textures[i])) |
---|
80 | { |
---|
81 | glDeleteTextures(1, &this->textures[i]); |
---|
82 | this->textures[i] = 0; |
---|
83 | } |
---|
84 | |
---|
85 | if (this->images[i] != NULL) |
---|
86 | this->textures[i] = loadTexToGL(this->images[i]); |
---|
87 | } |
---|
88 | return true; |
---|
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 | va_list textureNameList; |
---|
100 | va_start(textureNameList, count); |
---|
101 | |
---|
102 | return this->loadImageSeries(count, textureNameList); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * @brief loads an image Sequence |
---|
107 | * @param count how many images to load to the TextureSequence |
---|
108 | * @param textures the names of the Images to load |
---|
109 | * @returns true on success, false otherwise |
---|
110 | */ |
---|
111 | bool TextureSequence::loadImageSeries(unsigned int count, va_list textures) |
---|
112 | { |
---|
113 | bool retVal = true; |
---|
114 | for (unsigned int i = 0; i < count; i++) |
---|
115 | { |
---|
116 | if( !this->addFrame(va_arg(textures, char*))) |
---|
117 | retVal = false; |
---|
118 | } |
---|
119 | return retVal; |
---|
120 | } |
---|
121 | |
---|
122 | bool TextureSequence::loadImageSeries(const std::string& imagePrefix, unsigned int from, unsigned int to, const std::string& extension) |
---|
123 | { |
---|
124 | unsigned int index = 0; |
---|
125 | unsigned int frameSize = 0; |
---|
126 | // search for the special character # in the LoadParam |
---|
127 | if ((index = imagePrefix.find("_#")) != std::string::npos) |
---|
128 | { |
---|
129 | std::string _imagePrefix = imagePrefix; |
---|
130 | index++; // start at # |
---|
131 | while(imagePrefix[index+frameSize] == '#') |
---|
132 | { |
---|
133 | _imagePrefix[index+frameSize] = '0'; |
---|
134 | frameSize++; |
---|
135 | } |
---|
136 | _imagePrefix += "." + extension; |
---|
137 | printf("TESTING %s\n", _imagePrefix.c_str()); |
---|
138 | |
---|
139 | PRINTF(4)("Found %d '#'s in %s... searching for LOD's\n", frameSize, imagePrefix.c_str()); |
---|
140 | char tmpString[32]; |
---|
141 | for (unsigned int i = from; i < to; i++) |
---|
142 | { |
---|
143 | sprintf(tmpString, "%d", i); |
---|
144 | _imagePrefix.replace(index+frameSize -strlen(tmpString), strlen(tmpString), tmpString); |
---|
145 | this->addFrame(_imagePrefix); |
---|
146 | } |
---|
147 | return true; |
---|
148 | } |
---|
149 | return false; |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * @brief adds a new Frame to this Sequence (at the end) |
---|
154 | * @param imageName the Name of the Image to add |
---|
155 | * @returns true on success |
---|
156 | */ |
---|
157 | bool TextureSequence::addFrame(const std::string& imageName) |
---|
158 | { |
---|
159 | printf("adding %s\n", imageName.c_str()); |
---|
160 | SDL_Surface* addSurface = IMG_Load(imageName.c_str()); |
---|
161 | bool success = this->addFrame(addSurface); |
---|
162 | delete addSurface; |
---|
163 | |
---|
164 | return success; |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * @brief adds a new Frame at the end of the Sequence. |
---|
169 | * @param surface the Surface to add at the end of the Sequence. |
---|
170 | */ |
---|
171 | bool TextureSequence::addFrame(SDL_Surface* surface) |
---|
172 | { |
---|
173 | if (surface == NULL) |
---|
174 | return false; |
---|
175 | bool hasAlpha; |
---|
176 | SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha); |
---|
177 | if (newSurf != NULL) |
---|
178 | { |
---|
179 | this->images.push_back(newSurf); |
---|
180 | this->textures.push_back(Texture::loadTexToGL(newSurf)); |
---|
181 | } |
---|
182 | this->setAlpha(hasAlpha); |
---|
183 | |
---|
184 | return true; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * @brief adds a new Frame at the end of the Sequence. |
---|
189 | * @param texture the texture to add at the end of the Sequence. |
---|
190 | */ |
---|
191 | bool TextureSequence::addFrame(GLuint texture) |
---|
192 | { |
---|
193 | if (texture == 0) |
---|
194 | return false; |
---|
195 | this->textures.push_back(texture); |
---|
196 | |
---|
197 | return true; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * @brief moves to the n'th texture which can then be retrieved via the Texture function: this->getTexture() |
---|
202 | * @param frameNumber the n-th frame |
---|
203 | */ |
---|
204 | /*void TextureSequence::gotoFrame(unsigned int frameNumber) |
---|
205 | { |
---|
206 | if (this->textures.size() > frameNumber) |
---|
207 | this->setTexture(this->textures[frameNumber]); |
---|
208 | } |
---|
209 | */ |
---|