7b556a64244442a0e7e3d31adffce28bcb938748
[supertux.git] / src / video / texture_manager.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "texture_manager.hpp"
23
24 #include <assert.h>
25 #include <SDL.h>
26 #include <SDL_image.h>
27 #include <iostream>
28 #include <sstream>
29 #include <stdexcept>
30 #include "physfs/physfs_sdl.hpp"
31 #include "gl_texture.hpp"
32 #include "sdl_texture.hpp"
33 #include "glutil.hpp"
34 #include "gameconfig.hpp"
35 #include "file_system.hpp"
36 #include "log.hpp"
37
38 TextureManager* texture_manager = NULL;
39
40 TextureManager::TextureManager()
41 {
42 }
43
44 TextureManager::~TextureManager()
45 {
46   for(ImageTextures::iterator i = image_textures.begin();
47       i != image_textures.end(); ++i) {
48     if(i->second == NULL)
49       continue;
50     log_warning << "Texture '" << i->first << "' not freed" << std::endl;
51     delete i->second;
52   }
53 }
54
55 Texture*
56 TextureManager::get(const std::string& _filename)
57 {
58   std::string filename = FileSystem::normalize(_filename);
59   ImageTextures::iterator i = image_textures.find(filename);
60
61   Texture* texture = NULL;
62   if(i != image_textures.end())
63     texture = i->second;
64
65   if(texture == NULL) {
66     texture = create_image_texture(filename);
67     image_textures[filename] = texture;
68   }
69
70   return texture;
71 }
72
73 void
74 TextureManager::release(Texture* texture)
75 {
76   image_textures.erase(texture->get_filename());
77   delete texture;
78 }
79
80 #ifdef HAVE_OPENGL
81 void
82 TextureManager::register_texture(GL::Texture* texture)
83 {
84   textures.insert(texture);
85 }
86
87 void
88 TextureManager::remove_texture(GL::Texture* texture)
89 {
90   textures.erase(texture);
91 }
92 #endif
93
94 Texture*
95 TextureManager::create_image_texture(const std::string& filename)
96 {
97   SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
98   if(image == 0) {
99     std::ostringstream msg;
100     msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
101     throw std::runtime_error(msg.str());
102   }
103
104   Texture* result = 0;
105   try {
106 #ifdef HAVE_OPENGL
107     if(config->video == "opengl")
108     {
109       result = new GL::Texture(image);
110     }
111     else
112 #endif
113     {
114       result = new SDL::Texture(image);
115     }
116     result->set_filename(filename);
117   } catch(...) {
118     delete result;
119     SDL_FreeSurface(image);
120     throw;
121   }
122
123   SDL_FreeSurface(image);
124   return result;
125 }
126
127 #ifdef HAVE_OPENGL
128 void
129 TextureManager::save_textures()
130 {
131   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
132   glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
133   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
134   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
135   glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
136   glPixelStorei(GL_PACK_ALIGNMENT, 1);
137   for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
138     save_texture(*i);
139   }
140   for(ImageTextures::iterator i = image_textures.begin();
141       i != image_textures.end(); ++i) {
142     save_texture(dynamic_cast<GL::Texture *>(i->second));
143   }
144 }
145
146 void
147 TextureManager::save_texture(GL::Texture* texture)
148 {
149   SavedTexture saved_texture;
150   saved_texture.texture = texture;
151   glBindTexture(GL_TEXTURE_2D, texture->get_handle());
152   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
153                            &saved_texture.width);
154   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
155                            &saved_texture.height);
156   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
157                            &saved_texture.border);
158   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
159                       &saved_texture.min_filter);
160   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
161                       &saved_texture.mag_filter);
162   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
163                       &saved_texture.wrap_s);
164   glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
165                       &saved_texture.wrap_t);
166
167   size_t pixelssize = saved_texture.width * saved_texture.height * 4;
168   saved_texture.pixels = new char[pixelssize];
169
170   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
171                 saved_texture.pixels);
172
173   saved_textures.push_back(saved_texture);
174
175   glDeleteTextures(1, &(texture->get_handle()));
176   texture->set_handle(0);
177
178   assert_gl("retrieving texture for save");
179 }
180
181 void
182 TextureManager::reload_textures()
183 {
184   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
185   glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
186   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
187   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
188   glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
189   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
190
191   for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
192       i != saved_textures.end(); ++i) {
193     SavedTexture& saved_texture = *i;
194
195     GLuint handle;
196     glGenTextures(1, &handle);
197     assert_gl("creating texture handle");
198
199     glBindTexture(GL_TEXTURE_2D, handle);
200     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
201                  saved_texture.width, saved_texture.height,
202                  saved_texture.border, GL_RGBA,
203                  GL_UNSIGNED_BYTE, saved_texture.pixels);
204     delete[] saved_texture.pixels;
205     assert_gl("uploading texture pixel data");
206
207     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
208                     saved_texture.min_filter);
209     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
210                     saved_texture.mag_filter);
211     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
212                     saved_texture.wrap_s);
213     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
214                     saved_texture.wrap_t);
215
216     assert_gl("setting texture_params");
217     saved_texture.texture->set_handle(handle);
218   }
219
220   saved_textures.clear();
221 }
222 #endif