Skip to content

Commit

Permalink
Bezier added for Spine
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkemose committed Aug 10, 2014
1 parent 0e5445d commit b08b2d2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef NS_ENUM(NSInteger, CCSpineInterpolation)

// ----------------------------------------------------------------------------------------------

@interface CCSpineBezierCurve : NSObject
@interface CCSpineCurve : NSObject

// ----------------------------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@
* THE SOFTWARE.
*/

#import "CCSpineBezierCurve.h"
#import "CCSpineCurve.h"

// ----------------------------------------------------------------------------------------------

@implementation CCSpineBezierCurve
@implementation CCSpineCurve
{
CCSpineInterpolation _interpolation;
NSInteger _count;
CGPoint *_bezier;
CGPoint *_coefficient;
}

// ----------------------------------------------------------------------------------------------

+ (instancetype)bezierCurveWithDictionary:(NSDictionary *)dict
{
return([[CCSpineBezierCurve alloc] initWithDictionary:dict]);
return([[CCSpineCurve alloc] initWithDictionary:dict]);
}

// ----------------------------------------------------------------------------------------------
Expand All @@ -53,6 +54,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)dict
_interpolation = CCSpineInterpolationLinear;
_count = 0;
_bezier = NULL;
_coefficient = NULL;
// load values from dictionary
id object = [dict objectForKey:@"curve"];
if ([object isKindOfClass:[NSString class]])
Expand All @@ -71,9 +73,9 @@ - (instancetype)initWithDictionary:(NSDictionary *)dict
}
else if ([object isKindOfClass:[NSArray class]])
{
// curve defined as an array
// this must be an array of floats
// add 0 at the begining and 1 at the end, to make later interpolation easier
// curve defined as two points
// start point is 0,0
// end point is 1,1
NSArray* array = object;
if (array.count > 0)
{
Expand All @@ -84,9 +86,21 @@ - (instancetype)initWithDictionary:(NSDictionary *)dict
for (NSNumber* value in array)
{
// read alternating values as x, y
if ((index & 1) == 0) _bezier[index / 2].x = [value floatValue]; else _bezier[index / 2].y = [value floatValue];
if ((index & 1) == 0)
{
_bezier[index / 2].x = [value floatValue];
}
else
{
_bezier[index / 2].y = [value floatValue];
}
index ++;
}
// calculate coefficients
_coefficient = malloc(3 * sizeof(CGPoint));
_coefficient[2] = ccpMult(_bezier[0], 3);
_coefficient[1] = ccpSub(ccpMult(ccpSub(_bezier[1], _bezier[0]), 3), _coefficient[2]);
_coefficient[0] = ccpSub(ccpSub(ccp(1,1), _coefficient[2]), _coefficient[1]);
}
}

Expand All @@ -100,6 +114,7 @@ - (void)dealloc
{
// clean up
if (_bezier != NULL) free(_bezier);
if (_coefficient != NULL) free(_coefficient);

}

Expand All @@ -115,13 +130,20 @@ - (float)translate:(float)value
case CCSpineInterpolationStepped:
return(0);
case CCSpineInterpolationBezier:
{
float t2 = value * value;
float t3 = t2 * value;
float result = (_coefficient[0].y * t3) + (_coefficient[1].y * t2) + (_coefficient[2].y * value);




// TODO
// convert according to bezier curve
return(value);
return(result);
}
}
NSAssert(NO, @"[CCSpineBezierCurve translate] Invalid interpolation");
NSAssert(NO, @"[CCSpineCurve translate] Invalid interpolation");
return(0);
}

Expand Down
4 changes: 2 additions & 2 deletions Extensions/CCSpine/CCSpineSample.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#import <Foundation/Foundation.h>
#import "CCDictionary.h"
#import "CCSpineBezierCurve.h"
#import "CCSpineCurve.h"

// ----------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -55,7 +55,7 @@ typedef struct
@property (nonatomic, readonly) float time;
@property (nonatomic, readonly) CCSpineSampleType type;
@property (nonatomic, readonly) CCSpineBoneData data;
@property (nonatomic, readonly) CCSpineBezierCurve *curve;
@property (nonatomic, readonly) CCSpineCurve *curve;

// TODO add color

