[glfw] Add key callback to set new animation to basic example

This commit is contained in:
Mario Zechner 2025-10-07 13:06:37 +02:00
parent 2d8f815b40
commit 1b7144d711

View File

@ -37,6 +37,16 @@
using namespace spine; using namespace spine;
int width = 800, height = 600; int width = 800, height = 600;
AnimationState *globalAnimationState = nullptr;
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
SP_UNUSED(window);
SP_UNUSED(scancode);
SP_UNUSED(mods);
if (action == GLFW_PRESS && key == GLFW_KEY_SPACE && globalAnimationState) {
globalAnimationState->setAnimation(0, "walk", true);
}
}
GLFWwindow *init_glfw() { GLFWwindow *init_glfw() {
if (!glfwInit()) { if (!glfwInit()) {
@ -85,8 +95,9 @@ int main() {
// Create an AnimationState to drive animations on the skeleton. Set the "portal" animation // Create an AnimationState to drive animations on the skeleton. Set the "portal" animation
// on track with index 0. // on track with index 0.
AnimationStateData animationStateData(*skeletonData); AnimationStateData animationStateData(*skeletonData);
animationStateData.setDefaultMix(0.2f); animationStateData.setDefaultMix(1.0f);
AnimationState animationState(animationStateData); AnimationState animationState(animationStateData);
globalAnimationState = &animationState;
animationState.setAnimation(0, "portal", true); animationState.setAnimation(0, "portal", true);
animationState.addAnimation(0, "run", true, 0) animationState.addAnimation(0, "run", true, 0)
.setListener([](AnimationState *state, EventType type, TrackEntry *entry, Event *event, void *userData) { .setListener([](AnimationState *state, EventType type, TrackEntry *entry, Event *event, void *userData) {
@ -104,6 +115,9 @@ int main() {
renderer_t *renderer = renderer_create(); renderer_t *renderer = renderer_create();
renderer_set_viewport_size(renderer, width, height); 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);
// Rendering loop // Rendering loop
double lastTime = glfwGetTime(); double lastTime = glfwGetTime();
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {