Skip to content

Commit

Permalink
Xcode templates fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoquesada committed Mar 16, 2011
1 parent 385472b commit badfe25
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 13 deletions.
13 changes: 12 additions & 1 deletion install-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ copy_xcode4_project_templates(){

echo done!

print_template_banner "Installing Xcode 4 chipmunk iOS template"
print_template_banner "Installing Xcode 4 Chipmunk iOS template"


LIBS_DIR="$DST_DIR""chipmunklib.xctemplate/libs/"
Expand All @@ -238,6 +238,17 @@ copy_xcode4_project_templates(){
copy_files external/Chipmunk "$LIBS_DIR"

echo done!

print_template_banner "Installing Xcode 4 Box2d iOS template"


LIBS_DIR="$DST_DIR""box2dlib.xctemplate/libs/"
mkdir -p "$LIBS_DIR"

echo ...copying Box2d files
copy_files external/Box2d/Box2D "$LIBS_DIR"

echo done!
}

# copy Xcode4 templates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"

// Importing Chipmunk headers
#import "chipmunk.h"

// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
{
cpSpace *space;
}

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
-(void) step: (ccTime) dt;
-(void) addNewSpriteX:(float)x y:(float)y;

@end
164 changes: 152 additions & 12 deletions templates/Xcode4_templates/chipmunk.xctemplate/HelloWorldLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@
// Import the interfaces
#import "HelloWorldLayer.h"

enum {
kTagBatchNode = 1,
};

static void
eachShape(void *ptr, void* unused)
{
cpShape *shape = (cpShape*) ptr;
CCSprite *sprite = shape->data;
if( sprite ) {
cpBody *body = shape->body;

// TIP: cocos2d and chipmunk uses the same struct to store it's position
// chipmunk uses: cpVect, and cocos2d uses CGPoint but in reality the are the same
// since v0.7.1 you can mix them if you want.
[sprite setPosition: body->p];

[sprite setRotation: (float) CC_RADIANS_TO_DEGREES( -body->a )];
}
}

// HelloWorldLayer implementation
@implementation HelloWorldLayer

Expand All @@ -28,24 +49,95 @@ +(CCScene *) scene
return scene;
}

-(void) addNewSpriteX: (float)x y:(float)y
{
int posx, posy;

CCSpriteBatchNode *batch = (CCSpriteBatchNode*) [self getChildByTag:kTagBatchNode];

posx = (CCRANDOM_0_1() * 200);
posy = (CCRANDOM_0_1() * 200);

posx = (posx % 4) * 85;
posy = (posy % 3) * 121;

CCSprite *sprite = [CCSprite spriteWithBatchNode:batch rect:CGRectMake(posx, posy, 85, 121)];
[batch addChild: sprite];

sprite.position = ccp(x,y);

int num = 4;
CGPoint verts[] = {
ccp(-24,-54),
ccp(-24, 54),
ccp( 24, 54),
ccp( 24,-54),
};

cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, CGPointZero));

// TIP:
// since v0.7.1 you can assign CGPoint to chipmunk instead of cpVect.
// cpVect == CGPoint
body->p = ccp(x, y);
cpSpaceAddBody(space, body);

cpShape* shape = cpPolyShapeNew(body, num, verts, CGPointZero);
shape->e = 0.5f; shape->u = 0.5f;
shape->data = sprite;
cpSpaceAddShape(space, shape);

}

// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {

// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];

// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
self.isTouchEnabled = YES;
self.isAccelerometerEnabled = YES;

CGSize wins = [[CCDirector sharedDirector] winSize];
cpInitChipmunk();

cpBody *staticBody = cpBodyNew(INFINITY, INFINITY);
space = cpSpaceNew();
cpSpaceResizeStaticHash(space, 400.0f, 40);
cpSpaceResizeActiveHash(space, 100, 600);

space->gravity = ccp(0, 0);
space->elasticIterations = space->iterations;

cpShape *shape;

// bottom
shape = cpSegmentShapeNew(staticBody, ccp(0,0), ccp(wins.width,0), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);

// top
shape = cpSegmentShapeNew(staticBody, ccp(0,wins.height), ccp(wins.width,wins.height), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);

// add the label as a child to this Layer
[self addChild: label];
// left
shape = cpSegmentShapeNew(staticBody, ccp(0,0), ccp(0,wins.height), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);

// right
shape = cpSegmentShapeNew(staticBody, ccp(wins.width,0), ccp(wins.width,wins.height), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);

CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"grossini_dance_atlas.png" capacity:100];
[self addChild:batch z:0 tag:kTagBatchNode];

[self addNewSpriteX: 200 y:200];

[self schedule: @selector(step:)];
}
return self;
}
Expand All @@ -54,10 +146,58 @@ -(id) init
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
cpSpaceFree(space);
space = NULL;

// don't forget to call "super dealloc"
[super dealloc];
}

-(void) onEnter
{
[super onEnter];

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)];
}

-(void) step: (ccTime) delta
{
int steps = 2;
CGFloat dt = delta/(CGFloat)steps;

for(int i=0; i<steps; i++){
cpSpaceStep(space, dt);
}
cpSpaceHashEach(space->activeShapes, &eachShape, nil);
cpSpaceHashEach(space->staticShapes, &eachShape, nil);
}


- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];

location = [[CCDirector sharedDirector] convertToGL: location];

[self addNewSpriteX: location.x y:location.y];
}
}

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
static float prevX=0, prevY=0;

#define kFilterFactor 0.05f

float accelX = (float) acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
float accelY = (float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;

prevX = accelX;
prevY = accelY;

CGPoint v = ccp( accelX, accelY);

space->gravity = ccpMult(v, 200);
}
@end
Binary file not shown.

0 comments on commit badfe25

Please sign in to comment.