Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/texture_sequence.cc @ 8595

Last change on this file since 8595 was 8324, checked in by bensch, 18 years ago

trunk: Texture-Sequence is initialized smoothly now in the GLGuiCursor

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