Browse Source

spine-cocos2dx, auto resize SkeletonBatch buffer.

NathanSweet 9 years ago
parent
commit
747a0a1e38

+ 4 - 0
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.");

+ 32 - 15
spine-cocos2dx/3/src/spine/SkeletonBatch.cpp

@@ -34,6 +34,7 @@
 #include <algorithm>
 
 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;
 }
 
 }

+ 6 - 5
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;

+ 1 - 1
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();

+ 1 - 0
spine-cocos2dx/3/src/spine/spine-cocos2dx.h

@@ -37,5 +37,6 @@
 #include <spine/Cocos2dAttachmentLoader.h>
 #include <spine/SkeletonRenderer.h>
 #include <spine/SkeletonAnimation.h>
+#include <spine/SkeletonBatch.h>
 
 #endif /* SPINE_COCOS2DX_H_ */