Browse Source

[cocos2dx] Add physics example

Mario Zechner 1 year ago
parent
commit
e7b47f64f3

+ 4 - 0
examples/export/runtimes.sh

@@ -108,6 +108,10 @@ cp -f ../mix-and-match/export/mix-and-match-pro.skel "$ROOT/spine-cocos2dx/examp
 cp -f ../mix-and-match/export/mix-and-match.atlas "$ROOT/spine-cocos2dx/example/Resources/common/"
 cp -f ../mix-and-match/export/mix-and-match.png "$ROOT/spine-cocos2dx/example/Resources/common/"
 
+cp -f ../celestial-circus/export/celestial-circus-pro.skel "$ROOT/spine-cocos2dx/example/Resources/common/"
+cp -f ../celestial-circus/export/celestial-circus.atlas "$ROOT/spine-cocos2dx/example/Resources/common/"
+cp -f ../celestial-circus/export/celestial-circus.png "$ROOT/spine-cocos2dx/example/Resources/common/"
+
 echo "spine-flutter"
 rm -rf "$ROOT/spine-flutter/example/assets/"*
 cp -f ../spineboy/export/spineboy-pro.json "$ROOT/spine-flutter/example/assets/"

+ 2 - 3
spine-cocos2dx/example/Classes/AppDelegate.cpp

@@ -33,8 +33,7 @@
 #include <vector>
 
 #include "AppMacros.h"
-#include "IKExample.h"
-#include "SequenceExample.h"
+#include "PhysicsExample.h"
 #include <spine/Debug.h>
 #include <spine/spine-cocos2dx.h>
 
@@ -111,7 +110,7 @@ bool AppDelegate::applicationDidFinishLaunching() {
 
 	// create a scene. it's an autorelease object
 	//auto scene = RaptorExample::scene();
-	auto scene = SequenceExample::scene();
+	auto scene = PhysicsExample::scene();
 
 	// run
 	director->runWithScene(scene);

+ 3 - 3
spine-cocos2dx/example/Classes/IKExample.cpp

@@ -28,7 +28,7 @@
  *****************************************************************************/
 
 #include "IKExample.h"
-#include "SpineboyExample.h"
+#include "PhysicsExample.h"
 
 USING_NS_CC;
 using namespace spine;
@@ -98,12 +98,12 @@ bool IKExample::init() {
 		crosshair->setX(localX);
 		crosshair->setY(localY);
 
-		node->getSkeleton()->updateWorldTransform();
+		node->getSkeleton()->updateWorldTransform(spine::Physics_Update);
 	});
 
 	EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
 	listener->onTouchBegan = [this](Touch *touch, cocos2d::Event *event) -> bool {
-		Director::getInstance()->replaceScene(SpineboyExample::scene());
+		Director::getInstance()->replaceScene(PhysicsExample::scene());
 		return true;
 	};
 

+ 88 - 0
spine-cocos2dx/example/Classes/PhysicsExample.cpp

@@ -0,0 +1,88 @@
+/******************************************************************************
+ * Spine Runtimes License Agreement
+ * Last updated July 28, 2023. Replaces all prior versions.
+ *
+ * Copyright (c) 2013-2023, 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 "PhysicsExample.h"
+#include "SpineboyExample.h"
+
+USING_NS_CC;
+using namespace spine;
+
+Scene *PhysicsExample::scene() {
+	Scene *scene = Scene::create();
+	scene->addChild(PhysicsExample::create());
+	return scene;
+}
+
+bool PhysicsExample::init() {
+	if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
+
+	// Load the Spineboy skeleton and create a SkeletonAnimation node from it
+	// centered on the screen.
+	skeletonNode = SkeletonAnimation::createWithBinaryFile("celestial-circus-pro.skel", "celestial-circus.atlas", 0.2f);
+	skeletonNode->setPosition(Vec2(_contentSize.width / 2, 200));
+	addChild(skeletonNode);
+
+	// Queue the "walk" animation on the first track.
+	// skeletonNode->setAnimation(0, "walk", true);
+
+	// Next we setup a listener that receives and stores
+	// the current mouse location and updates the skeleton position
+    // accordingly.
+	EventListenerMouse *mouseListener = EventListenerMouse::create();
+	mouseListener->onMouseMove = [this](cocos2d::Event *event) -> void {
+		// convert the mosue location to the skeleton's coordinate space
+		// and store it.
+		EventMouse *mouseEvent = dynamic_cast<EventMouse *>(event);
+		Vec2 mousePosition = skeletonNode->convertToNodeSpace(mouseEvent->getLocationInView());
+        if (firstUpdate) {
+            firstUpdate = false;
+            lastMousePosition = mousePosition;
+            return;
+        }
+        Vec2 delta = mousePosition - lastMousePosition;
+        skeletonNode->getSkeleton()->physicsTranslate(-delta.x, -delta.y);
+        lastMousePosition = mousePosition;
+	};
+	_eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
+
+	EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
+	listener->onTouchBegan = [this](Touch *touch, cocos2d::Event *event) -> bool {
+		Director::getInstance()->replaceScene(SpineboyExample::scene());
+		return true;
+	};
+
+	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
+
+	scheduleUpdate();
+
+	return true;
+}
+
+void PhysicsExample::update(float deltaTime) {
+}

+ 52 - 0
spine-cocos2dx/example/Classes/PhysicsExample.h

@@ -0,0 +1,52 @@
+/******************************************************************************
+ * Spine Runtimes License Agreement
+ * Last updated July 28, 2023. Replaces all prior versions.
+ *
+ * Copyright (c) 2013-2023, 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.
+ *****************************************************************************/
+
+#ifndef _PHYSICSEXAMPLE_H_
+#define _PHYSICSEXAMPLE_H_
+
+#include "cocos2d.h"
+#include <spine/spine-cocos2dx.h>
+
+class PhysicsExample : public cocos2d::LayerColor {
+public:
+	static cocos2d::Scene *scene();
+
+	CREATE_FUNC(PhysicsExample);
+
+	virtual bool init();
+
+	virtual void update(float deltaTime);
+
+private:
+	spine::SkeletonAnimation *skeletonNode;
+    bool firstUpdate = true;
+	cocos2d::Vec2 lastMousePosition;
+};
+
+#endif// _PHYSICSEXAMPLE_H_

