mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[glfw] Add dragon examples and fix sequence renderer follow-through
This commit is contained in:
parent
6e582e724f
commit
62af381897
@ -300,6 +300,10 @@ cp -f ../celestial-circus/export/celestial-circus-pro.json "$ROOT/spine-glfw/dat
|
||||
cp -f ../celestial-circus/export/celestial-circus-pro.skel "$ROOT/spine-glfw/data/"
|
||||
cp -f ../celestial-circus/export/celestial-circus-pma.atlas "$ROOT/spine-glfw/data/"
|
||||
cp -f ../celestial-circus/export/celestial-circus-pma.png "$ROOT/spine-glfw/data/"
|
||||
cp -f ../dragon/export/dragon-ess.json "$ROOT/spine-glfw/data/"
|
||||
cp -f ../dragon/export/dragon-ess.skel "$ROOT/spine-glfw/data/"
|
||||
cp -f ../dragon/export/dragon-pma.atlas "$ROOT/spine-glfw/data/"
|
||||
cp -f ../dragon/export/dragon-pma*.png "$ROOT/spine-glfw/data/"
|
||||
|
||||
echo "spine-sfml"
|
||||
rm -f "$ROOT/spine-sfml/data/"*
|
||||
|
||||
@ -78,6 +78,46 @@ target_link_libraries(spine-glfw-physics LINK_PUBLIC spine-glfw)
|
||||
target_link_libraries(spine-glfw-physics PRIVATE glbinding::glbinding)
|
||||
set_property(TARGET spine-glfw-physics PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
|
||||
|
||||
# Physics example (C)
|
||||
add_executable(spine-glfw-physics-c example/physics-c.cpp)
|
||||
target_link_libraries(spine-glfw-physics-c PRIVATE glfw)
|
||||
target_link_libraries(spine-glfw-physics-c PRIVATE OpenGL::GL)
|
||||
target_link_libraries(spine-glfw-physics-c LINK_PUBLIC spine-glfw)
|
||||
target_link_libraries(spine-glfw-physics-c PRIVATE glbinding::glbinding)
|
||||
set_property(TARGET spine-glfw-physics-c PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
|
||||
|
||||
# Dragon example (sequence attachments, binary)
|
||||
add_executable(spine-glfw-dragon example/dragon.cpp)
|
||||
target_link_libraries(spine-glfw-dragon PRIVATE glfw)
|
||||
target_link_libraries(spine-glfw-dragon PRIVATE OpenGL::GL)
|
||||
target_link_libraries(spine-glfw-dragon LINK_PUBLIC spine-glfw)
|
||||
target_link_libraries(spine-glfw-dragon PRIVATE glbinding::glbinding)
|
||||
set_property(TARGET spine-glfw-dragon PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
|
||||
|
||||
# Dragon example (sequence attachments, binary, C)
|
||||
add_executable(spine-glfw-dragon-c example/dragon-c.cpp)
|
||||
target_link_libraries(spine-glfw-dragon-c PRIVATE glfw)
|
||||
target_link_libraries(spine-glfw-dragon-c PRIVATE OpenGL::GL)
|
||||
target_link_libraries(spine-glfw-dragon-c LINK_PUBLIC spine-glfw)
|
||||
target_link_libraries(spine-glfw-dragon-c PRIVATE glbinding::glbinding)
|
||||
set_property(TARGET spine-glfw-dragon-c PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
|
||||
|
||||
# Dragon example (sequence attachments, json)
|
||||
add_executable(spine-glfw-dragon-json example/dragon-json.cpp)
|
||||
target_link_libraries(spine-glfw-dragon-json PRIVATE glfw)
|
||||
target_link_libraries(spine-glfw-dragon-json PRIVATE OpenGL::GL)
|
||||
target_link_libraries(spine-glfw-dragon-json LINK_PUBLIC spine-glfw)
|
||||
target_link_libraries(spine-glfw-dragon-json PRIVATE glbinding::glbinding)
|
||||
set_property(TARGET spine-glfw-dragon-json PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
|
||||
|
||||
# Dragon example (sequence attachments, json, C)
|
||||
add_executable(spine-glfw-dragon-json-c example/dragon-json-c.cpp)
|
||||
target_link_libraries(spine-glfw-dragon-json-c PRIVATE glfw)
|
||||
target_link_libraries(spine-glfw-dragon-json-c PRIVATE OpenGL::GL)
|
||||
target_link_libraries(spine-glfw-dragon-json-c LINK_PUBLIC spine-glfw)
|
||||
target_link_libraries(spine-glfw-dragon-json-c PRIVATE glbinding::glbinding)
|
||||
set_property(TARGET spine-glfw-dragon-json-c PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
|
||||
|
||||
# IK Following example
|
||||
add_executable(spine-glfw-ik-following example/ik-following.cpp)
|
||||
target_link_libraries(spine-glfw-ik-following PRIVATE glfw)
|
||||
@ -86,15 +126,37 @@ target_link_libraries(spine-glfw-ik-following LINK_PUBLIC spine-glfw)
|
||||
target_link_libraries(spine-glfw-ik-following PRIVATE glbinding::glbinding)
|
||||
set_property(TARGET spine-glfw-ik-following PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
|
||||
|
||||
# copy data to build directory
|
||||
add_custom_command(TARGET spine-glfw-example PRE_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_LIST_DIR}/data $<TARGET_FILE_DIR:spine-glfw-example>/data)
|
||||
# IK Following example (C)
|
||||
add_executable(spine-glfw-ik-following-c example/ik-following-c.cpp)
|
||||
target_link_libraries(spine-glfw-ik-following-c PRIVATE glfw)
|
||||
target_link_libraries(spine-glfw-ik-following-c PRIVATE OpenGL::GL)
|
||||
target_link_libraries(spine-glfw-ik-following-c LINK_PUBLIC spine-glfw)
|
||||
target_link_libraries(spine-glfw-ik-following-c PRIVATE glbinding::glbinding)
|
||||
set_property(TARGET spine-glfw-ik-following-c PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
|
||||
|
||||
add_custom_command(TARGET spine-glfw-physics PRE_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_LIST_DIR}/data $<TARGET_FILE_DIR:spine-glfw-physics>/data)
|
||||
# Mix-and-match binary loader repro example
|
||||
add_executable(spine-glfw-mix-and-match example/mix-and-match.cpp)
|
||||
target_link_libraries(spine-glfw-mix-and-match PRIVATE glfw)
|
||||
target_link_libraries(spine-glfw-mix-and-match PRIVATE OpenGL::GL)
|
||||
target_link_libraries(spine-glfw-mix-and-match LINK_PUBLIC spine-glfw)
|
||||
target_link_libraries(spine-glfw-mix-and-match PRIVATE glbinding::glbinding)
|
||||
set_property(TARGET spine-glfw-mix-and-match PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
|
||||
|
||||
add_custom_command(TARGET spine-glfw-ik-following PRE_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_LIST_DIR}/data $<TARGET_FILE_DIR:spine-glfw-ik-following>/data)
|
||||
# Copy data to the build directory once. Multiple PRE_BUILD copy_directory commands
|
||||
# targeting the same destination can race under ninja.
|
||||
set(SPINE_GLFW_DATA_DIR ${CMAKE_CURRENT_BINARY_DIR}/data)
|
||||
add_custom_target(spine-glfw-data ALL
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory ${SPINE_GLFW_DATA_DIR}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/data ${SPINE_GLFW_DATA_DIR})
|
||||
|
||||
add_dependencies(spine-glfw-example spine-glfw-data)
|
||||
add_dependencies(spine-glfw-example-c spine-glfw-data)
|
||||
add_dependencies(spine-glfw-dragon spine-glfw-data)
|
||||
add_dependencies(spine-glfw-dragon-c spine-glfw-data)
|
||||
add_dependencies(spine-glfw-dragon-json spine-glfw-data)
|
||||
add_dependencies(spine-glfw-dragon-json-c spine-glfw-data)
|
||||
add_dependencies(spine-glfw-physics spine-glfw-data)
|
||||
add_dependencies(spine-glfw-physics-c spine-glfw-data)
|
||||
add_dependencies(spine-glfw-ik-following spine-glfw-data)
|
||||
add_dependencies(spine-glfw-ik-following-c spine-glfw-data)
|
||||
add_dependencies(spine-glfw-mix-and-match spine-glfw-data)
|
||||
|
||||
1075
spine-glfw/data/dragon-ess.json
Normal file
1075
spine-glfw/data/dragon-ess.json
Normal file
File diff suppressed because it is too large
Load Diff
BIN
spine-glfw/data/dragon-ess.skel
Normal file
BIN
spine-glfw/data/dragon-ess.skel
Normal file
Binary file not shown.
114
spine-glfw/data/dragon-pma.atlas
Normal file
114
spine-glfw/data/dragon-pma.atlas
Normal file
@ -0,0 +1,114 @@
|
||||
dragon-pma.png
|
||||
size: 1024, 1024
|
||||
filter: Linear, Linear
|
||||
pma: true
|
||||
front-toe-a
|
||||
bounds: 300, 929, 29, 50
|
||||
front-toe-b
|
||||
bounds: 258, 660, 56, 57
|
||||
head
|
||||
bounds: 2, 719, 296, 260
|
||||
left-front-leg
|
||||
bounds: 99, 660, 84, 57
|
||||
left-wing09
|
||||
bounds: 2, 15, 264, 589
|
||||
rotate: 90
|
||||
right-wing07
|
||||
bounds: 647, 2, 365, 643
|
||||
right-wing08
|
||||
bounds: 2, 281, 365, 643
|
||||
rotate: 90
|
||||
right-wing09
|
||||
bounds: 354, 647, 365, 643
|
||||
rotate: 90
|
||||
tail04
|
||||
bounds: 185, 661, 56, 71
|
||||
rotate: 90
|
||||
tail06
|
||||
bounds: 2, 649, 95, 68
|
||||
thiagobrayner
|
||||
bounds: 2, 981, 350, 31
|
||||
|
||||
dragon-pma_2.png
|
||||
size: 1024, 1024
|
||||
filter: Linear, Linear
|
||||
pma: true
|
||||
back
|
||||
bounds: 647, 57, 190, 185
|
||||
chin
|
||||
bounds: 839, 28, 214, 146
|
||||
rotate: 90
|
||||
left-rear-leg
|
||||
bounds: 736, 244, 206, 177
|
||||
left-wing08
|
||||
bounds: 736, 423, 264, 589
|
||||
right-rear-toe
|
||||
bounds: 944, 312, 109, 77
|
||||
rotate: 90
|
||||
right-wing04
|
||||
bounds: 2, 2, 365, 643
|
||||
rotate: 90
|
||||
right-wing05
|
||||
bounds: 369, 369, 365, 643
|
||||
right-wing06
|
||||
bounds: 2, 369, 365, 643
|
||||
tail03
|
||||
bounds: 647, 275, 73, 92
|
||||
tail05
|
||||
bounds: 944, 251, 52, 59
|
||||
|
||||
dragon-pma_3.png
|
||||
size: 1024, 1024
|
||||
filter: Linear, Linear
|
||||
pma: true
|
||||
chest
|
||||
bounds: 858, 299, 136, 122
|
||||
left-front-thigh
|
||||
bounds: 647, 295, 84, 72
|
||||
left-rear-thigh
|
||||
bounds: 647, 117, 91, 149
|
||||
left-wing07
|
||||
bounds: 736, 423, 264, 589
|
||||
right-front-leg
|
||||
bounds: 647, 14, 101, 89
|
||||
rotate: 90
|
||||
right-front-thigh
|
||||
bounds: 740, 158, 108, 108
|
||||
right-rear-leg
|
||||
bounds: 740, 46, 116, 100
|
||||
right-rear-thigh
|
||||
bounds: 858, 148, 91, 149
|
||||
right-wing01
|
||||
bounds: 2, 2, 365, 643
|
||||
rotate: 90
|
||||
right-wing02
|
||||
bounds: 369, 369, 365, 643
|
||||
right-wing03
|
||||
bounds: 2, 369, 365, 643
|
||||
tail01
|
||||
bounds: 736, 268, 120, 153
|
||||
tail02
|
||||
bounds: 858, 26, 95, 120
|
||||
|
||||
dragon-pma_4.png
|
||||
size: 1024, 1024
|
||||
filter: Linear, Linear
|
||||
pma: true
|
||||
left-wing03
|
||||
bounds: 2, 2, 264, 589
|
||||
rotate: 90
|
||||
left-wing04
|
||||
bounds: 534, 268, 264, 589
|
||||
left-wing05
|
||||
bounds: 268, 268, 264, 589
|
||||
left-wing06
|
||||
bounds: 2, 268, 264, 589
|
||||
|
||||
dragon-pma_5.png
|
||||
size: 1024, 1024
|
||||
filter: Linear, Linear
|
||||
pma: true
|
||||
left-wing01
|
||||
bounds: 268, 2, 264, 589
|
||||
left-wing02
|
||||
bounds: 2, 2, 264, 589
|
||||
BIN
spine-glfw/data/dragon-pma.png
Normal file
BIN
spine-glfw/data/dragon-pma.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 226 KiB |
BIN
spine-glfw/data/dragon-pma_2.png
Normal file
BIN
spine-glfw/data/dragon-pma_2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 215 KiB |
BIN
spine-glfw/data/dragon-pma_3.png
Normal file
BIN
spine-glfw/data/dragon-pma_3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 277 KiB |
BIN
spine-glfw/data/dragon-pma_4.png
Normal file
BIN
spine-glfw/data/dragon-pma_4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
BIN
spine-glfw/data/dragon-pma_5.png
Normal file
BIN
spine-glfw/data/dragon-pma_5.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
120
spine-glfw/example/dragon-c.cpp
Normal file
120
spine-glfw/example/dragon-c.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
/******************************************************************************
|
||||
* Spine Runtimes License Agreement
|
||||
* Last updated April 5, 2025. Replaces all prior versions.
|
||||
*
|
||||
* Copyright (c) 2013-2025, Esoteric Software LLC
|
||||
*****************************************************************************/
|
||||
|
||||
#include <glbinding/gl/gl.h>
|
||||
#include <glbinding/glbinding.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <spine-glfw.h>
|
||||
|
||||
using namespace spine;
|
||||
|
||||
int width = 800, height = 600;
|
||||
|
||||
GLFWwindow *init_glfw() {
|
||||
if (!glfwInit()) {
|
||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
GLFWwindow *window = glfwCreateWindow(width, height, "spine-glfw dragon c", NULL, NULL);
|
||||
if (!window) {
|
||||
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glbinding::initialize(glfwGetProcAddress);
|
||||
return window;
|
||||
}
|
||||
|
||||
uint8_t *read_file(const char *path, int *length) {
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (!file) return 0;
|
||||
fseek(file, 0, SEEK_END);
|
||||
*length = (int) ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
uint8_t *data = (uint8_t *) malloc(*length);
|
||||
fread(data, 1, *length, file);
|
||||
fclose(file);
|
||||
return data;
|
||||
}
|
||||
|
||||
void *load_texture(const char *path) {
|
||||
return (void *) (uintptr_t) texture_load(path);
|
||||
}
|
||||
|
||||
void unload_texture(void *texture) {
|
||||
texture_dispose((texture_t) (uintptr_t) texture);
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
Bone::setYDown(true);
|
||||
|
||||
{
|
||||
|
||||
int atlas_length = 0;
|
||||
uint8_t *atlas_bytes = read_file("data/dragon-pma.atlas", &atlas_length);
|
||||
spine_atlas_result atlas_result = spine_atlas_load_callback((const char *) atlas_bytes, "data/", load_texture, unload_texture);
|
||||
spine_atlas atlas = spine_atlas_result_get_atlas(atlas_result);
|
||||
spine_atlas_result_dispose(atlas_result);
|
||||
|
||||
int skeleton_length = 0;
|
||||
uint8_t *skeleton_bytes = read_file("data/dragon-ess.skel", &skeleton_length);
|
||||
spine_skeleton_data_result skeleton_result = spine_skeleton_data_load_binary(atlas, skeleton_bytes, skeleton_length, "data/dragon-ess.skel");
|
||||
spine_skeleton_data skeleton_data = spine_skeleton_data_result_get_data(skeleton_result);
|
||||
spine_skeleton_data_result_dispose(skeleton_result);
|
||||
|
||||
spine_skeleton_drawable drawable = spine_skeleton_drawable_create(skeleton_data);
|
||||
spine_skeleton skeleton = spine_skeleton_drawable_get_skeleton(drawable);
|
||||
spine_skeleton_set_position(skeleton, width / 2.0f, height / 2.0f);
|
||||
spine_skeleton_set_scale(skeleton, 0.5f, 0.5f);
|
||||
spine_skeleton_setup_pose(skeleton);
|
||||
|
||||
spine_animation_state animation_state = spine_skeleton_drawable_get_animation_state(drawable);
|
||||
spine_animation_state_set_animation_1(animation_state, 0, "flying", true);
|
||||
|
||||
renderer_t *renderer = renderer_create();
|
||||
renderer_set_viewport_size(renderer, width, height);
|
||||
|
||||
double lastTime = glfwGetTime();
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
double currTime = glfwGetTime();
|
||||
float delta = (float) (currTime - lastTime);
|
||||
lastTime = currTime;
|
||||
|
||||
spine_animation_state_update(animation_state, delta);
|
||||
spine_animation_state_apply(animation_state, skeleton);
|
||||
spine_skeleton_update(skeleton, delta);
|
||||
spine_skeleton_update_world_transform(skeleton, SPINE_PHYSICS_UPDATE);
|
||||
|
||||
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
|
||||
renderer_draw_c(renderer, skeleton, true);
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
renderer_dispose(renderer);
|
||||
spine_skeleton_drawable_dispose(drawable);
|
||||
spine_skeleton_data_dispose(skeleton_data);
|
||||
spine_atlas_dispose(atlas);
|
||||
free(atlas_bytes);
|
||||
free(skeleton_bytes);
|
||||
}
|
||||
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
120
spine-glfw/example/dragon-json-c.cpp
Normal file
120
spine-glfw/example/dragon-json-c.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
/******************************************************************************
|
||||
* Spine Runtimes License Agreement
|
||||
* Last updated April 5, 2025. Replaces all prior versions.
|
||||
*
|
||||
* Copyright (c) 2013-2025, Esoteric Software LLC
|
||||
*****************************************************************************/
|
||||
|
||||
#include <glbinding/gl/gl.h>
|
||||
#include <glbinding/glbinding.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <spine-glfw.h>
|
||||
|
||||
using namespace spine;
|
||||
|
||||
int width = 800, height = 600;
|
||||
|
||||
GLFWwindow *init_glfw() {
|
||||
if (!glfwInit()) {
|
||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
GLFWwindow *window = glfwCreateWindow(width, height, "spine-glfw dragon json c", NULL, NULL);
|
||||
if (!window) {
|
||||
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glbinding::initialize(glfwGetProcAddress);
|
||||
return window;
|
||||
}
|
||||
|
||||
uint8_t *read_file(const char *path, int *length) {
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (!file) return 0;
|
||||
fseek(file, 0, SEEK_END);
|
||||
*length = (int) ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
uint8_t *data = (uint8_t *) malloc(*length);
|
||||
fread(data, 1, *length, file);
|
||||
fclose(file);
|
||||
return data;
|
||||
}
|
||||
|
||||
void *load_texture(const char *path) {
|
||||
return (void *) (uintptr_t) texture_load(path);
|
||||
}
|
||||
|
||||
void unload_texture(void *texture) {
|
||||
texture_dispose((texture_t) (uintptr_t) texture);
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
Bone::setYDown(true);
|
||||
|
||||
{
|
||||
|
||||
int atlas_length = 0;
|
||||
uint8_t *atlas_bytes = read_file("data/dragon-pma.atlas", &atlas_length);
|
||||
spine_atlas_result atlas_result = spine_atlas_load_callback((const char *) atlas_bytes, "data/", load_texture, unload_texture);
|
||||
spine_atlas atlas = spine_atlas_result_get_atlas(atlas_result);
|
||||
spine_atlas_result_dispose(atlas_result);
|
||||
|
||||
int skeleton_length = 0;
|
||||
uint8_t *skeleton_bytes = read_file("data/dragon-ess.json", &skeleton_length);
|
||||
spine_skeleton_data_result skeleton_result = spine_skeleton_data_load_json(atlas, (const char *) skeleton_bytes, "data/dragon-ess.json");
|
||||
spine_skeleton_data skeleton_data = spine_skeleton_data_result_get_data(skeleton_result);
|
||||
spine_skeleton_data_result_dispose(skeleton_result);
|
||||
|
||||
spine_skeleton_drawable drawable = spine_skeleton_drawable_create(skeleton_data);
|
||||
spine_skeleton skeleton = spine_skeleton_drawable_get_skeleton(drawable);
|
||||
spine_skeleton_set_position(skeleton, width / 2.0f, height / 2.0f);
|
||||
spine_skeleton_set_scale(skeleton, 0.5f, 0.5f);
|
||||
spine_skeleton_setup_pose(skeleton);
|
||||
|
||||
spine_animation_state animation_state = spine_skeleton_drawable_get_animation_state(drawable);
|
||||
spine_animation_state_set_animation_1(animation_state, 0, "flying", true);
|
||||
|
||||
renderer_t *renderer = renderer_create();
|
||||
renderer_set_viewport_size(renderer, width, height);
|
||||
|
||||
double lastTime = glfwGetTime();
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
double currTime = glfwGetTime();
|
||||
float delta = (float) (currTime - lastTime);
|
||||
lastTime = currTime;
|
||||
|
||||
spine_animation_state_update(animation_state, delta);
|
||||
spine_animation_state_apply(animation_state, skeleton);
|
||||
spine_skeleton_update(skeleton, delta);
|
||||
spine_skeleton_update_world_transform(skeleton, SPINE_PHYSICS_UPDATE);
|
||||
|
||||
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
|
||||
renderer_draw_c(renderer, skeleton, true);
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
renderer_dispose(renderer);
|
||||
spine_skeleton_drawable_dispose(drawable);
|
||||
spine_skeleton_data_dispose(skeleton_data);
|
||||
spine_atlas_dispose(atlas);
|
||||
free(atlas_bytes);
|
||||
free(skeleton_bytes);
|
||||
}
|
||||
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
119
spine-glfw/example/dragon-json.cpp
Normal file
119
spine-glfw/example/dragon-json.cpp
Normal file
@ -0,0 +1,119 @@
|
||||
/******************************************************************************
|
||||
* Spine Runtimes License Agreement
|
||||
* Last updated April 5, 2025. Replaces all prior versions.
|
||||
*
|
||||
* Copyright (c) 2013-2025, Esoteric Software LLC
|
||||
*
|
||||
* Integration of the Spine Runtimes into software or otherwise creating
|
||||
* derivative works of the Spine Runtimes is permitted under the terms and
|
||||
* conditions of Section 2 of the Spine Editor License Agreement:
|
||||
* http://esotericsoftware.com/spine-editor-license
|
||||
*
|
||||
* Otherwise, it is permitted to integrate the Spine Runtimes into software
|
||||
* or otherwise create derivative works of the Spine Runtimes (collectively,
|
||||
* "Products"), provided that each user of the Products must obtain their own
|
||||
* Spine Editor license and redistribution of the Products in any form must
|
||||
* include this license and copyright notice.
|
||||
*
|
||||
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
|
||||
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*****************************************************************************/
|
||||
|
||||
#include <glbinding/glbinding.h>
|
||||
#include <glbinding/gl/gl.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <spine-glfw.h>
|
||||
|
||||
using namespace spine;
|
||||
|
||||
int width = 800, height = 600;
|
||||
|
||||
GLFWwindow *init_glfw() {
|
||||
if (!glfwInit()) {
|
||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
GLFWwindow *window = glfwCreateWindow(width, height, "spine-glfw dragon json", NULL, NULL);
|
||||
if (!window) {
|
||||
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glbinding::initialize(glfwGetProcAddress);
|
||||
return window;
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
Bone::setYDown(true);
|
||||
|
||||
{
|
||||
|
||||
GlTextureLoader textureLoader;
|
||||
Atlas *atlas = new Atlas("data/dragon-pma.atlas", &textureLoader);
|
||||
|
||||
SkeletonJson json(*atlas);
|
||||
SkeletonData *skeletonData = json.readSkeletonDataFile("data/dragon-ess.json");
|
||||
if (!skeletonData) {
|
||||
std::cerr << "Failed to load dragon: " << json.getError().buffer() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Skeleton skeleton(*skeletonData);
|
||||
skeleton.setPosition(width / 2, height / 2);
|
||||
skeleton.setScaleX(0.5f);
|
||||
skeleton.setScaleY(0.5f);
|
||||
skeleton.setupPose();
|
||||
|
||||
AnimationStateData animationStateData(*skeletonData);
|
||||
AnimationState animationState(animationStateData);
|
||||
animationState.setAnimation(0, "flying", true);
|
||||
|
||||
renderer_t *renderer = renderer_create();
|
||||
renderer_set_viewport_size(renderer, width, height);
|
||||
|
||||
double lastTime = glfwGetTime();
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
double currTime = glfwGetTime();
|
||||
float delta = currTime - lastTime;
|
||||
lastTime = currTime;
|
||||
|
||||
animationState.update(delta);
|
||||
animationState.apply(skeleton);
|
||||
skeleton.update(delta);
|
||||
skeleton.updateWorldTransform(spine::Physics_Update);
|
||||
|
||||
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
|
||||
renderer_draw(renderer, &skeleton, true);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
renderer_dispose(renderer);
|
||||
delete skeletonData;
|
||||
delete atlas;
|
||||
}
|
||||
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
119
spine-glfw/example/dragon.cpp
Normal file
119
spine-glfw/example/dragon.cpp
Normal file
@ -0,0 +1,119 @@
|
||||
/******************************************************************************
|
||||
* Spine Runtimes License Agreement
|
||||
* Last updated April 5, 2025. Replaces all prior versions.
|
||||
*
|
||||
* Copyright (c) 2013-2025, Esoteric Software LLC
|
||||
*
|
||||
* Integration of the Spine Runtimes into software or otherwise creating
|
||||
* derivative works of the Spine Runtimes is permitted under the terms and
|
||||
* conditions of Section 2 of the Spine Editor License Agreement:
|
||||
* http://esotericsoftware.com/spine-editor-license
|
||||
*
|
||||
* Otherwise, it is permitted to integrate the Spine Runtimes into software
|
||||
* or otherwise create derivative works of the Spine Runtimes (collectively,
|
||||
* "Products"), provided that each user of the Products must obtain their own
|
||||
* Spine Editor license and redistribution of the Products in any form must
|
||||
* include this license and copyright notice.
|
||||
*
|
||||
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
|
||||
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*****************************************************************************/
|
||||
|
||||
#include <glbinding/glbinding.h>
|
||||
#include <glbinding/gl/gl.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <spine-glfw.h>
|
||||
|
||||
using namespace spine;
|
||||
|
||||
int width = 800, height = 600;
|
||||
|
||||
GLFWwindow *init_glfw() {
|
||||
if (!glfwInit()) {
|
||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
GLFWwindow *window = glfwCreateWindow(width, height, "spine-glfw dragon", NULL, NULL);
|
||||
if (!window) {
|
||||
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glbinding::initialize(glfwGetProcAddress);
|
||||
return window;
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
Bone::setYDown(true);
|
||||
|
||||
{
|
||||
|
||||
GlTextureLoader textureLoader;
|
||||
Atlas *atlas = new Atlas("data/dragon-pma.atlas", &textureLoader);
|
||||
|
||||
SkeletonBinary binary(*atlas);
|
||||
SkeletonData *skeletonData = binary.readSkeletonDataFile("data/dragon-ess.skel");
|
||||
if (!skeletonData) {
|
||||
std::cerr << "Failed to load dragon: " << binary.getError().buffer() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Skeleton skeleton(*skeletonData);
|
||||
skeleton.setPosition(width / 2, height / 2);
|
||||
skeleton.setScaleX(0.5);
|
||||
skeleton.setScaleY(0.5);
|
||||
skeleton.setupPose();
|
||||
|
||||
AnimationStateData animationStateData(*skeletonData);
|
||||
AnimationState animationState(animationStateData);
|
||||
animationState.setAnimation(0, "flying", true);
|
||||
|
||||
renderer_t *renderer = renderer_create();
|
||||
renderer_set_viewport_size(renderer, width, height);
|
||||
|
||||
double lastTime = glfwGetTime();
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
double currTime = glfwGetTime();
|
||||
float delta = currTime - lastTime;
|
||||
lastTime = currTime;
|
||||
|
||||
animationState.update(delta);
|
||||
animationState.apply(skeleton);
|
||||
skeleton.update(delta);
|
||||
skeleton.updateWorldTransform(spine::Physics_Update);
|
||||
|
||||
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
|
||||
renderer_draw(renderer, &skeleton, true);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
renderer_dispose(renderer);
|
||||
delete skeletonData;
|
||||
delete atlas;
|
||||
}
|
||||
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
142
spine-glfw/example/ik-following-c.cpp
Normal file
142
spine-glfw/example/ik-following-c.cpp
Normal file
@ -0,0 +1,142 @@
|
||||
/******************************************************************************
|
||||
* Spine Runtimes License Agreement
|
||||
* Last updated April 5, 2025. Replaces all prior versions.
|
||||
*
|
||||
* Copyright (c) 2013-2025, Esoteric Software LLC
|
||||
*****************************************************************************/
|
||||
|
||||
#include <glbinding/gl/gl.h>
|
||||
#include <glbinding/glbinding.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <spine-glfw.h>
|
||||
|
||||
using namespace spine;
|
||||
|
||||
int width = 800, height = 600;
|
||||
double mouseX = 400, mouseY = 300;
|
||||
|
||||
void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) {
|
||||
SP_UNUSED(window);
|
||||
mouseX = xpos;
|
||||
mouseY = ypos;
|
||||
}
|
||||
|
||||
GLFWwindow *init_glfw() {
|
||||
if (!glfwInit()) {
|
||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
GLFWwindow *window = glfwCreateWindow(width, height, "spine-glfw IK Following c - Move mouse to aim", NULL, NULL);
|
||||
if (!window) {
|
||||
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glbinding::initialize(glfwGetProcAddress);
|
||||
glfwSetCursorPosCallback(window, cursor_position_callback);
|
||||
return window;
|
||||
}
|
||||
|
||||
uint8_t *read_file(const char *path, int *length) {
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (!file) return 0;
|
||||
fseek(file, 0, SEEK_END);
|
||||
*length = (int) ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
uint8_t *data = (uint8_t *) malloc(*length);
|
||||
fread(data, 1, *length, file);
|
||||
fclose(file);
|
||||
return data;
|
||||
}
|
||||
|
||||
void *load_texture(const char *path) {
|
||||
return (void *) (uintptr_t) texture_load(path);
|
||||
}
|
||||
|
||||
void unload_texture(void *texture) {
|
||||
texture_dispose((texture_t) (uintptr_t) texture);
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
Bone::setYDown(true);
|
||||
|
||||
{
|
||||
|
||||
int atlas_length = 0;
|
||||
uint8_t *atlas_bytes = read_file("data/spineboy-pma.atlas", &atlas_length);
|
||||
spine_atlas_result atlas_result = spine_atlas_load_callback((const char *) atlas_bytes, "data/", load_texture, unload_texture);
|
||||
spine_atlas atlas = spine_atlas_result_get_atlas(atlas_result);
|
||||
spine_atlas_result_dispose(atlas_result);
|
||||
|
||||
int skeleton_length = 0;
|
||||
uint8_t *skeleton_bytes = read_file("data/spineboy-pro.skel", &skeleton_length);
|
||||
spine_skeleton_data_result skeleton_result = spine_skeleton_data_load_binary(atlas, skeleton_bytes, skeleton_length, "data/spineboy-pro.skel");
|
||||
spine_skeleton_data skeleton_data = spine_skeleton_data_result_get_data(skeleton_result);
|
||||
spine_skeleton_data_result_dispose(skeleton_result);
|
||||
|
||||
spine_skeleton_drawable drawable = spine_skeleton_drawable_create(skeleton_data);
|
||||
spine_skeleton skeleton = spine_skeleton_drawable_get_skeleton(drawable);
|
||||
spine_skeleton_set_scale(skeleton, 0.5f, 0.5f);
|
||||
spine_skeleton_setup_pose(skeleton);
|
||||
spine_skeleton_set_position(skeleton, 200.0f, height / 2.0f + 150.0f);
|
||||
|
||||
spine_animation_state animation_state = spine_skeleton_drawable_get_animation_state(drawable);
|
||||
spine_animation_state_set_animation_1(animation_state, 0, "walk", true);
|
||||
spine_animation_state_set_animation_1(animation_state, 1, "aim", true);
|
||||
|
||||
renderer_t *renderer = renderer_create();
|
||||
renderer_set_viewport_size(renderer, width, height);
|
||||
|
||||
double lastTime = glfwGetTime();
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
double currTime = glfwGetTime();
|
||||
float delta = (float) (currTime - lastTime);
|
||||
lastTime = currTime;
|
||||
|
||||
spine_animation_state_update(animation_state, delta);
|
||||
spine_animation_state_apply(animation_state, skeleton);
|
||||
spine_skeleton_update(skeleton, delta);
|
||||
|
||||
spine_bone crosshair_bone = spine_skeleton_find_bone(skeleton, "crosshair");
|
||||
if (crosshair_bone != nullptr) {
|
||||
spine_bone parent = spine_bone_get_parent(crosshair_bone);
|
||||
if (parent != nullptr) {
|
||||
float localX = 0, localY = 0;
|
||||
spine_bone_pose parent_pose = spine_bone_get_applied_pose(parent);
|
||||
spine_bone_pose_world_to_local(parent_pose, (float) mouseX, (float) mouseY, &localX, &localY);
|
||||
spine_bone_pose crosshair_pose = spine_bone_get_applied_pose(crosshair_bone);
|
||||
spine_bone_pose_set_x(crosshair_pose, localX);
|
||||
spine_bone_pose_set_y(crosshair_pose, localY);
|
||||
}
|
||||
}
|
||||
|
||||
spine_skeleton_update_world_transform(skeleton, SPINE_PHYSICS_UPDATE);
|
||||
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
|
||||
renderer_draw_c(renderer, skeleton, true);
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
renderer_dispose(renderer);
|
||||
spine_skeleton_drawable_dispose(drawable);
|
||||
spine_skeleton_data_dispose(skeleton_data);
|
||||
spine_atlas_dispose(atlas);
|
||||
free(atlas_bytes);
|
||||
free(skeleton_bytes);
|
||||
}
|
||||
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
@ -75,9 +75,13 @@ int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
// We use a y-down coordinate system, see renderer_set_viewport_size()
|
||||
Bone::setYDown(true);
|
||||
|
||||
{
|
||||
|
||||
// Load the atlas and the skeleton data
|
||||
GlTextureLoader textureLoader;
|
||||
Atlas *atlas = new Atlas("data/spineboy-pma.atlas", &textureLoader);
|
||||
@ -165,6 +169,9 @@ int main() {
|
||||
delete atlas;
|
||||
|
||||
// Kill the window and GLFW
|
||||
}
|
||||
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
@ -95,14 +95,19 @@ int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
// We use a y-down coordinate system, see renderer_set_viewport_size()
|
||||
Bone::setYDown(true);
|
||||
|
||||
{
|
||||
|
||||
// Load the atlas and the skeleton data
|
||||
int atlas_length = 0;
|
||||
uint8_t *atlas_bytes = read_file("data/spineboy-pma.atlas", &atlas_length);
|
||||
spine_atlas_result result = spine_atlas_load_callback((const char *) atlas_bytes, "data/", load_texture, unload_texture);
|
||||
spine_atlas atlas = spine_atlas_result_get_atlas(result);
|
||||
spine_atlas_result_dispose(result);
|
||||
|
||||
int skeleton_length = 0;
|
||||
// uint8_t *skeleton_bytes = read_file("data/spineboy-pro.skel", &skeleton_length);
|
||||
@ -110,6 +115,7 @@ int main() {
|
||||
uint8_t *skeleton_bytes = read_file("data/spineboy-pro.json", &skeleton_length);
|
||||
spine_skeleton_data_result result2 = spine_skeleton_data_load_json(atlas, (const char *) skeleton_bytes, "data/");
|
||||
spine_skeleton_data skeleton_data = spine_skeleton_data_result_get_data(result2);
|
||||
spine_skeleton_data_result_dispose(result2);
|
||||
|
||||
// Create a skeleton from the data, set the skeleton's position to the bottom center of
|
||||
// the screen and scale it to make it smaller.
|
||||
@ -163,10 +169,16 @@ int main() {
|
||||
|
||||
// Dispose everything
|
||||
renderer_dispose(renderer);
|
||||
// delete skeletonData;
|
||||
delete atlas;
|
||||
spine_skeleton_drawable_dispose(drawable);
|
||||
spine_skeleton_data_dispose(skeleton_data);
|
||||
spine_atlas_dispose(atlas);
|
||||
free(atlas_bytes);
|
||||
free(skeleton_bytes);
|
||||
|
||||
// Kill the window and GLFW
|
||||
}
|
||||
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -72,87 +72,93 @@ int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
// We use a y-down coordinate system, see renderer_set_viewport_size()
|
||||
Bone::setYDown(true);
|
||||
|
||||
// Load the atlas and the skeleton data
|
||||
GlTextureLoader textureLoader;
|
||||
Atlas *atlas = new Atlas("data/spineboy-pma.atlas", &textureLoader);
|
||||
SkeletonBinary binary(*atlas);
|
||||
// SkeletonData *skeletonData = binary.readSkeletonDataFile("data/spineboy-pro.skel");
|
||||
{
|
||||
// Load the atlas and the skeleton data
|
||||
GlTextureLoader textureLoader;
|
||||
Atlas *atlas = new Atlas("data/spineboy-pma.atlas", &textureLoader);
|
||||
SkeletonBinary binary(*atlas);
|
||||
// SkeletonData *skeletonData = binary.readSkeletonDataFile("data/spineboy-pro.skel");
|
||||
|
||||
SkeletonJson json(*atlas);
|
||||
SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.json");
|
||||
SkeletonJson json(*atlas);
|
||||
SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.json");
|
||||
|
||||
// Create a skeleton from the data, set the skeleton's position to the bottom center of
|
||||
// the screen and scale it to make it smaller.
|
||||
Skeleton skeleton(*skeletonData);
|
||||
skeleton.setPosition(width / 2, height - 100);
|
||||
skeleton.setScaleX(0.3);
|
||||
skeleton.setScaleY(0.3);
|
||||
skeleton.setupPose();
|
||||
// Create a skeleton from the data, set the skeleton's position to the bottom center of
|
||||
// the screen and scale it to make it smaller.
|
||||
Skeleton skeleton(*skeletonData);
|
||||
skeleton.setPosition(width / 2, height - 100);
|
||||
skeleton.setScaleX(0.3);
|
||||
skeleton.setScaleY(0.3);
|
||||
skeleton.setupPose();
|
||||
|
||||
// Create an AnimationState to drive animations on the skeleton. Set the "portal" animation
|
||||
// on track with index 0.
|
||||
AnimationStateData animationStateData(*skeletonData);
|
||||
animationStateData.setDefaultMix(0.2f);
|
||||
AnimationState animationState(animationStateData);
|
||||
globalAnimationState = &animationState;
|
||||
animationState.setAnimation(0, "portal", true);
|
||||
animationState.addAnimation(0, "run", true, 0)
|
||||
.setListener([](AnimationState *state, EventType type, TrackEntry *entry, Event *event, void *userData) {
|
||||
SP_UNUSED(state);
|
||||
SP_UNUSED(entry);
|
||||
SP_UNUSED(event);
|
||||
SP_UNUSED(userData);
|
||||
if (type == EventType_Event) {
|
||||
printf("Custom event fired: %s\n", event->getData().getName().buffer());
|
||||
}
|
||||
});
|
||||
// Create an AnimationState to drive animations on the skeleton. Set the "portal" animation
|
||||
// on track with index 0.
|
||||
AnimationStateData animationStateData(*skeletonData);
|
||||
animationStateData.setDefaultMix(0.2f);
|
||||
AnimationState animationState(animationStateData);
|
||||
globalAnimationState = &animationState;
|
||||
animationState.setAnimation(0, "portal", true);
|
||||
animationState.addAnimation(0, "run", true, 0)
|
||||
.setListener([](AnimationState *state, EventType type, TrackEntry *entry, Event *event, void *userData) {
|
||||
SP_UNUSED(state);
|
||||
SP_UNUSED(entry);
|
||||
SP_UNUSED(event);
|
||||
SP_UNUSED(userData);
|
||||
if (type == EventType_Event) {
|
||||
printf("Custom event fired: %s\n", event->getData().getName().buffer());
|
||||
}
|
||||
});
|
||||
|
||||
// Create the renderer and set the viewport size to match the window size. This sets up a
|
||||
// pixel perfect orthogonal projection for 2D rendering.
|
||||
renderer_t *renderer = renderer_create();
|
||||
renderer_set_viewport_size(renderer, width, height);
|
||||
// Create the renderer and set the viewport size to match the window size. This sets up a
|
||||
// pixel perfect orthogonal projection for 2D rendering.
|
||||
renderer_t *renderer = renderer_create();
|
||||
renderer_set_viewport_size(renderer, width, height);
|
||||
|
||||
// Set up keyboard callback. When space is pressed, we switch to the "walk" animation.
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
// Set up keyboard callback. When space is pressed, we switch to the "walk" animation.
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
|
||||
// Rendering loop
|
||||
double lastTime = glfwGetTime();
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// Calculate the delta time in seconds
|
||||
double currTime = glfwGetTime();
|
||||
float delta = currTime - lastTime;
|
||||
lastTime = currTime;
|
||||
// Rendering loop
|
||||
double lastTime = glfwGetTime();
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// Calculate the delta time in seconds
|
||||
double currTime = glfwGetTime();
|
||||
float delta = currTime - lastTime;
|
||||
lastTime = currTime;
|
||||
|
||||
// Update and apply the animation state to the skeleton
|
||||
animationState.update(delta);
|
||||
animationState.apply(skeleton);
|
||||
// Update and apply the animation state to the skeleton
|
||||
animationState.update(delta);
|
||||
animationState.apply(skeleton);
|
||||
|
||||
// Update the skeleton time (used for physics)
|
||||
skeleton.update(delta);
|
||||
// Update the skeleton time (used for physics)
|
||||
skeleton.update(delta);
|
||||
|
||||
// Calculate the new pose
|
||||
skeleton.updateWorldTransform(spine::Physics_Update);
|
||||
// Calculate the new pose
|
||||
skeleton.updateWorldTransform(spine::Physics_Update);
|
||||
|
||||
// Clear the screen
|
||||
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
|
||||
// Clear the screen
|
||||
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// Render the skeleton in its current pose
|
||||
renderer_draw(renderer, &skeleton, true);
|
||||
// Render the skeleton in its current pose
|
||||
renderer_draw(renderer, &skeleton, true);
|
||||
|
||||
// Present the rendering results and poll for events
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
// Present the rendering results and poll for events
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// Dispose everything
|
||||
globalAnimationState = nullptr;
|
||||
renderer_dispose(renderer);
|
||||
delete skeletonData;
|
||||
delete atlas;
|
||||
}
|
||||
|
||||
// Dispose everything
|
||||
renderer_dispose(renderer);
|
||||
delete skeletonData;
|
||||
delete atlas;
|
||||
|
||||
// Kill the window and GLFW
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
96
spine-glfw/example/mix-and-match.cpp
Normal file
96
spine-glfw/example/mix-and-match.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
/******************************************************************************
|
||||
* Spine Runtimes License Agreement
|
||||
* Last updated April 5, 2025. Replaces all prior versions.
|
||||
*
|
||||
* Copyright (c) 2013-2025, Esoteric Software LLC
|
||||
*
|
||||
* Integration of the Spine Runtimes into software or otherwise creating
|
||||
* derivative works of the Spine Runtimes is permitted under the terms and
|
||||
* conditions of Section 2 of the Spine Editor License Agreement:
|
||||
* http://esotericsoftware.com/spine-editor-license
|
||||
*
|
||||
* Otherwise, it is permitted to integrate the Spine Runtimes into software
|
||||
* or otherwise create derivative works of the Spine Runtimes (collectively,
|
||||
* "Products"), provided that each user of the Products must obtain their own
|
||||
* Spine Editor license and redistribution of the Products in any form must
|
||||
* include this license and copyright notice.
|
||||
*
|
||||
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
|
||||
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*****************************************************************************/
|
||||
|
||||
#include <glbinding/glbinding.h>
|
||||
#include <glbinding/gl/gl.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <spine-glfw.h>
|
||||
|
||||
using namespace spine;
|
||||
|
||||
static GLFWwindow *init_glfw() {
|
||||
if (!glfwInit()) {
|
||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
||||
GLFWwindow *window = glfwCreateWindow(64, 64, "spine-glfw mix-and-match", NULL, NULL);
|
||||
if (!window) {
|
||||
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glbinding::initialize(glfwGetProcAddress);
|
||||
return window;
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
Bone::setYDown(true);
|
||||
|
||||
const char *atlasPath = "../spine-ue/Content/GettingStarted/Assets/mix-and-match/mix-and-match.atlas";
|
||||
const char *skeletonPath = "../spine-ue/Content/GettingStarted/Assets/mix-and-match/mix-and-match-pro.skel";
|
||||
|
||||
{
|
||||
GlTextureLoader textureLoader;
|
||||
Atlas *atlas = new Atlas(atlasPath, &textureLoader);
|
||||
SkeletonBinary binary(*atlas);
|
||||
SkeletonData *skeletonData = binary.readSkeletonDataFile(skeletonPath);
|
||||
if (!skeletonData) {
|
||||
std::cerr << "Failed to load mix-and-match: " << binary.getError().buffer() << std::endl;
|
||||
delete atlas;
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
Skeleton skeleton(*skeletonData);
|
||||
skeleton.setupPose();
|
||||
skeleton.updateWorldTransform(spine::Physics_None);
|
||||
|
||||
std::cout << "Loaded mix-and-match successfully. Bones=" << skeletonData->getBones().size()
|
||||
<< ", slots=" << skeletonData->getSlots().size()
|
||||
<< ", skins=" << skeletonData->getSkins().size() << std::endl;
|
||||
|
||||
delete skeletonData;
|
||||
delete atlas;
|
||||
}
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
153
spine-glfw/example/physics-c.cpp
Normal file
153
spine-glfw/example/physics-c.cpp
Normal file
@ -0,0 +1,153 @@
|
||||
/******************************************************************************
|
||||
* Spine Runtimes License Agreement
|
||||
* Last updated April 5, 2025. Replaces all prior versions.
|
||||
*
|
||||
* Copyright (c) 2013-2025, Esoteric Software LLC
|
||||
*****************************************************************************/
|
||||
|
||||
#include <glbinding/gl/gl.h>
|
||||
#include <glbinding/glbinding.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <spine-glfw.h>
|
||||
|
||||
using namespace spine;
|
||||
|
||||
int width = 800, height = 600;
|
||||
bool isDragging = false;
|
||||
double lastMouseX = 0, lastMouseY = 0;
|
||||
spine_skeleton dragSkeleton = nullptr;
|
||||
|
||||
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) {
|
||||
SP_UNUSED(mods);
|
||||
if (button == GLFW_MOUSE_BUTTON_LEFT) {
|
||||
if (action == GLFW_PRESS) {
|
||||
isDragging = true;
|
||||
glfwGetCursorPos(window, &lastMouseX, &lastMouseY);
|
||||
} else if (action == GLFW_RELEASE) {
|
||||
isDragging = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) {
|
||||
SP_UNUSED(window);
|
||||
if (isDragging && dragSkeleton != nullptr) {
|
||||
double deltaX = xpos - lastMouseX;
|
||||
double deltaY = ypos - lastMouseY;
|
||||
spine_skeleton_set_x(dragSkeleton, spine_skeleton_get_x(dragSkeleton) + (float) deltaX);
|
||||
spine_skeleton_set_y(dragSkeleton, spine_skeleton_get_y(dragSkeleton) + (float) deltaY);
|
||||
lastMouseX = xpos;
|
||||
lastMouseY = ypos;
|
||||
}
|
||||
}
|
||||
|
||||
GLFWwindow *init_glfw() {
|
||||
if (!glfwInit()) {
|
||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
GLFWwindow *window = glfwCreateWindow(width, height, "spine-glfw physics c - Drag anywhere", NULL, NULL);
|
||||
if (!window) {
|
||||
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glbinding::initialize(glfwGetProcAddress);
|
||||
glfwSetMouseButtonCallback(window, mouse_button_callback);
|
||||
glfwSetCursorPosCallback(window, cursor_position_callback);
|
||||
return window;
|
||||
}
|
||||
|
||||
uint8_t *read_file(const char *path, int *length) {
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (!file) return 0;
|
||||
fseek(file, 0, SEEK_END);
|
||||
*length = (int) ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
uint8_t *data = (uint8_t *) malloc(*length);
|
||||
fread(data, 1, *length, file);
|
||||
fclose(file);
|
||||
return data;
|
||||
}
|
||||
|
||||
void *load_texture(const char *path) {
|
||||
return (void *) (uintptr_t) texture_load(path);
|
||||
}
|
||||
|
||||
void unload_texture(void *texture) {
|
||||
texture_dispose((texture_t) (uintptr_t) texture);
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
Bone::setYDown(true);
|
||||
|
||||
{
|
||||
|
||||
int atlas_length = 0;
|
||||
uint8_t *atlas_bytes = read_file("data/celestial-circus-pma.atlas", &atlas_length);
|
||||
spine_atlas_result atlas_result = spine_atlas_load_callback((const char *) atlas_bytes, "data/", load_texture, unload_texture);
|
||||
spine_atlas atlas = spine_atlas_result_get_atlas(atlas_result);
|
||||
spine_atlas_result_dispose(atlas_result);
|
||||
|
||||
int skeleton_length = 0;
|
||||
uint8_t *skeleton_bytes = read_file("data/celestial-circus-pro.skel", &skeleton_length);
|
||||
spine_skeleton_data_result skeleton_result = spine_skeleton_data_load_binary(atlas, skeleton_bytes, skeleton_length,
|
||||
"data/celestial-circus-pro.skel");
|
||||
spine_skeleton_data skeleton_data = spine_skeleton_data_result_get_data(skeleton_result);
|
||||
spine_skeleton_data_result_dispose(skeleton_result);
|
||||
|
||||
spine_skeleton_drawable drawable = spine_skeleton_drawable_create(skeleton_data);
|
||||
spine_skeleton skeleton = spine_skeleton_drawable_get_skeleton(drawable);
|
||||
spine_skeleton_set_scale(skeleton, 0.2f, 0.2f);
|
||||
spine_skeleton_setup_pose(skeleton);
|
||||
spine_skeleton_update(skeleton, 0);
|
||||
spine_skeleton_update_world_transform(skeleton, SPINE_PHYSICS_UPDATE);
|
||||
spine_skeleton_set_position(skeleton, width / 2.0f, height / 2.0f + 150.0f);
|
||||
dragSkeleton = skeleton;
|
||||
|
||||
spine_animation_state animation_state = spine_skeleton_drawable_get_animation_state(drawable);
|
||||
spine_animation_state_set_animation_1(animation_state, 0, "eyeblink-long", true);
|
||||
spine_animation_state_set_animation_1(animation_state, 1, "wind-idle", true);
|
||||
|
||||
renderer_t *renderer = renderer_create();
|
||||
renderer_set_viewport_size(renderer, width, height);
|
||||
|
||||
double lastTime = glfwGetTime();
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
double currTime = glfwGetTime();
|
||||
float delta = (float) (currTime - lastTime);
|
||||
lastTime = currTime;
|
||||
spine_animation_state_update(animation_state, delta);
|
||||
spine_animation_state_apply(animation_state, skeleton);
|
||||
spine_skeleton_update(skeleton, delta);
|
||||
spine_skeleton_update_world_transform(skeleton, SPINE_PHYSICS_UPDATE);
|
||||
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
|
||||
renderer_draw_c(renderer, skeleton, true);
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
dragSkeleton = nullptr;
|
||||
renderer_dispose(renderer);
|
||||
spine_skeleton_drawable_dispose(drawable);
|
||||
spine_skeleton_data_dispose(skeleton_data);
|
||||
spine_atlas_dispose(atlas);
|
||||
free(atlas_bytes);
|
||||
free(skeleton_bytes);
|
||||
}
|
||||
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
@ -97,9 +97,13 @@ int main() {
|
||||
GLFWwindow *window = init_glfw();
|
||||
if (!window) return -1;
|
||||
|
||||
spine_enable_debug_extension(true);
|
||||
|
||||
// We use a y-down coordinate system, see renderer_set_viewport_size()
|
||||
Bone::setYDown(true);
|
||||
|
||||
{
|
||||
|
||||
// Load the atlas and the skeleton data
|
||||
GlTextureLoader textureLoader;
|
||||
Atlas *atlas = new Atlas("data/celestial-circus-pma.atlas", &textureLoader);
|
||||
@ -170,6 +174,9 @@ int main() {
|
||||
delete atlas;
|
||||
|
||||
// Kill the window and GLFW
|
||||
}
|
||||
|
||||
spine_report_leaks();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user