From a765e5e7e1d54cc9f2557734693d6cf1f2d05ea2 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Thu, 16 Jan 2014 16:37:29 +0800 Subject: [PATCH 1/6] closed #3628:Integrate LabelAtlas with new Label --- cocos/2d/CCFont.cpp | 16 ++ cocos/2d/CCFont.h | 4 + cocos/2d/CCFontAtlasCache.cpp | 59 ++++++ cocos/2d/CCFontAtlasCache.h | 4 + cocos/2d/CCFontAtlasFactory.cpp | 42 +++++ cocos/2d/CCFontAtlasFactory.h | 4 + cocos/2d/CCFontCharMap.cpp | 171 ++++++++++++++++++ cocos/2d/CCFontCharMap.h | 69 +++++++ cocos/2d/CCLabel.cpp | 87 +++++++++ cocos/2d/CCLabel.h | 18 +- cocos/2d/cocos2d.vcxproj | 2 + cocos/2d/cocos2d.vcxproj.filters | 6 + .../Classes/LabelTest/LabelTestNew.cpp | 47 +++++ .../TestCpp/Classes/LabelTest/LabelTestNew.h | 16 ++ 14 files changed, 540 insertions(+), 5 deletions(-) create mode 100644 cocos/2d/CCFontCharMap.cpp create mode 100644 cocos/2d/CCFontCharMap.h diff --git a/cocos/2d/CCFont.cpp b/cocos/2d/CCFont.cpp index dc363cfe02..1c2d3bab86 100644 --- a/cocos/2d/CCFont.cpp +++ b/cocos/2d/CCFont.cpp @@ -28,6 +28,7 @@ #include "CCFontFNT.h" #include "CCFontFreeType.h" +#include "CCFontCharMap.h" #include "edtaa3func.h" NS_CC_BEGIN @@ -116,6 +117,21 @@ Font* Font::createWithFNT(const std::string& fntFilePath) return FontFNT::create(fntFilePath); } +Font* Font::createWithCharMap(const std::string& plistFile) +{ + return FontCharMap::create(plistFile); +} + +Font* Font::createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) +{ + return FontCharMap::create(texture,itemWidth,itemHeight,startCharMap); +} + +Font* Font::createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) +{ + return FontCharMap::create(charMapFile,itemWidth,itemHeight,startCharMap); +} + unsigned char * Font::makeDistanceMap( unsigned char *img, unsigned int width, unsigned int height) { unsigned int pixelAmount = (width + 2 * DistanceMapSpread) * (height + 2 * DistanceMapSpread); diff --git a/cocos/2d/CCFont.h b/cocos/2d/CCFont.h index 440da0808b..abbbf0098d 100644 --- a/cocos/2d/CCFont.h +++ b/cocos/2d/CCFont.h @@ -45,6 +45,10 @@ public: // create the font static Font* createWithTTF(const std::string& fntName, int fontSize, GlyphCollection glyphs, const char *customGlyphs); static Font* createWithFNT(const std::string& fntFilePath); + + static Font * createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); + static Font * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); + static Font * createWithCharMap(const std::string& plistFile); static unsigned char * makeDistanceMap(unsigned char *img, unsigned int width, unsigned int height); void setDistanceFieldEnabled(bool distanceFieldEnabled); diff --git a/cocos/2d/CCFontAtlasCache.cpp b/cocos/2d/CCFontAtlasCache.cpp index ccea76b254..3850791f7d 100644 --- a/cocos/2d/CCFontAtlasCache.cpp +++ b/cocos/2d/CCFontAtlasCache.cpp @@ -69,6 +69,65 @@ FontAtlas * FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName) return tempAtlas; } +FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile) +{ + std::string atlasName = generateFontName(plistFile, 0, GlyphCollection::CUSTOM,false); + FontAtlas *tempAtlas = _atlasMap[atlasName]; + + if ( !tempAtlas ) + { + tempAtlas = FontAtlasFactory::createAtlasFromCharMap(plistFile); + if (tempAtlas) + _atlasMap[atlasName] = tempAtlas; + } + else + { + tempAtlas->retain(); + } + + return tempAtlas; +} + +FontAtlas * FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) +{ + char tmp[30]; + sprintf_s(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap); + std::string atlasName = generateFontName(tmp, 0, GlyphCollection::CUSTOM,false); + FontAtlas *tempAtlas = _atlasMap[atlasName]; + + if ( !tempAtlas ) + { + tempAtlas = FontAtlasFactory::createAtlasFromCharMap(texture,itemWidth,itemHeight,startCharMap); + if (tempAtlas) + _atlasMap[atlasName] = tempAtlas; + } + else + { + tempAtlas->retain(); + } + + return tempAtlas; +} + +FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) +{ + std::string atlasName = generateFontName(charMapFile, 0, GlyphCollection::CUSTOM,false); + FontAtlas *tempAtlas = _atlasMap[atlasName]; + + if ( !tempAtlas ) + { + tempAtlas = FontAtlasFactory::createAtlasFromCharMap(charMapFile,itemWidth,itemHeight,startCharMap); + if (tempAtlas) + _atlasMap[atlasName] = tempAtlas; + } + else + { + tempAtlas->retain(); + } + + return tempAtlas; +} + std::string FontAtlasCache::generateFontName(const std::string& fontFileName, int size, GlyphCollection theGlyphs, bool useDistanceField) { std::string tempName(fontFileName); diff --git a/cocos/2d/CCFontAtlasCache.h b/cocos/2d/CCFontAtlasCache.h index bdc7dd24d1..f9e84ba904 100644 --- a/cocos/2d/CCFontAtlasCache.h +++ b/cocos/2d/CCFontAtlasCache.h @@ -41,6 +41,10 @@ public: static FontAtlas * getFontAtlasTTF(const std::string& fontFileName, int size, GlyphCollection glyphs, const char *customGlyphs = 0, bool useDistanceField = false); static FontAtlas * getFontAtlasFNT(const std::string& fontFileName); + + static FontAtlas * getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); + static FontAtlas * getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); + static FontAtlas * getFontAtlasCharMap(const std::string& plistFile); static bool releaseFontAtlas(FontAtlas *atlas); diff --git a/cocos/2d/CCFontAtlasFactory.cpp b/cocos/2d/CCFontAtlasFactory.cpp index c9d3ebbb3b..3b6d536223 100644 --- a/cocos/2d/CCFontAtlasFactory.cpp +++ b/cocos/2d/CCFontAtlasFactory.cpp @@ -60,4 +60,46 @@ FontAtlas * FontAtlasFactory::createAtlasFromFNT(const std::string& fntFilePath) } } +FontAtlas * FontAtlasFactory::createAtlasFromCharMap(const std::string& plistFile) +{ + Font *font = Font::createWithCharMap(plistFile); + + if(font) + { + return font->createFontAtlas(); + } + else + { + return nullptr; + } +} + +FontAtlas * FontAtlasFactory::createAtlasFromCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) +{ + Font *font = Font::createWithCharMap(texture,itemWidth,itemHeight,startCharMap); + + if(font) + { + return font->createFontAtlas(); + } + else + { + return nullptr; + } +} + +FontAtlas * FontAtlasFactory::createAtlasFromCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) +{ + Font *font = Font::createWithCharMap(charMapFile,itemWidth,itemHeight,startCharMap); + + if(font) + { + return font->createFontAtlas(); + } + else + { + return nullptr; + } +} + NS_CC_END diff --git a/cocos/2d/CCFontAtlasFactory.h b/cocos/2d/CCFontAtlasFactory.h index 2a10cc8d63..095ad134ec 100644 --- a/cocos/2d/CCFontAtlasFactory.h +++ b/cocos/2d/CCFontAtlasFactory.h @@ -39,6 +39,10 @@ public: static FontAtlas * createAtlasFromTTF(const std::string& fntFilePath, int fontSize, GlyphCollection glyphs, const char *customGlyphs = 0, bool useDistanceField = false); static FontAtlas * createAtlasFromFNT(const std::string& fntFilePath); + + static FontAtlas * createAtlasFromCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); + static FontAtlas * createAtlasFromCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); + static FontAtlas * createAtlasFromCharMap(const std::string& plistFile); private: }; diff --git a/cocos/2d/CCFontCharMap.cpp b/cocos/2d/CCFontCharMap.cpp new file mode 100644 index 0000000000..217312762b --- /dev/null +++ b/cocos/2d/CCFontCharMap.cpp @@ -0,0 +1,171 @@ +/**************************************************************************** + Copyright (c) 2013 Zynga Inc. + Copyright (c) 2013-2014 Chukong Technologies 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. + ****************************************************************************/ + +#include "CCFontCharMap.h" +#include "CCFontAtlas.h" + +NS_CC_BEGIN + +FontCharMap * FontCharMap::create(const std::string& plistFile) +{ + std::string pathStr = FileUtils::getInstance()->fullPathForFilename(plistFile); + std::string relPathStr = pathStr.substr(0, pathStr.find_last_of("/"))+"/"; + + ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr.c_str()); + + CCASSERT(dict["version"].asInt() == 1, "Unsupported version. Upgrade cocos2d version"); + + std::string textureFilename = relPathStr + dict["textureFilename"].asString(); + + unsigned int width = dict["itemWidth"].asInt() / CC_CONTENT_SCALE_FACTOR(); + unsigned int height = dict["itemHeight"].asInt() / CC_CONTENT_SCALE_FACTOR(); + unsigned int startChar = dict["firstChar"].asInt(); + + Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(textureFilename); + if (!tempTexture) + { + return nullptr; + } + + FontCharMap *tempFont = new FontCharMap(tempTexture,width,height,startChar); + + if (!tempFont) + { + return nullptr; + } + tempFont->autorelease(); + return tempFont; +} + +FontCharMap* FontCharMap::create(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) +{ + Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(charMapFile); + + if (!tempTexture) + { + return nullptr; + } + + FontCharMap *tempFont = new FontCharMap(tempTexture,itemWidth,itemHeight,startCharMap); + + if (!tempFont) + { + return nullptr; + } + tempFont->autorelease(); + return tempFont; +} + +FontCharMap* FontCharMap::create(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) +{ + FontCharMap *tempFont = new FontCharMap(texture,itemWidth,itemHeight,startCharMap); + + if (!tempFont) + { + return nullptr; + } + tempFont->autorelease(); + return tempFont; +} + +FontCharMap::~FontCharMap() +{ + +} + +Size * FontCharMap::getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const +{ + if (!text) + return 0; + + outNumLetters = cc_wcslen(text); + + if (!outNumLetters) + return 0; + + Size *sizes = new Size[outNumLetters]; + if (!sizes) + return 0; + + int advance = _itemWidth * CC_CONTENT_SCALE_FACTOR(); + for (int c = 0; c < outNumLetters; ++c) + { + sizes[c].width = advance; + } + + return sizes; +} + +Rect FontCharMap::getRectForChar(unsigned short theChar) const +{ + return _charRect; +} + +FontAtlas * FontCharMap::createFontAtlas() +{ + FontAtlas *tempAtlas = new FontAtlas(*this); + if (!tempAtlas) + return nullptr; + + Size s = _texture->getContentSize(); + + int itemsPerColumn = (int)(s.height / _itemHeight); + int itemsPerRow = (int)(s.width / _itemWidth); + + tempAtlas->setCommonLineHeight(_itemHeight); + + FontLetterDefinition tempDefinition; + tempDefinition.textureID = 0; + tempDefinition.anchorX = 0.5f; + tempDefinition.anchorY = 0.5f; + tempDefinition.offsetX = 0.0f; + tempDefinition.offsetY = 0.0f; + tempDefinition.validDefinition = true; + tempDefinition.width = _itemWidth; + tempDefinition.height = _itemHeight; + + int charId = _mapStartChar; + float itemWidthInPixels = _itemWidth * CC_CONTENT_SCALE_FACTOR(); + float itemHeightInPixels = _itemHeight * CC_CONTENT_SCALE_FACTOR(); + for (int row = 0; row < itemsPerColumn; ++row) + { + for (int col = 0; col < itemsPerRow; ++col) + { + tempDefinition.letteCharUTF16 = charId; + + tempDefinition.U = _itemWidth * col; + tempDefinition.V = _itemHeight * row; + + tempAtlas->addLetterDefinition(tempDefinition); + charId++; + } + } + + tempAtlas->addTexture(*_texture,0); + + return tempAtlas; +} + +NS_CC_END \ No newline at end of file diff --git a/cocos/2d/CCFontCharMap.h b/cocos/2d/CCFontCharMap.h new file mode 100644 index 0000000000..f77c855c08 --- /dev/null +++ b/cocos/2d/CCFontCharMap.h @@ -0,0 +1,69 @@ +/**************************************************************************** + Copyright (c) 2013 Zynga Inc. + Copyright (c) 2013-2014 Chukong Technologies 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. + ****************************************************************************/ + +#ifndef _CCFontCharMap_h_ +#define _CCFontCharMap_h_ + +#include "cocos2d.h" +#include "CCFont.h" + +NS_CC_BEGIN + +class FontCharMap : public Font +{ + +public: + + static FontCharMap * create(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); + static FontCharMap * create(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); + static FontCharMap * create(const std::string& plistFile); + + virtual Size* getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const override; + virtual Rect getRectForChar(unsigned short theChar) const override; + virtual FontAtlas *createFontAtlas() override; + +protected: + + FontCharMap(Texture2D* texture,int itemWidth, int itemHeight, int startCharMap) : + _texture(texture),_itemWidth(itemWidth),_itemHeight(itemHeight),_mapStartChar(startCharMap),_charRect(0,0,itemWidth,itemHeight){} + /** + * @js NA + * @lua NA + */ + virtual ~FontCharMap(); + +private: + + Texture2D* _texture; + int _mapStartChar; + int _itemWidth; + int _itemHeight; + + Rect _charRect; +}; + +NS_CC_END + +#endif /* defined(_CCFontCharMap_h_) */ diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index a267a41938..1bd74756e8 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -92,6 +92,93 @@ Label* Label::createWithBMFont(const std::string& bmfontFilePath, const std::str } } +Label* Label::createWithCharMap(const std::string& plistFile) +{ + Label *ret = new Label(); + + if (!ret) + return nullptr; + + if (ret->setCharMap(plistFile)) + { + ret->autorelease(); + return ret; + } + else + { + delete ret; + return nullptr; + } +} + +Label* Label::createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) +{ + Label *ret = new Label(); + + if (!ret) + return nullptr; + + if (ret->setCharMap(texture,itemWidth,itemHeight,startCharMap)) + { + ret->autorelease(); + return ret; + } + else + { + delete ret; + return nullptr; + } +} + +Label* Label::createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) +{ + Label *ret = new Label(); + + if (!ret) + return nullptr; + + if (ret->setCharMap(charMapFile,itemWidth,itemHeight,startCharMap)) + { + ret->autorelease(); + return ret; + } + else + { + delete ret; + return nullptr; + } +} + +bool Label::setCharMap(const std::string& plistFile) +{ + FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(plistFile); + + if (!newAtlas) + return false; + + return initWithFontAtlas(newAtlas); +} + +bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) +{ + FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(texture,itemWidth,itemHeight,startCharMap); + + if (!newAtlas) + return false; + + return initWithFontAtlas(newAtlas); +} + +bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) +{ + FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(charMapFile,itemWidth,itemHeight,startCharMap); + + if (!newAtlas) + return false; + + return initWithFontAtlas(newAtlas); +} + Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,bool useA8Shader) : _reusedLetter(nullptr) , _commonLineHeight(0.0f) diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index 26dcc89f9a..1e312d422d 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -62,12 +62,12 @@ typedef struct _ttfConfig const char *customGlyphs; bool distanceFieldEnabled; - _ttfConfig(const char* filePath,int fontSize = 36, const GlyphCollection& glyphs = GlyphCollection::NEHE, - const char *customGlyphs = nullptr,bool useDistanceField = false) + _ttfConfig(const char* filePath,int size = 36, const GlyphCollection& glyphCollection = GlyphCollection::NEHE, + const char *customGlyphCollection = nullptr,bool useDistanceField = false) :fontFilePath(filePath) - ,fontSize(fontSize) - ,glyphs(glyphs) - ,customGlyphs(customGlyphs) + ,fontSize(size) + ,glyphs(glyphCollection) + ,customGlyphs(customGlyphCollection) ,distanceFieldEnabled(useDistanceField) {} }TTFConfig; @@ -82,10 +82,18 @@ public: static Label* createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& alignment = TextHAlignment::CENTER, int lineWidth = 0); + static Label * createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); + static Label * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); + static Label * createWithCharMap(const std::string& plistFile); + bool setTTFConfig(const TTFConfig& ttfConfig); bool setBMFontFilePath(const std::string& bmfontFilePath); + bool setCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); + bool setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); + bool setCharMap(const std::string& plistFile); + bool setString(const std::string& text, const TextHAlignment& alignment = TextHAlignment::CENTER, float lineWidth = -1, bool lineBreakWithoutSpaces = false); //only support for TTF diff --git a/cocos/2d/cocos2d.vcxproj b/cocos/2d/cocos2d.vcxproj index 95f7f385fd..9c7bd8f40f 100644 --- a/cocos/2d/cocos2d.vcxproj +++ b/cocos/2d/cocos2d.vcxproj @@ -248,6 +248,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou + @@ -429,6 +430,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou + diff --git a/cocos/2d/cocos2d.vcxproj.filters b/cocos/2d/cocos2d.vcxproj.filters index 6c887936b6..6a28597e2d 100644 --- a/cocos/2d/cocos2d.vcxproj.filters +++ b/cocos/2d/cocos2d.vcxproj.filters @@ -601,6 +601,9 @@ renderer + + label_nodes + @@ -1213,5 +1216,8 @@ renderer + + label_nodes + \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp index c28585dfbe..604ff0cb8b 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.cpp @@ -67,6 +67,7 @@ static std::function createFunctions[] = CL(LabelBMFontTestNew), CL(LabelTTFDistanceField), CL(LabelTTFDistanceFieldEffect), + CL(LabelCharMapTest), CL(LabelCrashTest) }; @@ -1244,6 +1245,52 @@ std::string LabelTTFDistanceFieldEffect::subtitle() const return "Testing effect base on DistanceField"; } +LabelCharMapTest::LabelCharMapTest() +{ + _time = 0.0f; + + auto label1 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist"); + addChild(label1, 0, kTagSprite1); + label1->setPosition( Point(10,100) ); + label1->setOpacity( 200 ); + + auto label2 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist"); + addChild(label2, 0, kTagSprite2); + label2->setPosition( Point(10,160) ); + label2->setOpacity( 32 ); + + auto label3 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.png", 48, 64, ' '); + label3->setString("123 Test"); + addChild(label3, 0, kTagSprite3); + label3->setPosition( Point(10,220) ); + + schedule(schedule_selector(LabelCharMapTest::step)); +} + +void LabelCharMapTest::step(float dt) +{ + _time += dt; + char string[12] = {0}; + sprintf(string, "%2.2f Test", _time); + + auto label1 = (Label*)getChildByTag(kTagSprite1); + label1->setString(string); + + auto label2 = (Label*)getChildByTag(kTagSprite2); + sprintf(string, "%d", (int)_time); + label2->setString(string); +} + +std::string LabelCharMapTest::title() const +{ + return "New Label + char map file"; +} + +std::string LabelCharMapTest::subtitle() const +{ + return "Updating label should be fast."; +} + LabelCrashTest::LabelCrashTest() { auto size = Director::getInstance()->getWinSize(); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.h b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.h index b0b120b063..45d9374d4c 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.h +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTestNew.h @@ -347,6 +347,22 @@ public: virtual std::string subtitle() const override; }; +class LabelCharMapTest : public AtlasDemoNew +{ +public: + CREATE_FUNC(LabelCharMapTest); + + LabelCharMapTest(); + + virtual std::string title() const override; + virtual std::string subtitle() const override; + + void step(float dt); + +private: + float _time; +}; + class LabelCrashTest : public AtlasDemoNew { public: From 671b008f3dd2db6c00d67e0b5a25eb18148202e9 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Thu, 16 Jan 2014 17:50:09 +0800 Subject: [PATCH 2/6] fix compiling error cause by sprintf_s. --- cocos/2d/CCFontAtlasCache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/CCFontAtlasCache.cpp b/cocos/2d/CCFontAtlasCache.cpp index 3850791f7d..673c15d38a 100644 --- a/cocos/2d/CCFontAtlasCache.cpp +++ b/cocos/2d/CCFontAtlasCache.cpp @@ -91,7 +91,7 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile) FontAtlas * FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) { char tmp[30]; - sprintf_s(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap); + sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap); std::string atlasName = generateFontName(tmp, 0, GlyphCollection::CUSTOM,false); FontAtlas *tempAtlas = _atlasMap[atlasName]; From 53be67c6530457dde0d6483ecded287daa75abda Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Thu, 16 Jan 2014 18:12:53 +0800 Subject: [PATCH 3/6] update project file. --- cocos/2d/Android.mk | 1 + cocos/2d/CMakeLists.txt | 1 + tools/project-creator/module/cocos_files.json.REMOVED.git-id | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cocos/2d/Android.mk b/cocos/2d/Android.mk index 9fca766934..2253bcd7d9 100644 --- a/cocos/2d/Android.mk +++ b/cocos/2d/Android.mk @@ -47,6 +47,7 @@ CCEventListenerTouch.cpp \ CCEventMouse.cpp \ CCEventTouch.cpp \ CCFont.cpp \ +CCFontCharMap.cpp \ CCFontAtlas.cpp \ CCFontAtlasCache.cpp \ CCFontAtlasFactory.cpp \ diff --git a/cocos/2d/CMakeLists.txt b/cocos/2d/CMakeLists.txt index 8340903c8b..886c2ebc21 100644 --- a/cocos/2d/CMakeLists.txt +++ b/cocos/2d/CMakeLists.txt @@ -71,6 +71,7 @@ set(COCOS2D_SRC CCFontDefinition.cpp CCFontFNT.cpp CCFontFreeType.cpp + CCFontCharMap.cpp CCLabel.cpp CCLabelAtlas.cpp CCLabelBMFont.cpp diff --git a/tools/project-creator/module/cocos_files.json.REMOVED.git-id b/tools/project-creator/module/cocos_files.json.REMOVED.git-id index 681e527955..0b8e553434 100644 --- a/tools/project-creator/module/cocos_files.json.REMOVED.git-id +++ b/tools/project-creator/module/cocos_files.json.REMOVED.git-id @@ -1 +1 @@ -5ae50c3f2080b46e18ba3f5aee4c5c686d54e13a \ No newline at end of file +f056b511a4fb5efc921792e46fe57597f669de83 \ No newline at end of file From f5afa09de10cd36927b166b03c722053238920e2 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Fri, 17 Jan 2014 09:46:59 +0800 Subject: [PATCH 4/6] remove unneeded empty line. --- cocos/2d/CCFontAtlasCache.h | 7 ++----- cocos/2d/CCFontCharMap.h | 8 ++------ cocos/2d/CCLabelTextFormatter.cpp | 2 +- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/cocos/2d/CCFontAtlasCache.h b/cocos/2d/CCFontAtlasCache.h index f9e84ba904..31d785dc73 100644 --- a/cocos/2d/CCFontAtlasCache.h +++ b/cocos/2d/CCFontAtlasCache.h @@ -35,10 +35,8 @@ NS_CC_BEGIN class CC_DLL FontAtlasCache -{ - +{ public: - static FontAtlas * getFontAtlasTTF(const std::string& fontFileName, int size, GlyphCollection glyphs, const char *customGlyphs = 0, bool useDistanceField = false); static FontAtlas * getFontAtlasFNT(const std::string& fontFileName); @@ -48,8 +46,7 @@ public: static bool releaseFontAtlas(FontAtlas *atlas); -private: - +private: static std::string generateFontName(const std::string& fontFileName, int size, GlyphCollection theGlyphs, bool useDistanceField); static std::unordered_map _atlasMap; }; diff --git a/cocos/2d/CCFontCharMap.h b/cocos/2d/CCFontCharMap.h index f77c855c08..23caaeff22 100644 --- a/cocos/2d/CCFontCharMap.h +++ b/cocos/2d/CCFontCharMap.h @@ -32,10 +32,8 @@ NS_CC_BEGIN class FontCharMap : public Font -{ - +{ public: - static FontCharMap * create(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); static FontCharMap * create(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); static FontCharMap * create(const std::string& plistFile); @@ -44,8 +42,7 @@ public: virtual Rect getRectForChar(unsigned short theChar) const override; virtual FontAtlas *createFontAtlas() override; -protected: - +protected: FontCharMap(Texture2D* texture,int itemWidth, int itemHeight, int startCharMap) : _texture(texture),_itemWidth(itemWidth),_itemHeight(itemHeight),_mapStartChar(startCharMap),_charRect(0,0,itemWidth,itemHeight){} /** @@ -55,7 +52,6 @@ protected: virtual ~FontCharMap(); private: - Texture2D* _texture; int _mapStartChar; int _itemWidth; diff --git a/cocos/2d/CCLabelTextFormatter.cpp b/cocos/2d/CCLabelTextFormatter.cpp index 550d017beb..69d6efaae8 100644 --- a/cocos/2d/CCLabelTextFormatter.cpp +++ b/cocos/2d/CCLabelTextFormatter.cpp @@ -353,7 +353,7 @@ bool LabelTextFormatter::createStringSprites(LabelTextFormatProtocol *theLabel) Point fontPos = Point((float)nextFontPositionX + charXOffset + charRect.size.width * 0.5f + kerningAmount, - (float)nextFontPositionY + yOffset - charRect.size.height * 0.5f); + (float)nextFontPositionY + yOffset - charRect.size.height * 0.5f); if( theLabel->recordLetterInfo(CC_POINT_PIXELS_TO_POINTS(fontPos),c,i) == false) { From d309ae76d051076c433017c6175c44e778d397bb Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 17 Jan 2014 11:13:40 +0800 Subject: [PATCH 5/6] issue #3628: Adds missing files for iOS and Mac. --- build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index e14906c550..b052741ae6 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -88c095bbe123ab56df3f7870692c6631f4464c8d \ No newline at end of file +e1e5a1169e92834330092c45165660c6cbd03609 \ No newline at end of file From 54d587153bbbcfc696db3f3d0a06a189a98ca570 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 17 Jan 2014 11:55:20 +0800 Subject: [PATCH 6/6] Update CHANGELOG [ci skip] --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index fe1e346007..c4a8ea7282 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,7 @@ cocos2d-x-3.0final ?.? ? [NEW] Console: added the 'textures', 'fileutils dump' and 'config' commands [NEW] GLCache: glActiveTexture() is cached with GL::activeTexture(). All code MUST call the cached version in order to work correctly [NEW] Label: Uses a struct of TTF configuration for Label::createWithTTF to reduce parameters and make this interface more easily to use. + [NEW] Label: Integrates LabelAtlas into new Label. [NEW] Renderer: Added BatchCommand. This command is not "batchable" with other commands, but improves performance in about 10% [FIX] CocoStudio: TestColliderDetector in ArmatureTest can't work.