2010-08-12 17:39:25 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2010-12-24 14:00:49 +08:00
|
|
|
#include "CCLabelBMFont.h"
|
2010-10-08 17:53:04 +08:00
|
|
|
|
|
|
|
#include "platform/platform.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCMutableDictionary.h"
|
2010-08-12 17:39:25 +08:00
|
|
|
#include "CCConfiguration.h"
|
|
|
|
#include "CCDrawingPrimitives.h"
|
|
|
|
#include "CCSprite.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCPointExtension.h"
|
2010-10-08 17:53:04 +08:00
|
|
|
|
2011-03-26 13:52:33 +08:00
|
|
|
#include "CCFileUtils.h"
|
2010-09-04 11:34:37 +08:00
|
|
|
#include "support/data_support/uthash.h"
|
2010-10-08 17:53:04 +08:00
|
|
|
|
2010-08-12 17:39:25 +08:00
|
|
|
namespace cocos2d{
|
|
|
|
|
|
|
|
//
|
|
|
|
//FNTConfig Cache - free functions
|
|
|
|
//
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableDictionary<std::string, CCBMFontConfiguration*> *configurations = NULL;
|
2010-12-24 17:07:31 +08:00
|
|
|
CCBMFontConfiguration* FNTConfigLoadFile( const char *fntFile)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
2010-12-24 17:07:31 +08:00
|
|
|
CCBMFontConfiguration *pRet = NULL;
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
if( configurations == NULL )
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
configurations = new CCMutableDictionary<std::string, CCBMFontConfiguration*>();
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
|
|
|
std::string key(fntFile);
|
|
|
|
pRet = configurations->objectForKey(key);
|
2010-08-18 12:03:31 +08:00
|
|
|
if( pRet == NULL )
|
|
|
|
{
|
2010-12-24 17:07:31 +08:00
|
|
|
pRet = CCBMFontConfiguration::configurationWithFNTFile(fntFile);
|
2010-08-12 17:39:25 +08:00
|
|
|
configurations->setObject(pRet, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FNTConfigRemoveCache( void )
|
|
|
|
{
|
2010-08-18 12:03:31 +08:00
|
|
|
if (configurations)
|
|
|
|
{
|
|
|
|
configurations->removeAllObjects();
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(configurations);
|
2010-08-18 12:03:31 +08:00
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
|
|
|
//
|
|
|
|
//Hash Element
|
|
|
|
//
|
|
|
|
// Equal function for targetSet.
|
|
|
|
typedef struct _KerningHashElement
|
|
|
|
{
|
|
|
|
int key; // key for the hash. 16-bit for 1st element, 16-bit for 2nd element
|
|
|
|
int amount;
|
|
|
|
UT_hash_handle hh;
|
|
|
|
} tKerningHashElement;
|
|
|
|
//
|
|
|
|
//BitmapFontConfiguration
|
|
|
|
//
|
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
CCBMFontConfiguration * CCBMFontConfiguration::configurationWithFNTFile(const char *FNTfile)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
2010-12-24 17:07:31 +08:00
|
|
|
CCBMFontConfiguration * pRet = new CCBMFontConfiguration();
|
2010-09-04 12:02:52 +08:00
|
|
|
if (pRet->initWithFNTfile(FNTfile))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
2010-09-04 12:02:52 +08:00
|
|
|
return NULL;
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
bool CCBMFontConfiguration::initWithFNTfile(const char *FNTfile)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
assert(FNTfile != NULL && strlen(FNTfile)!=0);
|
2010-08-12 17:39:25 +08:00
|
|
|
m_pKerningDictionary = NULL;
|
|
|
|
this->parseConfigFile(FNTfile);
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
CCBMFontConfiguration::~CCBMFontConfiguration()
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
2010-12-24 17:07:31 +08:00
|
|
|
CCLOGINFO( "cocos2d: deallocing CCBMFontConfiguration" );
|
2010-08-12 17:39:25 +08:00
|
|
|
this->purgeKerningDictionary();
|
|
|
|
m_sAtlasName.clear();
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
char * CCBMFontConfiguration::description(void)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
char *ret = new char[100];
|
2010-12-24 17:07:31 +08:00
|
|
|
sprintf(ret, "<CCBMFontConfiguration | Kernings:%d | Image = %s>", HASH_COUNT(m_pKerningDictionary), m_sAtlasName.c_str());
|
2010-08-12 17:39:25 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCBMFontConfiguration::purgeKerningDictionary()
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
tKerningHashElement *current;
|
|
|
|
while(m_pKerningDictionary)
|
|
|
|
{
|
|
|
|
current = m_pKerningDictionary;
|
|
|
|
HASH_DEL(m_pKerningDictionary,current);
|
|
|
|
free(current);
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCBMFontConfiguration::parseConfigFile(const char *controlFile)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
std::string fullpath = CCFileUtils::fullPathFromRelativePath(controlFile);
|
|
|
|
|
2010-11-29 10:49:43 +08:00
|
|
|
FileData data;
|
|
|
|
unsigned long nBufSize = 0;
|
|
|
|
char* pBuffer = (char*) data.getFileData(fullpath.c_str(), "r", &nBufSize);
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(pBuffer, "CCBMFontConfiguration::parseConfigFile | Open file error.");
|
2010-11-29 10:49:43 +08:00
|
|
|
|
|
|
|
if (!pBuffer)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-24 15:53:52 +08:00
|
|
|
// parse spacing / padding
|
|
|
|
std::string line;
|
2011-01-28 16:05:21 +08:00
|
|
|
std::string strLeft(pBuffer, nBufSize);
|
2011-01-24 15:53:52 +08:00
|
|
|
while (strLeft.length() > 0)
{
|
|
|
|
int pos = strLeft.find('\n');
|
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
2010-11-29 10:49:43 +08:00
|
|
|
{
|
2011-01-24 15:53:52 +08:00
|
|
|
// the data is more than a line.get one line
|
|
|
|
line = strLeft.substr(0, pos);
|
|
|
|
strLeft = strLeft.substr(pos + 1);
|
2010-11-29 10:49:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-24 15:53:52 +08:00
|
|
|
// get the left data
|
|
|
|
line = strLeft;
|
|
|
|
strLeft.erase();
|
2010-11-29 10:49:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(line.substr(0,strlen("info face")) == "info face")
|
|
|
|
{
|
|
|
|
// XXX: info parsing is incomplete
|
|
|
|
// Not needed for the Hiero editors, but needed for the AngelCode editor
|
|
|
|
// [self parseInfoArguments:line];
|
2011-01-24 15:53:52 +08:00
|
|
|
this->parseInfoArguments(line);
|
2010-11-29 10:49:43 +08:00
|
|
|
}
|
|
|
|
// Check to see if the start of the line is something we are interested in
|
|
|
|
else if(line.substr(0,strlen("common lineHeight")) == "common lineHeight")
|
|
|
|
{
|
|
|
|
this->parseCommonArguments(line);
|
|
|
|
}
|
|
|
|
else if(line.substr(0,strlen("page id")) == "page id")
|
|
|
|
{
|
|
|
|
this->parseImageFileName(line, controlFile);
|
|
|
|
}
|
|
|
|
else if(line.substr(0,strlen("chars c")) == "chars c")
|
|
|
|
{
|
|
|
|
// Ignore this line
|
|
|
|
}
|
|
|
|
else if(line.substr(0,strlen("char")) == "char")
|
|
|
|
{
|
|
|
|
// Parse the current line and create a new CharDef
|
2010-12-24 17:07:31 +08:00
|
|
|
ccBMFontDef characterDefinition;
|
2010-11-29 10:49:43 +08:00
|
|
|
this->parseCharacterDefinition(line, &characterDefinition);
|
|
|
|
|
|
|
|
// Add the CharDef returned to the charArray
|
|
|
|
m_pBitmapFontArray[ characterDefinition.charID ] = characterDefinition;
|
|
|
|
}
|
|
|
|
else if(line.substr(0,strlen("kernings count")) == "kernings count")
|
|
|
|
{
|
|
|
|
this->parseKerningCapacity(line);
|
|
|
|
}
|
|
|
|
else if(line.substr(0,strlen("kerning first")) == "kerning first")
|
|
|
|
{
|
|
|
|
this->parseKerningEntry(line);
|
|
|
|
}
|
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCBMFontConfiguration::parseImageFileName(std::string line, const char *fntFile)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// line to parse:
|
|
|
|
// page id=0 file="bitmapFontTest.png"
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// page ID. Sanity check
|
|
|
|
int index = line.find('=')+1;
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(atoi(value.c_str()) == 0, "BitmapFontAtlas file could not be found");
|
2010-08-12 17:39:25 +08:00
|
|
|
// file
|
|
|
|
index = line.find('"')+1;
|
|
|
|
index2 = line.find('"', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
|
2010-09-07 18:04:57 +08:00
|
|
|
m_sAtlasName = CCFileUtils::fullPathFromRelativeFile(value.c_str(), fntFile);
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCBMFontConfiguration::parseInfoArguments(std::string line)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// possible lines to parse:
|
|
|
|
// info face="Script" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=1,4,3,2 spacing=0,0 outline=0
|
|
|
|
// info face="Cracked" size=36 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// padding
|
|
|
|
int index = line.find("padding=");
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "padding=%d,%d,%d,%d", &m_tPadding.top, &m_tPadding.right, &m_tPadding.bottom, &m_tPadding.left);
|
|
|
|
CCLOG("cocos2d: padding: %d,%d,%d,%d", m_tPadding.left, m_tPadding.top, m_tPadding.right, m_tPadding.bottom);
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCBMFontConfiguration::parseCommonArguments(std::string line)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// line to parse:
|
|
|
|
// common lineHeight=104 base=26 scaleW=1024 scaleH=512 pages=1 packed=0
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Height
|
|
|
|
int index = line.find("lineHeight=");
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "lineHeight=%u", &m_uCommonHeight);
|
|
|
|
// scaleW. sanity check
|
|
|
|
index = line.find("scaleW=") + strlen("scaleW=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(atoi(value.c_str()) <= CCConfiguration::sharedConfiguration()->getMaxTextureSize(), "CCLabelBMFont: page can't be larger than supported");
|
2010-08-12 17:39:25 +08:00
|
|
|
// scaleH. sanity check
|
|
|
|
index = line.find("scaleH=") + strlen("scaleH=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(atoi(value.c_str()) <= CCConfiguration::sharedConfiguration()->getMaxTextureSize(), "CCLabelBMFont: page can't be larger than supported");
|
2010-08-12 17:39:25 +08:00
|
|
|
// pages. sanity check
|
|
|
|
index = line.find("pages=") + strlen("pages=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(atoi(value.c_str()) == 1, "CCBitfontAtlas: only supports 1 page");
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
// packed (ignore) What does this mean ??
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCBMFontConfiguration::parseCharacterDefinition(std::string line, ccBMFontDef *characterDefinition)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// line to parse:
|
|
|
|
// char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=44 xadvance=14 page=0 chnl=0
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Character ID
|
|
|
|
int index = line.find("id=");
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "id=%u", &characterDefinition->charID);
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(characterDefinition->charID < kCCBMFontMaxChars, "BitmpaFontAtlas: CharID bigger than supported");
|
2010-08-12 17:39:25 +08:00
|
|
|
// Character x
|
|
|
|
index = line.find("x=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "x=%f", &characterDefinition->rect.origin.x);
|
|
|
|
// Character y
|
|
|
|
index = line.find("y=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "y=%f", &characterDefinition->rect.origin.y);
|
|
|
|
// Character width
|
|
|
|
index = line.find("width=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "width=%f", &characterDefinition->rect.size.width);
|
|
|
|
// Character height
|
|
|
|
index = line.find("height=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "height=%f", &characterDefinition->rect.size.height);
|
|
|
|
// Character xoffset
|
|
|
|
index = line.find("xoffset=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "xoffset=%d", &characterDefinition->xOffset);
|
|
|
|
// Character yoffset
|
|
|
|
index = line.find("yoffset=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "yoffset=%d", &characterDefinition->yOffset);
|
|
|
|
// Character xadvance
|
|
|
|
index = line.find("xadvance=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "xadvance=%d", &characterDefinition->xAdvance);
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCBMFontConfiguration::parseKerningCapacity(std::string line)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
// When using uthash there is not need to parse the capacity.
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
// CCAssert(!kerningDictionary, @"dictionary already initialized");
|
2010-08-12 17:39:25 +08:00
|
|
|
//
|
|
|
|
// // Break the values for this line up using =
|
2011-03-18 14:31:29 +08:00
|
|
|
// CCMutableArray *values = [line componentsSeparatedByString:@"="];
|
2010-08-12 17:39:25 +08:00
|
|
|
// NSEnumerator *nse = [values objectEnumerator];
|
2011-03-07 17:11:57 +08:00
|
|
|
// CCString *propertyValue;
|
2010-08-12 17:39:25 +08:00
|
|
|
//
|
|
|
|
// // We need to move past the first entry in the array before we start assigning values
|
|
|
|
// [nse nextObject];
|
|
|
|
//
|
|
|
|
// // count
|
|
|
|
// propertyValue = [nse nextObject];
|
|
|
|
// int capacity = [propertyValue intValue];
|
|
|
|
//
|
|
|
|
// if( capacity != -1 )
|
|
|
|
// kerningDictionary = ccHashSetNew(capacity, targetSetEql);
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCBMFontConfiguration::parseKerningEntry(std::string line)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// line to parse:
|
|
|
|
// kerning first=121 second=44 amount=-7
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// first
|
|
|
|
int first;
|
|
|
|
int index = line.find("first=");
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "first=%d", &first);
|
|
|
|
|
|
|
|
// second
|
|
|
|
int second;
|
|
|
|
index = line.find("second=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "second=%d", &second);
|
|
|
|
|
|
|
|
// amount
|
|
|
|
int amount;
|
|
|
|
index = line.find("amount=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "amount=%d", &amount);
|
|
|
|
|
|
|
|
tKerningHashElement *element = (tKerningHashElement *)calloc( sizeof( *element ), 1 );
|
|
|
|
element->amount = amount;
|
|
|
|
element->key = (first<<16) | (second&0xffff);
|
|
|
|
HASH_ADD_INT(m_pKerningDictionary,key, element);
|
|
|
|
}
|
|
|
|
//
|
2010-12-24 17:07:31 +08:00
|
|
|
//CCLabelBMFont
|
2010-08-12 17:39:25 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
//BitmapFontAtlas - Purge Cache
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCLabelBMFont::purgeCachedData()
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
FNTConfigRemoveCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
//BitmapFontAtlas - Creation & Init
|
2010-12-24 17:07:31 +08:00
|
|
|
CCLabelBMFont *CCLabelBMFont::labelWithString(const char *str, const char *fntFile)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
2010-12-24 17:07:31 +08:00
|
|
|
CCLabelBMFont *pRet = new CCLabelBMFont();
|
2010-08-19 14:09:40 +08:00
|
|
|
if(pRet && pRet->initWithString(str, fntFile))
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pRet)
|
2010-08-12 17:39:25 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
|
|
|
|
CCLabelBMFont * CCLabelBMFont::bitmapFontAtlasWithString(const char *str, const char *fntFile)
|
|
|
|
{
|
|
|
|
return labelWithString(str, fntFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCLabelBMFont::initWithString(const char *theString, const char *fntFile)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
assert(theString != NULL);
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_RELEASE(m_pConfiguration);// allow re-init
|
2010-08-12 17:39:25 +08:00
|
|
|
m_pConfiguration = FNTConfigLoadFile(fntFile);
|
|
|
|
m_pConfiguration->retain();
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( m_pConfiguration, "Error creating config for BitmapFontAtlas");
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2010-12-31 09:36:53 +08:00
|
|
|
if (CCSpriteBatchNode::initWithFile(m_pConfiguration->m_sAtlasName.c_str(), strlen(theString)))
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
m_cOpacity = 255;
|
|
|
|
m_tColor = ccWHITE;
|
2011-03-07 17:11:57 +08:00
|
|
|
m_tContentSize = CCSizeZero;
|
2010-08-12 17:39:25 +08:00
|
|
|
m_bIsOpacityModifyRGB = m_pobTextureAtlas->getTexture()->getHasPremultipliedAlpha();
|
|
|
|
m_tAnchorPoint = ccp(0.5f, 0.5f);
|
|
|
|
this->setString(theString);
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
2010-09-04 12:02:52 +08:00
|
|
|
return false;
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
CCLabelBMFont::~CCLabelBMFont()
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
m_sString.clear();
|
|
|
|
m_pConfiguration->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// BitmapFontAtlas - Atlas generation
|
2010-12-24 17:07:31 +08:00
|
|
|
int CCLabelBMFont::kerningAmountForFirst(unsigned short first, unsigned short second)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned int key = (first<<16) | (second & 0xffff);
|
|
|
|
|
|
|
|
if( m_pConfiguration->m_pKerningDictionary ) {
|
|
|
|
tKerningHashElement *element = NULL;
|
|
|
|
HASH_FIND_INT(m_pConfiguration->m_pKerningDictionary, &key, element);
|
|
|
|
if(element)
|
|
|
|
ret = element->amount;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCLabelBMFont::createFontChars()
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
int nextFontPositionX = 0;
|
2010-12-24 17:07:31 +08:00
|
|
|
int nextFontPositionY = 0;
|
2011-03-28 10:44:14 +08:00
|
|
|
unsigned short prev = -1;
|
2010-08-12 17:39:25 +08:00
|
|
|
int kerningAmount = 0;
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCSize tmpSize = CCSizeZero;
|
2010-12-24 17:07:31 +08:00
|
|
|
|
|
|
|
int longestLine = 0;
|
|
|
|
int totalHeight = 0;
|
|
|
|
|
|
|
|
int quantityOfLines = 1;
|
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
int len = m_sString.length();
|
2010-12-24 17:07:31 +08:00
|
|
|
|
|
|
|
if (0 == len)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
for (int i = 0; i < len - 1; ++i)
|
2010-12-24 17:07:31 +08:00
|
|
|
{
|
2011-03-28 10:44:14 +08:00
|
|
|
unsigned short c = m_sString[i];
|
2010-12-24 17:07:31 +08:00
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
quantityOfLines++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
totalHeight = m_pConfiguration->m_uCommonHeight * quantityOfLines;
|
|
|
|
nextFontPositionY = m_pConfiguration->m_uCommonHeight * (quantityOfLines - 1);
|
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
for(int i=0; i<len; i++)
|
2010-08-13 11:16:04 +08:00
|
|
|
{
|
2011-03-28 10:44:14 +08:00
|
|
|
unsigned short c = m_sString[i];
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( c < kCCBMFontMaxChars, "BitmapFontAtlas: character outside bounds");
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
nextFontPositionX = 0;
|
|
|
|
nextFontPositionY -= m_pConfiguration->m_uCommonHeight;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-13 11:16:04 +08:00
|
|
|
kerningAmount = this->kerningAmountForFirst(prev, c);
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
ccBMFontDef fontDef = m_pConfiguration->m_pBitmapFontArray[c];
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCRect rect = fontDef.rect;
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
CCSprite *fontChar;
|
|
|
|
|
2010-08-31 11:20:37 +08:00
|
|
|
fontChar = (CCSprite*)(this->getChildByTag(i));
|
2010-08-13 11:16:04 +08:00
|
|
|
if( ! fontChar )
|
|
|
|
{
|
2010-08-12 17:39:25 +08:00
|
|
|
fontChar = new CCSprite();
|
2010-12-31 09:36:53 +08:00
|
|
|
fontChar->initWithSpriteSheet((CCSpriteSheetInternalOnly *) this, rect);
|
2010-08-12 17:39:25 +08:00
|
|
|
this->addChild(fontChar, 0, i);
|
|
|
|
fontChar->release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// reusing fonts
|
2010-12-31 16:37:00 +08:00
|
|
|
fontChar->setTextureRectInPixels(rect, false, rect.size);
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
// restore to default in case they were modified
|
|
|
|
fontChar->setIsVisible(true);
|
|
|
|
fontChar->setOpacity(255);
|
|
|
|
}
|
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
float yOffset = (float) (m_pConfiguration->m_uCommonHeight - fontDef.yOffset);
|
|
|
|
fontChar->setPositionInPixels( ccp( nextFontPositionX + fontDef.xOffset + fontDef.rect.size.width / 2.0f + kerningAmount,
|
|
|
|
(float) nextFontPositionY + yOffset - rect.size.height/2.0f ) );
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
// NSLog(@"position.y: %f", fontChar.position.y);
|
|
|
|
|
|
|
|
// update kerning
|
|
|
|
nextFontPositionX += m_pConfiguration->m_pBitmapFontArray[c].xAdvance + kerningAmount;
|
|
|
|
prev = c;
|
|
|
|
|
|
|
|
// Apply label properties
|
|
|
|
fontChar->setIsOpacityModifyRGB(m_bIsOpacityModifyRGB);
|
|
|
|
// Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on
|
|
|
|
fontChar->setColor(m_tColor);
|
|
|
|
|
|
|
|
// only apply opaccity if it is different than 255 )
|
|
|
|
// to prevent modifying the color too (issue #610)
|
|
|
|
if( m_cOpacity != 255 )
|
|
|
|
{
|
|
|
|
fontChar->setOpacity(m_cOpacity);
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
|
|
|
|
if (longestLine < nextFontPositionX)
|
|
|
|
{
|
|
|
|
longestLine = nextFontPositionX;
|
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
|
|
|
|
tmpSize.width = (float) longestLine;
|
|
|
|
tmpSize.height = (float) totalHeight;
|
|
|
|
|
|
|
|
this->setContentSizeInPixels(tmpSize);
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//BitmapFontAtlas - CCLabelProtocol protocol
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCLabelBMFont::setString(const char *newString)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
m_sString.clear();
|
|
|
|
m_sString = newString;
|
|
|
|
|
|
|
|
if (m_pChildren && m_pChildren->count() != 0)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray<CCNode*>::CCMutableArrayIterator it;
|
2010-08-12 17:39:25 +08:00
|
|
|
for(it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
|
|
|
(*it)->setIsVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->createFontChars();
|
|
|
|
}
|
|
|
|
|
2010-12-28 15:05:55 +08:00
|
|
|
const char* CCLabelBMFont::getString(void)
|
|
|
|
{
|
|
|
|
return m_sString.c_str();
|
|
|
|
}
|
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCLabelBMFont::setCString(const char *label)
|
|
|
|
{
|
|
|
|
setString(label);
|
|
|
|
}
|
|
|
|
|
2010-08-12 17:39:25 +08:00
|
|
|
//BitmapFontAtlas - CCRGBAProtocol protocol
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCLabelBMFont::setColor(ccColor3B var)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
m_tColor = var;
|
|
|
|
if (m_pChildren && m_pChildren->count() != 0)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray<CCNode*>::CCMutableArrayIterator it;
|
2010-08-12 17:39:25 +08:00
|
|
|
for(it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
((CCSprite*)(*it))->setColor(m_tColor);
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
ccColor3B CCLabelBMFont::getColor()
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
return m_tColor;
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCLabelBMFont::setOpacity(GLubyte var)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
m_cOpacity = var;
|
|
|
|
|
|
|
|
if (m_pChildren && m_pChildren->count() != 0)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray<CCNode*>::CCMutableArrayIterator it;
|
2010-08-12 17:39:25 +08:00
|
|
|
for(it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
CCRGBAProtocol *pRGBAProtocol = (*it)->convertToRGBAProtocol();
|
|
|
|
if (pRGBAProtocol)
|
|
|
|
{
|
|
|
|
pRGBAProtocol->setOpacity(m_cOpacity);
|
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
GLubyte CCLabelBMFont::getOpacity()
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
return m_cOpacity;
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCLabelBMFont::setIsOpacityModifyRGB(bool var)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
m_bIsOpacityModifyRGB = var;
|
|
|
|
if (m_pChildren && m_pChildren->count() != 0)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray<CCNode*>::CCMutableArrayIterator it;
|
2010-08-12 17:39:25 +08:00
|
|
|
for(it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
CCRGBAProtocol *pRGBAProtocol = (*it)->convertToRGBAProtocol();
|
|
|
|
if (pRGBAProtocol)
|
|
|
|
{
|
|
|
|
pRGBAProtocol->setIsOpacityModifyRGB(m_bIsOpacityModifyRGB);
|
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
bool CCLabelBMFont::getIsOpacityModifyRGB()
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
return m_bIsOpacityModifyRGB;
|
|
|
|
}
|
|
|
|
|
|
|
|
// BitmapFontAtlas - AnchorPoint
|
2011-03-07 17:11:57 +08:00
|
|
|
void CCLabelBMFont::setAnchorPoint(CCPoint point)
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
if( ! CCPoint::CCPointEqualToPoint(point, m_tAnchorPoint) )
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
2010-12-31 09:36:53 +08:00
|
|
|
CCSpriteBatchNode::setAnchorPoint(point);
|
2010-08-12 17:39:25 +08:00
|
|
|
this->createFontChars();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//BitmapFontAtlas - Debug draw
|
|
|
|
#if CC_BITMAPFONTATLAS_DEBUG_DRAW
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCLabelBMFont::draw()
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
2010-12-31 09:36:53 +08:00
|
|
|
CCSpriteBatchNode::draw();
|
2011-03-07 17:11:57 +08:00
|
|
|
CCSize s = this->getContentSize();
|
|
|
|
CCPoint vertices[4]={
|
2010-08-12 17:39:25 +08:00
|
|
|
ccp(0,0),ccp(s.width,0),
|
|
|
|
ccp(s.width,s.height),ccp(0,s.height),
|
|
|
|
};
|
|
|
|
ccDrawPoly(vertices, 4, true);
|
|
|
|
}
|
|
|
|
#endif // CC_BITMAPFONTATLAS_DEBUG_DRAW
|
|
|
|
|
2010-09-02 14:54:42 +08:00
|
|
|
}
|