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/Texture.hpp>
7 #include <unison/video/backend/Texture.hpp>
15 std::set<Texture *> Texture::textures = std::set<Texture*>();
18 id(INVALID_TEXTURE_ID)
22 Texture::Texture(const std::string &filename) :
23 id(Backend::Texture::get_texture_id(filename))
25 assert(id != INVALID_TEXTURE_ID);
26 Backend::Texture::get_texture(id)->ref();
27 textures.insert(this);
30 Texture::Texture(const std::string &filename, const Color &colorkey) :
31 id(Backend::Texture::get_texture_id(filename, colorkey))
33 assert(id != INVALID_TEXTURE_ID);
34 Backend::Texture::get_texture(id)->ref();
35 textures.insert(this);
38 Texture::Texture(const Surface &surface) :
39 id(Backend::Texture::get_texture_id(surface))
41 assert(id != INVALID_TEXTURE_ID);
42 Backend::Texture::get_texture(id)->ref();
43 textures.insert(this);
46 Texture::Texture(const Texture &rhs) :
50 if(id != INVALID_TEXTURE_ID)
52 Backend::Texture::get_texture(id)->ref();
54 textures.insert(this);
60 if(id != INVALID_TEXTURE_ID)
62 Backend::Texture::get_texture(id)->unref();
66 Texture &Texture::operator =(const Texture &rhs)
68 if(rhs.id != INVALID_TEXTURE_ID)
70 Backend::Texture::get_texture(rhs.id)->ref();
72 if(id != INVALID_TEXTURE_ID)
74 Backend::Texture::get_texture(id)->unref();
80 TextureID Texture::get_id() const
85 Area Texture::get_size() const
87 assert(id != INVALID_TEXTURE_ID);
88 return Backend::Texture::get_texture(id)->get_size();
91 void Texture::blit(const Surface &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options)
93 assert(id != INVALID_TEXTURE_ID);
95 Backend::Texture::get_texture(id)->blit(src, dst_pos, src_rect, options);
98 void Texture::blit(const Texture &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options)
100 assert(id != INVALID_TEXTURE_ID);
102 Backend::Texture::get_texture(id)->blit(src, dst_pos, src_rect, options);
105 void Texture::fill(const Color &color, const Rect &rect)
107 assert(id != INVALID_TEXTURE_ID);
109 Backend::Texture::get_texture(id)->fill(color, rect);
112 void Texture::fill_blend(const Color &color, const Rect &rect)
114 assert(id != INVALID_TEXTURE_ID);
116 Backend::Texture::get_texture(id)->fill_blend(color, rect);
119 std::vector<Surface> Texture::save_textures()
121 recover_texture_ids();
122 return Backend::Texture::save_textures();
127 void ref(Texture *texture)
130 TextureID id = texture->get_id();
131 if(id != INVALID_TEXTURE_ID)
133 Backend::Texture::get_texture(id)->ref();
138 void Texture::load_textures(const std::vector<Surface> &surfaces)
140 Backend::Texture::load_textures(surfaces);
141 std::for_each(textures.begin(), textures.end(), ref);
144 void Texture::recover_texture_ids()
146 std::map<TextureID, TextureID> change_map = Backend::Texture::recover_texture_ids();
147 if(!change_map.empty())
149 for(std::set<Texture *>::iterator iter = textures.begin(), end = textures.end();iter != end;++iter)
151 if(change_map.find((*iter)->id) != change_map.end())
153 (*iter)->id = change_map[(*iter)->id];
161 assert(id != INVALID_TEXTURE_ID);
162 if(Backend::Texture::get_texture(id)->get_refcount() > 1)
165 id = Backend::Texture::get_texture_id(Backend::Texture::get_texture(id)->get_surface());
166 assert(id != INVALID_TEXTURE_ID);
167 Backend::Texture::get_texture(id)->ref();
168 Backend::Texture::get_texture(old)->unref();