SkeletonBatch.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated May 1, 2019. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2019, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. * NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. * INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #include <spine/SkeletonBatch.h>
  30. #include <spine/Extension.h>
  31. #include <algorithm>
  32. USING_NS_CC;
  33. #define EVENT_AFTER_DRAW_RESET_POSITION "director_after_draw"
  34. using std::max;
  35. #define INITIAL_SIZE (10000)
  36. namespace spine {
  37. static SkeletonBatch* instance = nullptr;
  38. SkeletonBatch* SkeletonBatch::getInstance () {
  39. if (!instance) instance = new SkeletonBatch();
  40. return instance;
  41. }
  42. void SkeletonBatch::destroyInstance () {
  43. if (instance) {
  44. delete instance;
  45. instance = nullptr;
  46. }
  47. }
  48. SkeletonBatch::SkeletonBatch () {
  49. for (unsigned int i = 0; i < INITIAL_SIZE; i++) {
  50. _commandsPool.push_back(new TrianglesCommand());
  51. }
  52. reset ();
  53. // callback after drawing is finished so we can clear out the batch state
  54. // for the next frame
  55. Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_AFTER_DRAW_RESET_POSITION, [this](EventCustom* eventCustom){
  56. this->update(0);
  57. });;
  58. }
  59. SkeletonBatch::~SkeletonBatch () {
  60. Director::getInstance()->getEventDispatcher()->removeCustomEventListeners(EVENT_AFTER_DRAW_RESET_POSITION);
  61. for (unsigned int i = 0; i < _commandsPool.size(); i++) {
  62. delete _commandsPool[i];
  63. _commandsPool[i] = nullptr;
  64. }
  65. }
  66. void SkeletonBatch::update (float delta) {
  67. reset();
  68. }
  69. cocos2d::V3F_C4B_T2F* SkeletonBatch::allocateVertices(uint32_t numVertices) {
  70. if (_vertices.size() - _numVertices < numVertices) {
  71. cocos2d::V3F_C4B_T2F* oldData = _vertices.data();
  72. _vertices.resize((_vertices.size() + numVertices) * 2 + 1);
  73. cocos2d::V3F_C4B_T2F* newData = _vertices.data();
  74. for (uint32_t i = 0; i < this->_nextFreeCommand; i++) {
  75. TrianglesCommand* command = _commandsPool[i];
  76. cocos2d::TrianglesCommand::Triangles& triangles = (cocos2d::TrianglesCommand::Triangles&)command->getTriangles();
  77. triangles.verts = newData + (triangles.verts - oldData);
  78. }
  79. }
  80. cocos2d::V3F_C4B_T2F* vertices = _vertices.data() + _numVertices;
  81. _numVertices += numVertices;
  82. return vertices;
  83. }
  84. void SkeletonBatch::deallocateVertices(uint32_t numVertices) {
  85. _numVertices -= numVertices;
  86. }
  87. unsigned short* SkeletonBatch::allocateIndices(uint32_t numIndices) {
  88. if (_indices.getCapacity() - _indices.size() < numIndices) {
  89. unsigned short* oldData = _indices.buffer();
  90. int oldSize = _indices.size();
  91. _indices.ensureCapacity(_indices.size() + numIndices);
  92. unsigned short* newData = _indices.buffer();
  93. for (uint32_t i = 0; i < this->_nextFreeCommand; i++) {
  94. TrianglesCommand* command = _commandsPool[i];
  95. cocos2d::TrianglesCommand::Triangles& triangles = (cocos2d::TrianglesCommand::Triangles&)command->getTriangles();
  96. if (triangles.indices >= oldData && triangles.indices < oldData + oldSize) {
  97. triangles.indices = newData + (triangles.indices - oldData);
  98. }
  99. }
  100. }
  101. unsigned short* indices = _indices.buffer() + _indices.size();
  102. _indices.setSize(_indices.size() + numIndices, 0);
  103. return indices;
  104. }
  105. void SkeletonBatch::deallocateIndices(uint32_t numIndices) {
  106. _indices.setSize(_indices.size() - numIndices, 0);
  107. }
  108. cocos2d::TrianglesCommand* SkeletonBatch::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) {
  109. TrianglesCommand* command = nextFreeCommand();
  110. command->init(globalOrder, texture, glProgramState, blendType, triangles, mv, flags);
  111. renderer->addCommand(command);
  112. return command;
  113. }
  114. void SkeletonBatch::reset() {
  115. _nextFreeCommand = 0;
  116. _numVertices = 0;
  117. _indices.setSize(0, 0);
  118. }
  119. cocos2d::TrianglesCommand* SkeletonBatch::nextFreeCommand() {
  120. if (_commandsPool.size() <= _nextFreeCommand) {
  121. unsigned int newSize = _commandsPool.size() * 2 + 1;
  122. for (int i = _commandsPool.size(); i < newSize; i++) {
  123. _commandsPool.push_back(new TrianglesCommand());
  124. }
  125. }
  126. return _commandsPool[_nextFreeCommand++];
  127. }
  128. }