main-cpp-lite.cpp 4.2 KB

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