BIN
spine-cocos2dx/example/Resources/common/celestial-circus-pro.skel


+ 174 - 0
spine-cocos2dx/example/Resources/common/celestial-circus.atlas

@@ -0,0 +1,174 @@
+
+celestial-circus.png
+	size: 1024, 1024
+	filter: Linear, Linear
+	scale: 0.4
+arm-back-down
+	bounds: 324, 401, 38, 82
+	rotate: 90
+arm-back-up
+	bounds: 290, 44, 83, 116
+	rotate: 90
+arm-front-down
+	bounds: 706, 2, 36, 78
+	rotate: 90
+arm-front-up
+	bounds: 860, 138, 77, 116
+bench
+	bounds: 725, 256, 189, 48
+body-bottom
+	bounds: 879, 868, 154, 124
+	rotate: 90
+body-top
+	bounds: 725, 128, 126, 133
+	rotate: 90
+chest
+	bounds: 408, 26, 104, 93
+cloud-back
+	bounds: 752, 378, 202, 165
+cloud-front
+	bounds: 2, 2, 325, 196
+	rotate: 90
+collar
+	bounds: 786, 13, 47, 26
+ear
+	bounds: 1002, 643, 20, 28
+eye-back-shadow
+	bounds: 428, 395, 14, 10
+eye-front-shadow
+	bounds: 704, 529, 24, 14
+eye-reflex-back
+	bounds: 860, 128, 8, 7
+	rotate: 90
+eye-reflex-front
+	bounds: 726, 386, 10, 7
+eye-white-back
+	bounds: 835, 23, 13, 16
+eye-white-front
+	bounds: 1005, 1000, 22, 17
+	rotate: 90
+eyelashes-down-back
+	bounds: 232, 329, 11, 6
+	rotate: 90
+eyelashes-down-front
+	bounds: 913, 851, 15, 6
+	rotate: 90
+eyelashes-top-back
+	bounds: 408, 395, 18, 10
+eyelashes-top-front
+	bounds: 702, 179, 30, 16
+	rotate: 90
+face
+	bounds: 514, 26, 93, 102
+	rotate: 90
+feathers-back
+	bounds: 954, 625, 46, 46
+feathers-front
+	bounds: 706, 40, 72, 86
+fringe-middle-back
+	bounds: 200, 6, 33, 52
+	rotate: 90
+fringe-middle-front
+	bounds: 878, 76, 60, 50
+	rotate: 90
+fringe-side-back
+	bounds: 780, 41, 27, 94
+	rotate: 90
+fringe-side-front
+	bounds: 939, 161, 26, 93
+glove-bottom-back
+	bounds: 954, 572, 51, 41
+	rotate: 90
+glove-bottom-front
+	bounds: 916, 256, 47, 48
+hair-back-1
+	bounds: 444, 395, 132, 306
+	rotate: 90
+hair-back-2
+	bounds: 438, 211, 80, 285
+	rotate: 90
+hair-back-3
+	bounds: 719, 306, 70, 268
+	rotate: 90
+hair-back-4
+	bounds: 438, 121, 88, 262
+	rotate: 90
+hair-back-5
+	bounds: 438, 293, 88, 279
+	rotate: 90
+hair-back-6
+	bounds: 200, 41, 88, 286
+hair-hat-shadow
+	bounds: 232, 398, 90, 41
+hand-back
+	bounds: 954, 673, 60, 47
+	rotate: 90
+hand-front
+	bounds: 967, 172, 53, 60
+hat-back
+	bounds: 954, 802, 64, 45
+	rotate: 90
+hat-front
+	bounds: 780, 70, 96, 56
+head-back
+	bounds: 618, 17, 102, 86
+	rotate: 90
+jabot
+	bounds: 967, 234, 70, 55
+	rotate: 90
+leg-back
+	bounds: 232, 441, 210, 333
+leg-front
+	bounds: 444, 529, 258, 320
+logo-brooch
+	bounds: 954, 545, 16, 25
+mouth
+	bounds: 408, 121, 22, 6
+neck
+	bounds: 232, 342, 39, 56
+	rotate: 90
+nose
+	bounds: 742, 529, 6, 7
+	rotate: 90
+nose-highlight
+	bounds: 719, 300, 4, 4
+nose-shadow
+	bounds: 869, 128, 7, 8
+pupil-back
+	bounds: 730, 529, 10, 14
+pupil-front
+	bounds: 254, 21, 12, 18
+rope-back
+	bounds: 232, 383, 10, 492
+	rotate: 90
+rope-front
+	bounds: 232, 383, 10, 492
+	rotate: 90
+rope-front-bottom
+	bounds: 954, 735, 42, 65
+skirt
+	bounds: 2, 776, 440, 246
+sock-bow
+	bounds: 408, 407, 33, 32
+spine-logo-body
+	bounds: 879, 853, 13, 32
+	rotate: 90
+star-big
+	bounds: 939, 141, 18, 24
+	rotate: 90
+star-medium
+	bounds: 742, 537, 6, 8
+	rotate: 90
+star-small
+	bounds: 719, 378, 3, 4
+	rotate: 90
+underskirt
+	bounds: 2, 329, 445, 228
+	rotate: 90
+underskirt-back
+	bounds: 444, 851, 433, 171
+wing-back
+	bounds: 290, 129, 146, 252
+wing-front
+	bounds: 704, 545, 304, 248
+	rotate: 90

