| 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 ); |
| 46 |
m_collectTimer.setCallback(std::bind( &TextRenderPool::removeOldEntries, this) ); |
| 47 |
} |
| 48 |
|
| 49 |
TextRenderPool::~TextRenderPool() { |
| 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; |
| 95 |
centry.timestamp = TimeManager::instance()->getTime(); |
| 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 ) |
| 101 |
m_collectTimer.start(); |
| 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 |
|
| 113 |
void TextRenderPool::removeOldEntries() { |
| 114 |
|
| 115 |
type_pool::iterator it = m_pool.begin(); |
| 116 |
uint32_t now = TimeManager::instance()->getTime(); |
| 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 ) |
| 130 |
m_collectTimer.stop(); |
| 131 |
} |
| 132 |
|
| 133 |
void TextRenderPool::invalidateCachedText() { |
| 134 |
type_pool::iterator it = m_pool.begin(); |
| 135 |
while (it != m_pool.end()) { |
| 136 |
it->image->invalidate(); |
| 137 |
++it; |
| 138 |
} |
| 139 |
} |
| 140 |
} |