mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
spine-cocos2dx, auto resize SkeletonBatch buffer.
This commit is contained in:
parent
6316879c1c
commit
747a0a1e38
@ -44,6 +44,10 @@ 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.");
|
||||||
|
|||||||
@ -34,6 +34,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
USING_NS_CC;
|
USING_NS_CC;
|
||||||
|
using std::max;
|
||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
|
|
||||||
@ -64,7 +65,7 @@ SkeletonBatch::~SkeletonBatch () {
|
|||||||
|
|
||||||
Command* command = _firstCommand;
|
Command* command = _firstCommand;
|
||||||
while (command) {
|
while (command) {
|
||||||
Command* next = command->_next;
|
Command* next = command->next;
|
||||||
delete command;
|
delete command;
|
||||||
command = next;
|
command = next;
|
||||||
}
|
}
|
||||||
@ -80,33 +81,49 @@ void SkeletonBatch::update (float delta) {
|
|||||||
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
|
||||||
) {
|
) {
|
||||||
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);
|
memcpy(_buffer + _position, triangles.verts, sizeof(V3F_C4B_T2F) * triangles.vertCount);
|
||||||
_command->_triangles->verts = _buffer + _position;
|
_command->triangles->verts = _buffer + _position;
|
||||||
_position += triangles.vertCount;
|
_position += triangles.vertCount;
|
||||||
|
|
||||||
_command->_triangles->vertCount = triangles.vertCount;
|
_command->triangles->vertCount = triangles.vertCount;
|
||||||
_command->_triangles->indexCount = triangles.indexCount;
|
_command->triangles->indexCount = triangles.indexCount;
|
||||||
_command->_triangles->indices = triangles.indices;
|
_command->triangles->indices = triangles.indices;
|
||||||
|
|
||||||
_command->_trianglesCommand->init(globalZOrder, textureID, glProgramState, blendFunc, *_command->_triangles, transform, transformFlags);
|
_command->trianglesCommand->init(globalZOrder, textureID, glProgramState, blendFunc, *_command->triangles, transform, transformFlags);
|
||||||
renderer->addCommand(_command->_trianglesCommand);
|
renderer->addCommand(_command->trianglesCommand);
|
||||||
|
|
||||||
if (!_command->_next) _command->_next = new Command();
|
if (!_command->next) _command->next = new Command();
|
||||||
_command = _command->_next;
|
_command = _command->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonBatch::Command::Command () :
|
SkeletonBatch::Command::Command () :
|
||||||
_next(nullptr)
|
next(nullptr)
|
||||||
{
|
{
|
||||||
_trianglesCommand = new TrianglesCommand();
|
trianglesCommand = new TrianglesCommand();
|
||||||
_triangles = new TrianglesCommand::Triangles();
|
triangles = new TrianglesCommand::Triangles();
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonBatch::Command::~Command () {
|
SkeletonBatch::Command::~Command () {
|
||||||
delete _triangles;
|
delete triangles;
|
||||||
delete _trianglesCommand;
|
delete trianglesCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,8 +39,9 @@ namespace spine {
|
|||||||
|
|
||||||
class SkeletonBatch {
|
class SkeletonBatch {
|
||||||
public:
|
public:
|
||||||
/* Sets the max number of vertices that can be drawn in a single frame. Best to call before getInstance is called for the
|
/* Sets the max number of vertices that can be drawn in a single frame. The buffer will grow automatically as needed, but
|
||||||
* first time. Default is 8192. */
|
* 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 void setBufferSize (int vertexCount);
|
||||||
|
|
||||||
static SkeletonBatch* getInstance ();
|
static SkeletonBatch* getInstance ();
|
||||||
@ -63,9 +64,9 @@ protected:
|
|||||||
Command ();
|
Command ();
|
||||||
virtual ~Command ();
|
virtual ~Command ();
|
||||||
|
|
||||||
cocos2d::TrianglesCommand* _trianglesCommand;
|
cocos2d::TrianglesCommand* trianglesCommand;
|
||||||
cocos2d::TrianglesCommand::Triangles* _triangles;
|
cocos2d::TrianglesCommand::Triangles* triangles;
|
||||||
Command* _next;
|
Command* next;
|
||||||
};
|
};
|
||||||
|
|
||||||
Command* _firstCommand;
|
Command* _firstCommand;
|
||||||
|
|||||||
@ -63,7 +63,7 @@ void _spAtlasPage_createTexture (spAtlasPage* self, const char* path) {
|
|||||||
texture->retain();
|
texture->retain();
|
||||||
|
|
||||||
Texture2D::TexParams textureParams = {filter(self->minFilter), filter(self->magFilter), wrap(self->uWrap), wrap(self->vWrap)};
|
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->rendererObject = texture;
|
||||||
self->width = texture->getPixelsWide();
|
self->width = texture->getPixelsWide();
|
||||||
|
|||||||
@ -37,5 +37,6 @@
|
|||||||
#include <spine/Cocos2dAttachmentLoader.h>
|
#include <spine/Cocos2dAttachmentLoader.h>
|
||||||
#include <spine/SkeletonRenderer.h>
|
#include <spine/SkeletonRenderer.h>
|
||||||
#include <spine/SkeletonAnimation.h>
|
#include <spine/SkeletonAnimation.h>
|
||||||
|
#include <spine/SkeletonBatch.h>
|
||||||
|
|
||||||
#endif /* SPINE_COCOS2DX_H_ */
|
#endif /* SPINE_COCOS2DX_H_ */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user