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