Skip to content

Commit

Permalink
Basic cloth node
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkemose committed Dec 2, 2014
1 parent cdb3a44 commit 55199a1
Show file tree
Hide file tree
Showing 13 changed files with 473 additions and 27 deletions.
1 change: 1 addition & 0 deletions Extensions/CCLayerSmoothLine/CCLayerSmoothLine.m
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ - (NSMutableArray *)calculateSmoothLinePoints
finalPoint.width = (cur.width + prev1.width) * 0.5f;
[smoothedPoints addObject:finalPoint];
}

//! we need to leave last 2 points for next draw
[_points removeObjectsInRange:NSMakeRange(0, [_points count] - 2)];
return smoothedPoints;
Expand Down
8 changes: 8 additions & 0 deletions Extensions/CCSpriteGrid/CCSpriteGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
// -----------------------------------------------------------------------

@property (nonatomic, readonly) CCVertex *vertex;
@property (nonatomic, readonly) NSUInteger gridWidth;
@property (nonatomic, readonly) NSUInteger gridHeight;
@property (nonatomic, readonly) NSUInteger vertexCount;
@property (nonatomic, readonly) BOOL dirty;

Expand All @@ -56,6 +58,12 @@
- (void)resetTextureCoordinate:(NSUInteger)index;
- (void)resetColor:(NSUInteger)index;

- (float)vertexDistance:(NSUInteger)indexA indexB:(NSUInteger)indexB;

- (void)lock:(NSUInteger)index;
- (void)unLock:(NSUInteger)index;
- (BOOL)isLocked:(NSUInteger)index;

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

@end
Expand Down
57 changes: 53 additions & 4 deletions Extensions/CCSpriteGrid/CCSpriteGrid.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@

@implementation CCSpriteGrid
{
NSUInteger _gridWidth;
NSUInteger _gridHeight;
CCVertex *_originalVertex;
BOOL *_vertexIsEdge;
BOOL *_vertexIsLocked;
CGPoint _textureOffset;
CGSize _textureSize;
}
Expand All @@ -56,6 +55,7 @@ - (instancetype)init
_vertex = NULL;
_originalVertex = NULL;
_vertexIsEdge = NULL;
_vertexIsLocked = NULL;
_dirty = NO;

// done
Expand Down Expand Up @@ -87,9 +87,13 @@ - (void)draw:(CCRenderer *)renderer transform:(const GLKMatrix4 *)transform
andVertexes:vertexCount
withState:self.renderState
globalSortOrder:0];

// transform vertices
for (NSUInteger index = 0; index < vertexCount; index ++)
{
_vertex[index].color.a = self.opacity;
CCRenderBufferSetVertex(buffer, (int)index, CCVertexApplyTransform(_vertex[index], transform));
}

