Skip to content

Commit

Permalink
Merge pull request cocos2d#860 from cardboardBox/master
Browse files Browse the repository at this point in the history
fixed cocos2d#1189: CCLabelBMFont updateLabel() optimizations and fixes.
  • Loading branch information
James Chen committed Apr 25, 2012
2 parents 0ea037d + 0bb0bfb commit 725103f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
13 changes: 9 additions & 4 deletions cocos2dx/include/CCLabelBMFont.h
Expand Up @@ -42,8 +42,8 @@ Use any of these editors to generate BMFonts:

NS_CC_BEGIN

enum {
kCCLabelAutomaticWidth = -1,
enum {
kCCLabelAutomaticWidth = -1,
};

struct _KerningHashElement;
Expand Down Expand Up @@ -89,7 +89,7 @@ class CC_DLL CCBMFontConfiguration : public CCObject
{
// XXX: Creating a public interface so that the bitmapFontArray[] is accesible
public://@public
// BMFont definitions
// BMFont definitions
struct _FontDefHashElement* m_pFontDefDictionary;

//! FNTConfig: Common Height
Expand Down Expand Up @@ -166,7 +166,7 @@ class CC_DLL CCLabelBMFont : public CCSpriteBatchNode, public CCLabelProtocol, p
CCTextAlignment m_pAlignment;
float m_fWidth;
bool m_bLineBreakWithoutSpaces;
// offset of the texture atlas
// offset of the texture atlas
CCPoint m_tImageOffset;
public:
CCLabelBMFont();
Expand Down Expand Up @@ -199,13 +199,18 @@ class CC_DLL CCLabelBMFont : public CCSpriteBatchNode, public CCLabelProtocol, p
virtual void setAlignment(CCTextAlignment alignment);
virtual void setWidth(float width);
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace);
virtual void setScale(float scale);
virtual void setScaleX(float scaleX);
virtual void setScaleY(float scaleY);

#if CC_LABELBMFONT_DEBUG_DRAW
virtual void draw();
#endif // CC_LABELBMFONT_DEBUG_DRAW
private:
char * atlasNameFromFntFile(const char *fntFile);
int kerningAmountForFirst(unsigned short first, unsigned short second);
float getLetterPosXLeft( CCSprite* characterSprite );
float getLetterPosXRight( CCSprite* characterSprite );

};

Expand Down
72 changes: 48 additions & 24 deletions cocos2dx/label_nodes/CCLabelBMFont.cpp
Expand Up @@ -911,7 +911,7 @@ void CCLabelBMFont::createFontChars()
}

