From 3aab7443953713d574ee2c28469044ab9c257c07 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Fri, 22 Mar 2013 16:18:03 +0100 Subject: [PATCH] Utility methods to read from file and other minor improvements. --- spine-cpp/include/spine/AnimationState.h | 2 +- spine-cpp/include/spine/BaseSkeleton.h | 2 ++ spine-cpp/include/spine/BaseSkeletonJson.h | 2 ++ spine-cpp/src/spine/BaseSkeleton.cpp | 4 +++ spine-cpp/src/spine/BaseSkeletonJson.cpp | 10 +++++++ .../spine/AnimationState.java | 26 ++++++++++--------- .../org.eclipse.cdt.codan.core.prefs | 2 +- spine-sfml/include/spine-sfml/Skeleton.h | 2 +- spine-sfml/include/spine-sfml/spine.h | 2 ++ spine-sfml/src/main.cpp | 10 +++---- 10 files changed, 40 insertions(+), 22 deletions(-) diff --git a/spine-cpp/include/spine/AnimationState.h b/spine-cpp/include/spine/AnimationState.h index eb61a10f2..2f67a5365 100644 --- a/spine-cpp/include/spine/AnimationState.h +++ b/spine-cpp/include/spine/AnimationState.h @@ -53,7 +53,7 @@ public: void apply (BaseSkeleton *skeleton); void setAnimation (Animation *newAnimation, bool loop, float time); - void setAnimation (Animation *animation, bool loop); + void setAnimation (Animation *animation, bool loop = false); }; } /* namespace spine */ diff --git a/spine-cpp/include/spine/BaseSkeleton.h b/spine-cpp/include/spine/BaseSkeleton.h index a0993f495..c18852831 100644 --- a/spine-cpp/include/spine/BaseSkeleton.h +++ b/spine-cpp/include/spine/BaseSkeleton.h @@ -72,6 +72,8 @@ public: Attachment* getAttachment (const std::string &slotName, const std::string &attachmentName) const; Attachment* getAttachment (int slotIndex, const std::string &attachmentName) const; void setAttachment (const std::string &slotName, const std::string &attachmentName); + + void update (float deltaTime); }; } /* namespace spine */ diff --git a/spine-cpp/include/spine/BaseSkeletonJson.h b/spine-cpp/include/spine/BaseSkeletonJson.h index 7e0966aa9..3f30c1002 100644 --- a/spine-cpp/include/spine/BaseSkeletonJson.h +++ b/spine-cpp/include/spine/BaseSkeletonJson.h @@ -44,11 +44,13 @@ public: BaseSkeletonJson (BaseAttachmentLoader *attachmentLoader); virtual ~BaseSkeletonJson (); + SkeletonData* readSkeletonDataFile (const std::string &path) const; SkeletonData* readSkeletonData (std::ifstream &file) const; SkeletonData* readSkeletonData (std::istream &file) const; SkeletonData* readSkeletonData (const std::string &json) const; SkeletonData* readSkeletonData (const char *begin, const char *end) const; + Animation* readAnimationFile (const std::string &path, const SkeletonData *skeletonData) const; Animation* readAnimation (std::ifstream &file, const SkeletonData *skeletonData) const; Animation* readAnimation (std::istream &file, const SkeletonData *skeletonData) const; Animation* readAnimation (const std::string &json, const SkeletonData *skeletonData) const; diff --git a/spine-cpp/src/spine/BaseSkeleton.cpp b/spine-cpp/src/spine/BaseSkeleton.cpp index 349885a05..c98fa6012 100644 --- a/spine-cpp/src/spine/BaseSkeleton.cpp +++ b/spine-cpp/src/spine/BaseSkeleton.cpp @@ -176,4 +176,8 @@ void BaseSkeleton::setAttachment (const string &slotName, const string &attachme throw invalid_argument("Slot not found: " + slotName); } +void BaseSkeleton::update (float deltaTime) { + time += deltaTime; +} + } /* namespace spine */ diff --git a/spine-cpp/src/spine/BaseSkeletonJson.cpp b/spine-cpp/src/spine/BaseSkeletonJson.cpp index e9da5cb71..0a14e02fa 100644 --- a/spine-cpp/src/spine/BaseSkeletonJson.cpp +++ b/spine-cpp/src/spine/BaseSkeletonJson.cpp @@ -65,6 +65,11 @@ BaseSkeletonJson::~BaseSkeletonJson () { delete attachmentLoader; } +SkeletonData* BaseSkeletonJson::readSkeletonDataFile (const string &path) const { + std::ifstream file(path.c_str()); + return readSkeletonData(file); +} + SkeletonData* BaseSkeletonJson::readSkeletonData (std::ifstream &file) const { if (!file) throw invalid_argument("file cannot be null."); if (!file.is_open()) throw runtime_error("Skeleton file is not open."); @@ -204,6 +209,11 @@ SkeletonData* BaseSkeletonJson::readSkeletonData (const char *begin, const char return skeletonData; } +Animation* BaseSkeletonJson::readAnimationFile (const string &path, const SkeletonData *skeletonData) const { + std::ifstream file(path.c_str()); + return readAnimation(file, skeletonData); +} + Animation* BaseSkeletonJson::readAnimation (std::ifstream &file, const SkeletonData *skeletonData) const { if (!file) throw invalid_argument("file cannot be null."); if (!file.is_open()) throw runtime_error("Animation file is not open."); diff --git a/spine-libgdx/src/com/esotericsoftware/spine/AnimationState.java b/spine-libgdx/src/com/esotericsoftware/spine/AnimationState.java index b6adc14a8..2521650d0 100644 --- a/spine-libgdx/src/com/esotericsoftware/spine/AnimationState.java +++ b/spine-libgdx/src/com/esotericsoftware/spine/AnimationState.java @@ -39,24 +39,26 @@ public class AnimationState { this.data = data; } - public void apply (Skeleton skeleton) { - if (current == null) return; - if (previous != null) { - previous.apply(skeleton, previousTime, previousLoop); - float alpha = MathUtils.clamp(mixTime / mixDuration, 0, 1); - current.mix(skeleton, currentTime, currentLoop, alpha); - if (alpha == 1) previous = null; - } else { - current.apply(skeleton, currentTime, currentLoop); - } - } - public void update (float delta) { currentTime += delta; previousTime += delta; mixTime += delta; } + public void apply (Skeleton skeleton) { + if (current == null) return; + if (previous != null) { + previous.apply(skeleton, previousTime, previousLoop); + float alpha = mixTime / mixDuration; + if (alpha >= 1) { + alpha = 1; + previous = null; + } + current.mix(skeleton, currentTime, currentLoop, alpha); + } else + current.apply(skeleton, currentTime, currentLoop); + } + /** Set the current animation. */ public void setAnimation (Animation animation, boolean loop) { setAnimation(animation, loop, 0); diff --git a/spine-sfml/.settings/org.eclipse.cdt.codan.core.prefs b/spine-sfml/.settings/org.eclipse.cdt.codan.core.prefs index 4fa6ad5b7..c7be96702 100644 --- a/spine-sfml/.settings/org.eclipse.cdt.codan.core.prefs +++ b/spine-sfml/.settings/org.eclipse.cdt.codan.core.prefs @@ -19,7 +19,7 @@ org.eclipse.cdt.codan.internal.checkers.CatchByReference=Warning org.eclipse.cdt.codan.internal.checkers.CatchByReference.params={launchModes\=>{RUN_ON_FULL_BUILD\=>true,RUN_ON_INC_BUILD\=>true,RUN_ON_FILE_OPEN\=>false,RUN_ON_FILE_SAVE\=>false,RUN_AS_YOU_TYPE\=>true,RUN_ON_DEMAND\=>true},unknown\=>false,exceptions\=>()} org.eclipse.cdt.codan.internal.checkers.CircularReferenceProblem=Error org.eclipse.cdt.codan.internal.checkers.CircularReferenceProblem.params={launchModes\=>{RUN_ON_FULL_BUILD\=>true,RUN_ON_INC_BUILD\=>true,RUN_ON_FILE_OPEN\=>false,RUN_ON_FILE_SAVE\=>false,RUN_AS_YOU_TYPE\=>true,RUN_ON_DEMAND\=>true}} -org.eclipse.cdt.codan.internal.checkers.ClassMembersInitialization=-Warning +org.eclipse.cdt.codan.internal.checkers.ClassMembersInitialization=Warning org.eclipse.cdt.codan.internal.checkers.ClassMembersInitialization.params={launchModes\=>{RUN_ON_FULL_BUILD\=>true,RUN_ON_INC_BUILD\=>true,RUN_ON_FILE_OPEN\=>false,RUN_ON_FILE_SAVE\=>false,RUN_AS_YOU_TYPE\=>true,RUN_ON_DEMAND\=>true},skip\=>true} org.eclipse.cdt.codan.internal.checkers.FieldResolutionProblem=Error org.eclipse.cdt.codan.internal.checkers.FieldResolutionProblem.params={launchModes\=>{RUN_ON_FULL_BUILD\=>true,RUN_ON_INC_BUILD\=>true,RUN_ON_FILE_OPEN\=>false,RUN_ON_FILE_SAVE\=>false,RUN_AS_YOU_TYPE\=>true,RUN_ON_DEMAND\=>true}} diff --git a/spine-sfml/include/spine-sfml/Skeleton.h b/spine-sfml/include/spine-sfml/Skeleton.h index 0a5807e6a..3dce7fe17 100644 --- a/spine-sfml/include/spine-sfml/Skeleton.h +++ b/spine-sfml/include/spine-sfml/Skeleton.h @@ -34,7 +34,7 @@ namespace spine { class Skeleton: public BaseSkeleton, public sf::Drawable { public: sf::VertexArray vertexArray; - sf::Texture *texture; // This is a bit ugly and means all region attachments must use the same textures. + sf::Texture *texture; // All region attachments must use the same texture. Skeleton (SkeletonData *skeletonData); diff --git a/spine-sfml/include/spine-sfml/spine.h b/spine-sfml/include/spine-sfml/spine.h index 9a8efd5a8..acf81dc09 100644 --- a/spine-sfml/include/spine-sfml/spine.h +++ b/spine-sfml/include/spine-sfml/spine.h @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include diff --git a/spine-sfml/src/main.cpp b/spine-sfml/src/main.cpp index 4bd99720a..ca97d8ab7 100644 --- a/spine-sfml/src/main.cpp +++ b/spine-sfml/src/main.cpp @@ -37,13 +37,9 @@ int main () { ifstream atlasFile("../data/spineboy.atlas"); Atlas *atlas = new Atlas(atlasFile); - SkeletonJson skeletonJson(atlas); - - ifstream skeletonFile("../data/spineboy-skeleton.json"); - SkeletonData *skeletonData = skeletonJson.readSkeletonData(skeletonFile); - - ifstream animationFile("../data/spineboy-walk.json"); - Animation *animation = skeletonJson.readAnimation(animationFile, skeletonData); + SkeletonJson json(atlas); + SkeletonData *skeletonData = json.readSkeletonDataFile("../data/spineboy-skeleton.json"); + Animation *animation = json.readAnimationFile("../data/spineboy-walk.json", skeletonData); Skeleton *skeleton = new Skeleton(skeletonData); skeleton->flipX = false;