Merge pull request #870 from dumganhar/iss_1186_gles20_bmf

fixed #1189: syn 1.0 branch: CCLabelBMFont updateLabel() optimizations and fixes
This commit is contained in:
James Chen 2012-04-25 03:19:39 -07:00
commit f54d9b4184
11 changed files with 351 additions and 326 deletions

View File

@ -83,9 +83,9 @@ bool CCConfiguration::init(void)
bEnableProfilers ? "YES - *** Disable it when you finish profiling ***" : "NO");
#if CC_ENABLE_GL_STATE_CACHE == 0
CCLOG("");
CCLOG("cocos2d: **** WARNING **** CC_ENABLE_GL_STATE_CACHE is disabled. To improve performance, enable it by editing ccConfig.h");
printf("\n");
CCLOG("");
CCLOG("cocos2d: **** WARNING **** CC_ENABLE_GL_STATE_CACHE is disabled. To improve performance, enable it by editing ccConfig.h");
printf("\n");
#endif
CHECK_GL_ERROR_DEBUG();

View File

@ -954,15 +954,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);
}
@ -1082,16 +1076,17 @@ 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;
@ -1113,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)
@ -1127,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;
@ -1147,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)
@ -1170,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)
{
@ -1209,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)
@ -1260,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;
@ -1323,6 +1316,33 @@ 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()

View File

@ -198,6 +198,9 @@ public:
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();
@ -205,6 +208,8 @@ public:
private:
char * atlasNameFromFntFile(const char *fntFile);
int kerningAmountForFirst(unsigned short first, unsigned short second);
float getLetterPosXLeft( CCSprite* characterSprite );
float getLetterPosXRight( CCSprite* characterSprite );
};

View File

@ -229,19 +229,19 @@ void CCRenderTexture::end(bool bIsTOCacheTexture)
{
CC_SAFE_DELETE(m_pUITextureImage);
// to get the rendered texture data
const CCSize& s = m_pTexture->getContentSizeInPixels();
// to get the rendered texture data
const CCSize& s = m_pTexture->getContentSizeInPixels();
m_pUITextureImage = newCCImage();
if (m_pUITextureImage)
{
VolatileTexture::addDataTexture(m_pTexture, m_pUITextureImage->getData(), kTexture2DPixelFormat_RGBA8888, s);
}
else
{
CCLOG("Cache rendertexture failed!");
}
}
if (m_pUITextureImage)
{
VolatileTexture::addDataTexture(m_pTexture, m_pUITextureImage->getData(), kTexture2DPixelFormat_RGBA8888, s);
}
else
{
CCLOG("Cache rendertexture failed!");
}
}
#endif
}