Skip to content

Commit 590c404

Browse files
committed
Merge pull request #814 from jtwigg/develop
fix debug define & animation Divergence.
2 parents e8b2054 + e262077 commit 590c404

File tree

6 files changed

+191
-13
lines changed

6 files changed

+191
-13
lines changed

UnitTests/CCAnimationTest.m

Lines changed: 140 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
#import <XCTest/XCTest.h>
1010
#import "cocos2d.h"
11-
#import "CCBAnimationManager.h"
11+
#import "CCAnimationManager.h"
1212
#import "CCBSequence.h"
1313
#import "CCBKeyframe.h"
14+
#import "CCAnimationManager_Private.h"
15+
16+
#define NUM_ELEMENTS(array) (sizeof(array)/sizeof(array[0]))
1417

1518
#define AssertNotEqualWithAccuracy(a1,a2,accuracy,format...)\
1619
{\
@@ -51,7 +54,7 @@ - (void)tearDown
5154

5255
- (void)testSmallKeyframesTest
5356
{
54-
CCBAnimationManager * animationManager = [[CCBAnimationManager alloc] init];
57+
CCAnimationManager * animationManager = [[CCAnimationManager alloc] init];
5558
CCNode * node = [[CCNode alloc] init];
5659
node.name = @"testNode";
5760
node.position = ccp(0.0f,0.0f);
@@ -98,7 +101,6 @@ - (void)testSmallKeyframesTest
98101
[seqs setObject:seqNodeProps forKey:[NSNumber numberWithInt:seq.sequenceId]];
99102

100103
[animationManager addNode:node andSequences:seqs];
101-
102104
[animationManager runAnimationsForSequenceId:seq.sequenceId tweenDuration:0];
103105

104106
CGFloat accuracy = 0.01f;
@@ -132,4 +134,139 @@ - (void)testSmallKeyframesTest
132134
}
133135
}
134136

