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 | /** |
---|
31 | * Constructor for a Texture |
---|
32 | */ |
---|
33 | TextureSequence::TextureSequence(unsigned int count, ...) |
---|
34 | { |
---|
35 | this->setClassID(CL_TEXTURE_SEQUENCE, "TextureSequence"); |
---|
36 | |
---|
37 | va_list textureNameList; |
---|
38 | va_start(textureNameList, count); |
---|
39 | |
---|
40 | this->loadImageSeries(count, textureNameList); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | * Destructor of a TextureSequence |
---|
46 | * |
---|
47 | * Frees Data, and deletes the textures from GL |
---|
48 | */ |
---|
49 | TextureSequence::~TextureSequence() |
---|
50 | { |
---|
51 | this->clearLists(); |
---|
52 | } |
---|
53 | |
---|
54 | void TextureSequence::clearLists() |
---|
55 | { |
---|
56 | // delete all images |
---|
57 | while(!this->images.empty()) |
---|
58 | { |
---|
59 | SDL_FreeSurface(this->images.back()); |
---|
60 | this->images.pop_back(); |
---|
61 | } |
---|
62 | |
---|
63 | // delete all textures. |
---|
64 | while(!this->textures.empty()) |
---|
65 | { |
---|
66 | if (glIsTexture(this->textures.back())) |
---|
67 | glDeleteTextures(1, &this->textures.back()); |
---|
68 | this->textures.pop_back(); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * @brief loads an image Sequence |
---|
74 | * @param count how many images to load to the TextureSequence |
---|
75 | * @param ... the names of the Images to load |
---|
76 | * @returns true on success, false otherwise |
---|
77 | */ |
---|
78 | bool TextureSequence::loadImageSeries(unsigned int count, ...) |
---|
79 | { |
---|
80 | bool retVal = true; |
---|
81 | va_list textureNameList; |
---|
82 | va_start(textureNameList, count); |
---|
83 | |
---|
84 | for (unsigned int i = 0; i < count; i++) |
---|
85 | { |
---|
86 | if( !this->addFrame(va_arg(textureNameList, char*))) |
---|
87 | retVal = false; |
---|
88 | } |
---|
89 | return retVal; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | /** |
---|
94 | * @brief Loads an Image-Series into the TextureSequence. |
---|
95 | * @param imagePrefix The Prefix of the Image |
---|
96 | * @param from From which image |
---|
97 | * @param to To which image |
---|
98 | * @return true on success. |
---|
99 | * |
---|
100 | * @example to load the Files image_001.jpg, image_002.jpg, ... image_099.jpg |
---|
101 | * use loadImageSeries("image_###.jpg", 0, 99); |
---|
102 | * @note important is, that the count of ###'s is correct. |
---|
103 | */ |
---|
104 | bool TextureSequence::loadImageSeries(const std::string& imagePrefix, unsigned int from, unsigned int to) |
---|
105 | { |
---|
106 | unsigned int index = 0; |
---|
107 | unsigned int frameSize = 0; |
---|
108 | // search for the special character # in the LoadParam |
---|
109 | if ((index = imagePrefix.find("_#")) != std::string::npos) |
---|
110 | { |
---|
111 | std::string _imagePrefix = imagePrefix; |
---|
112 | index++; // start at # |
---|
113 | while(imagePrefix[index+frameSize] == '#') |
---|
114 | { |
---|
115 | _imagePrefix[index+frameSize] = '0'; |
---|
116 | frameSize++; |
---|
117 | } |
---|
118 | |
---|
119 | PRINTF(4)("Found %d '#'s in %s... searching for LOD's\n", frameSize, imagePrefix.c_str()); |
---|
120 | char tmpString[32]; |
---|
121 | for (unsigned int i = from; i < to; i++) |
---|
122 | { |
---|
123 | sprintf(tmpString, "%d", i); |
---|
124 | _imagePrefix.replace(index+frameSize -strlen(tmpString), strlen(tmpString), tmpString); |
---|
125 | this->addFrame(_imagePrefix); |
---|
126 | } |
---|
127 | return true; |
---|
128 | } |
---|
129 | return false; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | /** |
---|
134 | * @brief adds a new Frame to this Sequence (at the end) |
---|
135 | * @param imageName the Name of the Image to add |
---|
136 | * @returns true on success |
---|
137 | */ |
---|
138 | bool TextureSequence::addFrame(const std::string& imageName) |
---|
139 | { |
---|
140 | printf("adding %s\n", imageName.c_str()); |
---|
141 | SDL_Surface* addSurface = IMG_Load(imageName.c_str()); |
---|
142 | bool success = this->addFrame(addSurface); |
---|
143 | delete addSurface; |
---|
144 | |
---|
145 | return success; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * @brief adds a new Frame at the end of the Sequence. |
---|
150 | * @param surface the Surface to add at the end of the Sequence. |
---|
151 | */ |
---|
152 | bool TextureSequence::addFrame(SDL_Surface* surface) |
---|
153 | { |
---|
154 | if (surface == NULL) |
---|
155 | return false; |
---|
156 | bool hasAlpha; |
---|
157 | SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha); |
---|
158 | if (newSurf != NULL) |
---|
159 | { |
---|
160 | this->images.push_back(newSurf); |
---|
161 | this->textures.push_back(Texture::loadTexToGL(newSurf)); |
---|
162 | } |
---|
163 | this->setAlpha(hasAlpha); |
---|
164 | |
---|
165 | return true; |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * @brief adds a new Frame at the end of the Sequence. |
---|
170 | * @param texture the texture to add at the end of the Sequence. |
---|
171 | */ |
---|
172 | bool TextureSequence::addFrame(GLuint texture) |
---|
173 | { |
---|
174 | if (texture == 0) |
---|
175 | return false; |
---|
176 | this->textures.push_back(texture); |
---|
177 | |
---|
178 | return true; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | /** |
---|
184 | * @brief rebuilds all the textures from the Images stored in this FrameSequence |
---|
185 | */ |
---|
186 | bool TextureSequence::rebuild() |
---|
187 | { |
---|
188 | PRINTF(3)("Reloading TextureSequence of %s '%s'\n", this->getClassName(), this->getName()); |
---|
189 | |
---|
190 | for (unsigned int i = 0; i < this->textures.size(); i++) |
---|
191 | { |
---|
192 | if (glIsTexture(this->textures[i])) |
---|
193 | { |
---|
194 | glDeleteTextures(1, &this->textures[i]); |
---|
195 | this->textures[i] = 0; |
---|
196 | } |
---|
197 | |
---|
198 | if (this->images[i] != NULL) |
---|
199 | this->textures[i] = loadTexToGL(this->images[i]); |
---|
200 | } |
---|
201 | return true; |
---|
202 | } |
---|