mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[cocos2dx] Closes #611, cocos2d-x v3 auto-batcher does it's work, makes SkeletonBatch a lot simpler
This commit is contained in:
parent
e3cdea94a0
commit
48c5b06676
@ -44,10 +44,6 @@ Scene* BatchingExample::scene () {
|
|||||||
bool BatchingExample::init () {
|
bool BatchingExample::init () {
|
||||||
if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
|
if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
|
||||||
|
|
||||||
// To avoid the SkeletonBatch buffer from being resized, set this to the number of vertices ever rendered in one frame.
|
|
||||||
// BatchingExample needs ~3200, but let's set it low to test the buffer resizing.
|
|
||||||
SkeletonBatch::setBufferSize(512);
|
|
||||||
|
|
||||||
// Load the texture atlas.
|
// Load the texture atlas.
|
||||||
_atlas = spAtlas_createFromFile("spineboy.atlas", 0);
|
_atlas = spAtlas_createFromFile("spineboy.atlas", 0);
|
||||||
CCASSERT(_atlas, "Error reading atlas file.");
|
CCASSERT(_atlas, "Error reading atlas file.");
|
||||||
|
|||||||
@ -39,101 +39,71 @@ using std::max;
|
|||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
|
|
||||||
static SkeletonBatch* instance = nullptr;
|
static SkeletonBatch* instance = nullptr;
|
||||||
|
|
||||||
void SkeletonBatch::setBufferSize (int vertexCount) {
|
SkeletonBatch* SkeletonBatch::getInstance () {
|
||||||
if (instance) delete instance;
|
if (!instance) instance = new SkeletonBatch();
|
||||||
instance = new SkeletonBatch(vertexCount);
|
return instance;
|
||||||
}
|
|
||||||
|
|
||||||
SkeletonBatch* SkeletonBatch::getInstance () {
|
|
||||||
if (!instance) instance = new SkeletonBatch(8192);
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SkeletonBatch::destroyInstance () {
|
|
||||||
if (instance) {
|
|
||||||
delete instance;
|
|
||||||
instance = nullptr;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
SkeletonBatch::SkeletonBatch (int capacity) :
|
void SkeletonBatch::destroyInstance () {
|
||||||
_capacity(capacity), _position(0)
|
if (instance) {
|
||||||
{
|
delete instance;
|
||||||
_buffer = new V3F_C4B_T2F[capacity];
|
instance = nullptr;
|
||||||
_firstCommand = new Command();
|
}
|
||||||
_command = _firstCommand;
|
}
|
||||||
|
|
||||||
Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_AFTER_DRAW_RESET_POSITION, [this](EventCustom* eventCustom){
|
SkeletonBatch::SkeletonBatch ()
|
||||||
this->update(0);
|
{
|
||||||
});;
|
_firstCommand = new Command();
|
||||||
}
|
_command = _firstCommand;
|
||||||
|
|
||||||
SkeletonBatch::~SkeletonBatch () {
|
Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_AFTER_DRAW_RESET_POSITION, [this](EventCustom* eventCustom){
|
||||||
Director::getInstance()->getEventDispatcher()->removeCustomEventListeners(EVENT_AFTER_DRAW_RESET_POSITION);
|
this->update(0);
|
||||||
|
});;
|
||||||
|
}
|
||||||
|
|
||||||
Command* command = _firstCommand;
|
SkeletonBatch::~SkeletonBatch () {
|
||||||
while (command) {
|
Director::getInstance()->getEventDispatcher()->removeCustomEventListeners(EVENT_AFTER_DRAW_RESET_POSITION);
|
||||||
Command* next = command->next;
|
|
||||||
delete command;
|
|
||||||
command = next;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete [] _buffer;
|
Command* command = _firstCommand;
|
||||||
}
|
while (command) {
|
||||||
|
Command* next = command->next;
|
||||||
|
delete command;
|
||||||
|
command = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SkeletonBatch::update (float delta) {
|
void SkeletonBatch::update (float delta) {
|
||||||
_position = 0;
|
_command = _firstCommand;
|
||||||
_command = _firstCommand;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void SkeletonBatch::addCommand (cocos2d::Renderer* renderer, float globalZOrder, GLuint textureID, GLProgramState* glProgramState,
|
void SkeletonBatch::addCommand (cocos2d::Renderer* renderer, float globalZOrder, GLuint textureID, GLProgramState* glProgramState,
|
||||||
BlendFunc blendFunc, const TrianglesCommand::Triangles& triangles, const Mat4& transform, uint32_t transformFlags
|
BlendFunc blendFunc, const TrianglesCommand::Triangles& triangles, const Mat4& transform, uint32_t transformFlags
|
||||||
) {
|
) {
|
||||||
if (_position + triangles.vertCount > _capacity) {
|
_command->triangles->verts = triangles.verts;
|
||||||
int newCapacity = max(_capacity + _capacity / 2, _position + triangles.vertCount);
|
|
||||||
V3F_C4B_T2F* newBuffer = new V3F_C4B_T2F[newCapacity];
|
|
||||||
memcpy(newBuffer, _buffer, _position);
|
|
||||||
|
|
||||||
int newPosition = 0;
|
_command->triangles->vertCount = triangles.vertCount;
|
||||||
Command* command = _firstCommand;
|
_command->triangles->indexCount = triangles.indexCount;
|
||||||
while (newPosition < _position) {
|
_command->triangles->indices = triangles.indices;
|
||||||
command->triangles->verts = newBuffer + newPosition;
|
|
||||||
newPosition += command->triangles->vertCount;
|
|
||||||
command = command->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete [] _buffer;
|
_command->trianglesCommand->init(globalZOrder, textureID, glProgramState, blendFunc, *_command->triangles, transform);
|
||||||
_buffer = newBuffer;
|
renderer->addCommand(_command->trianglesCommand);
|
||||||
_capacity = newCapacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(_buffer + _position, triangles.verts, sizeof(V3F_C4B_T2F) * triangles.vertCount);
|
if (!_command->next) _command->next = new Command();
|
||||||
_command->triangles->verts = _buffer + _position;
|
_command = _command->next;
|
||||||
_position += triangles.vertCount;
|
}
|
||||||
|
|
||||||
_command->triangles->vertCount = triangles.vertCount;
|
SkeletonBatch::Command::Command () :
|
||||||
_command->triangles->indexCount = triangles.indexCount;
|
next(nullptr)
|
||||||
_command->triangles->indices = triangles.indices;
|
{
|
||||||
|
trianglesCommand = new TrianglesCommand();
|
||||||
|
triangles = new TrianglesCommand::Triangles();
|
||||||
|
}
|
||||||
|
|
||||||
_command->trianglesCommand->init(globalZOrder, textureID, glProgramState, blendFunc, *_command->triangles, transform, transformFlags);
|
SkeletonBatch::Command::~Command () {
|
||||||
renderer->addCommand(_command->trianglesCommand);
|
delete triangles;
|
||||||
|
delete trianglesCommand;
|
||||||
if (!_command->next) _command->next = new Command();
|
}
|
||||||
_command = _command->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
SkeletonBatch::Command::Command () :
|
|
||||||
next(nullptr)
|
|
||||||
{
|
|
||||||
trianglesCommand = new TrianglesCommand();
|
|
||||||
triangles = new TrianglesCommand::Triangles();
|
|
||||||
}
|
|
||||||
|
|
||||||
SkeletonBatch::Command::~Command () {
|
|
||||||
delete triangles;
|
|
||||||
delete trianglesCommand;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -37,43 +37,34 @@
|
|||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
|
|
||||||
class SkeletonBatch {
|
class SkeletonBatch {
|
||||||
public:
|
public:
|
||||||
/* Sets the max number of vertices that can be drawn in a single frame. The buffer will grow automatically as needed, but
|
static SkeletonBatch* getInstance ();
|
||||||
* setting it to the appropriate is more efficient. Best to call before getInstance is called for the first time. Default is
|
|
||||||
* 8192. */
|
|
||||||
static void setBufferSize (int vertexCount);
|
|
||||||
|
|
||||||
static SkeletonBatch* getInstance ();
|
static void destroyInstance ();
|
||||||
|
|
||||||
static void destroyInstance ();
|
void update (float delta);
|
||||||
|
|
||||||
void update (float delta);
|
void addCommand (cocos2d::Renderer* renderer, float globalOrder, GLuint textureID, cocos2d::GLProgramState* glProgramState,
|
||||||
|
cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand:: Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags);
|
||||||
|
|
||||||
void addCommand (cocos2d::Renderer* renderer, float globalOrder, GLuint textureID, cocos2d::GLProgramState* glProgramState,
|
protected:
|
||||||
cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand:: Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags);
|
SkeletonBatch ();
|
||||||
|
virtual ~SkeletonBatch ();
|
||||||
|
|
||||||
protected:
|
class Command {
|
||||||
SkeletonBatch (int capacity);
|
public:
|
||||||
virtual ~SkeletonBatch ();
|
Command ();
|
||||||
|
virtual ~Command ();
|
||||||
|
|
||||||
cocos2d::V3F_C4B_T2F* _buffer;
|
cocos2d::TrianglesCommand* trianglesCommand;
|
||||||
int _capacity;
|
cocos2d::TrianglesCommand::Triangles* triangles;
|
||||||
int _position;
|
Command* next;
|
||||||
|
};
|
||||||
|
|
||||||
class Command {
|
Command* _firstCommand;
|
||||||
public:
|
Command* _command;
|
||||||
Command ();
|
};
|
||||||
virtual ~Command ();
|
|
||||||
|
|
||||||
cocos2d::TrianglesCommand* trianglesCommand;
|
|
||||||
cocos2d::TrianglesCommand::Triangles* triangles;
|
|
||||||
Command* next;
|
|
||||||
};
|
|
||||||
|
|
||||||
Command* _firstCommand;
|
|
||||||
Command* _command;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user