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"
38 TextureManager* texture_manager = NULL;
40 TextureManager::TextureManager()
44 TextureManager::~TextureManager()
46 for(ImageTextures::iterator i = image_textures.begin();
47 i != image_textures.end(); ++i) {
50 log_warning << "Texture '" << i->first << "' not freed" << std::endl;
56 TextureManager::get(const std::string& _filename)
58 std::string filename = FileSystem::normalize(_filename);
59 ImageTextures::iterator i = image_textures.find(filename);
61 Texture* texture = NULL;
62 if(i != image_textures.end())
66 texture = create_image_texture(filename);
67 image_textures[filename] = texture;
74 TextureManager::release(Texture* texture)
76 image_textures.erase(texture->get_filename());
82 TextureManager::register_texture(GL::Texture* texture)
84 textures.insert(texture);
88 TextureManager::remove_texture(GL::Texture* texture)
90 textures.erase(texture);
95 TextureManager::create_image_texture(const std::string& filename)
97 SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
99 std::ostringstream msg;
100 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
101 throw std::runtime_error(msg.str());
106 result = new_texture(image);
107 result->set_filename(filename);
110 SDL_FreeSurface(image);
114 SDL_FreeSurface(image);
120 TextureManager::save_textures()
122 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
123 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
124 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
125 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
126 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
127 glPixelStorei(GL_PACK_ALIGNMENT, 1);
128 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
131 for(ImageTextures::iterator i = image_textures.begin();
132 i != image_textures.end(); ++i) {
133 save_texture(dynamic_cast<GL::Texture *>(i->second));
138 TextureManager::save_texture(GL::Texture* texture)
140 SavedTexture saved_texture;
141 saved_texture.texture = texture;
142 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
143 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
144 &saved_texture.width);
145 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
146 &saved_texture.height);
147 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
148 &saved_texture.border);
149 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
150 &saved_texture.min_filter);
151 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
152 &saved_texture.mag_filter);
153 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
154 &saved_texture.wrap_s);
155 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
156 &saved_texture.wrap_t);
158 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
159 saved_texture.pixels = new char[pixelssize];
161 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
162 saved_texture.pixels);
164 saved_textures.push_back(saved_texture);
166 glDeleteTextures(1, &(texture->get_handle()));
167 texture->set_handle(0);
169 assert_gl("retrieving texture for save");
173 TextureManager::reload_textures()
175 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
176 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
177 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
178 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
179 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
180 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
182 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
183 i != saved_textures.end(); ++i) {
184 SavedTexture& saved_texture = *i;
187 glGenTextures(1, &handle);
188 assert_gl("creating texture handle");
190 glBindTexture(GL_TEXTURE_2D, handle);
191 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
192 saved_texture.width, saved_texture.height,
193 saved_texture.border, GL_RGBA,
194 GL_UNSIGNED_BYTE, saved_texture.pixels);
195 delete[] saved_texture.pixels;
196 assert_gl("uploading texture pixel data");
198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
199 saved_texture.min_filter);
200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
201 saved_texture.mag_filter);
202 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
203 saved_texture.wrap_s);
204 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
205 saved_texture.wrap_t);
207 assert_gl("setting texture_params");
208 saved_texture.texture->set_handle(handle);
211 saved_textures.clear();