2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "video/texture_manager.hpp"
19 #include <SDL_image.h>
22 #include "physfs/physfs_sdl.hpp"
23 #include "util/file_system.hpp"
24 #include "util/log.hpp"
25 #include "video/gl/gl_texture.hpp"
26 #include "video/video_systems.hpp"
28 TextureManager* texture_manager = NULL;
30 TextureManager::TextureManager() :
39 TextureManager::~TextureManager()
41 for(ImageTextures::iterator i = image_textures.begin();
42 i != image_textures.end(); ++i) {
45 log_warning << "Texture '" << i->first << "' not freed" << std::endl;
51 TextureManager::get(const std::string& _filename)
53 std::string filename = FileSystem::normalize(_filename);
54 ImageTextures::iterator i = image_textures.find(filename);
56 Texture* texture = NULL;
57 if(i != image_textures.end())
61 texture = create_image_texture(filename);
62 image_textures[filename] = texture;
69 TextureManager::release(Texture* texture)
71 image_textures.erase(texture->get_filename());
77 TextureManager::register_texture(GLTexture* texture)
79 textures.insert(texture);
83 TextureManager::remove_texture(GLTexture* texture)
85 textures.erase(texture);
90 TextureManager::create_image_texture(const std::string& filename)
94 SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
96 std::ostringstream msg;
97 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
98 throw std::runtime_error(msg.str());
103 result = new_texture(image);
104 result->set_filename(filename);
107 SDL_FreeSurface(image);
111 SDL_FreeSurface(image);
114 } catch (const std::runtime_error& err) {
115 const std::string dummy_texture_fname = "images/engine/missing.png";
116 if (filename == dummy_texture_fname) throw err;
118 // on error, try loading placeholder file
121 Texture* tex = create_image_texture(dummy_texture_fname);
122 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
127 // on error (when loading placeholder), try using empty surface
130 SDL_Surface* image = SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0);
137 result = new_texture(image);
138 result->set_filename("-dummy-texture-.png");
141 SDL_FreeSurface(image);
145 SDL_FreeSurface(image);
146 log_warning << "Couldn't load texture '" << filename << "' (now using empty one): " << err.what() << std::endl;
149 // on error (when trying to use empty surface), give up
150 } catch (const std::runtime_error& err) {
159 TextureManager::save_textures()
161 #ifdef GL_PACK_ROW_LENGTH
162 /* all this stuff is not support by OpenGL ES */
163 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
164 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
165 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
166 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
167 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
170 glPixelStorei(GL_PACK_ALIGNMENT, 1);
171 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
174 for(ImageTextures::iterator i = image_textures.begin();
175 i != image_textures.end(); ++i) {
176 save_texture(dynamic_cast<GLTexture *>(i->second));
181 TextureManager::save_texture(GLTexture* texture)
183 SavedTexture saved_texture;
184 saved_texture.texture = texture;
185 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
187 //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
188 #ifndef GL_VERSION_ES_CM_1_0
189 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
190 &saved_texture.width);
191 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
192 &saved_texture.height);
193 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
194 &saved_texture.border);
195 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
196 &saved_texture.min_filter);
197 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
198 &saved_texture.mag_filter);
199 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
200 &saved_texture.wrap_s);
201 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
202 &saved_texture.wrap_t);
204 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
205 saved_texture.pixels = new char[pixelssize];
207 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
208 saved_texture.pixels);
211 saved_textures.push_back(saved_texture);
213 glDeleteTextures(1, &(texture->get_handle()));
214 texture->set_handle(0);
216 assert_gl("retrieving texture for save");
220 TextureManager::reload_textures()
222 #ifdef GL_UNPACK_ROW_LENGTH
223 /* OpenGL ES doesn't support these */
224 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
225 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
226 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
227 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
228 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
230 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
232 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
233 i != saved_textures.end(); ++i) {
234 SavedTexture& saved_texture = *i;
237 glGenTextures(1, &handle);
238 assert_gl("creating texture handle");
240 glBindTexture(GL_TEXTURE_2D, handle);
241 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
242 saved_texture.width, saved_texture.height,
243 saved_texture.border, GL_RGBA,
244 GL_UNSIGNED_BYTE, saved_texture.pixels);
245 delete[] saved_texture.pixels;
246 assert_gl("uploading texture pixel data");
248 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
249 saved_texture.min_filter);
250 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
251 saved_texture.mag_filter);
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
253 saved_texture.wrap_s);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
255 saved_texture.wrap_t);
257 assert_gl("setting texture_params");
258 saved_texture.texture->set_handle(handle);
261 saved_textures.clear();