[glfw] Added physics example

This commit is contained in:
Mario Zechner 2025-09-11 01:26:50 +02:00
parent b96e7c2461
commit b9c394fc8d
7 changed files with 3987 additions and 0 deletions

View File

@ -253,6 +253,10 @@ cp -f ../spineboy/export/spineboy-pro.json "$ROOT/spine-glfw/data/"
cp -f ../spineboy/export/spineboy-pro.skel "$ROOT/spine-glfw/data/"
cp -f ../spineboy/export/spineboy-pma.atlas "$ROOT/spine-glfw/data/"
cp -f ../spineboy/export/spineboy-pma.png "$ROOT/spine-glfw/data/"
cp -f ../celestial-circus/export/celestial-circus-pro.json "$ROOT/spine-glfw/data/"
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/"
echo "spine-sfml"
rm -f "$ROOT/spine-sfml/data/"*

View File

@ -70,7 +70,19 @@ target_link_libraries(spine-glfw-example-c PRIVATE glbinding::glbinding)
set_property(TARGET spine-glfw-example-c PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/spine-glfw")
# Physics example
add_executable(spine-glfw-physics example/physics.cpp)
target_link_libraries(spine-glfw-physics PRIVATE glfw)
target_link_libraries(spine-glfw-physics PRIVATE OpenGL::GL)
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")
# 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)
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)

View File

@ -0,0 +1,158 @@
celestial-circus-pma.png
size: 1024, 1024
filter: Linear, Linear
pma: true
scale: 0.4
arm-back-down
bounds: 971, 683, 38, 82
arm-back-up
bounds: 939, 44, 83, 116
arm-front-down
bounds: 971, 603, 36, 78
arm-front-up
bounds: 289, 22, 77, 116
rotate: 90
bench
bounds: 586, 47, 189, 48
body-bottom
bounds: 868, 270, 154, 124
body-top
bounds: 2, 156, 126, 132
offsets: 0, 0, 126, 133
rotate: 90
chest
bounds: 490, 267, 104, 93
rotate: 180
cloud-back
bounds: 804, 563, 202, 165
rotate: 90
cloud-front
bounds: 606, 440, 325, 196
rotate: 270
collar
bounds: 373, 739, 47, 26
ear
bounds: 106, 737, 20, 28
eye-back-shadow
bounds: 233, 755, 14, 10
eye-front-shadow
bounds: 128, 751, 24, 14
eye-reflex-back
bounds: 787, 758, 8, 7
eye-reflex-front
bounds: 154, 758, 10, 7
eye-white-back
bounds: 616, 749, 13, 16
eye-white-front
bounds: 477, 748, 22, 17
eyelashes-down-back
bounds: 655, 759, 11, 6
eyelashes-down-front
bounds: 549, 759, 15, 6
eyelashes-top-back
bounds: 353, 755, 18, 10
eyelashes-top-front
bounds: 749, 749, 30, 16
face
bounds: 775, 277, 91, 102
offsets: 2, 0, 93, 102
feathers-back
bounds: 192, 611, 46, 46
feathers-front
bounds: 415, 679, 72, 86
fringe-middle-back
bounds: 794, 509, 33, 52
fringe-middle-front
bounds: 679, 202, 60, 50
fringe-side-back
bounds: 407, 5, 27, 94
fringe-side-front
bounds: 14, 331, 26, 93
glove-bottom-back
bounds: 14, 681, 51, 41
glove-bottom-front
bounds: 313, 288, 47, 48
hair-back-1
bounds: 716, 91, 132, 306
rotate: 270
hair-back-2
bounds: 124, 100, 80, 285
rotate: 90
hair-back-3
bounds: 410, 78, 70, 268
rotate: 270
hair-back-4
bounds: 42, 250, 88, 262
rotate: 90
hair-back-5
bounds: 320, 141, 88, 279
rotate: 90
hair-back-6
bounds: 2, 36, 88, 286
rotate: 90
hair-hat-shadow
bounds: 14, 724, 90, 41
hand-back
bounds: 2, 42, 60, 47
hand-front
bounds: 909, 208, 53, 60
hat-back
bounds: 741, 189, 64, 45
hat-front
bounds: 926, 396, 96, 56
head-back
bounds: 777, 2, 102, 86
jabot
bounds: 692, 384, 70, 55
leg-back
bounds: 362, 164, 210, 333
rotate: 90
leg-front
bounds: 590, 181, 258, 320
rotate: 90
logo-brooch
bounds: 584, 740, 16, 25
mouth
bounds: 631, 759, 22, 6
neck
bounds: 597, 441, 39, 56
nose
bounds: 556, 750, 6, 7
nose-highlight
bounds: 166, 761, 4, 4
nose-shadow
bounds: 778, 757, 7, 8
pupil-back
bounds: 442, 751, 10, 14
pupil-front
bounds: 602, 747, 12, 18
rope-back
bounds: 2, 273, 10, 492
rope-front
bounds: 2, 273, 10, 492
rope-front-bottom
bounds: 895, 69, 42, 65
skirt
bounds: 14, 325, 440, 246
rotate: 90
sock-bow
bounds: 253, 733, 33, 32
spine-logo-body
bounds: 569, 733, 13, 32
star-big
bounds: 422, 741, 18, 24
star-medium
bounds: 1011, 757, 6, 8
star-small
bounds: 218, 761, 3, 4
underskirt
bounds: 212, 320, 445, 228
rotate: 270
underskirt-back
bounds: 434, 332, 433, 171
rotate: 270
wing-back
bounds: 137, 137, 146, 252
rotate: 270
wing-front
bounds: 718, 314, 304, 248

