testbed.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <SFML/Graphics.hpp>
  30. #include <spine/Log.h>
  31. #include <spine/spine-sfml.h>
  32. using namespace spine;
  33. class NullTextureLoader : public TextureLoader {
  34. public:
  35. virtual void load(AtlasPage &, const String &) {}
  36. virtual void unload(void *) {}
  37. };
  38. class NullAttachmentLoader : public AttachmentLoader {
  39. virtual RegionAttachment *newRegionAttachment(Skin &, const String &name, const String &, Sequence *) {
  40. return new (__FILE__, __LINE__) RegionAttachment(name);
  41. }
  42. virtual MeshAttachment *newMeshAttachment(Skin &, const String &name, const String &, Sequence *) {
  43. return new (__FILE__, __LINE__) MeshAttachment(name);
  44. }
  45. virtual BoundingBoxAttachment *newBoundingBoxAttachment(Skin &, const String &name) {
  46. return new (__FILE__, __LINE__) BoundingBoxAttachment(name);
  47. }
  48. virtual PathAttachment *newPathAttachment(Skin &, const String &name) {
  49. return new (__FILE__, __LINE__) PathAttachment(name);
  50. }
  51. virtual PointAttachment *newPointAttachment(Skin &, const String &name) {
  52. return new (__FILE__, __LINE__) PointAttachment(name);
  53. }
  54. virtual ClippingAttachment *newClippingAttachment(Skin &, const String &name) {
  55. return new (__FILE__, __LINE__) ClippingAttachment(name);
  56. }
  57. virtual void configureAttachment(Attachment *) {
  58. }
  59. };
  60. int main(void) {
  61. String atlasFile("data/sack-pma.atlas");
  62. String skeletonFile("data/sack-pro.json");
  63. String animation = "walk";
  64. String skin = "";
  65. float scale = 0.6f;
  66. SFMLTextureLoader textureLoader;
  67. NullAttachmentLoader nullLoader;
  68. Atlas *atlas = atlasFile.length() == 0 ? nullptr : new Atlas(atlasFile, &textureLoader);
  69. SkeletonData *skeletonData = nullptr;
  70. if (strnstr(skeletonFile.buffer(), ".skel", skeletonFile.length()) != nullptr) {
  71. SkeletonBinary *binary = nullptr;
  72. if (atlas) {
  73. binary = new SkeletonBinary(atlas);
  74. } else {
  75. binary = new SkeletonBinary(&nullLoader);
  76. }
  77. binary->setScale(scale);
  78. skeletonData = binary->readSkeletonDataFile(skeletonFile);
  79. delete binary;
  80. } else {
  81. SkeletonJson *json = nullptr;
  82. if (atlas) {
  83. json = new SkeletonJson(atlas);
  84. } else {
  85. json = new SkeletonJson(&nullLoader);
  86. }
  87. json->setScale(scale);
  88. skeletonData = json->readSkeletonDataFile(skeletonFile);
  89. delete json;
  90. }
  91. AnimationStateData stateData(skeletonData);
  92. SkeletonDrawable drawable(skeletonData, &stateData);
  93. drawable.skeleton->updateWorldTransform(Physics_Update);
  94. drawable.skeleton->setPosition(320, 960);
  95. if (animation.length() > 0) drawable.state->setAnimation(0, animation, true);
  96. if (skin.length() > 0) drawable.skeleton->setSkin(skin);
  97. sf::RenderWindow window(sf::VideoMode(1024, 1024), "Spine SFML - testbed");
  98. window.setFramerateLimit(60);
  99. sf::Event event;
  100. sf::Clock deltaClock;
  101. while (window.isOpen()) {
  102. while (window.pollEvent(event))
  103. if (event.type == sf::Event::Closed) window.close();
  104. float delta = deltaClock.getElapsedTime().asSeconds();
  105. deltaClock.restart();
  106. drawable.update(delta);
  107. window.clear();
  108. window.draw(drawable);
  109. window.display();
  110. }
  111. return 0;
  112. }