1 | // SuperTux |
2 | // Copyright (C) 2006 Matthias Braun <matze@braunis.de> |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | |
17 | #ifndef HEADER_SUPERTUX_VIDEO_TEXTURE_MANAGER_HPP |
18 | #define |
19 | |
20 | #include <config.h> |
21 | #include <map> |
22 | #include <memory> |
23 | #include <ostream> |
24 | #include <set> |
25 | #include <string> |
26 | #include <vector> |
27 | #include <boost/optional.hpp> |
28 | |
29 | #include "math/rect.hpp" |
30 | #include "util/currenton.hpp" |
31 | #include "video/sampler.hpp" |
32 | #include "video/sdl_surface_ptr.hpp" |
33 | #include "video/texture.hpp" |
34 | #include "video/texture_ptr.hpp" |
35 | |
36 | class GLTexture; |
37 | class ReaderMapping; |
38 | struct SDL_Surface; |
39 | |
40 | class TextureManager final : public Currenton<TextureManager> |
41 | { |
42 | public: |
43 | friend class Texture; |
44 | |
45 | public: |
46 | TextureManager(); |
47 | ~TextureManager(); |
48 | |
49 | TexturePtr get(const ReaderMapping& mapping, const boost::optional<Rect>& region = boost::none); |
50 | TexturePtr get(const std::string& filename); |
51 | TexturePtr get(const std::string& filename, |
52 | const boost::optional<Rect>& rect, |
53 | const Sampler& sampler = Sampler()); |
54 | |
55 | void debug_print(std::ostream& out) const; |
56 | |
57 | private: |
58 | const SDL_Surface& get_surface(const std::string& filename); |
59 | void reap_cache_entry(const Texture::Key& key); |
60 | |
61 | TexturePtr create_image_texture(const std::string& filename, const Rect& rect, const Sampler& sampler); |
62 | |
63 | /** on failure a dummy texture is returned and no exception is thrown */ |
64 | TexturePtr create_image_texture(const std::string& filename, const Sampler& sampler); |
65 | |
66 | /** throw an exception on error */ |
67 | TexturePtr create_image_texture_raw(const std::string& filename, const Sampler& sampler); |
68 | TexturePtr create_image_texture_raw(const std::string& filename, const Rect& rect, const Sampler& sampler); |
69 | |
70 | TexturePtr create_dummy_texture(); |
71 | |
72 | private: |
73 | std::map<Texture::Key, std::weak_ptr<Texture> > m_image_textures; |
74 | std::map<std::string, SDLSurfacePtr> m_surfaces; |
75 | |
76 | private: |
77 | TextureManager(const TextureManager&) = delete; |
78 | TextureManager& operator=(const TextureManager&) = delete; |
79 | }; |
80 | |
81 | #endif |
82 | |
83 | /* EOF */ |
84 | |