Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions CCBlade.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -26,8 +26,9 @@
#import <Foundation/Foundation.h>
#import "cocos2d.h"

#define USE_LAGRANGE 1
#define USE_STL_LIST 0
#define USE_LAGRANGE 1
#define USE_STL_LIST 0
#define USE_UPDATE_FOR_POP 1

inline float fangle(CGPoint vect);
inline float lagrange1(CGPoint p1, CGPoint p2, float x);
Expand All @@ -36,21 +37,21 @@ inline void CGPointSet(CGPoint *v, float x, float y);
inline void f1(CGPoint p1, CGPoint p2, float d, CGPoint *o1, CGPoint *o2);

@interface CCBlade : CCNode {
NSMutableArray *path;
unsigned int pointLimit;
int count;
CGPoint *vertices;
CGPoint *coordinates;
BOOL reset;
CCTexture2D *_texture;
float width;
BOOL _finish;
BOOL _willPop;

float timeSinceLastPop;
float popTimeInterval;
}
@property (readonly)unsigned int pointLimit;
@property(retain) CCTexture2D *texture;
@property (readonly) unsigned int pointLimit;
@property(strong) CCTexture2D *texture;
@property(nonatomic) float width;
@property (nonatomic, assign) BOOL autoDim;
@property (nonatomic) BOOL autoDim;
@property(nonatomic,strong)NSMutableArray *path;

+ (id) bladeWithMaximumPoint:(int) limit;
- (id) initWithMaximumPoint:(int) limit;
Expand Down
161 changes: 100 additions & 61 deletions CCBlade.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -60,76 +60,80 @@ inline void CGPointSet(CGPoint *v, float x, float y){
}

@implementation CCBlade
@synthesize texture = _texture;
@synthesize pointLimit;
@synthesize width;
@synthesize autoDim;

+ (id) bladeWithMaximumPoint:(int) limit{
return [[[self alloc] initWithMaximumPoint:limit] autorelease];
return [[self alloc] initWithMaximumPoint:limit];
}

#define POP_TIME_INTERVAL 1./60.

- (id) initWithMaximumPoint:(int) limit{
self = [super init];
pointLimit = limit;
self.width = 5;

_pointLimit = limit;
_width = 5;

vertices = (CGPoint *)calloc(2*limit+5, sizeof(vertices[0]));
coordinates = (CGPoint *)calloc(2*limit+5, sizeof(coordinates[0]));

CGPointSet(coordinates+0, 0.00, 0.5);
reset = NO;

path = [[NSMutableArray alloc] init];

_path = [[NSMutableArray alloc] init];

#if USE_UPDATE_FOR_POP
popTimeInterval = POP_TIME_INTERVAL;

timeSinceLastPop = 0;
[self scheduleUpdateWithPriority:0];
#endif

self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTexture];

return self;
}

- (void) dealloc{
[_texture release];
free(vertices);
free(coordinates);

[path release];
[super dealloc];
}

