Precalculated renderer specific surface data, better management of multiple renderers...
[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
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     result = new_texture(image);
107     result->set_filename(filename);
108   } catch(...) {
109     delete result;
110     SDL_FreeSurface(image);
111     throw;
112   }
113
114   SDL_FreeSurface(image);
115   return result;
116 }
117
118 #ifdef HAVE_OPENGL
119 void
120 TextureManager::save_textures()
121 {
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) {
129     save_texture(*i);
130   }
131   for(ImageTextures::iterator i = image_textures.begin();
132       i != image_textures.end(); ++i) {
133     save_texture(dynamic_cast<GL::Texture *>(i->second));
134   }
135 }
136
137 void
138 TextureManager::save_texture(GL::Texture* texture)
139 {
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);
157
158   size_t pixelssize = saved_texture.width * saved_texture.height * 4;
159   saved_texture.pixels = new char[pixelssize];
160
161   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
162                 saved_texture.pixels);
163
164   saved_textures.push_back(saved_texture);
165
166   glDeleteTextures(1, &(texture->get_handle()));
167   texture->set_handle(0);
168
169   assert_gl("retrieving texture for save");
170 }
171
172 void
173 TextureManager::reload_textures()
174 {
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);
181
182   for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
183       i != saved_textures.end(); ++i) {
184     SavedTexture& saved_texture = *i;
185
186     GLuint handle;
187     glGenTextures(1, &handle);
188     assert_gl("creating texture handle");
189
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");
197
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);
206
207     assert_gl("setting texture_params");
208     saved_texture.texture->set_handle(handle);
209   }
210
211   saved_textures.clear();
212 }
213 #endif