diff --git a/cocos2dx/label_nodes/CCLabelBMFont.cpp b/cocos2dx/label_nodes/CCLabelBMFont.cpp index 690aae1090..36e13ac79f 100644 --- a/cocos2dx/label_nodes/CCLabelBMFont.cpp +++ b/cocos2dx/label_nodes/CCLabelBMFont.cpp @@ -1,48 +1,48 @@ -/**************************************************************************** -Copyright (c) 2010-2011 cocos2d-x.org -Copyright (c) 2008-2010 Ricardo Quesada -Copyright (c) 2011 Zynga Inc. - -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. - -Use any of these editors to generate BMFonts: -http://glyphdesigner.71squared.com/ (Commercial, Mac OS X) -http://www.n4te.com/hiero/hiero.jnlp (Free, Java) -http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) -http://www.angelcode.com/products/bmfont/ (Free, Windows only) - -****************************************************************************/ -#include "CCLabelBMFont.h" - -#include "platform/platform.h" -#include "CCMutableDictionary.h" -#include "CCConfiguration.h" -#include "CCDrawingPrimitives.h" -#include "CCSprite.h" -#include "CCPointExtension.h" - -#include "CCFileUtils.h" -#include "support/data_support/uthash.h" - -namespace cocos2d{ +/**************************************************************************** +Copyright (c) 2010-2011 cocos2d-x.org +Copyright (c) 2008-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. + +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. + +Use any of these editors to generate BMFonts: +http://glyphdesigner.71squared.com/ (Commercial, Mac OS X) +http://www.n4te.com/hiero/hiero.jnlp (Free, Java) +http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) +http://www.angelcode.com/products/bmfont/ (Free, Windows only) + +****************************************************************************/ +#include "CCLabelBMFont.h" + +#include "platform/platform.h" +#include "CCMutableDictionary.h" +#include "CCConfiguration.h" +#include "CCDrawingPrimitives.h" +#include "CCSprite.h" +#include "CCPointExtension.h" + +#include "CCFileUtils.h" +#include "support/data_support/uthash.h" + +namespace cocos2d{ // //FNTConfig Cache - free functions @@ -69,11 +69,11 @@ namespace cocos2d{ void FNTConfigRemoveCache( void ) { - if (configurations) - { + if (configurations) + { configurations->removeAllObjects(); CC_SAFE_RELEASE_NULL(configurations); - } + } } // //Hash Element @@ -139,30 +139,31 @@ namespace cocos2d{ CCAssert(pBuffer, "CCBMFontConfiguration::parseConfigFile | Open file error."); - if (!pBuffer) - { - return; - } - - // parse spacing / padding - std::string line; - std::string strLeft(pBuffer, nBufSize); - while (strLeft.length() > 0) { - int pos = strLeft.find('\n'); - - if (pos != (int)std::string::npos) - { - // the data is more than a line.get one line - line = strLeft.substr(0, pos); - strLeft = strLeft.substr(pos + 1); - } - else - { - // get the left data - line = strLeft; - strLeft.erase(); - } - + if (!pBuffer) + { + return; + } + + // parse spacing / padding + std::string line; + std::string strLeft(pBuffer, nBufSize); + while (strLeft.length() > 0) + { + int pos = strLeft.find('\n'); + + if (pos != (int)std::string::npos) + { + // the data is more than a line.get one line + line = strLeft.substr(0, pos); + strLeft = strLeft.substr(pos + 1); + } + else + { + // get the left data + line = strLeft; + strLeft.erase(); + } + if(line.substr(0,strlen("info face")) == "info face") { // XXX: info parsing is incomplete @@ -199,16 +200,16 @@ namespace cocos2d{ else if(line.substr(0,strlen("kerning first")) == "kerning first") { this->parseKerningEntry(line); - } + } } } void CCBMFontConfiguration::parseImageFileName(std::string line, const char *fntFile) { - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// // line to parse: // page id=0 file="bitmapFontTest.png" - ////////////////////////////////////////////////////////////////////////// - + ////////////////////////////////////////////////////////////////////////// + // page ID. Sanity check int index = line.find('=')+1; int index2 = line.find(' ', index); @@ -223,11 +224,11 @@ namespace cocos2d{ } void CCBMFontConfiguration::parseInfoArguments(std::string line) { - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// // 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="); @@ -238,10 +239,10 @@ namespace cocos2d{ } void CCBMFontConfiguration::parseCommonArguments(std::string line) { - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// // line to parse: // common lineHeight=104 base=26 scaleW=1024 scaleH=512 pages=1 packed=0 - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// // Height int index = line.find("lineHeight="); @@ -268,10 +269,10 @@ namespace cocos2d{ } void CCBMFontConfiguration::parseCharacterDefinition(std::string line, ccBMFontDef *characterDefinition) { - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// // 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="); @@ -337,11 +338,11 @@ namespace cocos2d{ // kerningDictionary = ccHashSetNew(capacity, targetSetEql); } void CCBMFontConfiguration::parseKerningEntry(std::string line) - { - ////////////////////////////////////////////////////////////////////////// + { + ////////////////////////////////////////////////////////////////////////// // line to parse: // kerning first=121 second=44 amount=-7 - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// // first int first; @@ -400,17 +401,17 @@ namespace cocos2d{ m_pConfiguration->retain(); CCAssert( m_pConfiguration, "Error creating config for LabelBMFont"); - if (CCSpriteBatchNode::initWithFile(m_pConfiguration->m_sAtlasName.c_str(), strlen(theString))) - { - m_cOpacity = 255; - m_tColor = ccWHITE; - m_tContentSize = CCSizeZero; - m_bIsOpacityModifyRGB = m_pobTextureAtlas->getTexture()->getHasPremultipliedAlpha(); - setAnchorPoint(ccp(0.5f, 0.5f)); - this->setString(theString); - return true; - } - return false; + if (CCSpriteBatchNode::initWithFile(m_pConfiguration->m_sAtlasName.c_str(), strlen(theString))) + { + m_cOpacity = 255; + m_tColor = ccWHITE; + m_tContentSize = CCSizeZero; + m_bIsOpacityModifyRGB = m_pobTextureAtlas->getTexture()->getHasPremultipliedAlpha(); + setAnchorPoint(ccp(0.5f, 0.5f)); + this->setString(theString); + return true; + } + return false; } CCLabelBMFont::~CCLabelBMFont() { @@ -441,41 +442,41 @@ namespace cocos2d{ CCSize tmpSize = CCSizeZero; - int longestLine = 0; - unsigned int totalHeight = 0; - + int longestLine = 0; + unsigned int totalHeight = 0; + unsigned int quantityOfLines = 1; unsigned int stringLen = m_sString.length(); - if (0 == stringLen) - { - return; - } - - for (unsigned int i = 0; i < stringLen - 1; ++i) - { - unsigned short c = m_sString[i]; - if (c == '\n') - { - quantityOfLines++; - } - } - - totalHeight = m_pConfiguration->m_uCommonHeight * quantityOfLines; - nextFontPositionY = -(m_pConfiguration->m_uCommonHeight - m_pConfiguration->m_uCommonHeight * quantityOfLines); + if (0 == stringLen) + { + return; + } + + for (unsigned int i = 0; i < stringLen - 1; ++i) + { + unsigned short c = m_sString[i]; + if (c == '\n') + { + quantityOfLines++; + } + } + + totalHeight = m_pConfiguration->m_uCommonHeight * quantityOfLines; + nextFontPositionY = -(m_pConfiguration->m_uCommonHeight - m_pConfiguration->m_uCommonHeight * quantityOfLines); for (unsigned int i= 0; i < stringLen; i++) { unsigned short c = m_sString[i]; CCAssert( c < kCCBMFontMaxChars, "LabelBMFont: character outside bounds"); - if (c == '\n') - { - nextFontPositionX = 0; - nextFontPositionY -= m_pConfiguration->m_uCommonHeight; - continue; - } + if (c == '\n') + { + nextFontPositionX = 0; + nextFontPositionY -= m_pConfiguration->m_uCommonHeight; + continue; + } kerningAmount = this->kerningAmountForFirst(prev, c); @@ -525,13 +526,13 @@ namespace cocos2d{ fontChar->setOpacity(m_cOpacity); } - if (longestLine < nextFontPositionX) - { + if (longestLine < nextFontPositionX) + { longestLine = nextFontPositionX; } } - tmpSize.width = (float) longestLine; + tmpSize.width = (float) longestLine; tmpSize.height = (float) totalHeight; this->setContentSizeInPixels(tmpSize); @@ -543,17 +544,18 @@ namespace cocos2d{ m_sString.clear(); m_sString = newString; - if (m_pChildren && m_pChildren->count() != 0) - { + if (m_pChildren && m_pChildren->count() != 0) + { CCObject* child; CCARRAY_FOREACH(m_pChildren, child) { - CCNode* pNode = (CCNode*) child; - if (pNode) - { - pNode->setIsVisible(false); - } - } } + CCNode* pNode = (CCNode*) child; + if (pNode) + { + pNode->setIsVisible(false); + } + } + } this->createFontChars(); } @@ -571,17 +573,18 @@ namespace cocos2d{ void CCLabelBMFont::setColor(const ccColor3B& var) { m_tColor = var; - if (m_pChildren && m_pChildren->count() != 0) - { + if (m_pChildren && m_pChildren->count() != 0) + { CCObject* child; CCARRAY_FOREACH(m_pChildren, child) { - CCSprite* pNode = (CCSprite*) child; - if (pNode) - { - pNode->setColor(m_tColor); - } - } } + CCSprite* pNode = (CCSprite*) child; + if (pNode) + { + pNode->setColor(m_tColor); + } + } + } } const ccColor3B& CCLabelBMFont::getColor() { @@ -591,21 +594,22 @@ namespace cocos2d{ { m_cOpacity = var; - if (m_pChildren && m_pChildren->count() != 0) - { + if (m_pChildren && m_pChildren->count() != 0) + { CCObject* child; CCARRAY_FOREACH(m_pChildren, child) { - CCNode* pNode = (CCNode*) child; - if (pNode) - { - CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol(); - if (pRGBAProtocol) - { - pRGBAProtocol->setOpacity(m_cOpacity); - } - } - } } + CCNode* pNode = (CCNode*) child; + if (pNode) + { + CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol(); + if (pRGBAProtocol) + { + pRGBAProtocol->setOpacity(m_cOpacity); + } + } + } + } } GLubyte CCLabelBMFont::getOpacity() { @@ -614,21 +618,22 @@ namespace cocos2d{ void CCLabelBMFont::setIsOpacityModifyRGB(bool var) { m_bIsOpacityModifyRGB = var; - if (m_pChildren && m_pChildren->count() != 0) - { + if (m_pChildren && m_pChildren->count() != 0) + { CCObject* child; CCARRAY_FOREACH(m_pChildren, child) { - CCNode* pNode = (CCNode*) child; - if (pNode) - { - CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol(); - if (pRGBAProtocol) - { - pRGBAProtocol->setIsOpacityModifyRGB(m_bIsOpacityModifyRGB); - } - } - } } + CCNode* pNode = (CCNode*) child; + if (pNode) + { + CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol(); + if (pRGBAProtocol) + { + pRGBAProtocol->setIsOpacityModifyRGB(m_bIsOpacityModifyRGB); + } + } + } + } } bool CCLabelBMFont::getIsOpacityModifyRGB() { @@ -658,5 +663,5 @@ namespace cocos2d{ ccDrawPoly(vertices, 4, true); } #endif // CC_LABELBMFONT_DEBUG_DRAW - -} + +}