# cocos2d源码剖析

1. TextureAtlas

2. Label，LabelTTF，LabelBMFont等

http://www.jellythink.com/archives/766

http://www.2cto.com/kf/201409/336234.html

• Problem

Currently Cocos2D supports mainly two types of "label" objects: CCLabelTTF ad CCLabelBMFont. CCLabelTTF uses true type font rendering (very accurate and nicely antialiased fonts) into a texture while CCLabelBMFont uses texture atlases based fonts rendering (the atlases must be created offline and rendering quality is poorer that TTF).
CCLabelTTF grants hight quality text rendition, but (almost) impractically slow when it comes to update the label's text (rendering TTF text into a texture is currently really slow). CCLabeBMFont on the other hand is pretty quick when if comes to rendering and update but requires the fonts to be created beforehand (offline) and also rendering quality doesn't match TTF rendering.

• Requirements (what we want)

We would (ideally) like to improve CCLabelTTF so that updating the label's text at runtime would be (almost) as quick as when using bitmap based fonts while mantaining visual fidelity (and quality) matching the current implementation.
The API for using this "new" CCLabelTTF should not change and have minimal impact (possibly zero impact) on the already existing client (100% backward compatible).
Also, every new API element (new parameter or new method) IF needed, should be optional and (when not provided by the client code) don't alter in any way the expected API's behavior.
We also want the "new" TTF label to have "attributes" like shadow and colors.

• Technical road map (how we want to do it)

1) Using a mechanism similar to the one used by CCLabelBM font we could build an Atlas (for the chosen font and size) "on the fly" and use that atlas for composing the string in the label. This would make the first (and one time) label (and Atlas) creation as slow as the current implementation but would make updating the label much faster.
This new implementation would allow for fast label updates, but could potentially use more memory (for large fonts, the texture Atlas could be pretty large) and not satisfy the rendering quality expected from a "real" TTF font (see areas of risk below).
This solution would be then an "hybrid" solution between the two current ones, allowing for fast updates at the cost of (most probably) worst rendering quality than a real TTF font.

2) We could profile (and analyze) the current implementation of CCLabelTTF and come up with an optimization plan for making it performing better on the platform.

• Areas of risk and comments

TTF rendering is a pretty complicated (and computationally intensive) process but it generates highly accurate letters aliasing. The aliasing is somehow commanded by the spacial positioning of the letters composing the word (i.e. the string "aaa" would generate different aliasing than the string "a a a"). Generating letter atlases "on the fly" would cause part of this "pixel perfect" aliasing to be lost. This could be noticeable (and perceived as not tolerable) from certain users and could be potentially show really bad when used in combination with very small fonts.
Also, some more research on the topic is still needed on my side.

https://github.com/cocos2d/cocos2d-js__old__/issues/15

// Trigger an assert if the reference count is 0 but the Ref is still in autorelease pool.
// This happens when 'autorelease/release' were not used in pairs with 'new/retain'.
//
// Wrong usage (1):
//
// auto obj = Node::create();   // Ref = 1, but it's an autorelease Ref which means it was in the autorelease pool.
// obj->autorelease();   // Wrong: If you wish to invoke autorelease several times, you should retain obj first.
//
// Wrong usage (2):
//
// auto obj = Node::create();
// obj->release();   // Wrong: obj is an autorelease Ref, it will be released when clearing current pool.
//
// Correct usage (1):
//
// auto obj = Node::create();
//                     |-   new Node();     // new is the pair of the autorelease of next line
//                     |-   autorelease();  // The pair of new Node.
//
// obj->retain();
// obj->autorelease();  // This autorelease is the pair of retain of previous line.
//
// Correct usage (2):
//
// auto obj = Node::create();
// obj->retain();
// obj->release();   // This release is the pair of retain of previous line.

# COCOS2D 释放资源的最佳时机

 1 2 3 4 CCAnimationCache::purgeSharedAnimationCache(); SpriteFrameCache::getInstance()->removeUnusedSpriteFrame(); CCTextureCache::getInstance()->removeUnusedTextures(); CCTextureCache::getInstance()->getCachedTextureInfo();

1.

2.

3.

 1 2 3 4 5 6 7 replaceScene : SceneB init : SceneB onExitTransitionDidStart : SceneA onExit : SceneA dealloc : SceneA onEnter : SceneB onEnterTransitionDidFinish : SceneB

4.

 1 Director::getInstance()->replaceScene(CCTransitionFade::create(0.5f, SceneB::createScene()));

 1 2 3 4 5 6 replaceScene : SceneB init : SceneB onExitTransitionDidStart : SceneA onEnter : SceneB onExit : SceneA onEnterTransitionDidFinish : SceneB

onEnterTransitionDidFinish 里释放资源并不一定可行。那该怎么解决？

 1 scheduleOnce(CC_SCHEDULE_SELECTOR(SceneB::release), 2.0f);

5.

Lable性能问题：

# 3.6CCLabel渲染效率问题

3.x里面Label纹理缓存的疑问

# cocos2d-x V3.x内存管理分析

SpriteBatchNode和CCTextureCache