// set up triangles
NSUInteger triangleIndex = 0;
Expand All @@ -115,6 +119,7 @@ - (void)cleanUp
free(_vertex);
free(_originalVertex);
free(_vertexIsEdge);
free(_vertexIsLocked);
_vertex = NULL;
_originalVertex = NULL;
_vertexIsEdge = NULL;
Expand Down Expand Up @@ -144,6 +149,7 @@ - (void)setGridWidth:(NSUInteger)width andHeight:(NSUInteger)height
_vertex = malloc(_vertexCount * sizeof(CCVertex));
_originalVertex = malloc(_vertexCount * sizeof(CCVertex));
_vertexIsEdge = malloc(_vertexCount * sizeof(BOOL));
_vertexIsLocked = malloc(_vertexCount * sizeof(BOOL));
[self createGrid];
[self resetGrid];
}
Expand All @@ -170,7 +176,7 @@ - (void)createGrid
for (NSUInteger width = 0; width <= _gridWidth; width ++)
{
float progressWidth = (float)width / (float)_gridWidth;
float progressHeight = (float)height / (float)_gridHeight;
float progressHeight = 1.0f - ((float)height / (float)_gridHeight);

_originalVertex[vertexIndex].position = (GLKVector4)
{
Expand All @@ -190,6 +196,8 @@ - (void)createGrid

_vertexIsEdge[vertexIndex] = (height == 0) || (height == _gridHeight) || (width == 0) || (width == _gridWidth);

_vertexIsLocked[vertexIndex] = NO;

vertexIndex ++;
}
}
Expand Down Expand Up @@ -242,6 +250,7 @@ - (CCColor *)color:(NSUInteger)index
- (void)adjustVertex:(NSUInteger)index adjustment:(CGPoint)adjustment
{
NSAssert(index < _vertexCount, @"Invalid vertex index");
if (_vertexIsLocked[index]) return;
_vertex[index].position.x = _vertex[index].position.x + adjustment.x;
_vertex[index].position.y = _vertex[index].position.y + adjustment.y;
_dirty = YES;
Expand All @@ -250,14 +259,20 @@ - (void)adjustVertex:(NSUInteger)index adjustment:(CGPoint)adjustment
- (void)adjustTextureCoordinate:(NSUInteger)index adjustment:(CGPoint)adjustment
{
NSAssert(index < _vertexCount, @"Invalid vertex index");
if (_vertexIsLocked[index]) return;
_vertex[index].texCoord1.v[0] = _vertex[index].texCoord1.v[0] + (adjustment.x / _textureSize.width);
_vertex[index].texCoord1.v[1] = _vertex[index].texCoord1.v[1] + (adjustment.y / _textureSize.height);
_dirty = YES;
}

- (void)adjustColor:(NSUInteger)index adjustment:(CCColor *)adjustment
{

NSAssert(index < _vertexCount, @"Invalid vertex index");
if (_vertexIsLocked[index]) return;
_vertex[index].color.r += adjustment.red;
_vertex[index].color.g += adjustment.green;
_vertex[index].color.b += adjustment.blue;
_dirty = YES;
}

// -----------------------------------------------------------------------
Expand All @@ -283,4 +298,38 @@ - (void)resetColor:(NSUInteger)index

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

- (float)vertexDistance:(NSUInteger)indexA indexB:(NSUInteger)indexB
{
NSAssert(indexA < _vertexCount, @"Invalid vertex index");
NSAssert(indexB < _vertexCount, @"Invalid vertex index");
return ccpDistance((CGPoint){_vertex[indexA].position.x, _vertex[indexA].position.y},
(CGPoint){_vertex[indexB].position.x, _vertex[indexB].position.y});
}

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

- (void)lock:(NSUInteger)index
{
NSAssert(index < _vertexCount, @"Invalid vertex index");
_vertexIsLocked[index] = YES;
}

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

- (void)unLock:(NSUInteger)index
{
NSAssert(index < _vertexCount, @"Invalid vertex index");
_vertexIsLocked[index] = NO;
}

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

- (BOOL)isLocked:(NSUInteger)index
{
NSAssert(index < _vertexCount, @"Invalid vertex index");
return _vertexIsLocked[index];
}

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

@end
16 changes: 14 additions & 2 deletions cocos2d-iphone-ext.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
/* Begin PBXBuildFile section */
0E21D30C198A484600E84AB1 /* CCSpriteMultiTouch.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E21D30B198A484600E84AB1 /* CCSpriteMultiTouch.m */; };
0E21D30F198A637900E84AB1 /* CCSpriteMultiTouchTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E21D30E198A637900E84AB1 /* CCSpriteMultiTouchTest.m */; };
0E2A746719D311230054F5B6 /* clothRope.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E2A746619D311230054F5B6 /* clothRope.m */; };
0E57EA3B19C6CF9B008CBC9E /* CCSpriteGrid.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E57EA3A19C6CF9B008CBC9E /* CCSpriteGrid.m */; };
0E57EA3E19C6D272008CBC9E /* CCSpriteGridTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E57EA3D19C6D272008CBC9E /* CCSpriteGridTest.m */; };
0E57EA4119C6D5D4008CBC9E /* pebbles.png in Resources */ = {isa = PBXBuildFile; fileRef = 0E57EA4019C6D5D4008CBC9E /* pebbles.png */; };
0E99598B19C73B0500A02D2B /* rippleNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E99598A19C73B0500A02D2B /* rippleNode.m */; };
0E9A092A199CDDD10049E409 /* CCLayerSmoothLineTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9A0929199CDDD10049E409 /* CCLayerSmoothLineTest.m */; };
0ECEB48819D29EB100BDBB9E /* clothNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ECEB48719D29EB100BDBB9E /* clothNode.m */; };
0ED73BB51997951A00C83507 /* CCSpineCurve.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ED73BB41997951A00C83507 /* CCSpineCurve.m */; };
0EF602FD199CDC8F007AEBD9 /* CCLayerSmoothLine.m in Sources */ = {isa = PBXBuildFile; fileRef = 0EF602FC199CDC8F007AEBD9 /* CCLayerSmoothLine.m */; };
A6270E62193757E400196BD3 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A6270E61193757E400196BD3 /* QuartzCore.framework */; };
Expand Down Expand Up @@ -130,14 +132,18 @@
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>"; };
0E2A746519D311230054F5B6 /* clothRope.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = clothRope.h; path = "Classes/Tests/Test helper classes/clothRope.h"; sourceTree = "<group>"; };
0E2A746619D311230054F5B6 /* clothRope.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = clothRope.m; path = "Classes/Tests/Test helper classes/clothRope.m"; sourceTree = "<group>"; };
0E57EA3719C6CF83008CBC9E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
0E57EA3919C6CF9B008CBC9E /* CCSpriteGrid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCSpriteGrid.h; path = ../Extensions/CCSpriteGrid/CCSpriteGrid.h; sourceTree = "<group>"; };
0E57EA3919C6CF9B008CBC9E /* CCSpriteGrid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCSpriteGrid.h; sourceTree = "<group>"; };
0E57EA3A19C6CF9B008CBC9E /* CCSpriteGrid.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCSpriteGrid.m; sourceTree = "<group>"; };
0E57EA3D19C6D272008CBC9E /* CCSpriteGridTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpriteGridTest.m; path = Classes/Tests/CCSpriteGridTest.m; sourceTree = "<group>"; };
0E57EA4019C6D5D4008CBC9E /* pebbles.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pebbles.png; sourceTree = "<group>"; };
0E99598919C73B0500A02D2B /* rippleNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = rippleNode.h; path = "Classes/Tests/Test helper classes/rippleNode.h"; sourceTree = "<group>"; };
0E99598A19C73B0500A02D2B /* rippleNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = rippleNode.m; path = "Classes/Tests/Test helper classes/rippleNode.m"; sourceTree = "<group>"; };
0E9A0929199CDDD10049E409 /* CCLayerSmoothLineTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCLayerSmoothLineTest.m; path = Classes/Tests/CCLayerSmoothLineTest.m; sourceTree = "<group>"; };
0ECEB48619D29EB100BDBB9E /* clothNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = clothNode.h; path = "Classes/Tests/Test helper classes/clothNode.h"; sourceTree = "<group>"; };
0ECEB48719D29EB100BDBB9E /* clothNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = clothNode.m; path = "Classes/Tests/Test helper classes/clothNode.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; };
0EF602FB199CDC8F007AEBD9 /* CCLayerSmoothLine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCLayerSmoothLine.h; path = Extensions/CCLayerSmoothLine/CCLayerSmoothLine.h; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -298,6 +304,7 @@
isa = PBXGroup;
children = (
0E57EA3719C6CF83008CBC9E /* README.md */,
0E57EA3919C6CF9B008CBC9E /* CCSpriteGrid.h */,
0E57EA3A19C6CF9B008CBC9E /* CCSpriteGrid.m */,
);
name = CCSpriteGrid;
Expand Down Expand Up @@ -508,7 +515,6 @@
A66C5808193A0C24009D2BD4 /* Extensions */ = {
isa = PBXGroup;
children = (
0E57EA3919C6CF9B008CBC9E /* CCSpriteGrid.h */,
0E57EA3619C6CF83008CBC9E /* CCSpriteGrid */,
0EF602F9199CDC4A007AEBD9 /* CCLayerSmoothLine */,
0E21D303198A45F800E84AB1 /* CCSpriteMultiTouch */,
Expand Down Expand Up @@ -610,6 +616,10 @@
A6994F89194A2FBB00B97481 /* spineNode.m */,
0E99598919C73B0500A02D2B /* rippleNode.h */,
0E99598A19C73B0500A02D2B /* rippleNode.m */,
0ECEB48619D29EB100BDBB9E /* clothNode.h */,
0ECEB48719D29EB100BDBB9E /* clothNode.m */,
0E2A746519D311230054F5B6 /* clothRope.h */,
0E2A746619D311230054F5B6 /* clothRope.m */,
);
name = "Test helper classes";
sourceTree = "<group>";
Expand Down Expand Up @@ -789,8 +799,10 @@
A66C5830193A0C87009D2BD4 /* CCSpineAnimation.m in Sources */,
0EF602FD199CDC8F007AEBD9 /* CCLayerSmoothLine.m in Sources */,
A66C5835193A0C87009D2BD4 /* CCSpineSkeleton.m in Sources */,
0E2A746719D311230054F5B6 /* clothRope.m in Sources */,
A66C5836193A0C87009D2BD4 /* CCSpineSkin.m in Sources */,
0E9A092A199CDDD10049E409 /* CCLayerSmoothLineTest.m in Sources */,
0ECEB48819D29EB100BDBB9E /* clothNode.m in Sources */,
A6271107193757E600196BD3 /* main.m in Sources */,
0E21D30C198A484600E84AB1 /* CCSpriteMultiTouch.m in Sources */,
A66C5837193A0C87009D2BD4 /* CCSpineSprite.m in Sources */,
Expand Down
Binary file not shown.
10 changes: 8 additions & 2 deletions cocos2d-iphone-ext/Classes/Tests/CCCropNodeTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ - (void)cropNodeTest
_card.scale = 2;
[_transformation addChild:_card];



CCButton *toggle = [CCButton buttonWithTitle:@""
spriteFrame:[CCSpriteFrame frameWithImageNamed:[cardNode randomCardName]]
highlightedSpriteFrame:[CCSpriteFrame frameWithImageNamed:[cardNode randomCardName]]
disabledSpriteFrame:[CCSpriteFrame frameWithImageNamed:[cardNode randomCardName]]];
toggle.positionType = CCPositionTypeNormalized;
toggle.position = ccp(0.2, 0.75);
toggle.togglesSelectedState = YES;
[self.contentNode addChild:toggle];


}
Expand Down
Loading

0 comments on commit 55199a1

Please sign in to comment.