4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "texture_manager.hpp"
26 #include <SDL_image.h>
30 #include "physfs/physfs_sdl.hpp"
31 #include "video_systems.hpp"
32 #include "gl_texture.hpp"
34 #include "gameconfig.hpp"
35 #include "file_system.hpp"
37 #include "texture.hpp"
39 TextureManager* texture_manager = NULL;
41 TextureManager::TextureManager()
45 TextureManager::~TextureManager()
47 for(ImageTextures::iterator i = image_textures.begin();
48 i != image_textures.end(); ++i) {
51 log_warning << "Texture '" << i->first << "' not freed" << std::endl;
57 TextureManager::get(const std::string& _filename)
59 std::string filename = FileSystem::normalize(_filename);
60 ImageTextures::iterator i = image_textures.find(filename);
62 Texture* texture = NULL;
63 if(i != image_textures.end())
67 texture = create_image_texture(filename);
68 image_textures[filename] = texture;
75 TextureManager::release(Texture* texture)
77 image_textures.erase(texture->get_filename());
83 TextureManager::register_texture(GL::Texture* texture)
85 textures.insert(texture);
89 TextureManager::remove_texture(GL::Texture* texture)
91 textures.erase(texture);
96 TextureManager::create_image_texture(const std::string& filename)
98 SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
100 std::ostringstream msg;
101 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
102 throw std::runtime_error(msg.str());
107 result = new_texture(image);
108 result->set_filename(filename);
111 SDL_FreeSurface(image);
115 SDL_FreeSurface(image);
121 TextureManager::save_textures()
123 #ifdef GL_PACK_ROW_LENGTH
124 /* all this stuff is not support by OpenGL ES */
125 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
126 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
127 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
128 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
129 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
132 glPixelStorei(GL_PACK_ALIGNMENT, 1);
133 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
136 for(ImageTextures::iterator i = image_textures.begin();
137 i != image_textures.end(); ++i) {
138 save_texture(dynamic_cast<GL::Texture *>(i->second));
143 TextureManager::save_texture(GL::Texture* texture)
145 SavedTexture saved_texture;
146 saved_texture.texture = texture;
147 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
148 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
149 &saved_texture.width);
150 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
151 &saved_texture.height);
152 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
153 &saved_texture.border);
154 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
155 &saved_texture.min_filter);
156 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
157 &saved_texture.mag_filter);
158 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
159 &saved_texture.wrap_s);
160 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
161 &saved_texture.wrap_t);
163 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
164 saved_texture.pixels = new char[pixelssize];
166 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
167 saved_texture.pixels);
169 saved_textures.push_back(saved_texture);
171 glDeleteTextures(1, &(texture->get_handle()));
172 texture->set_handle(0);
174 assert_gl("retrieving texture for save");
178 TextureManager::reload_textures()
180 #ifdef GL_UNPACK_ROW_LENGTH
181 /* OpenGL ES doesn't support these */
182 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
183 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
184 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
185 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
186 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
188 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
190 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
191 i != saved_textures.end(); ++i) {
192 SavedTexture& saved_texture = *i;
195 glGenTextures(1, &handle);
196 assert_gl("creating texture handle");
198 glBindTexture(GL_TEXTURE_2D, handle);
199 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
200 saved_texture.width, saved_texture.height,
201 saved_texture.border, GL_RGBA,
202 GL_UNSIGNED_BYTE, saved_texture.pixels);
203 delete[] saved_texture.pixels;
204 assert_gl("uploading texture pixel data");
206 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
207 saved_texture.min_filter);
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
209 saved_texture.mag_filter);
210 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
211 saved_texture.wrap_s);
212 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
213 saved_texture.wrap_t);
215 assert_gl("setting texture_params");
216 saved_texture.texture->set_handle(handle);
219 saved_textures.clear();