issue #3812: using std::vector<QuadCommand*> to hold current batched command

This commit is contained in:
Huabing.Xu 2014-01-22 15:18:27 +08:00
parent 778292738f
commit cf6785ffcf
2 changed files with 20 additions and 13 deletions

View File

@ -244,12 +244,12 @@ void Renderer::render()
if(commandType == RenderCommand::Type::QUAD_COMMAND) if(commandType == RenderCommand::Type::QUAD_COMMAND)
{ {
QuadCommand* cmd = static_cast<QuadCommand*>(command); QuadCommand* cmd = static_cast<QuadCommand*>(command);
ssize_t cmdQuadCount = cmd->getQuadCount(); CCASSERT(nullptr!= cmd, "Illegal command for RenderCommand Taged as QUAD_COMMAND");
//Batch quads //Batch quads
if(_numQuads + cmdQuadCount > VBO_SIZE) if(_numQuads + cmd->getQuadCount() > VBO_SIZE)
{ {
CCASSERT(cmdQuadCount < VBO_SIZE, "VBO is not big enough for quad data, please break the quad data down or use customized render command"); CCASSERT(cmd->getQuadCount() < VBO_SIZE, "VBO is not big enough for quad data, please break the quad data down or use customized render command");
//Draw batched quads if VBO is full //Draw batched quads if VBO is full
_lastCommand --; _lastCommand --;
@ -257,10 +257,12 @@ void Renderer::render()
_lastCommand ++; _lastCommand ++;
} }
memcpy(_quads + _numQuads, cmd->getQuads(), sizeof(V3F_C4B_T2F_Quad) * cmdQuadCount); _batchedQuadCommands.push_back(cmd);
convertToWorldCoordiantes(_quads + _numQuads, cmdQuadCount, cmd->getModelView());
_numQuads += cmdQuadCount; memcpy(_quads + _numQuads, cmd->getQuads(), sizeof(V3F_C4B_T2F_Quad) * cmd->getQuadCount());
convertToWorldCoordiantes(_quads + _numQuads, cmd->getQuadCount(), cmd->getModelView());
_numQuads += cmd->getQuadCount();
} }
else if(commandType == RenderCommand::Type::CUSTOM_COMMAND) else if(commandType == RenderCommand::Type::CUSTOM_COMMAND)
{ {
@ -355,7 +357,7 @@ void Renderer::drawBatchedQuads()
int startQuad = 0; int startQuad = 0;
//Upload buffer to VBO //Upload buffer to VBO
if(_numQuads <= 0) if(_numQuads <= 0 || 0 == _batchedQuadCommands.size())
{ {
_firstCommand = _lastCommand; _firstCommand = _lastCommand;
return; return;
@ -398,12 +400,13 @@ void Renderer::drawBatchedQuads()
} }
//Start drawing verties in batch //Start drawing verties in batch
for(size_t i = _firstCommand; i <= _lastCommand; i++) //for(size_t i = _firstCommand; i <= _lastCommand; i++)
for(auto i = _batchedQuadCommands.begin(); i != _batchedQuadCommands.end(); ++i)
{ {
RenderCommand* command = _renderGroups[_renderStack.top().renderQueueID][i]; //RenderCommand* command = _renderGroups[_renderStack.top().renderQueueID][i];
if (command->getType() == RenderCommand::Type::QUAD_COMMAND) //if (command->getType() == RenderCommand::Type::QUAD_COMMAND)
{ {
QuadCommand* cmd = static_cast<QuadCommand*>(command); QuadCommand* cmd = *i;
if(_lastMaterialID != cmd->getMaterialID()) if(_lastMaterialID != cmd->getMaterialID())
{ {
//Draw quads //Draw quads
@ -443,7 +446,7 @@ void Renderer::drawBatchedQuads()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
_batchedQuadCommands.clear();
_firstCommand = _lastCommand + 1; _firstCommand = _lastCommand + 1;
_numQuads = 0; _numQuads = 0;
} }

View File

@ -32,10 +32,12 @@
#include "CCGL.h" #include "CCGL.h"
#include <vector> #include <vector>
#include <stack> #include <stack>
#include <list>
NS_CC_BEGIN NS_CC_BEGIN
class EventListenerCustom; class EventListenerCustom;
class QuadCommand;
typedef std::vector<RenderCommand*> RenderQueue; typedef std::vector<RenderCommand*> RenderQueue;
@ -91,6 +93,8 @@ protected:
size_t _firstCommand; size_t _firstCommand;
size_t _lastCommand; size_t _lastCommand;
std::vector<QuadCommand*> _batchedQuadCommands;
V3F_C4B_T2F_Quad _quads[VBO_SIZE]; V3F_C4B_T2F_Quad _quads[VBO_SIZE];
GLushort _indices[6 * VBO_SIZE]; GLushort _indices[6 * VBO_SIZE];
GLuint _quadVAO; GLuint _quadVAO;