textrenderpool.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <algorithm>
00024
00025
00026
00027
00028 #include <boost/bind.hpp>
00029
00030
00031
00032
00033
00034 #include "video/image.h"
00035 #include "util/time/timemanager.h"
00036
00037 #include "fontbase.h"
00038 #include "textrenderpool.h"
00039
00040 namespace FIFE {
00041
00042 TextRenderPool::TextRenderPool(size_t poolSize) {
00043 m_poolMaxSize = poolSize;
00044 m_poolSize = 0;
00045
00046 m_collectTimer.setInterval( 1000 * 60 );
00047 m_collectTimer.setCallback( boost::bind( &TextRenderPool::removeOldEntries, this) );
00048 }
00049
00050 TextRenderPool::~TextRenderPool() {
00051 type_pool::iterator it= m_pool.begin();
00052 for(;it != m_pool.end(); ++it) {
00053 delete it->image;
00054 }
00055 }
00056
00057 Image* TextRenderPool::getRenderedText( FontBase* fontbase, const std::string& text) {
00058 SDL_Color c = fontbase->getColor();
00059
00060 type_pool::iterator it= m_pool.begin();
00061 for(;it != m_pool.end(); ++it) {
00062 if( it->antialias != fontbase->isAntiAlias() )
00063 continue;
00064
00065 if( it->glyph_spacing != fontbase->getGlyphSpacing() )
00066 continue;
00067
00068 if( it->row_spacing != fontbase->getRowSpacing() )
00069 continue;
00070
00071 if( it->color.r != c.r || it->color.g != c.g || it->color.b != c.b )
00072 continue;
00073
00074 if( it->text != text )
00075 continue;
00076
00077
00078 it->timestamp = TimeManager::instance()->getTime();
00079 m_pool.push_front( *it );
00080 m_pool.erase( it );
00081
00082 return m_pool.front().image;
00083 }
00084 return 0;
00085 }
00086
00087 void TextRenderPool::addRenderedText( FontBase* fontbase,const std::string& text, Image* image) {
00088
00089 s_pool_entry centry;
00090 centry.antialias = fontbase->isAntiAlias();
00091 centry.glyph_spacing = fontbase->getGlyphSpacing();
00092 centry.row_spacing = fontbase->getRowSpacing();
00093 centry.text = text;
00094 centry.color = fontbase->getColor();
00095 centry.image = image;
00096 centry.timestamp = TimeManager::instance()->getTime();
00097 m_pool.push_front( centry );
00098
00099
00100
00101 if( m_poolSize >= m_poolMaxSize/10 )
00102 m_collectTimer.start();
00103
00104
00105 if( m_poolSize < m_poolMaxSize ) {
00106 m_poolSize++;
00107 return;
00108 } else {
00109 delete m_pool.back().image;
00110 m_pool.pop_back();
00111 }
00112 }
00113
00114 void TextRenderPool::removeOldEntries() {
00115
00116 type_pool::iterator it = m_pool.begin();
00117 uint32_t now = TimeManager::instance()->getTime();
00118 while (it != m_pool.end()) {
00119 if( (now - it->timestamp) > 1000*60 ) {
00120 delete it->image;
00121 it = m_pool.erase(it);
00122 --m_poolSize;
00123 }
00124 else {
00125 ++it;
00126 }
00127 }
00128
00129
00130 if( m_poolSize == 0 )
00131 m_collectTimer.stop();
00132 }
00133
00134 void TextRenderPool::invalidateCachedText() {
00135 type_pool::iterator it = m_pool.begin();
00136 while (it != m_pool.end()) {
00137 it->image->invalidate();
00138 ++it;
00139 }
00140 }
00141 }