Expand Down
2 changes: 1 addition & 1 deletion Extensions/CCSpine/CCSpineSample.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (instancetype)initDictionary:(NSDictionary *)dict andType:(CCSpineSampleType)t
}

// load curve
_curve = [CCSpineBezierCurve bezierCurveWithDictionary:dict];
_curve = [CCSpineCurve bezierCurveWithDictionary:dict];

// done
return(self);
Expand Down
12 changes: 6 additions & 6 deletions cocos2d-iphone-ext.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
0E21D306198A47A400E84AB1 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 0E21D305198A47A400E84AB1 /* README.md */; };
0E21D30C198A484600E84AB1 /* CCSpriteMultiTouch.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E21D30B198A484600E84AB1 /* CCSpriteMultiTouch.m */; };
0E21D30F198A637900E84AB1 /* CCSpriteMultiTouchTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E21D30E198A637900E84AB1 /* CCSpriteMultiTouchTest.m */; };
0ED73BB51997951A00C83507 /* CCSpineCurve.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ED73BB41997951A00C83507 /* CCSpineCurve.m */; };
A6270E62193757E400196BD3 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A6270E61193757E400196BD3 /* QuartzCore.framework */; };
A6270E64193757E400196BD3 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A6270E63193757E400196BD3 /* OpenGLES.framework */; };
A6270E66193757E400196BD3 /* OpenAL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A6270E65193757E400196BD3 /* OpenAL.framework */; };
Expand Down Expand Up @@ -83,7 +84,6 @@
A66C582E193A0C87009D2BD4 /* chase_johnson.png in Resources */ = {isa = PBXBuildFile; fileRef = A66C5815193A0C87009D2BD4 /* chase_johnson.png */; };
A66C5830193A0C87009D2BD4 /* CCSpineAnimation.m in Sources */ = {isa = PBXBuildFile; fileRef = A66C5819193A0C87009D2BD4 /* CCSpineAnimation.m */; };
A66C5831193A0C87009D2BD4 /* CCSpineAnimationControl.m in Sources */ = {isa = PBXBuildFile; fileRef = A66C581B193A0C87009D2BD4 /* CCSpineAnimationControl.m */; };
A66C5832193A0C87009D2BD4 /* CCSpineBezierCurve.m in Sources */ = {isa = PBXBuildFile; fileRef = A66C581D193A0C87009D2BD4 /* CCSpineBezierCurve.m */; };
A66C5833193A0C87009D2BD4 /* CCSpineBone.m in Sources */ = {isa = PBXBuildFile; fileRef = A66C581F193A0C87009D2BD4 /* CCSpineBone.m */; };
A66C5834193A0C87009D2BD4 /* CCSpineSample.m in Sources */ = {isa = PBXBuildFile; fileRef = A66C5821193A0C87009D2BD4 /* CCSpineSample.m */; };
A66C5835193A0C87009D2BD4 /* CCSpineSkeleton.m in Sources */ = {isa = PBXBuildFile; fileRef = A66C5823193A0C87009D2BD4 /* CCSpineSkeleton.m */; };
Expand Down Expand Up @@ -125,6 +125,8 @@
0E21D30A198A484600E84AB1 /* CCSpriteMultiTouch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCSpriteMultiTouch.h; path = Extensions/CCSpriteMultiTouch/CCSpriteMultiTouch.h; sourceTree = SOURCE_ROOT; };
0E21D30B198A484600E84AB1 /* CCSpriteMultiTouch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpriteMultiTouch.m; path = Extensions/CCSpriteMultiTouch/CCSpriteMultiTouch.m; sourceTree = SOURCE_ROOT; };
0E21D30E198A637900E84AB1 /* CCSpriteMultiTouchTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpriteMultiTouchTest.m; path = Classes/Tests/CCSpriteMultiTouchTest.m; sourceTree = "<group>"; };
0ED73BB31997951A00C83507 /* CCSpineCurve.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCSpineCurve.h; path = Extensions/CCSpine/CCSpineCurve.h; sourceTree = SOURCE_ROOT; };
0ED73BB41997951A00C83507 /* CCSpineCurve.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpineCurve.m; path = Extensions/CCSpine/CCSpineCurve.m; sourceTree = SOURCE_ROOT; };
A6254BAF193A1438006BD09E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = Extensions/CCNodeTag/README.md; sourceTree = SOURCE_ROOT; };
A6254BB1193A144A006BD09E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = Extensions/CCDictionary/README.md; sourceTree = SOURCE_ROOT; };
A6254BB3193A145D006BD09E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = Extensions/CCSpine/README.md; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -212,8 +214,6 @@
A66C5819193A0C87009D2BD4 /* CCSpineAnimation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpineAnimation.m; path = Extensions/CCSpine/CCSpineAnimation.m; sourceTree = SOURCE_ROOT; };
A66C581A193A0C87009D2BD4 /* CCSpineAnimationControl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCSpineAnimationControl.h; path = Extensions/CCSpine/CCSpineAnimationControl.h; sourceTree = SOURCE_ROOT; };
A66C581B193A0C87009D2BD4 /* CCSpineAnimationControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpineAnimationControl.m; path = Extensions/CCSpine/CCSpineAnimationControl.m; sourceTree = SOURCE_ROOT; };
A66C581C193A0C87009D2BD4 /* CCSpineBezierCurve.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCSpineBezierCurve.h; path = Extensions/CCSpine/CCSpineBezierCurve.h; sourceTree = SOURCE_ROOT; };
A66C581D193A0C87009D2BD4 /* CCSpineBezierCurve.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpineBezierCurve.m; path = Extensions/CCSpine/CCSpineBezierCurve.m; sourceTree = SOURCE_ROOT; };
A66C581E193A0C87009D2BD4 /* CCSpineBone.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCSpineBone.h; path = Extensions/CCSpine/CCSpineBone.h; sourceTree = SOURCE_ROOT; };
A66C581F193A0C87009D2BD4 /* CCSpineBone.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpineBone.m; path = Extensions/CCSpine/CCSpineBone.m; sourceTree = SOURCE_ROOT; };
A66C5820193A0C87009D2BD4 /* CCSpineSample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCSpineSample.h; path = Extensions/CCSpine/CCSpineSample.h; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -498,8 +498,8 @@
A66C5819193A0C87009D2BD4 /* CCSpineAnimation.m */,
A66C581A193A0C87009D2BD4 /* CCSpineAnimationControl.h */,
A66C581B193A0C87009D2BD4 /* CCSpineAnimationControl.m */,
A66C581C193A0C87009D2BD4 /* CCSpineBezierCurve.h */,
A66C581D193A0C87009D2BD4 /* CCSpineBezierCurve.m */,
0ED73BB31997951A00C83507 /* CCSpineCurve.h */,
0ED73BB41997951A00C83507 /* CCSpineCurve.m */,
A66C581E193A0C87009D2BD4 /* CCSpineBone.h */,
A66C581F193A0C87009D2BD4 /* CCSpineBone.m */,
A66C5820193A0C87009D2BD4 /* CCSpineSample.h */,
Expand Down Expand Up @@ -715,6 +715,7 @@
buildActionMask = 2147483647;
files = (
0E21D30F198A637900E84AB1 /* CCSpriteMultiTouchTest.m in Sources */,
0ED73BB51997951A00C83507 /* CCSpineCurve.m in Sources */,
A6994F85194A2FAC00B97481 /* CCTransformationNodeTest.m in Sources */,
A627126E19375F2700196BD3 /* TestBase.m in Sources */,
A66C5838193A0C87009D2BD4 /* CCSpineTexture.m in Sources */,
Expand All @@ -736,7 +737,6 @@
0E21D30C198A484600E84AB1 /* CCSpriteMultiTouch.m in Sources */,
A66C5837193A0C87009D2BD4 /* CCSpineSprite.m in Sources */,
A66C580E193A0C69009D2BD4 /* CCNodeTag.m in Sources */,
A66C5832193A0C87009D2BD4 /* CCSpineBezierCurve.m in Sources */,
A627110B193757E600196BD3 /* AppDelegate.m in Sources */,
A66C5834193A0C87009D2BD4 /* CCSpineSample.m in Sources */,
);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges</key>
<true/>
<key>SnapshotAutomaticallyBeforeSignificantChanges</key>
<false/>
</dict>
</plist>

0 comments on commit b08b2d2

Please sign in to comment.