Binary file not shown.

After

Width:  |  Height:  |  Size: 788 KiB

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -0,0 +1,175 @@
/******************************************************************************
* 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;
// Mouse dragging state
bool isDragging = false;
double lastMouseX = 0, lastMouseY = 0;
Skeleton* dragSkeleton = nullptr;
void mouse_button_callback(GLFWwindow* window, int button, int action, int 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) {
if (isDragging && dragSkeleton != nullptr) {
// Both GLFW and our renderer use y-down coordinate system
double deltaX = xpos - lastMouseX;
double deltaY = ypos - lastMouseY;
dragSkeleton->setX(dragSkeleton->getX() + deltaX);
dragSkeleton->setY(dragSkeleton->getY() + 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 - Drag anywhere", NULL, NULL);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
glbinding::initialize(glfwGetProcAddress);
// Set mouse callbacks
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
return window;
}
int main() {
// Initialize GLFW and glbinding
GLFWwindow *window = init_glfw();
if (!window) return -1;
// 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);
SkeletonBinary binary(*atlas);
SkeletonData *skeletonData = binary.readSkeletonDataFile("data/celestial-circus-pro.skel");
// Create a skeleton from the data
Skeleton skeleton(*skeletonData);
skeleton.setScaleX(0.2);
skeleton.setScaleY(0.2);
skeleton.setupPose();
skeleton.update(0);
skeleton.updateWorldTransform(Physics_Update);
// Center the skeleton on screen
skeleton.setPosition(width / 2, height / 2 + 150);
// Set the global skeleton pointer for mouse dragging
dragSkeleton = &skeleton;
// Create an AnimationState to drive animations on the skeleton
AnimationStateData animationStateData(*skeletonData);
AnimationState animationState(animationStateData);
// Set the "eyeblink-long" animation like in the web example
animationState.setAnimation(0, "eyeblink-long", true);
// 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);
// 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 the skeleton time (used for physics)
skeleton.update(delta);
// Calculate the new pose
skeleton.updateWorldTransform(spine::Physics_Update);
// Clear the screen
gl::glClear(gl::GL_COLOR_BUFFER_BIT);
// Render the skeleton in its current pose
renderer_draw(renderer, &skeleton, true);
// Present the rendering results and poll for events
glfwSwapBuffers(window);
glfwPollEvents();
}
// Clear the drag skeleton pointer
dragSkeleton = nullptr;
// Dispose everything
renderer_dispose(renderer);
delete skeletonData;
delete atlas;
// Kill the window and GLFW
glfwTerminate();
return 0;
}