float yOffset = (float)(m_pConfiguration->m_uCommonHeight) - fontDef.yOffset;
fontChar->setPositionInPixels( ccp( nextFontPositionX + fontDef.xOffset + fontDef.rect.size.width / 2.0f + kerningAmount,
fontChar->setPositionInPixels( ccp( nextFontPositionX + fontDef.xOffset + fontDef.rect.size.width / 2.0f + kerningAmount,
(float) nextFontPositionY + yOffset - rect.size.height/2.0f ) );

// update kerning
Expand Down Expand Up @@ -951,15 +951,9 @@ void CCLabelBMFont::setString(const char *newString)

void CCLabelBMFont::setString(const char *newString, bool fromUpdate)
{
if (fromUpdate)
{
CC_SAFE_DELETE_ARRAY(m_sString);
m_sString = cc_utf8_from_cstr(newString);
}
else
{
m_sString_initial = newString;
}
CC_SAFE_DELETE_ARRAY(m_sString);
m_sString = cc_utf8_from_cstr(newString);
m_sString_initial = newString;

updateString(fromUpdate);
}
Expand Down Expand Up @@ -1079,16 +1073,20 @@ void CCLabelBMFont::setAnchorPoint(const CCPoint& point)
// LabelBMFont - Alignment
void CCLabelBMFont::updateLabel()
{
this->setString(m_sString_initial.c_str(), true);

if (m_fWidth > 0)
{
// Step 1: Make multiline
vector<unsigned short> str_whole = cc_utf8_vec_from_cstr(m_sString);

unsigned int stringLength = str_whole.size();

vector<unsigned short> multiline_string;
multiline_string.reserve( stringLength );

vector<unsigned short> last_word;
int line = 1, i = 0;
last_word.reserve( stringLength );

unsigned int line = 1, i = 0;
bool start_line = false, start_word = false;
float startOfLine = -1, startOfWord = -1;
int skip = 0;
Expand All @@ -1110,7 +1108,7 @@ void CCLabelBMFont::updateLabel()

if (!start_word)
{
startOfWord = characterSprite->getPosition().x - characterSprite->getContentSize().width/2.0f;
startOfWord = getLetterPosXLeft( characterSprite );
start_word = true;
}
if (!start_line)
Expand All @@ -1124,10 +1122,7 @@ void CCLabelBMFont::updateLabel()
{
cc_utf8_trim_ws(&last_word);

// int found = cc_utf8_find_char(last_word, ' ');
// if (found != -1)
// cc_utf8_trim_from(&last_word, found + 1);

last_word.push_back('\n');
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
last_word.clear();
start_word = false;
Expand All @@ -1144,7 +1139,7 @@ void CCLabelBMFont::updateLabel()

if (!startOfWord)
{
startOfWord = characterSprite->getPosition().x - characterSprite->getContentSize().width/2.0f;
startOfWord = getLetterPosXLeft( characterSprite );
start_word = true;
}
if (!startOfLine)
Expand All @@ -1167,8 +1162,7 @@ void CCLabelBMFont::updateLabel()
}

// Out of bounds.
if (characterSprite->getPosition().x + characterSprite->getContentSize().width / 2.0f - startOfLine
> m_fWidth )
if ( getLetterPosXRight( characterSprite ) - startOfLine > m_fWidth )
{
if (!m_bLineBreakWithoutSpaces)
{
Expand Down Expand Up @@ -1206,7 +1200,7 @@ void CCLabelBMFont::updateLabel()

if (!startOfWord)
{
startOfWord = characterSprite->getPosition().x - characterSprite->getContentSize().width/2.0f;
startOfWord = getLetterPosXLeft( characterSprite );
start_word = true;
}
if (!startOfLine)
Expand Down Expand Up @@ -1257,11 +1251,13 @@ void CCLabelBMFont::updateLabel()
if (m_sString[ctr] == '\n' || m_sString[ctr] == 0)
{
float lineWidth = 0.0f;
int line_length = last_line.size();
unsigned int line_length = last_line.size();
int index = i + line_length - 1 + lineNumber;
if (index < 0) continue;

CCSprite* lastChar = (CCSprite*)getChildByTag(index);
if ( lastChar == NULL )
continue;

lineWidth = lastChar->getPosition().x + lastChar->getContentSize().width/2.0f;

Expand All @@ -1279,7 +1275,7 @@ void CCLabelBMFont::updateLabel()

if (shift != 0)
{
for (int j = 0; j < line_length; j++)
for (unsigned j = 0; j < line_length; j++)
{
index = i + j + lineNumber;
if (index < 0) continue;
Expand Down Expand Up @@ -1320,6 +1316,34 @@ void CCLabelBMFont::setLineBreakWithoutSpace( bool breakWithoutSpace )
updateLabel();
}

void CCLabelBMFont::setScale(float scale)
{
CCSpriteBatchNode::setScale(scale);
updateLabel();
}

void CCLabelBMFont::setScaleX(float scaleX)
{
CCSpriteBatchNode::setScaleX(scaleX);
updateLabel();
}

void CCLabelBMFont::setScaleY(float scaleY)
{
CCSpriteBatchNode::setScaleY(scaleY);
updateLabel();
}

float CCLabelBMFont::getLetterPosXLeft( CCSprite* sp )
{
return sp->getPosition().x * m_fScaleX - (sp->getContentSize().width * m_fScaleX * sp->getAnchorPoint().x);
}

float CCLabelBMFont::getLetterPosXRight( CCSprite* sp )
{
return sp->getPosition().x * m_fScaleX + (sp->getContentSize().width * m_fScaleX * sp->getAnchorPoint().x);
}

//LabelBMFont - Debug draw
#if CC_LABELBMFONT_DEBUG_DRAW
void CCLabelBMFont::draw()
Expand Down

0 comments on commit 725103f

Please sign in to comment.