X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fvideo%2Ftexture_manager.cpp;h=d9ff2a74be2ee1c1c1af3fac2184980033117ff5;hb=8c205abd2d811ba36e1a3639f5bfab6263bf9077;hp=73c71fd2615e78639e37241739e2c50e69f59030;hpb=eb40ead9684fd22440c6deeaaf5c3408eead208a;p=supertux.git diff --git a/src/video/texture_manager.cpp b/src/video/texture_manager.cpp index 73c71fd26..d9ff2a74b 100644 --- a/src/video/texture_manager.cpp +++ b/src/video/texture_manager.cpp @@ -1,3 +1,22 @@ +// $Id$ +// +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// 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 2 +// 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, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + #include #include "texture_manager.hpp" @@ -5,14 +24,17 @@ #include #include #include -#include #include #include #include #include "physfs/physfs_sdl.hpp" -#include "image_texture.hpp" +#include "video_systems.hpp" +#include "gl_texture.hpp" #include "glutil.hpp" +#include "gameconfig.hpp" #include "file_system.hpp" +#include "log.hpp" +#include "texture.hpp" TextureManager* texture_manager = NULL; @@ -26,20 +48,18 @@ TextureManager::~TextureManager() i != image_textures.end(); ++i) { if(i->second == NULL) continue; -#ifdef DEBUG - std::cerr << "Warning: Texture '" << i->first << "' not freed\n"; -#endif + log_warning << "Texture '" << i->first << "' not freed" << std::endl; delete i->second; } } -ImageTexture* +Texture* TextureManager::get(const std::string& _filename) { std::string filename = FileSystem::normalize(_filename); ImageTextures::iterator i = image_textures.find(filename); - ImageTexture* texture = NULL; + Texture* texture = NULL; if(i != image_textures.end()) texture = i->second; @@ -52,77 +72,51 @@ TextureManager::get(const std::string& _filename) } void -TextureManager::release(ImageTexture* texture) +TextureManager::release(Texture* texture) { - image_textures[texture->filename] = NULL; + image_textures.erase(texture->get_filename()); delete texture; } +#ifdef HAVE_OPENGL void -TextureManager::register_texture(Texture* texture) +TextureManager::register_texture(GL::Texture* texture) { textures.insert(texture); } void -TextureManager::remove_texture(Texture* texture) +TextureManager::remove_texture(GL::Texture* texture) { textures.erase(texture); } +#endif -static inline int next_power_of_two(int val) -{ - int result = 1; - while(result < val) - result *= 2; - return result; -} - -ImageTexture* +Texture* TextureManager::create_image_texture(const std::string& filename) { SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1); - if(image == NULL) { + if(image == 0) { std::ostringstream msg; msg << "Couldn't load image '" << filename << "' :" << SDL_GetError(); throw std::runtime_error(msg.str()); } - int texture_w = next_power_of_two(image->w); - int texture_h = next_power_of_two(image->h); - -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE, - texture_w, texture_h, 32, - 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff); -#else - SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE, - texture_w, texture_h, 32, - 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); -#endif - - if(convert == 0) - throw std::runtime_error("Couldn't create texture: out of memory"); - - SDL_SetAlpha(image, 0, 0); - SDL_BlitSurface(image, 0, convert, 0); - - ImageTexture* result = NULL; + Texture* result = 0; try { - result = new ImageTexture(convert); - result->filename = filename; - result->image_width = image->w; - result->image_height = image->h; + result = new_texture(image); + result->set_filename(filename); } catch(...) { delete result; - SDL_FreeSurface(convert); + SDL_FreeSurface(image); throw; } - - SDL_FreeSurface(convert); + + SDL_FreeSurface(image); return result; } +#ifdef HAVE_OPENGL void TextureManager::save_textures() { @@ -137,12 +131,12 @@ TextureManager::save_textures() } for(ImageTextures::iterator i = image_textures.begin(); i != image_textures.end(); ++i) { - save_texture(i->second); + save_texture(dynamic_cast(i->second)); } } void -TextureManager::save_texture(Texture* texture) +TextureManager::save_texture(GL::Texture* texture) { SavedTexture saved_texture; saved_texture.texture = texture; @@ -164,16 +158,16 @@ TextureManager::save_texture(Texture* texture) 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); saved_textures.push_back(saved_texture); - glDeleteTextures(1, &(texture->handle)); - texture->handle = 0; + glDeleteTextures(1, &(texture->get_handle())); + texture->set_handle(0); - assert_gl("retrieving texture"); + assert_gl("retrieving texture for save"); } void @@ -185,11 +179,11 @@ TextureManager::reload_textures() glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - + for(std::vector::iterator i = saved_textures.begin(); i != saved_textures.end(); ++i) { SavedTexture& saved_texture = *i; - + GLuint handle; glGenTextures(1, &handle); assert_gl("creating texture handle"); @@ -212,9 +206,9 @@ TextureManager::reload_textures() saved_texture.wrap_t); assert_gl("setting texture_params"); - saved_texture.texture->handle = handle; + saved_texture.texture->set_handle(handle); } saved_textures.clear(); } - +#endif