The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
text_cache.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2017 by Chris Beck<render787@gmail.com>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "font/text_cache.hpp"
16 #include "sdl/surface.hpp"
17 
18 #include <algorithm>
19 
20 namespace font {
21 
23 
24 unsigned int text_cache::max_size_ = 50;
25 
26 void text_cache::resize(unsigned int size)
27 {
28  // DBG_FT << "Text cache: resize from: " << max_size_ << " to: "
29  // << size << " items in cache: " << cache_.size() << '\n';
30 
31  while(size < cache_.size()) {
32  cache_.pop_back();
33  }
34  max_size_ = size;
35 }
36 
38 {
39  static size_t lookup_ = 0, hit_ = 0;
40  text_list::iterator it_bgn = cache_.begin(), it_end = cache_.end();
41  text_list::iterator it = std::find(it_bgn, it_end, t);
42  if (it != it_end) {
43  cache_.splice(it_bgn, cache_, it);
44  ++hit_;
45  } else {
46  if (cache_.size() >= max_size_)
47  cache_.pop_back();
48  cache_.push_front(t);
49  }
50  if (++lookup_ % 1000 == 0) {
51  // DBG_FT << "Text cache: " << lookup_ << " lookups, " << (hit_ / 10) << "% hits\n";
52  hit_ = 0;
53  }
54  return cache_.front();
55 }
56 
57 } // end namespace font
Note: Specific to sdl_ttf.
static text_surface & find(text_surface const &t)
Definition: text_cache.cpp:37
static text_list cache_
Definition: text_cache.hpp:34
std::list< text_surface > text_list
Definition: text_cache.hpp:33
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
static void resize(unsigned int size)
Definition: text_cache.cpp:26
static unsigned int max_size_
Definition: text_cache.hpp:35
double t
Definition: astarsearch.cpp:64
bool find(E event, F functor)
Tests whether an event handler is available.
std::string::const_iterator iterator
Definition: tokenizer.hpp:24