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>
23 #include "physfs/physfs_sdl.hpp"
24 #include "util/file_system.hpp"
25 #include "util/log.hpp"
26 #include "video/gl/gl_texture.hpp"
27 #include "video/sdl_surface_ptr.hpp"
28 #include "video/video_systems.hpp"
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::get(const std::string& filename, const Rect& rect)
75 TextureManager::release(Texture* texture)
77 image_textures.erase(texture->get_filename());
83 TextureManager::register_texture(GLTexture* texture)
85 textures.insert(texture);
89 TextureManager::remove_texture(GLTexture* texture)
91 textures.erase(texture);
96 TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
98 assert(!"not implemented");
103 TextureManager::create_image_texture(const std::string& filename)
107 return create_image_texture_raw(filename);
109 catch (const std::exception& err)
111 log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
112 Texture* texture = create_dummy_texture();
118 TextureManager::create_image_texture_raw(const std::string& filename)
120 SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
123 std::ostringstream msg;
124 msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
125 throw std::runtime_error(msg.str());
129 Texture* result = VideoSystem::new_texture(image.get());
130 result->set_filename(filename);
136 TextureManager::create_dummy_texture()
138 const std::string dummy_texture_fname = "images/engine/missing.png";
140 // on error, try loading placeholder file
143 Texture* tex = create_image_texture_raw(dummy_texture_fname);
146 catch (const std::exception& err)
148 // on error (when loading placeholder), try using empty surface,
149 // when that fails to, just give up
150 SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
157 Texture* result = VideoSystem::new_texture(image.get());
158 result->set_filename("-dummy-texture-.png");
159 log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
167 TextureManager::save_textures()
169 #ifdef GL_PACK_ROW_LENGTH
170 /* all this stuff is not support by OpenGL ES */
171 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
172 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
173 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
174 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
175 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
178 glPixelStorei(GL_PACK_ALIGNMENT, 1);
179 for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
182 for(ImageTextures::iterator i = image_textures.begin();
183 i != image_textures.end(); ++i) {
184 save_texture(dynamic_cast<GLTexture *>(i->second));
189 TextureManager::save_texture(GLTexture* texture)
191 SavedTexture saved_texture;
192 saved_texture.texture = texture;
193 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
195 //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
196 #ifndef GL_VERSION_ES_CM_1_0
197 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
198 &saved_texture.width);
199 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
200 &saved_texture.height);
201 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
202 &saved_texture.border);
203 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
204 &saved_texture.min_filter);
205 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
206 &saved_texture.mag_filter);
207 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
208 &saved_texture.wrap_s);
209 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
210 &saved_texture.wrap_t);
212 size_t pixelssize = saved_texture.width * saved_texture.height * 4;
213 saved_texture.pixels = new char[pixelssize];
215 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
216 saved_texture.pixels);
219 saved_textures.push_back(saved_texture);
221 glDeleteTextures(1, &(texture->get_handle()));
222 texture->set_handle(0);
224 assert_gl("retrieving texture for save");
228 TextureManager::reload_textures()
230 #ifdef GL_UNPACK_ROW_LENGTH
231 /* OpenGL ES doesn't support these */
232 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
233 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
234 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
235 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
236 glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
238 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
240 for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
241 i != saved_textures.end(); ++i) {
242 SavedTexture& saved_texture = *i;
245 glGenTextures(1, &handle);
246 assert_gl("creating texture handle");
248 glBindTexture(GL_TEXTURE_2D, handle);
249 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
250 saved_texture.width, saved_texture.height,
251 saved_texture.border, GL_RGBA,
252 GL_UNSIGNED_BYTE, saved_texture.pixels);
253 delete[] saved_texture.pixels;
254 assert_gl("uploading texture pixel data");
256 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
257 saved_texture.min_filter);
258 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
259 saved_texture.mag_filter);
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
261 saved_texture.wrap_s);
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
263 saved_texture.wrap_t);
265 assert_gl("setting texture_params");
266 saved_texture.texture->set_handle(handle);
269 saved_textures.clear();