Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: new ImageLoading algo

File size: 5.0 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
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 */
32TextureSequence::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 */
47TextureSequence::~TextureSequence()
48{
49  this->clearLists();
50}
51
52void 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 */
73bool 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 */
97bool 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 */
111bool 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
122bool 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
137    PRINTF(4)("Found %d '#'s in %s... searching for LOD's\n", frameSize, imagePrefix.c_str());
138    char tmpString[32];
139    for (unsigned int i = from; i < to; i++)
140    {
141      sprintf(tmpString, "%d", i);
142      _imagePrefix.replace(index+frameSize -strlen(tmpString), index+frameSize, tmpString);
143      printf("TEST STRING %s\n", _imagePrefix.c_str());
144    }
145    return true;
146  }
147  return false;
148}
149
150/**
151 * @brief adds a new Frame to this Sequence (at the end)
152 * @param imageName the Name of the Image to add
153 * @returns true on success
154 */
155bool TextureSequence::addFrame(const std::string& imageName)
156{
157  SDL_Surface* addSurface = IMG_Load(imageName.c_str());
158  bool success = this->addFrame(addSurface);
159  delete addSurface;
160
161  return success;
162}
163
164/**
165 * @brief adds a new Frame at the end of the Sequence.
166 * @param surface the Surface to add at the end of the Sequence.
167 */
168bool TextureSequence::addFrame(SDL_Surface* surface)
169{
170  if (surface == NULL)
171    return false;
172  bool hasAlpha;
173  SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha);
174  if (newSurf != NULL)
175  {
176    this->images.push_back(newSurf);
177    this->textures.push_back(Texture::loadTexToGL(newSurf));
178  }
179  this->setAlpha(hasAlpha);
180
181  return true;
182}
183
184/**
185 * @brief adds a new Frame at the end of the Sequence.
186 * @param texture the texture to add at the end of the Sequence.
187 */
188bool TextureSequence::addFrame(GLuint texture)
189{
190  if (texture == 0)
191    return false;
192  this->textures.push_back(texture);
193
194  return true;
195}
196
197/**
198 * @brief moves to the n'th texture which can then be retrieved via the Texture function: this->getTexture()
199 * @param frameNumber the n-th frame
200 */
201/*void TextureSequence::gotoFrame(unsigned int frameNumber)
202{
203  if (this->textures.size() > frameNumber)
204    this->setTexture(this->textures[frameNumber]);
205}
206*/
Note: See TracBrowser for help on using the repository browser.