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 | // delete all images |
---|
50 | while(!this->images.empty()) |
---|
51 | { |
---|
52 | SDL_FreeSurface(this->images.back()); |
---|
53 | this->images.pop_back(); |
---|
54 | } |
---|
55 | |
---|
56 | // delete all textures. |
---|
57 | while(!this->textures.empty()) |
---|
58 | { |
---|
59 | if (glIsTexture(this->textures.back())) |
---|
60 | glDeleteTextures(1, &this->textures.back()); |
---|
61 | this->textures.pop_back(); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | /** |
---|
67 | * rebuilds all the textures from the Images stored in this FrameSequence |
---|
68 | */ |
---|
69 | bool TextureSequence::rebuild() |
---|
70 | { |
---|
71 | PRINTF(3)("Reloading TextureSequence of %s '%s'\n", this->getClassName(), this->getName()); |
---|
72 | |
---|
73 | for (unsigned int i = 0; i < this->textures.size(); i++) |
---|
74 | { |
---|
75 | if (glIsTexture(this->textures[i])) |
---|
76 | { |
---|
77 | glDeleteTextures(1, &this->textures[i]); |
---|
78 | this->textures[i] = 0; |
---|
79 | } |
---|
80 | |
---|
81 | if (this->images[i] != NULL) |
---|
82 | this->textures[i] = loadTexToGL(this->images[i]); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * loads an image Series |
---|
88 | * @param count how many images to load to the TextureSequence |
---|
89 | * @param ... the names of the Images to load |
---|
90 | * @returns true on success, false otherwise |
---|
91 | */ |
---|
92 | bool TextureSequence::loadImageSeries(unsigned int count, ...) |
---|
93 | { |
---|
94 | va_list textureNameList; |
---|
95 | va_start(textureNameList, count); |
---|
96 | |
---|
97 | return this->loadImageSeries(count, textureNameList); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * loads an image Series |
---|
102 | * @param count how many images to load to the TextureSequence |
---|
103 | * @param textures the names of the Images to load |
---|
104 | * @returns true on success, false otherwise |
---|
105 | */ |
---|
106 | bool TextureSequence::loadImageSeries(unsigned int count, va_list textures) |
---|
107 | { |
---|
108 | bool retVal = true; |
---|
109 | for (unsigned int i = 0; i < count; i++) |
---|
110 | { |
---|
111 | if( !this->addFrame(va_arg(textures, char*))) |
---|
112 | retVal = false; |
---|
113 | } |
---|
114 | return retVal; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | bool TextureSequence::addFrame(const char* imageName) |
---|
119 | { |
---|
120 | if (imageName == NULL) |
---|
121 | return false; |
---|
122 | |
---|
123 | SDL_Surface* addSurface = IMG_Load(imageName); |
---|
124 | bool success = this->addFrame(addSurface); |
---|
125 | delete addSurface; |
---|
126 | |
---|
127 | return success; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * adds a new Frame at the end of the Sequence. |
---|
132 | * @param surface the Surface to add at the end of the Sequence. |
---|
133 | */ |
---|
134 | bool TextureSequence::addFrame(SDL_Surface* surface) |
---|
135 | { |
---|
136 | if (surface == NULL) |
---|
137 | return false; |
---|
138 | bool hasAlpha; |
---|
139 | SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha); |
---|
140 | if (newSurf != NULL) |
---|
141 | { |
---|
142 | this->images.push_back(newSurf); |
---|
143 | this->textures.push_back(Texture::loadTexToGL(newSurf)); |
---|
144 | } |
---|
145 | this->setAlpha(hasAlpha); |
---|
146 | } |
---|