FIFE 2008.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2005-2008 by the FIFE team * 00003 * http://www.fifengine.de * 00004 * This file is part of FIFE. * 00005 * * 00006 * FIFE is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Lesser General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2.1 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00014 * Lesser General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Lesser General Public * 00017 * License along with this library; if not, write to the * 00018 * Free Software Foundation, Inc., * 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00020 ***************************************************************************/ 00021 00022 // Standard C++ library includes 00023 #include <algorithm> 00024 00025 // Platform specific includes 00026 00027 // 3rd party library includes 00028 #include <boost/bind.hpp> 00029 00030 // FIFE includes 00031 // These includes are split up in two parts, separated by one empty line 00032 // First block: files included from the FIFE root src directory 00033 // Second block: files included from the same folder 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 // Stay sorted after access time 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 // Construct a entry and add it. 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 // Some minimal amount of entries -> start collection timer 00100 // Don't have a timer active if only _some_ text is pooled. 00101 if( m_poolSize >= m_poolMaxSize/10 ) 00102 m_collectTimer.start(); 00103 00104 // Maintain max pool size 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 // Stop if nothing can grow old =) 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 }