/
texture_manager.cpp
347 lines (304 loc) · 9.72 KB
/
texture_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// SuperTux
// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "video/texture_manager.hpp"
#include <SDL_image.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include "math/rect.hpp"
#include "physfs/physfs_sdl.hpp"
#include "util/file_system.hpp"
#include "util/log.hpp"
#include "video/sdl_surface_ptr.hpp"
#include "video/texture.hpp"
#include "video/video_systems.hpp"
#ifdef HAVE_OPENGL
#include "video/gl/gl_texture.hpp"
#endif
TextureManager::TextureManager() :
image_textures()
,surfaces()
#ifdef HAVE_OPENGL
,textures(),
saved_textures()
#endif
{
}
TextureManager::~TextureManager()
{
for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i)
{
if(!i->second.expired())
{
log_warning << "Texture '" << i->first << "' not freed" << std::endl;
}
}
image_textures.clear();
for(auto& surface : surfaces)
{
SDL_FreeSurface(surface.second);
}
surfaces.clear();
}
TexturePtr
TextureManager::get(const std::string& _filename)
{
std::string filename = FileSystem::normalize(_filename);
ImageTextures::iterator i = image_textures.find(filename);
TexturePtr texture;
if(i != image_textures.end())
texture = i->second.lock();
if(!texture) {
texture = create_image_texture(filename);
texture->cache_filename = filename;
image_textures[filename] = texture;
}
return texture;
}
TexturePtr
TextureManager::get(const std::string& _filename, const Rect& rect)
{
std::string filename = FileSystem::normalize(_filename);
// FIXME: implement caching
return create_image_texture(filename, rect);
}
void
TextureManager::reap_cache_entry(const std::string& filename)
{
ImageTextures::iterator i = image_textures.find(filename);
assert(i != image_textures.end());
assert(i->second.expired());
image_textures.erase(i);
}
#ifdef HAVE_OPENGL
void
TextureManager::register_texture(GLTexture* texture)
{
textures.insert(texture);
}
void
TextureManager::remove_texture(GLTexture* texture)
{
textures.erase(texture);
}
#endif
TexturePtr
TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
{
try
{
return create_image_texture_raw(filename, rect);
}
catch(const std::exception& err)
{
log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
return create_dummy_texture();
}
}
TexturePtr
TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
{
SDL_Surface *image = nullptr;
Surfaces::iterator i = surfaces.find(filename);
if (i != surfaces.end())
{
image = i->second;
}
else
{
image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
if (!image)
{
std::ostringstream msg;
msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
throw std::runtime_error(msg.str());
}
surfaces[filename] = image;
}
SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
rect.top * image->pitch +
rect.left * image->format->BytesPerPixel,
rect.get_width(), rect.get_height(),
image->format->BitsPerPixel,
image->pitch,
image->format->Rmask,
image->format->Gmask,
image->format->Bmask,
image->format->Amask));
if (!subimage)
{
throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
}
#ifdef OLD_SDL
if (image->format->palette)
{ // copy the image palette to subimage if present
SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors);
}
#endif
return VideoSystem::new_texture(subimage.get());
}
TexturePtr
TextureManager::create_image_texture(const std::string& filename)
{
try
{
return create_image_texture_raw(filename);
}
catch (const std::exception& err)
{
log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
return create_dummy_texture();
}
}
TexturePtr
TextureManager::create_image_texture_raw(const std::string& filename)
{
SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
if (!image)
{
std::ostringstream msg;
msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
throw std::runtime_error(msg.str());
}
else
{
TexturePtr texture = VideoSystem::new_texture(image.get());
image.reset(NULL);
return texture;
}
}
TexturePtr
TextureManager::create_dummy_texture()
{
const std::string dummy_texture_fname = "images/engine/missing.png";
// on error, try loading placeholder file
try
{
TexturePtr tex = create_image_texture_raw(dummy_texture_fname);
return tex;
}
catch (const std::exception& err)
{
// on error (when loading placeholder), try using empty surface,
// when that fails to, just give up
SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
if (!image)
{
throw err;
}
else
{
log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
TexturePtr texture = VideoSystem::new_texture(image.get());
image.reset(NULL);
return texture;
}
}
}
#ifdef HAVE_OPENGL
void
TextureManager::save_textures()
{
#ifdef GL_PACK_ROW_LENGTH
/* all this stuff is not support by OpenGL ES */
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
#endif
glPixelStorei(GL_PACK_ALIGNMENT, 1);
for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
save_texture(*i);
}
for(ImageTextures::iterator i = image_textures.begin();
i != image_textures.end(); ++i) {
save_texture(dynamic_cast<GLTexture*>(i->second.lock().get()));
}
}
void
TextureManager::save_texture(GLTexture* texture)
{
SavedTexture saved_texture;
saved_texture.texture = texture;
glBindTexture(GL_TEXTURE_2D, texture->get_handle());
//this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
#ifndef GL_VERSION_ES_CM_1_0
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
&saved_texture.width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
&saved_texture.height);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
&saved_texture.border);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
&saved_texture.min_filter);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
&saved_texture.mag_filter);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
&saved_texture.wrap_s);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
&saved_texture.wrap_t);
size_t pixelssize = saved_texture.width * saved_texture.height * 4;
saved_texture.pixels = new char[pixelssize];
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
saved_texture.pixels);
#endif
saved_textures.push_back(saved_texture);
glDeleteTextures(1, &(texture->get_handle()));
texture->set_handle(0);
assert_gl("retrieving texture for save");
}
void
TextureManager::reload_textures()
{
#ifdef GL_UNPACK_ROW_LENGTH
/* OpenGL ES doesn't support these */
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
#endif
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
i != saved_textures.end(); ++i) {
SavedTexture& saved_texture = *i;
GLuint handle;
glGenTextures(1, &handle);
assert_gl("creating texture handle");
glBindTexture(GL_TEXTURE_2D, handle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
saved_texture.width, saved_texture.height,
saved_texture.border, GL_RGBA,
GL_UNSIGNED_BYTE, saved_texture.pixels);
delete[] saved_texture.pixels;
assert_gl("uploading texture pixel data");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
saved_texture.min_filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
saved_texture.mag_filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
saved_texture.wrap_s);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
saved_texture.wrap_t);
assert_gl("setting texture_params");
saved_texture.texture->set_handle(handle);
}
saved_textures.clear();
}
#endif
/* EOF */