main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <glbinding/glbinding.h>
  30. #include <glbinding/gl/gl.h>
  31. #define GLFW_INCLUDE_NONE
  32. #include <GLFW/glfw3.h>
  33. #include <iostream>
  34. #include <spine-glfw.h>
  35. using namespace spine;
  36. int width = 800, height = 600;
  37. GLFWwindow *init_glfw() {
  38. if (!glfwInit()) {
  39. std::cerr << "Failed to initialize GLFW" << std::endl;
  40. return nullptr;
  41. }
  42. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  43. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  44. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  45. GLFWwindow *window = glfwCreateWindow(width, height, "spine-glfw", NULL, NULL);
  46. if (!window) {
  47. std::cerr << "Failed to create GLFW window" << std::endl;
  48. glfwTerminate();
  49. return nullptr;
  50. }
  51. glfwMakeContextCurrent(window);
  52. glbinding::initialize(glfwGetProcAddress);
  53. return window;
  54. }
  55. int main() {
  56. // Initialize GLFW and glbinding
  57. GLFWwindow *window = init_glfw();
  58. if (!window) return -1;
  59. // We use a y-down coordinate system, see renderer_set_viewport_size()
  60. Bone::setYDown(true);
  61. // Load the atlas and the skeleton data
  62. GlTextureLoader textureLoader;
  63. Atlas *atlas = new Atlas("data/spineboy-pma.atlas", &textureLoader);
  64. SkeletonBinary binary(atlas);
  65. SkeletonData *skeletonData = binary.readSkeletonDataFile("data/spineboy-pro.skel");
  66. // Create a skeleton from the data, set the skeleton's position to the bottom center of
  67. // the screen and scale it to make it smaller.
  68. Skeleton skeleton(skeletonData);
  69. skeleton.setPosition(width / 2, height - 100);
  70. skeleton.setScaleX(0.3);
  71. skeleton.setScaleY(0.3);
  72. // Create an AnimationState to drive animations on the skeleton. Set the "portal" animation
  73. // on track with index 0.
  74. AnimationStateData animationStateData(skeletonData);
  75. animationStateData.setDefaultMix(0.2f);
  76. AnimationState animationState(&animationStateData);
  77. animationState.setAnimation(0, "portal", true);
  78. animationState.addAnimation(0, "run", true, 0);
  79. // Create the renderer and set the viewport size to match the window size. This sets up a
  80. // pixel perfect orthogonal projection for 2D rendering.
  81. renderer_t *renderer = renderer_create();
  82. renderer_set_viewport_size(renderer, width, height);
  83. // Rendering loop
  84. double lastTime = glfwGetTime();
  85. while (!glfwWindowShouldClose(window)) {
  86. // Calculate the delta time in seconds
  87. double currTime = glfwGetTime();
  88. float delta = currTime - lastTime;
  89. lastTime = currTime;
  90. // Update and apply the animation state to the skeleton
  91. animationState.update(delta);
  92. animationState.apply(skeleton);
  93. // Update the skeleton time (used for physics)
  94. skeleton.update(delta);
  95. // Calculate the new pose
  96. skeleton.updateWorldTransform(spine::Physics_Update);
  97. // Clear the screen
  98. gl::glClear(gl::GL_COLOR_BUFFER_BIT);
  99. // Render the skeleton in its current pose
  100. renderer_draw(renderer, &skeleton, true);
  101. // Present the rendering results and poll for events
  102. glfwSwapBuffers(window);
  103. glfwPollEvents();
  104. }
  105. // Dispose everything
  106. renderer_dispose(renderer);
  107. delete skeletonData;
  108. delete atlas;
  109. // Kill the window and GLFW
  110. glfwTerminate();
  111. return 0;
  112. }