Merge branch 'develop' of https://github.com/cocos2d/cocos2d-x into TableView

This commit is contained in:
samuele3hu 2013-09-04 14:03:21 +08:00
commit 32d056df18
15 changed files with 67 additions and 117 deletions

View File

@ -35,7 +35,7 @@
#define LOG_EVENTS_DEBUG(...) #define LOG_EVENTS_DEBUG(...)
// #define LOG_EVENTS_DEBUG(...) ((void)__android_log_print(ANDROID_LOG_INFO, "cocos2dx/nativeactivity.cpp", __VA_ARGS__)) // #define LOG_EVENTS_DEBUG(...) ((void)__android_log_print(ANDROID_LOG_INFO, "cocos2dx/nativeactivity.cpp", __VA_ARGS__))
void cocos_android_app_init(void); void cocos_android_app_init(struct android_app* app);
/** /**
* Our saved state data. * Our saved state data.
@ -72,19 +72,19 @@ typedef struct cocos_dimensions {
int h; int h;
} cocos_dimensions; } cocos_dimensions;
static void cocos_init(cocos_dimensions d, AAssetManager* assetmanager) { static void cocos_init(cocos_dimensions d, struct android_app* app) {
LOGI("cocos_init(...)"); LOGI("cocos_init(...)");
pthread_t thisthread = pthread_self(); pthread_t thisthread = pthread_self();
LOGI("pthread_self() = %X", thisthread); LOGI("pthread_self() = %X", thisthread);
cocos2d::FileUtilsAndroid::setassetmanager(assetmanager); cocos2d::FileUtilsAndroid::setassetmanager(app->activity->assetManager);
if (!cocos2d::Director::getInstance()->getOpenGLView()) if (!cocos2d::Director::getInstance()->getOpenGLView())
{ {
cocos2d::EGLView *view = cocos2d::EGLView::getInstance(); cocos2d::EGLView *view = cocos2d::EGLView::getInstance();
view->setFrameSize(d.w, d.h); view->setFrameSize(d.w, d.h);
cocos_android_app_init(); cocos_android_app_init(app);
cocos2d::Application::getInstance()->run(); cocos2d::Application::getInstance()->run();
} }
@ -431,7 +431,7 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
ccxhelperInit.methodID, ccxhelperInit.methodID,
app->activity->clazz); app->activity->clazz);
cocos_init(d, app->activity->assetManager); cocos_init(d, app);
} }
engine->animating = 1; engine->animating = 1;
engine_draw_frame(engine); engine_draw_frame(engine);

View File

@ -94,8 +94,7 @@ bool SpriteBatchNode::initWithTexture(Texture2D *tex, int capacity)
_children = new Array(); _children = new Array();
_children->initWithCapacity(capacity); _children->initWithCapacity(capacity);
_descendants = new Array(); _descendants.reserve(capacity);
_descendants->initWithCapacity(capacity);
setShaderProgram(ShaderCache::getInstance()->programForKey(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); setShaderProgram(ShaderCache::getInstance()->programForKey(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
return true; return true;
@ -119,14 +118,12 @@ bool SpriteBatchNode::initWithFile(const char* fileImage, int capacity)
SpriteBatchNode::SpriteBatchNode() SpriteBatchNode::SpriteBatchNode()
: _textureAtlas(NULL) : _textureAtlas(NULL)
, _descendants(NULL)
{ {
} }
SpriteBatchNode::~SpriteBatchNode() SpriteBatchNode::~SpriteBatchNode()
{ {
CC_SAFE_RELEASE(_textureAtlas); CC_SAFE_RELEASE(_textureAtlas);
CC_SAFE_RELEASE(_descendants);
} }
// override visit // override visit
@ -184,16 +181,6 @@ void SpriteBatchNode::addChild(Node *child, int zOrder, int tag)
appendChild(sprite); appendChild(sprite);
} }
void SpriteBatchNode::addChild(Node *child)
{
Node::addChild(child);
}
void SpriteBatchNode::addChild(Node *child, int zOrder)
{
Node::addChild(child, zOrder);
}
// override reorderChild // override reorderChild
void SpriteBatchNode::reorderChild(Node *child, int zOrder) void SpriteBatchNode::reorderChild(Node *child, int zOrder)
{ {
@ -238,11 +225,13 @@ void SpriteBatchNode::removeAllChildrenWithCleanup(bool doCleanup)
{ {
// Invalidate atlas index. issue #569 // Invalidate atlas index. issue #569
// useSelfRender should be performed on all descendants. issue #1216 // useSelfRender should be performed on all descendants. issue #1216
arrayMakeObjectsPerformSelectorWithObject(_descendants, setBatchNode, NULL, Sprite*); std::for_each(_descendants.begin(), _descendants.end(), [](Sprite* sprite) {
sprite->setBatchNode(nullptr);
});
Node::removeAllChildrenWithCleanup(doCleanup); Node::removeAllChildrenWithCleanup(doCleanup);
_descendants->removeAllObjects(); _descendants.clear();
_textureAtlas->removeAllQuads(); _textureAtlas->removeAllQuads();
} }
@ -361,10 +350,10 @@ void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, int* curIndex)
if (needNewIndex) if (needNewIndex)
{//all children have a zOrder < 0) {//all children have a zOrder < 0)
oldIndex=sprite->getAtlasIndex(); oldIndex = sprite->getAtlasIndex();
sprite->setAtlasIndex(*curIndex); sprite->setAtlasIndex(*curIndex);
sprite->setOrderOfArrival(0); sprite->setOrderOfArrival(0);
if (oldIndex!=*curIndex) { if (oldIndex != *curIndex) {
swap(oldIndex, *curIndex); swap(oldIndex, *curIndex);
} }
(*curIndex)++; (*curIndex)++;
@ -374,15 +363,20 @@ void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, int* curIndex)
void SpriteBatchNode::swap(int oldIndex, int newIndex) void SpriteBatchNode::swap(int oldIndex, int newIndex)
{ {
CCASSERT(oldIndex>=0 && oldIndex < _descendants->count() && newIndex >=0 && newIndex < _descendants->count(), "Invalid index"); CCASSERT(oldIndex>=0 && oldIndex < _descendants.size() && newIndex >=0 && newIndex < _descendants.size(), "Invalid index");
V3F_C4B_T2F_Quad* quads = _textureAtlas->getQuads(); V3F_C4B_T2F_Quad* quads = _textureAtlas->getQuads();
std::swap( quads[oldIndex], quads[newIndex] );
//update the index of other swapped item //update the index of other swapped item
static_cast<Sprite*>( _descendants->getObjectAtIndex(newIndex) )->setAtlasIndex(oldIndex);
std::swap( quads[oldIndex], quads[newIndex] ); auto oldIt = std::next( _descendants.begin(), oldIndex );
_descendants->swap( oldIndex, newIndex ); auto newIt = std::next( _descendants.begin(), newIndex );
(*newIt)->setAtlasIndex(oldIndex);
// (*oldIt)->setAtlasIndex(newIndex);
std::swap( *oldIt, *newIt );
} }
void SpriteBatchNode::reorderBatch(bool reorder) void SpriteBatchNode::reorderBatch(bool reorder)
@ -560,43 +554,6 @@ int SpriteBatchNode::atlasIndexForChild(Sprite *sprite, int nZ)
return 0; return 0;
} }
// add child helper
void SpriteBatchNode::insertChild(Sprite *sprite, int index)
{
sprite->setBatchNode(this);
sprite->setAtlasIndex(index);
sprite->setDirty(true);
if(_textureAtlas->getTotalQuads() == _textureAtlas->getCapacity())
{
increaseAtlasCapacity();
}
V3F_C4B_T2F_Quad quad = sprite->getQuad();
_textureAtlas->insertQuad(&quad, index);
_descendants->insertObject(sprite, index);
// update indices
int i = index+1;
Sprite* child = nullptr;
for(; i < _descendants->count(); i++){
child = static_cast<Sprite*>(_descendants->getObjectAtIndex(i));
child->setAtlasIndex(child->getAtlasIndex() + 1);
}
// add children recursively
Object* obj = nullptr;
CCARRAY_FOREACH(sprite->getChildren(), obj)
{
child = static_cast<Sprite*>(obj);
int idx = atlasIndexForChild(child, child->getZOrder());
insertChild(child, idx);
}
}
// addChild helper, faster than insertChild // addChild helper, faster than insertChild
void SpriteBatchNode::appendChild(Sprite* sprite) void SpriteBatchNode::appendChild(Sprite* sprite)
{ {
@ -608,8 +565,8 @@ void SpriteBatchNode::appendChild(Sprite* sprite)
increaseAtlasCapacity(); increaseAtlasCapacity();
} }
_descendants->addObject(sprite); _descendants.push_back(sprite);
int index=_descendants->count()-1; int index = _descendants.size()-1;
sprite->setAtlasIndex(index); sprite->setAtlasIndex(index);
@ -634,19 +591,16 @@ void SpriteBatchNode::removeSpriteFromAtlas(Sprite *sprite)
// Cleanup sprite. It might be reused (issue #569) // Cleanup sprite. It might be reused (issue #569)
sprite->setBatchNode(NULL); sprite->setBatchNode(NULL);
int index = _descendants->getIndexOfObject(sprite); auto it = std::find(_descendants.begin(), _descendants.end(), sprite );
if (index != -1) if( it != _descendants.end() )
{ {
_descendants->removeObjectAtIndex(index); auto next = std::next(it);
// update all sprites beyond this one std::for_each(next, _descendants.end(), [](Sprite *sprite) {
int count = _descendants->count(); sprite->setAtlasIndex( sprite->getAtlasIndex() - 1 );
});
for(; index < count; ++index)
{ _descendants.erase(it);
Sprite* s = static_cast<Sprite*>(_descendants->getObjectAtIndex(index));
s->setAtlasIndex( s->getAtlasIndex() - 1 );
}
} }
// remove children recursively // remove children recursively
@ -754,22 +708,17 @@ SpriteBatchNode * SpriteBatchNode::addSpriteWithoutQuad(Sprite*child, int z, int
child->setAtlasIndex(z); child->setAtlasIndex(z);
// XXX: optimize with a binary search // XXX: optimize with a binary search
int i=0; auto it = std::begin(_descendants);
std::for_each(_descendants.begin(), _descendants.end(), [&](Sprite *sprite) {
Object* object = NULL; if(sprite->getAtlasIndex() >= z)
CCARRAY_FOREACH(_descendants, object) std::next(it);
{ });
Sprite* child = static_cast<Sprite*>(object);
if (child && (child->getAtlasIndex() >= z)) _descendants.insert(it, child);
{
++i;
}
}
_descendants->insertObject(child, i);
// IMPORTANT: Call super, and not self. Avoid adding it to the texture atlas array // IMPORTANT: Call super, and not self. Avoid adding it to the texture atlas array
Node::addChild(child, z, aTag); Node::addChild(child, z, aTag);
//#issue 1262 don't use lazy sorting, tiles are added as quads not as sprites, so sprites need to be added in order //#issue 1262 don't use lazy sorting, tiles are added as quads not as sprites, so sprites need to be added in order
reorderBatch(false); reorderBatch(false);

View File

@ -28,11 +28,12 @@ THE SOFTWARE.
#ifndef __CC_SPRITE_BATCH_NODE_H__ #ifndef __CC_SPRITE_BATCH_NODE_H__
#define __CC_SPRITE_BATCH_NODE_H__ #define __CC_SPRITE_BATCH_NODE_H__
#include <vector>
#include "base_nodes/CCNode.h" #include "base_nodes/CCNode.h"
#include "CCProtocols.h" #include "CCProtocols.h"
#include "textures/CCTextureAtlas.h" #include "textures/CCTextureAtlas.h"
#include "ccMacros.h" #include "ccMacros.h"
#include "cocoa/CCArray.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -89,11 +90,11 @@ public:
bool initWithFile(const char* fileImage, int capacity); bool initWithFile(const char* fileImage, int capacity);
bool init(); bool init();
// property /** returns the TextureAtlas object */
// retain
inline TextureAtlas* getTextureAtlas(void) { return _textureAtlas; } inline TextureAtlas* getTextureAtlas(void) { return _textureAtlas; }
inline void setTextureAtlas(TextureAtlas* textureAtlas)
/** sets the TextureAtlas object */
inline void setTextureAtlas(TextureAtlas* textureAtlas)
{ {
if (textureAtlas != _textureAtlas) if (textureAtlas != _textureAtlas)
{ {
@ -103,7 +104,9 @@ public:
} }
} }
inline Array* getDescendants(void) { return _descendants; } /** returns an array with the descendants (children, gran children, etc.).
This is specific to BatchNode. In order to use the children, use getChildren() instead */
inline const std::vector<Sprite*>& getDescendants() const { return _descendants; }
void increaseAtlasCapacity(); void increaseAtlasCapacity();
@ -112,7 +115,6 @@ public:
*/ */
void removeChildAtIndex(int index, bool doCleanup); void removeChildAtIndex(int index, bool doCleanup);
void insertChild(Sprite *child, int index);
void appendChild(Sprite* sprite); void appendChild(Sprite* sprite);
void removeSpriteFromAtlas(Sprite *sprite); void removeSpriteFromAtlas(Sprite *sprite);
@ -133,12 +135,11 @@ public:
virtual const BlendFunc& getBlendFunc(void) const override; virtual const BlendFunc& getBlendFunc(void) const override;
virtual void visit(void) override; virtual void visit(void) override;
virtual void addChild(Node * child) override; using Node::addChild;
virtual void addChild(Node * child, int zOrder) override;
virtual void addChild(Node * child, int zOrder, int tag) override; virtual void addChild(Node * child, int zOrder, int tag) override;
virtual void reorderChild(Node * child, int zOrder) override; virtual void reorderChild(Node *child, int zOrder) override;
virtual void removeChild(Node* child, bool cleanup) override; virtual void removeChild(Node *child, bool cleanup) override;
virtual void removeAllChildrenWithCleanup(bool cleanup) override; virtual void removeAllChildrenWithCleanup(bool cleanup) override;
virtual void sortAllChildren() override; virtual void sortAllChildren() override;
virtual void draw(void) override; virtual void draw(void) override;
@ -157,19 +158,19 @@ protected:
/* This is the opposite of "addQuadFromSprite. /* This is the opposite of "addQuadFromSprite.
It add the sprite to the children and descendants array, but it doesn't update add it to the texture atlas It add the sprite to the children and descendants array, but it doesn't update add it to the texture atlas
*/ */
SpriteBatchNode * addSpriteWithoutQuad(Sprite*child, int z, int aTag); SpriteBatchNode * addSpriteWithoutQuad(Sprite *child, int z, int aTag);
private:
void updateAtlasIndex(Sprite* sprite, int* curIndex); void updateAtlasIndex(Sprite* sprite, int* curIndex);
void swap(int oldIndex, int newIndex); void swap(int oldIndex, int newIndex);
void updateBlendFunc(); void updateBlendFunc();
protected:
TextureAtlas *_textureAtlas; TextureAtlas *_textureAtlas;
BlendFunc _blendFunc; BlendFunc _blendFunc;
// all descendants: children, grand children, etc... // all descendants: children, grand children, etc...
Array* _descendants; // There is not need to retain/release these objects, since they are already retained by _children
// So, using std::vector<Sprite*> is slightly faster than using cocos2d::Array for this particular case
std::vector<Sprite*> _descendants;
}; };
// end of sprite_nodes group // end of sprite_nodes group

View File

@ -8,7 +8,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -10,7 +10,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -10,7 +10,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -1 +1 @@
7b8ff30c1fc482c4d7485032c73c1145a60168b4 71e41ffe1784a179756e14a9745e8b4c6e389d95

View File

@ -10,7 +10,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -10,7 +10,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -10,7 +10,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -10,7 +10,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -10,7 +10,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -10,7 +10,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -9,7 +9,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }

View File

@ -10,7 +10,7 @@
using namespace cocos2d; using namespace cocos2d;
void cocos_android_app_init (void) { void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init"); LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
} }