Add CustomCommand

This commit is contained in:
Nite Luo 2013-11-08 12:06:39 -08:00
parent be71eb8626
commit fa9d8fe077
6 changed files with 122 additions and 7 deletions

View File

@ -1 +1 @@
483a248a4f35f5351b3ffaecb0911023bed1812b
63359196a3b9f69ce5b8c2374708597ae31643e2

View File

@ -0,0 +1,44 @@
//
// Created by NiTe Luo on 11/8/13.
//
#include "CustomCommand.h"
NS_CC_BEGIN
CustomCommand::CustomCommand(int viewport, int32_t depth)
:RenderCommand()
, _viewport(viewport)
, _depth(depth)
, func(nullptr)
{
_type = CUSTOM_COMMAND;
}
CustomCommand::~CustomCommand()
{
}
int64_t CustomCommand::generateID()
{
_id = 0;
_id = (int64_t)_viewport << 61
| (int64_t)1 << 60 // translucent
| (int64_t)0 << 59 // is command
| (int64_t)_depth << 35;
return _id;
}
void CustomCommand::execute()
{
if(func)
{
func();
}
}
NS_CC_END

47
cocos/2d/CustomCommand.h Normal file
View File

@ -0,0 +1,47 @@
//
// Created by NiTe Luo on 11/8/13.
//
#ifndef _CC_CUSTOMCOMMAND_H_
#define _CC_CUSTOMCOMMAND_H_
#include "RenderCommand.h"
NS_CC_BEGIN
using namespace std;
class CustomCommand : public RenderCommand
{
public:
CustomCommand(int viewport, int32_t depth);
~CustomCommand();
// +----------+----------+-----+-----------------------------------+
// | | | | | |
// | ViewPort | Transluc | Cmd | Depth | |
// | 3 bits | 1 bit | 1 | 24 bits | |
// +----------+----------+-----+----------------+------------------+
virtual int64_t generateID();
void execute();
inline bool isTranslucent() { return true; }
//TODO support non-graphical command
inline bool isCommand() { return false; }
public:
function<void()> func;
protected:
int _viewport;
int32_t _depth;
};
NS_CC_END
#endif //_CC_CUSTOMCOMMAND_H_

View File

@ -17,7 +17,7 @@ NS_CC_BEGIN
enum RenderCommandType
{
QUAD_COMMAND,
CUSTOMIZE_COMMAND,
CUSTOM_COMMAND,
GROUP_COMMAND,
UNKNOWN_COMMAND,
};

View File

@ -6,6 +6,8 @@
#include "Renderer.h"
#include "CCShaderCache.h"
#include "ccGLStateCache.h"
#include "CustomCommand.h"
NS_CC_BEGIN
using namespace std;
@ -144,11 +146,12 @@ void Renderer::render()
if(_lastMaterialID != cmd->getMaterialID() || _numQuads == VBO_SIZE)
{
//Draw batched data
if(_numQuads > 0)
{
drawQuads();
}
drawQuads();
}
//Reset material if needed.
if(_lastMaterialID != cmd->getMaterialID())
{
//Set new material
_lastMaterialID = cmd->getMaterialID();
@ -161,10 +164,19 @@ void Renderer::render()
GL::bindTexture2D(cmd->getTextureID());
}
batchQuads(cmd);
break;
}
case CUSTOM_COMMAND:
{
flush();
CustomCommand* cmd = (CustomCommand*)command;
cmd->execute();
break;
}
default:
break;
}
@ -179,6 +191,9 @@ void Renderer::render()
void Renderer::drawQuads()
{
if(_numQuads <= 0)
return;
//Set VBO data
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
@ -207,4 +222,10 @@ void Renderer::batchQuads(QuadCommand* cmd)
_quads[_numQuads - 1] = *cmd->getQuad();
}
void Renderer::flush()
{
drawQuads();
_lastMaterialID = 0;
}
NS_CC_END

View File

@ -12,6 +12,7 @@
#include "RenderCommand.h"
#include "CCGLProgram.h"
#include "QuadCommand.h"
#include "CCGL.h"
#define VBO_SIZE 64
@ -39,11 +40,13 @@ protected:
void batchQuads(QuadCommand* cmd);
void drawQuads();
//Draw the previews queued quads and flush previous context
void flush();
protected:
vector<RenderCommand*> _renderQueue;
int _lastMaterialID;
V3F_C4B_T2F_Quad*_quads;
GLushort* _indices;
GLuint _VAOname;