comparison engine/core/video/fonts/textrenderpool.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, 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 );
47 m_collectTimer.setCallback( boost::bind( &TextRenderPool::removeOldEntries, this) );
48 }
49
50 TextRenderPool::~TextRenderPool() {
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;
96 centry.timestamp = TimeManager::instance()->getTime();
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 )
102 m_collectTimer.start();
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
114 void TextRenderPool::removeOldEntries() {
115
116 type_pool::iterator it = m_pool.begin();
117 uint32_t now = TimeManager::instance()->getTime();
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 )
131 m_collectTimer.stop();
132 }
133 }