1 | // SuperTux |
2 | // Copyright (C) 2016 Ingo Ruhnke <grumbel@gmail.com> |
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_TTF_SURFACE_MANAGER_HPP |
18 | #define |
19 | |
20 | #include <tuple> |
21 | #include <map> |
22 | #include <string> |
23 | #include <iosfwd> |
24 | |
25 | #include "util/currenton.hpp" |
26 | #include "video/color.hpp" |
27 | #include "video/surface_ptr.hpp" |
28 | #include "video/ttf_surface.hpp" |
29 | |
30 | class TTFFont; |
31 | |
32 | class TTFSurfaceManager final : public Currenton<TTFSurfaceManager> |
33 | { |
34 | public: |
35 | TTFSurfaceManager(); |
36 | |
37 | TTFSurfacePtr create_surface(const TTFFont& font, const std::string& text); |
38 | |
39 | void print_debug_info(std::ostream& out); |
40 | |
41 | private: |
42 | void cache_cleanup_step(); |
43 | |
44 | private: |
45 | struct CacheEntry |
46 | { |
47 | CacheEntry() : ttf_surface(), last_access() {} |
48 | CacheEntry(const TTFSurfacePtr& s); |
49 | |
50 | TTFSurfacePtr ttf_surface; |
51 | float last_access; |
52 | }; |
53 | |
54 | private: |
55 | using Key = std::tuple<void*, std::string>; |
56 | std::map<Key, CacheEntry> m_cache; |
57 | |
58 | std::map<Key, CacheEntry>::iterator m_cache_iter; |
59 | |
60 | private: |
61 | TTFSurfaceManager(const TTFSurfaceManager&) = delete; |
62 | TTFSurfaceManager& operator=(const TTFSurfaceManager&) = delete; |
63 | }; |
64 | |
65 | #endif |
66 | |
67 | /* EOF */ |
68 | |