supertux
texture_manager.hpp
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 HEADER_SUPERTUX_VIDEO_TEXTURE_MANAGER_HPP
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 <optional>
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  friend class Texture;
43 
44 private:
45  static const std::string s_dummy_texture;
46 
47 public:
49  ~TextureManager() override;
50 
51  TexturePtr get(const ReaderMapping& mapping, const std::optional<Rect>& region = std::nullopt);
52  TexturePtr get(const std::string& filename);
53  TexturePtr get(const std::string& filename,
54  const std::optional<Rect>& rect,
55  const Sampler& sampler = Sampler());
56  TexturePtr create_dummy_texture();
57 
58  void debug_print(std::ostream& out) const;
59 
60  bool last_load_successful() const { return m_load_successful; }
61 
62 private:
63  const SDL_Surface& get_surface(const std::string& filename);
64  void reap_cache_entry(const Texture::Key& key);
65 
66  TexturePtr create_image_texture(const std::string& filename, const Rect& rect, const Sampler& sampler);
67 
69  TexturePtr create_image_texture(const std::string& filename, const Sampler& sampler);
70 
72  TexturePtr create_image_texture_raw(const std::string& filename, const Sampler& sampler);
73  TexturePtr create_image_texture_raw(const std::string& filename, const Rect& rect, const Sampler& sampler);
74 
75 private:
76  std::map<Texture::Key, std::weak_ptr<Texture> > m_image_textures;
77  std::map<std::string, SDLSurfacePtr> m_surfaces;
78  bool m_load_successful;
79 
80 private:
81  TextureManager(const TextureManager&) = delete;
82  TextureManager& operator=(const TextureManager&) = delete;
83 };
84 
85 #endif
86 
87 /* EOF */
Definition: texture_manager.hpp:40
Definition: sampler.hpp:23
Definition: rect.hpp:30
This class is a wrapper around a texture handle.
Definition: texture.hpp:30
std::tuple< std::string, Rect > Key
filename, left, top, right, bottom
Definition: texture.hpp:36
A &#39;Currenton&#39; allows access to the currently active instance of a class via the static current() func...
Definition: currenton.hpp:30
This class is a wrapper around a texture handle.
Definition: gl_texture.hpp:32
Definition: reader_mapping.hpp:32