From 747a0a1e3835fbfff39ab513236b7659bb14157d Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Tue, 19 Apr 2016 11:16:59 +0200 Subject: [PATCH] spine-cocos2dx, auto resize SkeletonBatch buffer. --- .../3/example/Classes/BatchingExample.cpp | 4 ++ spine-cocos2dx/3/src/spine/SkeletonBatch.cpp | 47 +++++++++++++------ spine-cocos2dx/3/src/spine/SkeletonBatch.h | 11 +++-- spine-cocos2dx/3/src/spine/spine-cocos2dx.cpp | 2 +- spine-cocos2dx/3/src/spine/spine-cocos2dx.h | 1 + 5 files changed, 44 insertions(+), 21 deletions(-) diff --git a/spine-cocos2dx/3/example/Classes/BatchingExample.cpp b/spine-cocos2dx/3/example/Classes/BatchingExample.cpp index 0577fd077..fe919fa25 100644 --- a/spine-cocos2dx/3/example/Classes/BatchingExample.cpp +++ b/spine-cocos2dx/3/example/Classes/BatchingExample.cpp @@ -44,6 +44,10 @@ Scene* BatchingExample::scene () { bool BatchingExample::init () { 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. _atlas = spAtlas_createFromFile("spineboy.atlas", 0); CCASSERT(_atlas, "Error reading atlas file."); diff --git a/spine-cocos2dx/3/src/spine/SkeletonBatch.cpp b/spine-cocos2dx/3/src/spine/SkeletonBatch.cpp index e0428f508..d5e5ddcc0 100644 --- a/spine-cocos2dx/3/src/spine/SkeletonBatch.cpp +++ b/spine-cocos2dx/3/src/spine/SkeletonBatch.cpp @@ -34,6 +34,7 @@ #include USING_NS_CC; +using std::max; namespace spine { @@ -64,7 +65,7 @@ SkeletonBatch::~SkeletonBatch () { Command* command = _firstCommand; while (command) { - Command* next = command->_next; + Command* next = command->next; delete command; command = next; } @@ -80,33 +81,49 @@ void SkeletonBatch::update (float delta) { void SkeletonBatch::addCommand (cocos2d::Renderer* renderer, float globalZOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendFunc, const TrianglesCommand::Triangles& triangles, const Mat4& transform, uint32_t transformFlags ) { - CCASSERT(_position + triangles.vertCount < _capacity, "SkeletonBatch capacity is too small"); + if (_position + triangles.vertCount > _capacity) { + 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* command = _firstCommand; + while (newPosition < _position) { + command->triangles->verts = newBuffer + newPosition; + newPosition += command->triangles->vertCount; + command = command->next; + } + + delete [] _buffer; + _buffer = newBuffer; + _capacity = newCapacity; + } memcpy(_buffer + _position, triangles.verts, sizeof(V3F_C4B_T2F) * triangles.vertCount); - _command->_triangles->verts = _buffer + _position; + _command->triangles->verts = _buffer + _position; _position += triangles.vertCount; - _command->_triangles->vertCount = triangles.vertCount; - _command->_triangles->indexCount = triangles.indexCount; - _command->_triangles->indices = triangles.indices; + _command->triangles->vertCount = triangles.vertCount; + _command->triangles->indexCount = triangles.indexCount; + _command->triangles->indices = triangles.indices; - _command->_trianglesCommand->init(globalZOrder, textureID, glProgramState, blendFunc, *_command->_triangles, transform, transformFlags); - renderer->addCommand(_command->_trianglesCommand); + _command->trianglesCommand->init(globalZOrder, textureID, glProgramState, blendFunc, *_command->triangles, transform, transformFlags); + renderer->addCommand(_command->trianglesCommand); - if (!_command->_next) _command->_next = new Command(); - _command = _command->_next; + if (!_command->next) _command->next = new Command(); + _command = _command->next; } SkeletonBatch::Command::Command () : - _next(nullptr) + next(nullptr) { - _trianglesCommand = new TrianglesCommand(); - _triangles = new TrianglesCommand::Triangles(); + trianglesCommand = new TrianglesCommand(); + triangles = new TrianglesCommand::Triangles(); } SkeletonBatch::Command::~Command () { - delete _triangles; - delete _trianglesCommand; + delete triangles; + delete trianglesCommand; } } diff --git a/spine-cocos2dx/3/src/spine/SkeletonBatch.h b/spine-cocos2dx/3/src/spine/SkeletonBatch.h index 523919fcd..b0bc1370d 100644 --- a/spine-cocos2dx/3/src/spine/SkeletonBatch.h +++ b/spine-cocos2dx/3/src/spine/SkeletonBatch.h @@ -39,8 +39,9 @@ namespace spine { class SkeletonBatch { public: - /* Sets the max number of vertices that can be drawn in a single frame. Best to call before getInstance is called for the - * first time. Default is 8192. */ + /* Sets the max number of vertices that can be drawn in a single frame. The buffer will grow automatically as needed, but + * 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 (); @@ -63,9 +64,9 @@ protected: Command (); virtual ~Command (); - cocos2d::TrianglesCommand* _trianglesCommand; - cocos2d::TrianglesCommand::Triangles* _triangles; - Command* _next; + cocos2d::TrianglesCommand* trianglesCommand; + cocos2d::TrianglesCommand::Triangles* triangles; + Command* next; }; Command* _firstCommand; diff --git a/spine-cocos2dx/3/src/spine/spine-cocos2dx.cpp b/spine-cocos2dx/3/src/spine/spine-cocos2dx.cpp index 8bc8fc105..6d00b46ac 100644 --- a/spine-cocos2dx/3/src/spine/spine-cocos2dx.cpp +++ b/spine-cocos2dx/3/src/spine/spine-cocos2dx.cpp @@ -63,7 +63,7 @@ void _spAtlasPage_createTexture (spAtlasPage* self, const char* path) { texture->retain(); Texture2D::TexParams textureParams = {filter(self->minFilter), filter(self->magFilter), wrap(self->uWrap), wrap(self->vWrap)}; - texture->setTexParameters(&textureParams); + texture->setTexParameters(textureParams); self->rendererObject = texture; self->width = texture->getPixelsWide(); diff --git a/spine-cocos2dx/3/src/spine/spine-cocos2dx.h b/spine-cocos2dx/3/src/spine/spine-cocos2dx.h index 31f56ed40..447c0b84a 100644 --- a/spine-cocos2dx/3/src/spine/spine-cocos2dx.h +++ b/spine-cocos2dx/3/src/spine/spine-cocos2dx.h @@ -37,5 +37,6 @@ #include #include #include +#include #endif /* SPINE_COCOS2DX_H_ */