137+
struct PositionKeyframes
138+
{
139+
float time;
140+
CGPoint position;
141+
};
142+
143+
NSDictionary * createPositionSequencePropery(struct PositionKeyframes * keyframes, int count)
144+
{
145+
146+
const int kSequencerID = 0; //for now;
147+
148+
CCBSequenceProperty* seqProp = [[CCBSequenceProperty alloc] init];
149+
150+
seqProp.name = @"position";
151+
seqProp.type = 0;
152+
153+
154+
for (int k = 0; k < count; k++)
155+
{
156+
CCBKeyframe* keyframe = [[CCBKeyframe alloc] init];
157+
keyframe.time = keyframes[k].time;
158+
159+
keyframe.easingType = kCCBKeyframeEasingLinear;
160+
keyframe.easingOpt = 0;
161+
keyframe.value = @[@(keyframes[k].position.x),@(keyframes[k].position.y)];
162+
163+
[seqProp.keyframes addObject:keyframe];
164+
}
165+
166+
NSMutableDictionary* seqNodeProps = [NSMutableDictionary dictionary];
167+
[seqNodeProps setObject:seqProp forKey:seqProp.name];
168+
169+
NSMutableDictionary* seqs = [NSMutableDictionary dictionary];
170+
[seqs setObject:seqNodeProps forKey:[NSNumber numberWithInt:kSequencerID]];
171+
172+
return seqs;
173+
}
174+
175+
- (void)testAnimationSync
176+
{
177+
CCAnimationManager * animationManager = [[CCAnimationManager alloc] init];
178+
CCBSequence* seq = [[CCBSequence alloc] init];
179+
seq.duration = 4.0f;
180+
seq.name = @"TestSequence";
181+
seq.sequenceId = 0;
182+
seq.chainedSequenceId = 0;
183+
[animationManager.sequences addObject:seq];
184+
185+
/////////////////////////////////////////////
186+
187+
CCNode * nodeA = [[CCNode alloc] init];
188+
nodeA.name = @"testA";
189+
nodeA.position = ccp(0.0f,0.0f);
190+
191+
struct PositionKeyframes positionsA[] =
192+
{{0.0f,0.0f,0.0f},
193+
{1.0f,100.0f,0.0f},
194+
{3.0f,100.0f,0.0f},
195+
{4.0f,0.0f,0.0f}};
196+
197+
198+
[animationManager addNode:nodeA andSequences:createPositionSequencePropery(positionsA, NUM_ELEMENTS(positionsA))];
199+
200+
/////////////////////////////////////////////
201+
202+
CCNode * nodeB = [[CCNode alloc] init];
203+
nodeB.name = @"testB";
204+
nodeB.position = ccp(0.0f,0.0f);
205+
206+
struct PositionKeyframes positionsB[] =
207+
{{0.0f,0.0f,0.0f},
208+
{1.0f,0.0f,0.0f},
209+
{2.0f,100.0f,0.0f},
210+
{3.0f,100.0f,0.0f},
211+
{4.0f,0.0f,0.0f}};
212+
213+
[animationManager addNode:nodeB andSequences:createPositionSequencePropery(positionsB, NUM_ELEMENTS(positionsB))];
214+
215+
216+
/////////////////////////////////////////////
217+
218+
219+
CCNode * nodeC = [[CCNode alloc] init];
220+
nodeC.name = @"testC";
221+
nodeC.position = ccp(0.0f,0.0f);
222+
223+
struct PositionKeyframes positionsC[] =
224+
{ {0.0f,0.0f,0.0f},
225+
{2.0f,0.0f,0.0f},
226+
{3.0f,100.0f,0.0f},
227+
{4.0f,0.0f,0.0f}};
228+
229+
[animationManager addNode:nodeC andSequences:createPositionSequencePropery(positionsC, NUM_ELEMENTS(positionsC))];
230+
231+
/////////////////////////////////////////////
232+
233+
[animationManager runAnimationsForSequenceId:seq.sequenceId tweenDuration:0];
234+
235+
236+
const float kDelta = 0.1f;//100ms;
237+
const CGFloat kAccuracy = 0.01f;
238+
239+
float timeIntoSeq = 0.0f;
240+
float elapsed = 0.0f;
241+
242+
while(elapsed <= seq.duration * 10)
243+
{
244+
timeIntoSeq = fmod(elapsed, seq.duration);
245+
[animationManager update:kDelta];
246+
247+
elapsed += kDelta;
248+
249+
timeIntoSeq = fmod(elapsed, seq.duration);
250+
251+
if(timeIntoSeq >= 3.0f)
252+
{
253+
//All final translations go from x=100 -> x=0 over 1 second.
254+
float perentageIntroSyncedTranlation = 1.0f - (seq.duration - timeIntoSeq);
255+
float desiredXCoord = (1.0f - perentageIntroSyncedTranlation) * 100.0f;
256+
257+
258+
XCTAssertTrue(fabsf(nodeA.position.x - nodeB.position.x) < kAccuracy, @"They should all equal each other");
259+
260+
XCTAssertTrue(fabsf(nodeA.position.x - nodeC.position.x) < kAccuracy, @"They should all equal each other");
261+
262+
XCTAssertTrue(fabsf(nodeA.position.x - desiredXCoord) < kAccuracy, @"They should all equal each desiredXCoord: XPos:%0.2f DesiredPos:%0.2f elapsed:%0.2f", nodeA.position.x,desiredXCoord, elapsed);
263+
264+
}
265+
266+
}
267+
268+
}
269+
270+
271+
135272
@end

cocos2d-ui/CCBReader/CCAnimationManager.m

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,26 @@ - (CGSize)containerSize:(CCNode*)node {
8181
}
8282
}
8383

84-
- (void)addNode:(CCNode*)node andSequences:(NSDictionary*)seq {
84+
- (void)addNode:(CCNode*)node andSequences:(NSDictionary*)seq
85+
{
86+
#ifdef DEBUG
87+
//Sanity check sequences;
88+
89+
for (NSMutableDictionary* seqNodeProps in seq.allValues) {
90+
91+
for (CCBSequenceProperty* seqProp in seqNodeProps.allValues)
92+
{
93+
if(seqProp.keyframes.count > 0)
94+
{
95+
CCBKeyframe * keyFrameZero = seqProp.keyframes[0];
96+
NSAssert(keyFrameZero.time == 0.0f, @"The first keyframe should always be at time Zero.");
97+
}
98+
99+
}
100+
}
101+
102+
#endif
103+
85104
NSValue* nodePtr = [NSValue valueWithPointer:(__bridge const void *)(node)];
86105
[_nodeSequences setObject:seq forKey:nodePtr];
87106
}
@@ -441,6 +460,12 @@ - (void)runAnimationsForSequenceId:(int)seqId tweenDuration:(float) tweenDuratio
441460

442461
_paused = YES;
443462
[self clearAllActions];
463+
464+
// Set the running scene
465+
_runningSequence = [self sequenceFromSequenceId:seqId];
466+
_runningSequence.time = 0.0f;
467+
468+
[self addSequenceCallBacks:seqId tweenDuration:tweenDuration startTime:0];
444469

445470
// Contains all Sequence Propertys / Keyframe
446471
for (NSValue* nodePtr in _nodeSequences) {
@@ -484,12 +509,6 @@ - (void)runAnimationsForSequenceId:(int)seqId tweenDuration:(float) tweenDuratio
484509

485510
}
486511

487-
[self addSequenceCallBacks:seqId tweenDuration:tweenDuration startTime:0];
488-
489-
// Set the running scene
490-
_runningSequence = [self sequenceFromSequenceId:seqId];
491-
_runningSequence.time = 0.0f;
492-
493512
_paused = NO;
494513
}
495514