- (void) populateVertices{
vertices[0] = [[path objectAtIndex:0] CGPointValue];
vertices[0] = [[_path objectAtIndex:0] CGPointValue];
CGPoint pre = vertices[0];

unsigned int i = 0;
CGPoint it = [[path objectAtIndex:1] CGPointValue];
float dd = width / [path count];
while (i < [path count] - 2){
f1(pre, it, width - i * dd , vertices+2*i+1, vertices+2*i+2);
CGPoint it = [[_path objectAtIndex:1] CGPointValue];
float dd = _width / [_path count];
while (i < [_path count] - 2){
f1(pre, it, _width - i * dd , vertices+2*i+1, vertices+2*i+2);
CGPointSet(coordinates+2*i+1, .5, 1.0);
CGPointSet(coordinates+2*i+2, .5, 0.0);

i++;
pre = it;

it = [[path objectAtIndex:i+1] CGPointValue];
it = [[_path objectAtIndex:i+1] CGPointValue];
}

CGPointSet(coordinates+1, 0.25, 1.0);
CGPointSet(coordinates+1, 0.25, 1.0);
CGPointSet(coordinates+2, 0.25, 0.0);

vertices[2*[path count]-3] = it;
CGPointSet(coordinates+2*[path count]-3, 0.75, 0.5);
vertices[2*[_path count]-3] = it;
CGPointSet(coordinates+2*[_path count]-3, 0.75, 0.5);
}

- (void) shift{
int index = 2 * pointLimit - 1;
int index = 2 * _pointLimit - 1;
for (int i = index; i > 3; i -= 2) {
vertices[i] = vertices[i-2];
vertices[i-1] = vertices[i-3];
}
}

- (void) setWidth:(float)width_{
width = width_ * CC_CONTENT_SCALE_FACTOR();
- (void) set_width:(float)newWidth{
_width = newWidth ;//* CC_CONTENT_SCALE_FACTOR();
}

#define DISTANCE_TO_INTERPOLATE 10
Expand All @@ -140,61 +144,58 @@ - (void) push:(CGPoint) v{
if (reset) {
return;
}
if (CC_CONTENT_SCALE_FACTOR() != 1.0f) {
v = ccpMult(v, CC_CONTENT_SCALE_FACTOR());
}


#if USE_LAGRANGE

if ([path count] == 0) {
[path insertObject:[NSValue valueWithCGPoint:v] atIndex:0];
if ([_path count] == 0) {
[_path insertObject:[NSValue valueWithCGPoint:v] atIndex:0];
return;
}

CGPoint first = [[path objectAtIndex:0] CGPointValue];
CGPoint first = [[_path objectAtIndex:0] CGPointValue];
if (ccpDistance(v, first) < DISTANCE_TO_INTERPOLATE) {
[path insertObject:[NSValue valueWithCGPoint:v] atIndex:0];
if ([path count] > pointLimit) {
[path removeLastObject];
[_path insertObject:[NSValue valueWithCGPoint:v] atIndex:0];
if ([_path count] > _pointLimit) {
[_path removeLastObject];
}
}else{
int num = ccpDistance(v, first) / DISTANCE_TO_INTERPOLATE;
CGPoint iv = ccpMult(ccpSub(v, first), (float)1./(num + 1));
for (int i = 1; i <= num + 1; i++) {
[path insertObject:[NSValue valueWithCGPoint:ccpAdd(first, ccpMult(iv, i))] atIndex:0];
[_path insertObject:[NSValue valueWithCGPoint:ccpAdd(first, ccpMult(iv, i))] atIndex:0];
}
while ([path count] > pointLimit) {
[path removeLastObject];
while ([_path count] > _pointLimit) {
[_path removeLastObject];
}
}
#else // !USE_LAGRANGE
path.push_front(v);
if (path.size() > pointLimit) {
path.pop_back();
_path.push_front(v);
if (_path.size() > pointLimit) {
_path.pop_back();
}
#endif // !USE_LAGRANGE


[self populateVertices];
}

- (void) pop:(int) n{
while ([path count] > 0 && n > 0) {
[path removeLastObject];
while ([_path count] > 0 && n > 0) {
[_path removeLastObject];
n--;
}

if ([path count] > 2) {
if ([_path count] > 2) {
[self populateVertices];
}
}

- (void) clear{
[path removeAllObjects];
[_path removeAllObjects];
reset = NO;
if (_finish)
[self removeFromParentAndCleanup:YES];
}
}

- (void) reset{
reset = TRUE;
Expand All @@ -204,28 +205,66 @@ - (void) dim:(BOOL) dim{
reset = dim;
}

- (void) update:(ccTime)dt {

timeSinceLastPop += dt;

float precision = 1./60.;
float roundedTimeSinceLastPop = precision * roundf(timeSinceLastPop/precision); // helps because fps flucuate around 1./60.

int numberOfPops = (int) (roundedTimeSinceLastPop/popTimeInterval) ;
timeSinceLastPop = timeSinceLastPop - numberOfPops * popTimeInterval;

for (int pop = 0; pop < numberOfPops; pop++) {

if ((reset && [_path count] > 0) || (self.autoDim && _willPop)) {
[self pop:1];
if ([_path count] < 3) {
[self clear];
if (_finish) {
return; // if we continue self will have been deallocated
}
}
}

}
}

- (void) draw{
if ((reset && [path count] > 0) || (self.autoDim && _willPop)) {

#if !USE_UPDATE_FOR_POP
if ((reset && [_path count] > 0) || (self.autoDim && _willPop)) {
[self pop:1];
if ([path count] < 3) {
if ([_path count] < 3) {
[self clear];
if (_finish) {
return; // if we continue self will have been deallocated
}
}
}
#endif

if(_path == nil)
return;

if ([path count] < 3) {
if ([_path count] < 3) {
return;
}

_willPop = YES;
CC_NODE_DRAW_SETUP();
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords);

ccGLBindTexture2D( [_texture name] );
ccGLBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);

glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, coordinates);


glDrawArrays(GL_TRIANGLE_STRIP, 0, 2*[_path count]-2);

glDisableClientState(GL_COLOR_ARRAY);
NSAssert(_texture, @"NO TEXTURE SET");

glBindTexture(GL_TEXTURE_2D, _texture.name);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 2*[path count]-2);
glEnableClientState(GL_COLOR_ARRAY);
CC_INCREMENT_GL_DRAWS(1);
}

- (void) finish
Expand Down
16 changes: 10 additions & 6 deletions Example/Classes/ExampleAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

#import <UIKit/UIKit.h>

@class RootViewController;

@interface ExampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
@interface ExampleAppDelegate : NSObject <UIApplicationDelegate, CCDirectorDelegate>
{
UIWindow *window_;
UINavigationController *navController_;

CCDirectorIOS *__unsafe_unretained director_; // weak ref
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, strong) UIWindow *window;
@property (readonly) UINavigationController *navController;
@property (unsafe_unretained, readonly) CCDirectorIOS *director;

@end

Loading