main.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <glbinding/glbinding.h>
  2. #include <glbinding/gl/gl.h>
  3. #define GLFW_INCLUDE_NONE
  4. #include <GLFW/glfw3.h>
  5. #include <iostream>
  6. #include <spine-glfw.h>
  7. using namespace spine;
  8. int width = 800, height = 600;
  9. GLFWwindow *init_glfw() {
  10. if (!glfwInit()) {
  11. std::cerr << "Failed to initialize GLFW" << std::endl;
  12. return nullptr;
  13. }
  14. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  15. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  16. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  17. GLFWwindow *window = glfwCreateWindow(width, height, "spine-glfw", NULL, NULL);
  18. if (!window) {
  19. std::cerr << "Failed to create GLFW window" << std::endl;
  20. glfwTerminate();
  21. return nullptr;
  22. }
  23. glfwMakeContextCurrent(window);
  24. glbinding::initialize(glfwGetProcAddress);
  25. return window;
  26. }
  27. int main() {
  28. // Initialize GLFW and glbinding
  29. GLFWwindow *window = init_glfw();
  30. if (!window) return -1;
  31. // We use a y-down coordinate system, see renderer_set_viewport_size()
  32. Bone::setYDown(true);
  33. // Load the atlas and the skeleton data
  34. GlTextureLoader textureLoader;
  35. Atlas *atlas = new Atlas("data/spineboy-pma.atlas", &textureLoader);
  36. SkeletonJson json(atlas);
  37. SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.skel");
  38. // Create a skeleton from the data, set the skeleton's position to the bottom center of
  39. // the screen and scale it to make it smaller.
  40. Skeleton skeleton(skeletonData);
  41. skeleton.setPosition(width / 2, height - 100);
  42. skeleton.setScaleX(0.3);
  43. skeleton.setScaleY(0.3);
  44. // Create an AnimationState to drive animations on the skeleton. Set the "portal" animation
  45. // on track with index 0.
  46. AnimationStateData animationStateData(skeletonData);
  47. AnimationState animationState(&animationStateData);
  48. animationState.setAnimation(0, "portal", true);
  49. // Create the renderer and set the viewport size to match the window size. This sets up a
  50. // pixel perfect orthogonal projection for 2D rendering.
  51. renderer_t *renderer = renderer_create();
  52. renderer_set_viewport_size(renderer, width, height);
  53. // Rendering loop
  54. double lastTime = glfwGetTime();
  55. while (!glfwWindowShouldClose(window)) {
  56. // Calculate the delta time in seconds
  57. double currTime = glfwGetTime();
  58. float delta = currTime - lastTime;
  59. lastTime = currTime;
  60. // Update and apply the animation state to the skeleton
  61. animationState.update(delta);
  62. animationState.apply(skeleton);
  63. // Update the skeleton time (used for physics)
  64. skeleton.update(delta);
  65. // Calculate the new pose
  66. skeleton.updateWorldTransform(spine::Physics_Update);
  67. // Clear the screen
  68. gl::glClear(gl::GL_COLOR_BUFFER_BIT);
  69. // Render the skeleton in its current pose
  70. renderer_draw(renderer, &skeleton, true);
  71. // Present the rendering results and poll for events
  72. glfwSwapBuffers(window);
  73. glfwPollEvents();
  74. }
  75. // Dispose everything
  76. renderer_dispose(renderer);
  77. delete skeletonData;
  78. delete atlas;
  79. // Kill the window and GLFW
  80. glfwTerminate();
  81. return 0;
  82. }