From c9db868d93cabefc716bfba4941316c82c278a51 Mon Sep 17 00:00:00 2001 From: Nicolas Gramlich Date: Tue, 5 Jun 2012 17:12:41 -0700 Subject: [PATCH] CCLabelTTF: Now internally using std::string instead of std::string *. --- cocos2dx/label_nodes/CCLabelTTF.cpp | 41 +++++++++++------------------ cocos2dx/label_nodes/CCLabelTTF.h | 7 ++--- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/cocos2dx/label_nodes/CCLabelTTF.cpp b/cocos2dx/label_nodes/CCLabelTTF.cpp index 20b03b5b1e..ae75392954 100644 --- a/cocos2dx/label_nodes/CCLabelTTF.cpp +++ b/cocos2dx/label_nodes/CCLabelTTF.cpp @@ -40,16 +40,13 @@ NS_CC_BEGIN // CCLabelTTF::CCLabelTTF() : m_eAlignment(CCTextAlignmentCenter) - , m_pFontName(NULL) , m_fFontSize(0.0) - , m_pString(NULL) { } CCLabelTTF::~CCLabelTTF() { - CC_SAFE_DELETE(m_pFontName); - CC_SAFE_DELETE(m_pString); + } CCLabelTTF * CCLabelTTF::node() @@ -105,8 +102,7 @@ bool CCLabelTTF::initWithString(const char *label, const CCSize& dimensions, CCT m_tDimensions = CCSizeMake( dimensions.width * CC_CONTENT_SCALE_FACTOR(), dimensions.height * CC_CONTENT_SCALE_FACTOR() ); m_eAlignment = alignment; - CC_SAFE_DELETE(m_pFontName); - m_pFontName = new std::string(fontName); + m_sFontName = fontName; m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR(); this->setString(label); @@ -124,8 +120,7 @@ bool CCLabelTTF::initWithString(const char *label, const char *fontName, float f m_tDimensions = CCSizeZero; - CC_SAFE_DELETE(m_pFontName); - m_pFontName = new std::string(fontName); + m_sFontName = fontName; m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR(); this->setString(label); @@ -135,16 +130,15 @@ bool CCLabelTTF::initWithString(const char *label, const char *fontName, float f } const char * CCLabelTTF::getFontName() { - return this->m_pFontName->c_str(); + return this->m_sFontName.c_str(); } void CCLabelTTF::setFontName(const char *fontName) { - if(this->m_pFontName == NULL || strcmp(this->m_pFontName->c_str(), fontName)) + if(strcmp(this->m_sFontName.c_str(), fontName) != 0) { - CC_SAFE_DELETE(m_pFontName); - this->m_pFontName = new std::string(fontName); + this->m_sFontName = fontName; - this->setString(this->m_pString->c_str()); + this->setString(this->m_sString.c_str()); } } float CCLabelTTF::getFontSize() @@ -157,7 +151,7 @@ void CCLabelTTF::setFontSize(float fontSize) { this->m_fFontSize = fontSize; - this->setString(this->m_pString->c_str()); + this->setString(this->m_sString.c_str()); } } void CCLabelTTF::setDimensions(CCSize dimensions) @@ -166,7 +160,7 @@ void CCLabelTTF::setDimensions(CCSize dimensions) { this->m_tDimensions = dimensions; - this->setString(this->m_pString->c_str()); + this->setString(this->m_sString.c_str()); } } void CCLabelTTF::setHorizontalAlignment(CCTextAlignment pCCTextAlignment) @@ -175,28 +169,23 @@ void CCLabelTTF::setHorizontalAlignment(CCTextAlignment pCCTextAlignment) { this->m_eAlignment = pCCTextAlignment; - this->setString(this->m_pString->c_str()); + this->setString(this->m_sString.c_str()); } } void CCLabelTTF::setString(const char *label) { - if (m_pString) - { - delete m_pString; - m_pString = NULL; - } - m_pString = new std::string(label); + m_sString = label; CCTexture2D *texture; if( CCSize::CCSizeEqualToSize( m_tDimensions, CCSizeZero ) ) { texture = new CCTexture2D(); - texture->initWithString(label, m_pFontName->c_str(), m_fFontSize); + texture->initWithString(label, m_sFontName.c_str(), m_fFontSize); } else { texture = new CCTexture2D(); - texture->initWithString(label, m_tDimensions, m_eAlignment, m_pFontName->c_str(), m_fFontSize); + texture->initWithString(label, m_tDimensions, m_eAlignment, m_sFontName.c_str(), m_fFontSize); } // TODO @@ -227,12 +216,12 @@ void CCLabelTTF::setString(const char *label) const char* CCLabelTTF::getString(void) { - return m_pString->c_str(); + return m_sString.c_str(); } const char* CCLabelTTF::description() { - return CCString::stringWithFormat("", m_pFontName->c_str(), m_fFontSize)->getCString(); + return CCString::stringWithFormat("", m_sFontName.c_str(), m_fFontSize)->getCString(); } NS_CC_END diff --git a/cocos2dx/label_nodes/CCLabelTTF.h b/cocos2dx/label_nodes/CCLabelTTF.h index 65bea47cea..9a6ba56ccf 100644 --- a/cocos2dx/label_nodes/CCLabelTTF.h +++ b/cocos2dx/label_nodes/CCLabelTTF.h @@ -55,11 +55,12 @@ public: /** Creates an label. */ static CCLabelTTF * node(); + + virtual const char* getString(); /** changes the string to render * @warning Changing the string is as expensive as creating a new CCLabelTTF. To obtain better performance use CCLabelAtlas */ virtual void setString(const char *label); - virtual const char* getString(void); virtual const char* getFontName(); virtual void setFontName(const char *fontName); virtual float getFontSize(); @@ -74,10 +75,10 @@ protected: CCSize m_tDimensions; CCTextAlignment m_eAlignment; /** Font name used in the label */ - std::string * m_pFontName; + std::string m_sFontName; /** Font size of the label */ float m_fFontSize; - std::string * m_pString; + std::string m_sString; }; NS_CC_END