SkeletonBatch.cpp 5.7 KB

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