1 // Copyright Timothy Goya 2007.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #include <unison/video/backend/Texture.hpp>
7 #include <unison/video/Renderers.hpp>
8 #include <unison/video/backend/Renderer.hpp>
19 std::vector<Texture *> Texture::textures = std::vector<Texture *>();
20 std::map<std::string, TextureID> Texture::named_textures = std::map<std::string, TextureID>();
22 Texture::Texture(const Surface &surface) :
24 size(surface.get_size()),
27 assert(surface.get_size() != Area());
34 Area Texture::get_size()
50 std::string name = get_name(get_texture_id(this));
51 *std::find(textures.begin(), textures.end(), this) = 0;
54 named_textures.erase(name);
60 int Texture::get_refcount()
70 void operator () (Texture *texture)
75 surfaces.push_back(texture->get_surface());
79 surfaces.push_back(Surface());
83 std::vector<Surface> surfaces;
89 void operator () (Surface surface)
91 if(surface.get_size() != Area())
93 textures.push_back(Renderers::get().get_renderer().create_texture(surface));
97 textures.push_back(0);
100 std::vector<Texture *> textures;
104 std::vector<Surface> Texture::save_textures()
106 std::vector<Surface> surfaces = std::for_each(textures.begin(), textures.end(), TextureSaver()).surfaces;
111 void Texture::load_textures(const std::vector<Surface> &surfaces)
113 assert(textures.empty());
114 textures = std::for_each(surfaces.begin(), surfaces.end(), TextureLoader()).textures;
117 std::map<TextureID, TextureID> Texture::recover_texture_ids()
119 std::map<TextureID, TextureID> change_map;
120 std::vector<Texture *> new_textures;
121 bool null_texture_found = false;
122 for(std::vector<Texture *>::iterator iter = textures.begin(), end = textures.end();iter != end;++iter)
126 new_textures.push_back(*iter);
127 if(null_texture_found)
129 change_map[iter - textures.begin()] = new_textures.size() -1;
134 null_texture_found = true;
137 textures = new_textures;
141 TextureID Texture::get_texture_id(const std::string &filename)
143 if(named_textures.find(filename) != named_textures.end())
145 return named_textures[filename];
147 textures.push_back(Renderers::get().get_renderer().create_texture(Surface(filename)));
148 named_textures[filename] = textures.size() - 1;
149 return textures.size() - 1;
152 TextureID Texture::get_texture_id(const std::string &filename, const Color&colorkey)
154 if(named_textures.find(filename) != named_textures.end())
156 return named_textures[filename];
158 textures.push_back(Renderers::get().get_renderer().create_texture(Surface(filename, colorkey)));
159 named_textures[filename] = textures.size() - 1;
160 return textures.size() - 1;
163 TextureID Texture::get_texture_id(const Surface &surface)
165 textures.push_back(Renderers::get().get_renderer().create_texture(surface));
166 return textures.size() - 1;
169 TextureID Texture::get_texture_id(Texture *texture)
171 std::vector<Texture *>::iterator found = std::find(textures.begin(), textures.end(), texture);
172 if(found != textures.end())
174 return found - textures.begin();
176 return INVALID_TEXTURE_ID;
179 Texture *Texture::get_texture(TextureID id)
181 assert(id < textures.size());
182 assert(textures[id]);
188 bool match_id(std::pair<std::string, TextureID> pair, TextureID id)
190 return pair.second == id;
194 std::string Texture::get_name(TextureID id)
196 if(id == INVALID_TEXTURE_ID)
198 return std::string();
200 std::map<std::string, TextureID>::iterator found = std::find_if(named_textures.begin(), named_textures.end(), std::bind2nd(std::ptr_fun(match_id), id));
201 if(found == named_textures.end())
203 return std::string();