Permalink
Browse files

Improved Skew + Negative Scale patch

  • Loading branch information...
ricardoquesada committed Dec 23, 2011
1 parent 992f33d commit 029345aac6712efb01efe00f067e1cf6a0e14ed0
Showing with 103 additions and 10 deletions.
  1. +2 −0 AUTHORS
  2. +1 −0 CHANGELOG
  3. +7 −7 cocos2d/CCNode.m
  4. +4 −0 tests/SpriteTest.h
  5. +89 −3 tests/SpriteTest.m
View
@@ -168,6 +168,8 @@ People/companies who were/are contributing code to cocos2d for iPhone (alphabeti
actions uses FLT_EPSILON macro. patch.
* effervens:
patches for Director
+ * effervecents (https://github.com/effervescent):
+ Sprite: Skew is performed after scale
* Ernesto Corvi & On-Core (http://www.on-core.com/):
author of TGA RLE support
patches for TextureMgr
View
@@ -1,6 +1,7 @@
version 1.1-beta2 xx - 2011
. [NEW] All: Code is compatible with ARC (issue #1199)
. [NEW] TileMap: supports in-memory TMX map creation (issue #1239)
+. [FIX] Node & Sprite: Scale before Skew to prevent issues with negatives scales.
. [FIX] TouchDispatcher: correctly handles addition and removal of handlers when being inside a touch handler (issue #1084, #1139)
. [FIX-MAC] LabelTTF: word wrap works on Mac (issue #1074)
View
@@ -666,6 +666,10 @@ -(void) transform
if (rotation_ != 0.0f )
glRotatef( -rotation_, 0.0f, 0.0f, 1.0f );
+ // scale
+ if (scaleX_ != 1.0f || scaleY_ != 1.0f)
+ glScalef( scaleX_, scaleY_, 1.0f );
+
// skew
if ( (skewX_ != 0.0f) || (skewY_ != 0.0f) ) {
CGAffineTransform skewMatrix = CGAffineTransformMake( 1.0f, tanf(CC_DEGREES_TO_RADIANS(skewY_)), tanf(CC_DEGREES_TO_RADIANS(skewX_)), 1.0f, 0.0f, 0.0f );
@@ -674,10 +678,6 @@ -(void) transform
glMultMatrixf(glMatrix);
}
- // scale
- if (scaleX_ != 1.0f || scaleY_ != 1.0f)
- glScalef( scaleX_, scaleY_, 1.0f );
-
if ( camera_ && !(grid_ && grid_.active) )
[camera_ locate];
@@ -833,16 +833,16 @@ - (CGAffineTransform)nodeToParentTransform
if( rotation_ != 0 )
transform_ = CGAffineTransformRotate(transform_, -CC_DEGREES_TO_RADIANS(rotation_));
+ if( ! (scaleX_ == 1 && scaleY_ == 1) )
+ transform_ = CGAffineTransformScale(transform_, scaleX_, scaleY_);
+
if( skewX_ != 0 || skewY_ != 0 ) {
// create a skewed coordinate system
CGAffineTransform skew = CGAffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(skewY_)), tanf(CC_DEGREES_TO_RADIANS(skewX_)), 1.0f, 0.0f, 0.0f);
// apply the skew to the transform
transform_ = CGAffineTransformConcat(skew, transform_);
}
- if( ! (scaleX_ == 1 && scaleY_ == 1) )
- transform_ = CGAffineTransformScale(transform_, scaleX_, scaleY_);
-
if( ! CGPointEqualToPoint(anchorPointInPixels_, CGPointZero) )
transform_ = CGAffineTransformTranslate(transform_, -anchorPointInPixels_.x, -anchorPointInPixels_.y);
View
@@ -279,6 +279,10 @@
{}
@end
+@interface SpriteSkewNegativeScaleChildren : SpriteDemo
+{}
+@end
+
@interface SpriteNilTexture : SpriteDemo
{}
@end
View
@@ -61,6 +61,7 @@
@"SpriteBatchNodeChildrenScale",
@"SpriteChildrenChildren",
@"SpriteBatchNodeChildrenChildren",
+ @"SpriteSkewNegativeScaleChildren",
@"SpriteBatchNodeSkewNegativeScaleChildren",
@"SpriteNilTexture",
@"SpriteSubclass",
@@ -3909,7 +3910,12 @@ -(id) init
-(NSString *) title
{
- return @"Sprite multiple levels of children";
+ return @"Sprite";
+}
+
+-(NSString *) subtitle
+{
+ return @"Multiple levels of children: Grandparent, parent and child";
}
@end
@@ -3997,7 +4003,12 @@ -(id) init
-(NSString *) title
{
- return @"SpriteBatchNode multiple levels of children";
+ return @"SpriteBatchNode";
+}
+
+-(NSString *) subtitle
+{
+ return @"Multiple levels of children: Grandparent, parent and child";
}
@end
@@ -4031,7 +4042,7 @@ -(id) init
if(i == 1)
{
- [sprite setScaleX:-1.0f];
+ [sprite setScale:-1.0f];
}
id seq_skew = [CCSequence actions:skewX, skewX_back, skewY, skewY_back, nil];
@@ -4040,6 +4051,8 @@ -(id) init
CCSprite *child1 = [CCSprite spriteWithSpriteFrameName:@"grossini_dance_01.png"];
[child1 setPosition: ccp(sprite.contentSize.width / 2.0f, sprite.contentSize.height / 2.0f)];
+ [child1 setScale:0.8];
+
[sprite addChild: child1];
[spritebatch addChild:sprite z:i];
@@ -4057,11 +4070,84 @@ - (void) dealloc
}
-(NSString *) title
+{
+ return @"SpriteBatchNode + children + skew";
+}
+
+-(NSString *) subtitle
{
return @"SpriteBatchNode skew + negative scale with children";
}
@end
+#pragma mark -
+#pragma mark Example SpriteSkewNegativeScaleChildren
+
+@implementation SpriteSkewNegativeScaleChildren
+
+-(id) init
+{
+ if( (self=[super init]) ) {
+
+ CGSize s = [[CCDirector sharedDirector] winSize];
+
+ CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
+ [cache addSpriteFramesWithFile:@"animations/grossini.plist"];
+ [cache addSpriteFramesWithFile:@"animations/grossini_gray.plist" textureFile:@"animations/grossini_gray.png"];
+
+ CCNode *parent = [CCNode node];
+ [self addChild:parent];
+
+ for(int i=0;i<2;i++) {
+ CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"grossini_dance_01.png"];
+ sprite.position = ccp( s.width/4*(i+1), s.height/2);
+
+ // Skew
+ id skewX = [CCSkewBy actionWithDuration:2 skewX:45 skewY:0];
+ id skewX_back = [skewX reverse];
+ id skewY = [CCSkewBy actionWithDuration:2 skewX:0 skewY:45];
+ id skewY_back = [skewY reverse];
+
+ if(i == 1)
+ {
+ [sprite setScale:-1.0f];
+ }
+
+ id seq_skew = [CCSequence actions:skewX, skewX_back, skewY, skewY_back, nil];
+ [sprite runAction:[CCRepeatForever actionWithAction:seq_skew]];
+
+ CCSprite *child1 = [CCSprite spriteWithSpriteFrameName:@"grossini_dance_01.png"];
+ [child1 setPosition: ccp(sprite.contentSize.width / 2.0f, sprite.contentSize.height / 2.0f)];
+
+ [sprite addChild: child1];
+
+ [child1 setScale:0.8f];
+
+ [parent addChild:sprite z:i];
+ }
+ }
+ return self;
+}
+
+- (void) dealloc
+{
+ CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
+ [cache removeSpriteFramesFromFile:@"animations/grossini.plist"];
+ [cache removeSpriteFramesFromFile:@"animations/grossini_gray.plist"];
+ [super dealloc];
+}
+
+-(NSString *) title
+{
+ return @"Sprite + children + skew";
+}
+
+-(NSString *) subtitle
+{
+ return @"Sprite skew + negative scale with children";
+}
+@end
+
#pragma mark -
#pragma mark SpriteNilTexture

0 comments on commit 029345a

Please sign in to comment.