diff --git a/cocos2dx/label_nodes/CCFontDefinition.cpp b/cocos2dx/label_nodes/CCFontDefinition.cpp index 804d904974..4f956c0583 100644 --- a/cocos2dx/label_nodes/CCFontDefinition.cpp +++ b/cocos2dx/label_nodes/CCFontDefinition.cpp @@ -32,6 +32,27 @@ FontDefinitionTTF::FontDefinitionTTF():_textImages(0), _commonLineHeight(0) { } + + +FontDefinitionTTF* FontDefinitionTTF::create(const char *fontName, int fontSize, const char *letters, int textureSize ) +{ + FontDefinitionTTF *ret = new FontDefinitionTTF; + + if(!ret) + return 0; + + if ( ret->initDefinition( fontName, fontSize, letters, textureSize ) ) + { + ret->autorelease(); + return ret; + } + else + { + delete ret; + return 0; + } +} + FontDefinitionTTF::~FontDefinitionTTF() { if (_textImages) @@ -117,17 +138,18 @@ bool FontDefinitionTTF::prepareLetterDefinitions(TextFontPagesDef *pageDefs) // store the common line height _commonLineHeight = maxLineHeight; + // return true; } -bool FontDefinitionTTF::createFontDefinition(char *fontName, int fontSize, char *letters, int textureSize, bool debugOutput) +bool FontDefinitionTTF::initDefinition(const char *fontName, int fontSize, const char *letters, int textureSize) { // preare texture/image stuff _textImages = new TextImage(); if (!_textImages) return false; - if (!_textImages->initWithString(letters, textureSize, textureSize, fontName, fontSize, !debugOutput)) + if (!_textImages->initWithString(letters, textureSize, textureSize, fontName, fontSize, true)) { delete _textImages; _textImages = 0; diff --git a/cocos2dx/label_nodes/CCFontDefinition.h b/cocos2dx/label_nodes/CCFontDefinition.h index 51966414dd..7e94de6899 100644 --- a/cocos2dx/label_nodes/CCFontDefinition.h +++ b/cocos2dx/label_nodes/CCFontDefinition.h @@ -44,26 +44,28 @@ struct LetterDefinition float commonLineHeight; }; -class FontDefinitionTTF +/** + */ +class FontDefinitionTTF : public Object { public: - - FontDefinitionTTF(); - ~FontDefinitionTTF(); - - bool createFontDefinition(char *fontName, int fontSize, char *letters, int textureSize = 512, bool debugOutput = false); + + static FontDefinitionTTF* create(const char *fontName, int fontSize, const char *letters, int textureSize = 512); LetterDefinition & getLetterDefinition(unsigned short int theLetter); Texture2D * getTexture(int index); - int getNumTextures(); + int getNumTextures(); + Font * getFont() { return _textImages->getFont(); } + float getCommonLineHeight() { return _commonLineHeight; } - Font * getFont() { return _textImages->getFont(); } - float getCommonLineHeight() { return _commonLineHeight; } - - // carloX + // carloX new FontAtlas * createFontAtlas(); private: + FontDefinitionTTF(); + ~FontDefinitionTTF(); + + bool initDefinition(const char *fontName, int fontSize, const char *letters, int textureSize); bool prepareLetterDefinitions(TextFontPagesDef *pageDefs); void addLetterDefinition(LetterDefinition &defToAdd); diff --git a/cocos2dx/label_nodes/CCStringTTF.cpp b/cocos2dx/label_nodes/CCStringTTF.cpp index 053df9c2d7..00a63269e5 100644 --- a/cocos2dx/label_nodes/CCStringTTF.cpp +++ b/cocos2dx/label_nodes/CCStringTTF.cpp @@ -35,6 +35,29 @@ StringTTF::StringTTF(FontDefinitionTTF *theDef, TextAlignment alignment) : _cu _lineBreakWithoutSpaces(false), _advances(0) { + if (_fontDef) + _fontDef->retain(); +} + +StringTTF* StringTTF::create(FontDefinitionTTF *def, TextAlignment alignment) +{ + StringTTF *ret = new StringTTF(def, alignment); + + if (!ret) + return 0; + + if( ret->init() ) + { + ret->autorelease(); + return ret; + } + else + { + delete ret; + return 0; + } + + return ret; } StringTTF::~StringTTF() @@ -50,9 +73,17 @@ StringTTF::~StringTTF() delete [] _advances; _advances = 0; } + + if (_fontDef) + _fontDef->release(); } -bool StringTTF::setText(char *pStringToRender, float lineWidth, TextAlignment alignment, bool lineBreakWithoutSpaces) +bool StringTTF::init() +{ + return true; +} + +bool StringTTF::setText(const char *stringToRender, float lineWidth, TextAlignment alignment, bool lineBreakWithoutSpaces) { if (!_fontDef) return false; @@ -75,7 +106,8 @@ bool StringTTF::setText(char *pStringToRender, float lineWidth, TextAlignment al return false; int numLetter = 0; - unsigned short int *pUTF8Text = pFont->getUTF16Text(pStringToRender, numLetter); + unsigned short int *pUTF8Text = pFont->getUTF16Text(stringToRender, numLetter); + setCurrentString(pUTF8Text); // align text diff --git a/cocos2dx/label_nodes/CCStringTTF.h b/cocos2dx/label_nodes/CCStringTTF.h index 9559452c3e..db701d48cb 100644 --- a/cocos2dx/label_nodes/CCStringTTF.h +++ b/cocos2dx/label_nodes/CCStringTTF.h @@ -34,12 +34,10 @@ class StringTTF : public Node , public LabelTextFormatProtocol, public RGBAProto { public: - StringTTF(FontDefinitionTTF *pDefinition, TextAlignment alignment = kTextAlignmentLeft); - ~StringTTF(); - - bool setText(char *pStringToRender, float lineWidth, TextAlignment alignment = kTextAlignmentLeft, bool lineBreakWithoutSpaces = false); + static StringTTF* create(FontDefinitionTTF *pDefinition, TextAlignment alignment = kTextAlignmentLeft); // main interface + bool setText(const char *pStringToRender, float lineWidth, TextAlignment alignment = kTextAlignmentLeft, bool lineBreakWithoutSpaces = false); virtual void setAlignment(TextAlignment alignment); virtual void setWidth(float width); virtual void setLineBreakWithoutSpace(bool breakWithoutSpace); @@ -99,6 +97,10 @@ public: private: + StringTTF(FontDefinitionTTF *pDefinition, TextAlignment alignment = kTextAlignmentLeft); + ~StringTTF(); + + bool init(); void alignText(); void hideAllLetters(); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp index cfe3f6f2d3..b3d39a4db6 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp @@ -1658,16 +1658,30 @@ std::string LabelBMFontNewTest::subtitle() LabelDyamicTest::LabelDyamicTest() { Size size = Director::sharedDirector()->getWinSize(); - const char *pFontFileName = "fonts/arial.ttf"; - const char *pFontGlyphs = "abcdefghilmnopqrstuvzxywABCDEFGHILMNOPQRSTUVZXYW0123456789,. "; - FontDefinitionTTF *pDef = new FontDefinitionTTF; + const char *fontGlyphs = "abcdefghilmnopqrstuvzxywABCDEFGHILMNOPQRSTUVZXYW0123456789,. "; + FontDefinitionTTF *def = FontDefinitionTTF::create(pFontFileName, 26, fontGlyphs); - pDef->createFontDefinition((char *)pFontFileName, 26, (char *) pFontGlyphs); - StringTTF *pTestString = new StringTTF(pDef); - pTestString->setText( (char *)LongSentencesExample, size.width, kTextAlignmentCenter, false); - addChild(pTestString); - pTestString->setPosition(Point(0, size.height/2)); + if ( def ) + { + label1 = StringTTF::create(def); + + if (label1) + { + label1->setText( LongSentencesExample, size.width, kTextAlignmentCenter, false); + addChild(label1); + label1->setPosition(Point(0, size.height/2)); + label1->retain(); + } + } +} + +LabelDyamicTest::~LabelDyamicTest() +{ + if (label1) + { + label1->release(); + } } void LabelDyamicTest::draw() diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.h b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.h index 40f4fd6013..b083390787 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.h +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.h @@ -331,13 +331,18 @@ private: class LabelDyamicTest : public AtlasDemo { public: - LabelDyamicTest(); + + LabelDyamicTest(); + ~LabelDyamicTest(); virtual void draw(); virtual std::string title(); virtual std::string subtitle(); + private: + StringTTF *label1; + };