BIN
spine-cocos2dx/example/Resources/common/celestial-circus.png


+ 2 - 1
spine-cocos2dx/spine-cocos2dx/src/spine/SkeletonAnimation.cpp

@@ -138,7 +138,8 @@ namespace spine {
 		if (_preUpdateListener) _preUpdateListener(this);
 		_state->update(deltaTime);
 		_state->apply(*_skeleton);
-		_skeleton->updateWorldTransform();
+        _skeleton->update(deltaTime);
+		_skeleton->updateWorldTransform(Physics_Update);
 		if (_postUpdateListener) _postUpdateListener(this);
 	}
 

+ 3 - 3
spine-cocos2dx/spine-cocos2dx/src/spine/SkeletonRenderer.cpp

@@ -95,7 +95,7 @@ namespace spine {
 		setTwoColorTint(false);
 
 		_skeleton->setToSetupPose();
-		_skeleton->updateWorldTransform();
+		_skeleton->updateWorldTransform(Physics_Update);
 	}
 
 	void SkeletonRenderer::setupGLProgramState(bool twoColorTintEnabled) {
@@ -671,8 +671,8 @@ namespace spine {
 
 	// --- Convenience methods for Skeleton_* functions.
 
-	void SkeletonRenderer::updateWorldTransform() {
-		_skeleton->updateWorldTransform();
+	void SkeletonRenderer::updateWorldTransform(Physics physics) {
+		_skeleton->updateWorldTransform(physics);
 	}
 
 	void SkeletonRenderer::setToSetupPose() {

+ 1 - 1
spine-cocos2dx/spine-cocos2dx/src/spine/SkeletonRenderer.h

@@ -71,7 +71,7 @@ namespace spine {
 		bool getDebugBoundingRectEnabled() const;
 
 		// --- Convenience methods for common Skeleton_* functions.
-		void updateWorldTransform();
+		void updateWorldTransform(spine::Physics physics);
 
 		void setToSetupPose();
 		void setBonesToSetupPose();

+ 2 - 2
spine-cpp/spine-cpp/include/spine/SpineString.h

@@ -188,7 +188,7 @@ namespace spine {
 		}
 
         int lastIndexOf(const char c) {
-            for (int i = length() - 1; i >= 0; i--) {
+            for (int i = (int)length() - 1; i >= 0; i--) {
                 if (buffer()[i] == c) return i;
             }
             return -1;
@@ -208,7 +208,7 @@ namespace spine {
             if (startIndex < 0 || startIndex >= (int)_length) {
                 return String();
             }
-            int length = _length - startIndex;
+            int length = (int)_length - startIndex;
             char* subStr = SpineExtension::calloc<char>(length + 1, __FILE__, __LINE__);
             memcpy(subStr, _buffer + startIndex, length);
             subStr[length] = '\0';

+ 1 - 1
spine-cpp/spine-cpp/src/spine/Atlas.cpp

@@ -192,7 +192,7 @@ struct AtlasInput {
 		line.end = index;
 		if (index != end) index++;
 		line = line.trim();
-		line.length = end - start;
+		line.length = (int)(end - start);
 		return &line;
 	}