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
RevLine 
[4662]1/*
[3341]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
[3590]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
17
[5861]18#include "texture_sequence.h"
[3341]19
[4357]20#include "debug.h"
[3622]21#include "graphics_engine.h"
22
[4662]23#ifdef HAVE_SDL_IMAGE_H
[4357]24#include <SDL_image.h>
[4662]25#else
26#include <SDL/SDL_image.h>
27#endif
[4357]28
[3341]29/**
[4836]30 *  Constructor for a Texture
[5865]31 */
[5861]32TextureSequence::TextureSequence(unsigned int count, ...)
[3655]33{
[5861]34  this->setClassID(CL_TEXTURE_SEQUENCE, "TextureSequence");
[5304]35
[5860]36  va_list textureNameList;
37  va_start(textureNameList, count);
38
39  this->loadImageSeries(count, textureNameList);
[4662]40}
[3655]41
42/**
[5865]43 * Destructor of a TextureSequence
44 *
45 * Frees Data, and deletes the textures from GL
46 */
[5861]47TextureSequence::~TextureSequence()
[3344]48{
[6532]49  this->clearLists();
50}
51
52void TextureSequence::clearLists()
53{
[5863]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  }
[3344]68}
69
[5860]70/**
[5865]71 * @brief rebuilds all the textures from the Images stored in this FrameSequence
[5860]72 */
[5861]73bool TextureSequence::rebuild()
[5754]74{
[5863]75  PRINTF(3)("Reloading TextureSequence of %s '%s'\n", this->getClassName(), this->getName());
[5754]76
[5863]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  }
[8297]88  return true;
[5754]89}
[5860]90
[5863]91/**
[5865]92 * @brief loads an image Sequence
[5863]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 */
[5861]97bool TextureSequence::loadImageSeries(unsigned int count, ...)
[5860]98{
99  va_list textureNameList;
100  va_start(textureNameList, count);
101
[5863]102  return this->loadImageSeries(count, textureNameList);
[5860]103}
104
[5863]105/**
[5865]106 * @brief loads an image Sequence
[5863]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 */
[5861]111bool TextureSequence::loadImageSeries(unsigned int count, va_list textures)
[5860]112{
[5863]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;
[5860]120}
121
[8297]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
[5865]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 */
[7221]155bool TextureSequence::addFrame(const std::string& imageName)
[5860]156{
[7221]157  SDL_Surface* addSurface = IMG_Load(imageName.c_str());
[5863]158  bool success = this->addFrame(addSurface);
159  delete addSurface;
[5860]160
[5863]161  return success;
[5860]162}
163
[5863]164/**
[5865]165 * @brief adds a new Frame at the end of the Sequence.
[5863]166 * @param surface the Surface to add at the end of the Sequence.
167 */
[5861]168bool TextureSequence::addFrame(SDL_Surface* surface)
[5860]169{
[5863]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);
[6532]180
[5865]181  return true;
[5860]182}
[5865]183
[6532]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);
[5865]193
[6532]194  return true;
195}
[5865]196
197/**
[5885]198 * @brief moves to the n'th texture which can then be retrieved via the Texture function: this->getTexture()
[5865]199 * @param frameNumber the n-th frame
200 */
[6532]201/*void TextureSequence::gotoFrame(unsigned int frameNumber)
[5865]202{
203  if (this->textures.size() > frameNumber)
204    this->setTexture(this->textures[frameNumber]);
205}
[6624]206*/
Note: See TracBrowser for help on using the repository browser.