36
36
NS_CC_BEGIN
37
37
38
38
std::unordered_map<std::string, FontAtlas *> FontAtlasCache::_atlasMap;
39
- #define ATLAS_MAP_KEY_BUFFER 255
39
+ #define ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE 255
40
40
41
41
void FontAtlasCache::purgeCachedData ()
42
42
{
@@ -60,15 +60,11 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
60
60
useDistanceField = false ;
61
61
}
62
62
63
- char tmp[ATLAS_MAP_KEY_BUFFER];
64
- if (useDistanceField) {
65
- snprintf (tmp, ATLAS_MAP_KEY_BUFFER, " df %.2f %d %s" , config->fontSize , config->outlineSize ,
66
- realFontFilename.c_str ());
67
- } else {
68
- snprintf (tmp, ATLAS_MAP_KEY_BUFFER, " %.2f %d %s" , config->fontSize , config->outlineSize ,
69
- realFontFilename.c_str ());
70
- }
71
- std::string atlasName = tmp;
63
+ std::string key;
64
+ char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
65
+ snprintf (keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, useDistanceField ? " df %.2f %d " : " %.2f %d " , config->fontSize , config->outlineSize );
66
+ std::string atlasName (keyPrefix);
67
+ atlasName += realFontFilename;
72
68
73
69
auto it = _atlasMap.find (atlasName);
74
70
@@ -95,9 +91,10 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
95
91
FontAtlas* FontAtlasCache::getFontAtlasFNT (const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */ )
96
92
{
97
93
auto realFontFilename = FileUtils::getInstance ()->getNewFilename (fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
98
- char tmp[ATLAS_MAP_KEY_BUFFER];
99
- snprintf (tmp, ATLAS_MAP_KEY_BUFFER, " %.2f %.2f %s" , imageOffset.x , imageOffset.y , realFontFilename.c_str ());
100
- std::string atlasName = tmp;
94
+ char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
95
+ snprintf (keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, " %.2f %.2f " , imageOffset.x , imageOffset.y );
96
+ std::string atlasName (keyPrefix);
97
+ atlasName += realFontFilename;
101
98
102
99
auto it = _atlasMap.find (atlasName);
103
100
if ( it == _atlasMap.end () )
@@ -147,9 +144,9 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
147
144
148
145
FontAtlas* FontAtlasCache::getFontAtlasCharMap (Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
149
146
{
150
- char tmp[ 30 ];
151
- sprintf (tmp ," name:%u_%d_%d_%d" ,texture->getName (),itemWidth,itemHeight,startCharMap);
152
- std::string atlasName = tmp ;
147
+ char key[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE ];
148
+ sprintf (key ," name:%u_%d_%d_%d" ,texture->getName (),itemWidth,itemHeight,startCharMap);
149
+ std::string atlasName = key ;
153
150
154
151
auto it = _atlasMap.find (atlasName);
155
152
if ( it == _atlasMap.end () )
@@ -174,9 +171,10 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth
174
171
175
172
FontAtlas* FontAtlasCache::getFontAtlasCharMap (const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
176
173
{
177
- char tmp[ATLAS_MAP_KEY_BUFFER];
178
- snprintf (tmp, ATLAS_MAP_KEY_BUFFER, " %d %d %d %s" , itemWidth, itemHeight, startCharMap, charMapFile.c_str ());
179
- std::string atlasName = tmp;
174
+ char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
175
+ snprintf (keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, " %d %d %d " , itemWidth, itemHeight, startCharMap);
176
+ std::string atlasName (keyPrefix);
177
+ atlasName += charMapFile;
180
178
181
179
auto it = _atlasMap.find (atlasName);
182
180
if ( it == _atlasMap.end () )
@@ -224,9 +222,10 @@ bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas)
224
222
225
223
void FontAtlasCache::reloadFontAtlasFNT (const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/ )
226
224
{
227
- char tmp[ATLAS_MAP_KEY_BUFFER];
228
- snprintf (tmp, ATLAS_MAP_KEY_BUFFER, " %.2f %.2f %s" , imageOffset.x , imageOffset.y , fontFileName.c_str ());
229
- std::string atlasName = tmp;
225
+ char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
226
+ snprintf (keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, " %.2f %.2f " , imageOffset.x , imageOffset.y );
227
+ std::string atlasName (keyPrefix);
228
+ atlasName += fontFileName;
230
229
231
230
auto it = _atlasMap.find (atlasName);
232
231
if (it != _atlasMap.end ())
0 commit comments