FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
textrenderpool.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 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 
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 #include "video/image.h"
34 #include "util/time/timemanager.h"
35 
36 #include "fontbase.h"
37 #include "textrenderpool.h"
38 
39 namespace FIFE {
40 
41  TextRenderPool::TextRenderPool(size_t poolSize) {
42  m_poolMaxSize = poolSize;
43  m_poolSize = 0;
44 
45  m_collectTimer.setInterval( 1000 * 60 );
47  }
48 
50  type_pool::iterator it= m_pool.begin();
51  for(;it != m_pool.end(); ++it) {
52  delete it->image;
53  }
54  }
55 
56  Image* TextRenderPool::getRenderedText( FontBase* fontbase, const std::string& text) {
57  SDL_Color c = fontbase->getColor();
58 
59  type_pool::iterator it= m_pool.begin();
60  for(;it != m_pool.end(); ++it) {
61  if( it->antialias != fontbase->isAntiAlias() )
62  continue;
63 
64  if( it->glyph_spacing != fontbase->getGlyphSpacing() )
65  continue;
66 
67  if( it->row_spacing != fontbase->getRowSpacing() )
68  continue;
69 
70  if( it->color.r != c.r || it->color.g != c.g || it->color.b != c.b )
71  continue;
72 
73  if( it->text != text )
74  continue;
75 
76  // Stay sorted after access time
77  it->timestamp = TimeManager::instance()->getTime();
78  m_pool.push_front( *it );
79  m_pool.erase( it );
80 
81  return m_pool.front().image;
82  }
83  return 0;
84  }
85 
86  void TextRenderPool::addRenderedText( FontBase* fontbase,const std::string& text, Image* image) {
87  // Construct a entry and add it.
88  s_pool_entry centry;
89  centry.antialias = fontbase->isAntiAlias();
90  centry.glyph_spacing = fontbase->getGlyphSpacing();
91  centry.row_spacing = fontbase->getRowSpacing();
92  centry.text = text;
93  centry.color = fontbase->getColor();
94  centry.image = image;
96  m_pool.push_front( centry );
97 
98  // Some minimal amount of entries -> start collection timer
99  // Don't have a timer active if only _some_ text is pooled.
100  if( m_poolSize >= m_poolMaxSize/10 )
102 
103  // Maintain max pool size
104  if( m_poolSize < m_poolMaxSize ) {
105  m_poolSize++;
106  return;
107  } else {
108  delete m_pool.back().image;
109  m_pool.pop_back();
110  }
111  }
112 
114 
115  type_pool::iterator it = m_pool.begin();
117  while (it != m_pool.end()) {
118  if( (now - it->timestamp) > 1000*60 ) {
119  delete it->image;
120  it = m_pool.erase(it);
121  --m_poolSize;
122  }
123  else {
124  ++it;
125  }
126  }
127 
128  // Stop if nothing can grow old =)
129  if( m_poolSize == 0 )
131  }
132 
134  type_pool::iterator it = m_pool.begin();
135  while (it != m_pool.end()) {
136  it->image->invalidate();
137  ++it;
138  }
139  }
140 }
void removeOldEntries()
Remove entries not used since a minute Is a timer callback.
uint32_t getTime() const
Get the time.
Image * image
Base Class for Images.
Definition: image.h:48
virtual bool isAntiAlias() const
Checks if anti aliasing is used.
Definition: fontbase.cpp:81
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
SDL_Color getColor() const
Get the color the text was rendered in.
Definition: fontbase.cpp:125
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
void addRenderedText(FontBase *fontbase, const std::string &text, Image *image)
Add a string image.
~TextRenderPool()
Destructor.
void stop()
Stop the timer.
Definition: timer.cpp:53
int row_spacing
unsigned int uint32_t
Definition: core.h:40
std::string text
int32_t getGlyphSpacing() const
Gets the spacing between letters in pixels.
Definition: fontbase.cpp:73
void setCallback(const type_callback &callback)
Set the callback that will be called.
Definition: timer.cpp:64
int32_t getRowSpacing() const
Gets the spacing between rows in pixels.
Definition: fontbase.cpp:65
int glyph_spacing