BatchingExample.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated April 5, 2025. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2025, 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 "BatchingExample.h"
  30. #include "SequenceExample.h"
  31. USING_NS_CC;
  32. using namespace spine;
  33. #define NUM_SKELETONS 50
  34. Cocos2dTextureLoader textureLoader;
  35. Scene *BatchingExample::scene() {
  36. Scene *scene = Scene::create();
  37. scene->addChild(BatchingExample::create());
  38. return scene;
  39. }
  40. bool BatchingExample::init() {
  41. if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
  42. // Load the texture atlas. Note that the texture loader has to live
  43. // as long as the Atlas, as the Atlas destructor will call TextureLoader::unload.
  44. _atlas = new (__FILE__, __LINE__) Atlas("spineboy.atlas", &textureLoader, true);
  45. CCASSERT(_atlas, "Error reading atlas file.");
  46. // This attachment loader configures attachments with data needed for cocos2d-x rendering.
  47. // Do not dispose the attachment loader until the skeleton data is disposed!
  48. _attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
  49. // Load the skeleton data.
  50. SkeletonJson *json = new (__FILE__, __LINE__) SkeletonJson(_attachmentLoader);
  51. json->setScale(0.6f);// Resizes skeleton data to 60% of the size it was in Spine.
  52. _skeletonData = json->readSkeletonDataFile("spineboy-pro.json");
  53. CCASSERT(_skeletonData, json->getError().isEmpty() ? json->getError().buffer() : "Error reading skeleton data file.");
  54. delete json;
  55. // Setup mix times.
  56. _stateData = new (__FILE__, __LINE__) AnimationStateData(_skeletonData);
  57. _stateData->setMix("walk", "jump", 0.2f);
  58. _stateData->setMix("jump", "run", 0.2f);
  59. int xMin = _contentSize.width * 0.10f, xMax = _contentSize.width * 0.90f;
  60. int yMin = 0, yMax = _contentSize.height * 0.7f;
  61. for (int i = 0; i < NUM_SKELETONS; i++) {
  62. // Each skeleton node shares the same atlas, skeleton data, and mix times.
  63. SkeletonAnimation *skeletonNode = SkeletonAnimation::createWithData(_skeletonData, false);
  64. skeletonNode->setAnimationStateData(_stateData);
  65. skeletonNode->setAnimation(0, "walk", true);
  66. skeletonNode->addAnimation(0, "jump", true, RandomHelper::random_int(0, 300) / 100.0f);
  67. skeletonNode->addAnimation(0, "run", true);
  68. // alternative setting two color tint for groups of 10 skeletons
  69. // should end up with #skeletons / 10 batches
  70. // if (j++ < 10)
  71. // skeletonNode->setTwoColorTint(true);
  72. // if (j == 20) j = 0;
  73. // skeletonNode->setTwoColorTint(true);
  74. skeletonNode->setPosition(Vec2(
  75. RandomHelper::random_int(xMin, xMax),
  76. RandomHelper::random_int(yMin, yMax)));
  77. addChild(skeletonNode);
  78. }
  79. scheduleUpdate();
  80. EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
  81. listener->onTouchBegan = [this](Touch *touch, cocos2d::Event *event) -> bool {
  82. Director::getInstance()->replaceScene(SequenceExample::scene());
  83. return true;
  84. };
  85. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  86. return true;
  87. }
  88. BatchingExample::~BatchingExample() {
  89. // SkeletonAnimation instances are cocos2d-x nodes and are disposed of automatically as normal, but the data created
  90. // manually to be shared across multiple SkeletonAnimations needs to be disposed of manually.
  91. delete _skeletonData;
  92. delete _stateData;
  93. delete _attachmentLoader;
  94. delete _atlas;
  95. }