main-cpp-lite.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. uint8_t *read_file(const char *path, int *length) {
  56. FILE *file = fopen(path, "rb");
  57. if (!file) return 0;
  58. fseek(file, 0, SEEK_END);
  59. *length = (int) ftell(file);
  60. fseek(file, 0, SEEK_SET);
  61. uint8_t *data = (uint8_t *) malloc(*length);
  62. fread(data, 1, *length, file);
  63. fclose(file);
  64. return data;
  65. }
  66. void *load_texture(const char *path) {
  67. return (void *) (uintptr_t) texture_load(path);
  68. }
  69. void unload_texture(void *texture) {
  70. texture_dispose((texture_t) (uintptr_t) texture);
  71. }
  72. int main() {
  73. // Initialize GLFW and glbinding
  74. GLFWwindow *window = init_glfw();
  75. if (!window) return -1;
  76. // We use a y-down coordinate system, see renderer_set_viewport_size()
  77. Bone::setYDown(true);
  78. // Load the atlas and the skeleton data
  79. int atlas_length = 0;
  80. uint8_t *atlas_bytes = read_file("data/spineboy-pma.atlas", &atlas_length);
  81. spine_atlas atlas = spine_atlas_load_callback((utf8 *) atlas_bytes, "data/", load_texture, unload_texture);
  82. int skeleton_length = 0;
  83. uint8_t *skeleton_bytes = read_file("data/spineboy-pro.skel", &skeleton_length);
  84. spine_skeleton_data_result result = spine_skeleton_data_load_binary(atlas, skeleton_bytes, skeleton_length);
  85. spine_skeleton_data skeleton_data = spine_skeleton_data_result_get_data(result);
  86. // Create a skeleton from the data, set the skeleton's position to the bottom center of
  87. // the screen and scale it to make it smaller.
  88. spine_skeleton_drawable drawable = spine_skeleton_drawable_create(skeleton_data);
  89. spine_skeleton skeleton = spine_skeleton_drawable_get_skeleton(drawable);
  90. spine_skeleton_set_position(skeleton, width / 2, height - 100);
  91. spine_skeleton_set_scale(skeleton, 0.3f, 0.3f);
  92. // Create an AnimationState to drive animations on the skeleton. Set the "portal" animation
  93. // on track with index 0.
  94. spine_animation_state animation_state = spine_skeleton_drawable_get_animation_state(drawable);
  95. spine_animation_state_data animation_state_data = spine_animation_state_get_data(animation_state);
  96. spine_animation_state_data_set_default_mix(animation_state_data, 0.2f);
  97. spine_animation_state_set_animation_by_name(animation_state, 0, "portal", true);
  98. spine_animation_state_add_animation_by_name(animation_state, 0, "run", true, 0);
  99. // Create the renderer and set the viewport size to match the window size. This sets up a
  100. // pixel perfect orthogonal projection for 2D rendering.
  101. renderer_t *renderer = renderer_create();
  102. renderer_set_viewport_size(renderer, width, height);
  103. // Rendering loop
  104. double lastTime = glfwGetTime();
  105. while (!glfwWindowShouldClose(window)) {
  106. // Calculate the delta time in seconds
  107. double currTime = glfwGetTime();
  108. float delta = currTime - lastTime;
  109. lastTime = currTime;
  110. // Update and apply the animation state to the skeleton
  111. spine_animation_state_update(animation_state, delta);
  112. spine_animation_state_apply(animation_state, skeleton);
  113. // Update the skeleton time (used for physics)
  114. spine_skeleton_update(skeleton, delta);
  115. // Calculate the new pose
  116. spine_skeleton_update_world_transform(skeleton, SPINE_PHYSICS_UPDATE);
  117. // Clear the screen
  118. gl::glClear(gl::GL_COLOR_BUFFER_BIT);
  119. // Render the skeleton in its current pose
  120. renderer_draw_lite(renderer, skeleton, true);
  121. // Present the rendering results and poll for events
  122. glfwSwapBuffers(window);
  123. glfwPollEvents();
  124. }
  125. // Dispose everything
  126. renderer_dispose(renderer);
  127. spine_skeleton_data_dispose(skeleton_data);
  128. spine_atlas_dispose(atlas);
  129. // Kill the window and GLFW
  130. glfwTerminate();
  131. return 0;
  132. }