123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #include "CommandBuff.h"
- #include <algorithm>
- USING_NS_CC;
- #define EVENT_AFTER_DRAW_RESET_POSITION "director_after_draw"
- using std::max;
- #define INITIAL_SIZE (10000)
- static CommandBuff* instance = nullptr;
- CommandBuff* CommandBuff::getInstance () {
- if (!instance) instance = new CommandBuff();
- return instance;
- }
- void CommandBuff::destroyInstance () {
- if (instance) {
- delete instance;
- instance = nullptr;
- }
- }
- CommandBuff::CommandBuff () {
- for (unsigned int i = 0; i < INITIAL_SIZE; i++) {
- _commandsPool.push_back(new TrianglesCommand());
- }
- reset ();
- // callback after drawing is finished so we can clear out the batch state
- // for the next frame
- Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_AFTER_DRAW_RESET_POSITION, [this](EventCustom* eventCustom){
- this->update(0);
- });;
- }
- CommandBuff::~CommandBuff () {
- Director::getInstance()->getEventDispatcher()->removeCustomEventListeners(EVENT_AFTER_DRAW_RESET_POSITION);
- for (unsigned int i = 0; i < _commandsPool.size(); i++) {
- delete _commandsPool[i];
- _commandsPool[i] = nullptr;
- }
- }
- void CommandBuff::update (float delta) {
- reset();
- }
- cocos2d::TrianglesCommand* CommandBuff::addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, cocos2d::GLProgramState* glProgramState, cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand::Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags) {
- TrianglesCommand* command = nextFreeCommand();
- command->init(globalOrder, texture, glProgramState, blendType, triangles, mv, flags);
- renderer->addCommand(command);
- return command;
- }
- void CommandBuff::reset() {
- _nextFreeCommand = 0;
- }
- cocos2d::TrianglesCommand* CommandBuff::nextFreeCommand() {
- if (_commandsPool.size() <= _nextFreeCommand) {
- auto newSize = _commandsPool.size() * 2 + 1;
- for (auto i = _commandsPool.size(); i < newSize; i++) {
- _commandsPool.push_back(new TrianglesCommand());
- }
- }
- return _commandsPool[_nextFreeCommand++];
- }
|