ST_engine  0.3-ALPHA
font_cache.hpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #ifndef FONT_CACHE_DEF
11 #define FONT_CACHE_DEF
12 
13 #include <list>
14 #include <tuple>
15 #include <ST_util/bytell_hash_map.hpp>
16 #include <SDL_system.h>
17 
18 #ifndef CACHEHASH_DEF
19 #define CACHEHASH_DEF
20 
21 typedef std::tuple<std::string, uint16_t> font_cache_tuple;
22 namespace std {
23 
25  template<>
26  struct hash<font_cache_tuple> {
27  std::size_t operator()(const font_cache_tuple &k) const {
28  using std::uint16_t;
29  using std::hash;
30  using std::string;
31  return ((hash<string>()(std::get<0>(k)) ^ (hash<uint16_t>()(std::get<1>(k)) << 1)) >> 1);
32  }
33  };
34 }
35 
36 //redefine the equals operator for two font caches tuples <string, string, int>
37 bool operator==(const std::tuple<std::string, std::string, int> &tpl1,
38  const std::tuple<std::string, std::string, int> &tpl2);
39 
40 //A few typedefs to make working with these types easier.
41 typedef std::pair<font_cache_tuple, SDL_Texture *> key_pair;
42 typedef std::list<key_pair> cache_list;
43 typedef ska::bytell_hash_map<font_cache_tuple, cache_list::iterator> cache_hash;
44 
45 #endif
46 
48 
51 namespace ST::renderer_sdl {
52  namespace font_cache {
53 
54  void move_to_front(std::list<key_pair> &list, std::list<key_pair>::iterator element);
55 
56  void set_max(uint32_t max);
57 
58  void cache_string(const std::string &str, SDL_Texture *texture, uint16_t font);
59 
60  SDL_Texture *get_cached_string(const std::string &str, uint16_t font);
61 
62  void clear();
63 
64  void close();
65  };
66 }
67 
68 #endif // FONT_CACHE_DEF
The renderer for the engine.