@@ -545,6 +564,8 @@ - (void)sequenceCompleted {
545564
_lastSequence = _runningSequence;
546565
}
547566

567+
float overhang = _runningSequence.time - _runningSequence.duration;
568+
548569
// Play next sequence
549570
int nextSeqId = _runningSequence.chainedSequenceId;
550571

@@ -562,6 +583,8 @@ - (void)sequenceCompleted {
562583
// Run next sequence if callbacks did not start a new sequence
563584
if (_runningSequence == NULL && nextSeqId != -1) {
564585
[self runAnimationsForSequenceId:nextSeqId tweenDuration:0];
586+
[self updateInternal:0]; //Update initially one frame.
587+
[self updateInternal:overhang]; //More frame forward.
565588
}
566589
}
567590

@@ -856,6 +879,8 @@ - (void)updateInternal:(CCTime)delta {
856879
return;
857880
}
858881

882+
_runningSequence.time+=step;
883+
859884
if(_currentActions.count==0) return;
860885

861886
CCAction *action;
@@ -869,7 +894,7 @@ - (void)updateInternal:(CCTime)delta {
869894
}
870895
}
871896

872-
_runningSequence.time+=step;
897+
873898
}
874899

875900
- (void)clearAllActions {

cocos2d-ui/CCBReader/CCBKeyframe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
@class CCActionSequence;
2929

30-
@interface CCBKeyframe : NSObject
30+
@interface CCBKeyframe : NSObject <NSCopying>
3131

3232
@property (nonatomic,strong) id value;
3333
@property (nonatomic,assign) float time;

cocos2d-ui/CCBReader/CCBKeyframe.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,14 @@ - (NSString *) description {
4949
return description;
5050
}
5151

52+
- (id)copyWithZone:(NSZone *)zone
53+
{
54+
CCBKeyframe * copyKeyframe = [[self class] allocWithZone:zone];
55+
copyKeyframe->_value = self->_value;
56+
copyKeyframe->_time = self->_time;
57+
copyKeyframe->_easingType = self->_easingType;
58+
copyKeyframe->_easingOpt = self->_easingOpt;
5259

60+
return copyKeyframe;
61+
}
5362
@end

cocos2d-ui/CCBReader/CCBReader.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,13 @@ - (CCNode*) readNodeGraphParent:(CCNode*)parent
12881288
{
12891289
CCBKeyframe* keyframe = [self readKeyframeOfType:seqProp.type];
12901290

1291+
if(k==0 && keyframe.time > 0.0f)
1292+
{
1293+
CCBKeyframe * copyKeyframe = [keyframe copy];
1294+
copyKeyframe.time = 0.0f;
1295+
[seqProp.keyframes addObject:copyKeyframe];
1296+
}
1297+
12911298
[seqProp.keyframes addObject:keyframe];
12921299
}
12931300

cocos2d/CCPhysicsBody.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ -(void)trackParentTransformations:(CCPhysicsNode *)physics
531531
[node addObserver:self forKeyPath:kDependantProperties[i] options:NSKeyValueObservingOptionNew context:nil];
532532
}
533533

534-
#if DEBUG
534+
#ifdef DEBUG
535535
for (int i = 0; i < sizeof(kRestrictedProperties)/sizeof(kRestrictedProperties[0]); i++)
536536
{
537537
[node addObserver:self forKeyPath:kRestrictedProperties[i] options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

0 commit comments

Comments
 (0)