From 0e51504ee6547f43b1c83fbda7f0eef0a18afc14 Mon Sep 17 00:00:00 2001 From: Nite Luo Date: Wed, 20 Nov 2013 11:05:01 -0800 Subject: [PATCH] Use stack to manage current RenderGroup --- cocos/2d/NewClippingNode.cpp | 15 +++++++++------ cocos/2d/renderer/Renderer.cpp | 20 ++++++++++++++------ cocos/2d/renderer/Renderer.h | 9 +++++---- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/cocos/2d/NewClippingNode.cpp b/cocos/2d/NewClippingNode.cpp index 8e0a8151ca..b480e2d837 100644 --- a/cocos/2d/NewClippingNode.cpp +++ b/cocos/2d/NewClippingNode.cpp @@ -82,28 +82,31 @@ NewClippingNode::NewClippingNode() void NewClippingNode::visit() { //Add group command + + Renderer* renderer = Renderer::getInstance(); + GroupCommand* groupCommand = new GroupCommand(0,_vertexZ); - Renderer::getInstance()->addCommand(groupCommand); + renderer->addCommand(groupCommand); - Renderer::getInstance()->setCurrentRenderQueue(groupCommand->getRenderQueueID()); + renderer->pushGroup(groupCommand->getRenderQueueID()); CustomCommand* beforeVisitCmd = new CustomCommand(0,_vertexZ); beforeVisitCmd->func = CC_CALLBACK_0(NewClippingNode::beforeVisit, this); - Renderer::getInstance()->addCommand(beforeVisitCmd, groupCommand->getRenderQueueID()); + renderer->addCommand(beforeVisitCmd, groupCommand->getRenderQueueID()); _stencil->visit(); CustomCommand* afterDrawStencilCmd = new CustomCommand(0,_vertexZ); afterDrawStencilCmd->func = CC_CALLBACK_0(NewClippingNode::afterDrawStencil, this); - Renderer::getInstance()->addCommand(afterDrawStencilCmd, groupCommand->getRenderQueueID()); + renderer->addCommand(afterDrawStencilCmd, groupCommand->getRenderQueueID()); Node::visit(); CustomCommand* afterVisitCmd = new CustomCommand(0,_vertexZ); afterVisitCmd->func = CC_CALLBACK_0(NewClippingNode::afterVisit, this); - Renderer::getInstance()->addCommand(afterVisitCmd, groupCommand->getRenderQueueID()); + renderer->addCommand(afterVisitCmd, groupCommand->getRenderQueueID()); - Renderer::getInstance()->setCurrentRenderQueue(DEFAULT_RENDER_QUEUE); + renderer->popGroup(); } void NewClippingNode::beforeVisit() diff --git a/cocos/2d/renderer/Renderer.cpp b/cocos/2d/renderer/Renderer.cpp index 7ee9486e2c..0a240b3ecd 100644 --- a/cocos/2d/renderer/Renderer.cpp +++ b/cocos/2d/renderer/Renderer.cpp @@ -3,13 +3,11 @@ // -#include #include "Renderer.h" #include "CCShaderCache.h" #include "ccGLStateCache.h" #include "CustomCommand.h" #include "QuadCommand.h" -#include "CCGL.h" #include "GroupCommand.h" @@ -42,8 +40,8 @@ Renderer::Renderer() ,_firstCommand(0) ,_lastCommand(0) { - _currRenderQueueID = DEFAULT_RENDER_QUEUE; - + _commandGroupStack.push(DEFAULT_RENDER_QUEUE); + RenderQueue defaultRenderQueue; _renderGroups.push_back(defaultRenderQueue); _renderStack.push({DEFAULT_RENDER_QUEUE, 0}); @@ -112,7 +110,7 @@ void Renderer::setupVBOAndVAO() void Renderer::addCommand(RenderCommand* command) { command->generateID(); - _renderGroups[_currRenderQueueID].push_back(command); + _renderGroups[_commandGroupStack.top()].push_back(command); } void Renderer::addCommand(RenderCommand* command, int renderQueue) @@ -121,11 +119,21 @@ void Renderer::addCommand(RenderCommand* command, int renderQueue) _renderGroups[renderQueue].push_back(command); } +void Renderer::pushGroup(int renderQueueID) +{ + _commandGroupStack.push(renderQueueID); +} + +void Renderer::popGroup() +{ + _commandGroupStack.pop(); +} + int Renderer::createRenderQueue() { RenderQueue newRenderQueue; _renderGroups.push_back(newRenderQueue); - return _renderGroups.size() - 1; + return (int)_renderGroups.size() - 1; } bool compareRenderCommand(RenderCommand* a, RenderCommand* b) diff --git a/cocos/2d/renderer/Renderer.h b/cocos/2d/renderer/Renderer.h index 9faca1164e..9d0ca59e00 100644 --- a/cocos/2d/renderer/Renderer.h +++ b/cocos/2d/renderer/Renderer.h @@ -37,11 +37,12 @@ public: //TODO support multiple viewport void addCommand(RenderCommand* command); void addCommand(RenderCommand* command, int renderQueue); + void pushGroup(int renderQueueID); + void popGroup(); + int createRenderQueue(); void render(); - inline void setCurrentRenderQueue(int renderQueueID) { _currRenderQueueID = renderQueueID; } - protected: Renderer(); ~Renderer(); @@ -54,11 +55,11 @@ protected: void flush(); protected: + stack _commandGroupStack; + stack _renderStack; vector _renderGroups; - int _currRenderQueueID; - int _lastMaterialID; size_t _firstCommand;