From 3d4bfe778052c7f7cd85d24cd41e6932cc658d7e Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 1 Aug 2014 17:50:22 +0800 Subject: [PATCH] using VertexBuffer instead of raw buffer --- cocos/2d/CCFastTMXLayer.cpp | 31 ++++++++++++++----------------- cocos/2d/CCFastTMXLayer.h | 5 ++++- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index 7b422e766a..eb2531b3b9 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -121,8 +121,9 @@ TMXLayer::TMXLayer() , _useAutomaticVertexZ(false) , _dirty(true) , _quadsDirty(true) +, _vertexBuffer(nullptr) { - _buffersVBO[0] = _buffersVBO[1] = 0; + _buffersVBO = 0; } TMXLayer::~TMXLayer() @@ -130,15 +131,12 @@ TMXLayer::~TMXLayer() CC_SAFE_RELEASE(_tileSet); CC_SAFE_RELEASE(_texture); CC_SAFE_DELETE_ARRAY(_tiles); - if(glIsBuffer(_buffersVBO[0])) + CC_SAFE_RELEASE(_vertexBuffer); + if(glIsBuffer(_buffersVBO)) { - glDeleteBuffers(1, &_buffersVBO[0]); + glDeleteBuffers(1, &_buffersVBO); } - if(glIsBuffer(_buffersVBO[1])) - { - glDeleteBuffers(1, &_buffersVBO[1]); - } } void TMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) @@ -182,8 +180,8 @@ void TMXLayer::onDraw(int offset, int count) getGLProgramState()->apply(_modelViewTransform); GL::bindVAO(0); - glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); + glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer->getVBO()); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO); GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)0); @@ -291,23 +289,22 @@ void TMXLayer::updateTiles(const Rect& culledRect) void TMXLayer::updateVertexBuffer() { GL::bindVAO(0); - if(!glIsBuffer(_buffersVBO[0])) + if(nullptr == _vertexBuffer) { - glGenBuffers(1, &_buffersVBO[0]); + _vertexBuffer = VertexBuffer::create(sizeof(V3F_C4B_T2F), (int)_totalQuads.size() * 4); + CC_SAFE_RETAIN(_vertexBuffer); } + _vertexBuffer->updateVertices((void*)&_totalQuads[0], (int)_totalQuads.size() * 4, 0); - glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); - glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B_T2F_Quad) * _totalQuads.size(), (GLvoid*)&_totalQuads[0], GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); } void TMXLayer::updateIndexBuffer() { - if(!glIsBuffer(_buffersVBO[1])) + if(!glIsBuffer(_buffersVBO)) { - glGenBuffers(1, &_buffersVBO[1]); + glGenBuffers(1, &_buffersVBO); } - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * _indices.size(), &_indices[0], GL_DYNAMIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index f148eccc19..7f6bbbe51a 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -32,6 +32,7 @@ THE SOFTWARE. #include "CCNode.h" #include "renderer/CCCustomCommand.h" #include "renderer/CCQuadCommand.h" +#include "renderer/CCPrimitiveCommand.h" #include #include @@ -228,7 +229,7 @@ protected: /** container for sprite children. map > */ std::map > _spriteContainer; - GLuint _buffersVBO[2]; //0: vertex, 1: indices + GLuint _buffersVBO; //0: vertex, 1: indices Size _screenGridSize; Rect _screenGridRect; @@ -249,6 +250,8 @@ protected: std::vector _renderCommands; bool _dirty; + VertexBuffer* _vertexBuffer; + public: /** Possible orientations of the TMX map */ static const int FAST_TMX_ORIENTATION_ORTHO;