FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
textrenderpool.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <algorithm>
24 
25 // Platform specific includes
26 
27 // 3rd party library includes
28 #include <boost/bind.hpp>
29 
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34 #include "video/image.h"
35 #include "util/time/timemanager.h"
36 
37 #include "fontbase.h"
38 #include "textrenderpool.h"
39 
40 namespace FIFE {
41 
42  TextRenderPool::TextRenderPool(size_t poolSize) {
43  m_poolMaxSize = poolSize;
44  m_poolSize = 0;
45 
46  m_collectTimer.setInterval( 1000 * 60 );
48  }
49 
51  type_pool::iterator it= m_pool.begin();
52  for(;it != m_pool.end(); ++it) {
53  delete it->image;
54  }
55  }
56 
57  Image* TextRenderPool::getRenderedText( FontBase* fontbase, const std::string& text) {
58  SDL_Color c = fontbase->getColor();
59 
60  type_pool::iterator it= m_pool.begin();
61  for(;it != m_pool.end(); ++it) {
62  if( it->antialias != fontbase->isAntiAlias() )
63  continue;
64 
65  if( it->glyph_spacing != fontbase->getGlyphSpacing() )
66  continue;
67 
68  if( it->row_spacing != fontbase->getRowSpacing() )
69  continue;
70 
71  if( it->color.r != c.r || it->color.g != c.g || it->color.b != c.b )
72  continue;
73 
74  if( it->text != text )
75  continue;
76 
77  // Stay sorted after access time
78  it->timestamp = TimeManager::instance()->getTime();
79  m_pool.push_front( *it );
80  m_pool.erase( it );
81 
82  return m_pool.front().image;
83  }
84  return 0;
85  }
86 
87  void TextRenderPool::addRenderedText( FontBase* fontbase,const std::string& text, Image* image) {
88  // Construct a entry and add it.
89  s_pool_entry centry;
90  centry.antialias = fontbase->isAntiAlias();
91  centry.glyph_spacing = fontbase->getGlyphSpacing();
92  centry.row_spacing = fontbase->getRowSpacing();
93  centry.text = text;
94  centry.color = fontbase->getColor();
95  centry.image = image;
97  m_pool.push_front( centry );
98 
99  // Some minimal amount of entries -> start collection timer
100  // Don't have a timer active if only _some_ text is pooled.
101  if( m_poolSize >= m_poolMaxSize/10 )
103 
104  // Maintain max pool size
105  if( m_poolSize < m_poolMaxSize ) {
106  m_poolSize++;
107  return;
108  } else {
109  delete m_pool.back().image;
110  m_pool.pop_back();
111  }
112  }
113 
115 
116  type_pool::iterator it = m_pool.begin();
118  while (it != m_pool.end()) {
119  if( (now - it->timestamp) > 1000*60 ) {
120  delete it->image;
121  it = m_pool.erase(it);
122  --m_poolSize;
123  }
124  else {
125  ++it;
126  }
127  }
128 
129  // Stop if nothing can grow old =)
130  if( m_poolSize == 0 )
132  }
133 
135  type_pool::iterator it = m_pool.begin();
136  while (it != m_pool.end()) {
137  it->image->invalidate();
138  ++it;
139  }
140  }
141 }
void removeOldEntries()
Remove entries not used since a minute Is a timer callback.
int32_t getGlyphSpacing() const
Gets the spacing between letters in pixels.
Definition: fontbase.cpp:73
Image * image
Base Class for Images.
Definition: image.h:48
TextRenderPool(size_t poolSize=200)
Constructor Constructs a pool with a maximum of poolSize entries.
void invalidateCachedText()
Invalidates all cached text images.
void setInterval(int32_t msec)
Set the interval in milliseconds.
Definition: timer.cpp:60
void start()
Start the timer.
Definition: timer.cpp:45
Abstract Font Base Class Uses a pool for rendered strings.
Definition: fontbase.h:48
static TimeManager * instance()
Definition: singleton.h:84
bool antialias
uint32_t timestamp
Image * getRenderedText(FontBase *fontbase, const std::string &text)
Get a string image.
SDL_Color color
uint32_t getTime() const
Get the time.
void addRenderedText(FontBase *fontbase, const std::string &text, Image *image)
Add a string image.
~TextRenderPool()
Destructor.
int32_t getRowSpacing() const
Gets the spacing between rows in pixels.
Definition: fontbase.cpp:65
void stop()
Stop the timer.
Definition: timer.cpp:53
int row_spacing
SDL_Color getColor() const
Get the color the text was rendered in.
Definition: fontbase.cpp:125
unsigned int uint32_t
Definition: core.h:40
std::string text
virtual bool isAntiAlias() const
Checks if anti aliasing is used.
Definition: fontbase.cpp:81
void setCallback(const type_callback &callback)
Set the callback that will be called.
Definition: timer.cpp:64
int glyph_spacing