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()
34 TextureManager::~TextureManager()
36 for(ImageTextures::iterator i = image_textures.begin();
37 i != image_textures.end(); ++i) {
40 log_warning << "Texture '" << i->first << "' not freed" << std::endl;
46 TextureManager::get(const std::string& _filename)
48 std::string filename = FileSystem::normalize(_filename);
49 ImageTextures::iterator i = image_textures.find(filename);
51 Texture* texture = NULL;
52 if(i != image_textures.end())
56 texture = create_image_texture(filename);
57 image_textures[filename] = texture;
64 TextureManager::release(Texture* texture)
66 image_textures.erase(texture->get_filename());
72 TextureManager::register_texture(GLTexture* texture)
74 textures.insert(texture);
78 TextureManager::remove_texture(GLTexture* texture)
80 textures.erase(texture);
85 TextureManager::create_image_texture(const std::string& filename)
89 SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
91 std::ostringstream msg;
92 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
93 throw std::runtime_error(msg.str());
98 result = new_texture(image);
99 result->set_filename(filename);
102 SDL_FreeSurface(image);
106 SDL_FreeSurface(image);
109 } catch (const std::runtime_error& err) {
110 const std::string dummy_texture_fname = "images/engine/missing.png";
111 if (filename == dummy_texture_fname) throw err;
113 // on error, try loading placeholder file
116 Texture* tex = create_image_texture(dummy_texture_fname);
117 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
122 // on error (when loading placeholder), try using empty surface
125 SDL_Surface* image = SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0);
132 result = new_texture(image);
133 result->set_filename("-dummy-texture-.png");
136 SDL_FreeSurface(image);
140 SDL_FreeSurface(image);
141 log_warning << "Couldn't load texture '" << filename << "' (now using empty one): " << err.what() << std::endl;
144 // on error (when trying to use empty surface), give up
145 } catch (const std::runtime_error& err) {
154 TextureManager::save_textures()
156 #ifdef GL_PACK_ROW_LENGTH
157 /* all this stuff is not support by OpenGL ES */
158 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
159 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
160 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
161 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
162 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
165 glPixelStorei(GL_PACK_ALIGNMENT, 1);
166 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
169 for(ImageTextures::iterator i = image_textures.begin();
170 i != image_textures.end(); ++i) {
171 save_texture(dynamic_cast<GLTexture *>(i->second));
176 TextureManager::save_texture(GLTexture* texture)
178 SavedTexture saved_texture;
179 saved_texture.texture = texture;
180 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
182 //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
183 #ifndef GL_VERSION_ES_CM_1_0
184 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
185 &saved_texture.width);
186 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
187 &saved_texture.height);
188 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
189 &saved_texture.border);
190 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
191 &saved_texture.min_filter);
192 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
193 &saved_texture.mag_filter);
194 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
195 &saved_texture.wrap_s);
196 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
197 &saved_texture.wrap_t);
199 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
200 saved_texture.pixels = new char[pixelssize];
202 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
203 saved_texture.pixels);
206 saved_textures.push_back(saved_texture);
208 glDeleteTextures(1, &(texture->get_handle()));
209 texture->set_handle(0);
211 assert_gl("retrieving texture for save");
215 TextureManager::reload_textures()
217 #ifdef GL_UNPACK_ROW_LENGTH
218 /* OpenGL ES doesn't support these */
219 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
220 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
221 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
222 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
223 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
225 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
227 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
228 i != saved_textures.end(); ++i) {
229 SavedTexture& saved_texture = *i;
232 glGenTextures(1, &handle);
233 assert_gl("creating texture handle");
235 glBindTexture(GL_TEXTURE_2D, handle);
236 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
237 saved_texture.width, saved_texture.height,
238 saved_texture.border, GL_RGBA,
239 GL_UNSIGNED_BYTE, saved_texture.pixels);
240 delete[] saved_texture.pixels;
241 assert_gl("uploading texture pixel data");
243 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
244 saved_texture.min_filter);
245 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
246 saved_texture.mag_filter);
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
248 saved_texture.wrap_s);
249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
250 saved_texture.wrap_t);
252 assert_gl("setting texture_params");
253 saved_texture.texture->set_handle(handle);
256 saved_textures.clear();