Skip to content

Instantly share code, notes, and snippets.

@rolandoam
Created December 14, 2009 16:22
Show Gist options
  • Save rolandoam/256168 to your computer and use it in GitHub Desktop.
Save rolandoam/256168 to your computer and use it in GitHub Desktop.
GFFParallaxNode
//
// GFFParallaxNode.h
//
// Created by Rolando Abarca on 12/14/09.
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#define MAX_PARALLAX_CHILDREN 25
@interface GFFParallaxNode : CocosNode {
float ratios[MAX_PARALLAX_CHILDREN];
int childrenNo;
}
- (void)addChild:(CocosNode *)child z:(int)z parallaxRatio:(float)ratio;
- (void)scroll:(float)offset;
@end
//
// GFFParallaxNode.m
//
// Created by Rolando Abarca on 12/14/09.
//
#import "GFFParallaxNode.h"
#import "RepeatableLayer.h"
@implementation GFFParallaxNode
- (id)init {
if ((self = [super init])) {
childrenNo = 0;
}
return self;
}
- (void)addChild:(RepeatableLayer *)child z:(int)z parallaxRatio:(float)ratio {
NSAssert(childrenNo < MAX_PARALLAX_CHILDREN, @"Reached max of parallax children!");
ratios[childrenNo++] = ratio;
[super addChild:child z:z];
}
- (void)scroll:(float)offset {
int idx = 0;
for (RepeatableLayer *child in children) {
[child scroll:offset * ratios[idx++]];
}
}
- (void)scrollTest {
[self scroll:0.3f];
}
@end
//
// RepeatableLayer.h
//
// Created by Rolando Abarca on 12/13/09.
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface RepeatableLayer : Sprite {
CGPoint texOffset;
CGPoint firstOffset;
}
+ (id)layerWithFile:(NSString *)file;
- (void)scrollTest;
- (void)scroll:(float)offset;
@end
//
// RepeatableLayer.mm
//
// Created by Rolando Abarca on 12/13/09.
//
#import "RepeatableLayer.h"
@implementation RepeatableLayer
+ (id)layerWithFile:(NSString *)file {
RepeatableLayer *layer = [RepeatableLayer spriteWithFile:file];
layer.anchorPoint = CGPointZero;
ccTexParams params = { GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT };
[layer.texture setTexParameters:&params];
return layer;
}
- (void)draw {
glEnableClientState( GL_VERTEX_ARRAY);
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glEnable( GL_TEXTURE_2D);
glColor4ub( color_.r, color_.g, color_.b, opacity_);
//Adjust the texture matrix
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
//We are just doing horizontal scrolling here so only adjusting x
glTranslatef(texOffset.x/self.contentSize.width, 0, 0);
//Draw the texture
[texture_ drawAtPoint:CGPointZero];
//Restore texture matrix and switch back to modelview matrix
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glColor4ub( 255, 255, 255, 255);
glDisable( GL_TEXTURE_2D);
glDisableClientState(GL_VERTEX_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
}
//
// adapted from
// http://www.codza.com/making-seamless-repeating-backgrounds-photoshop-cocos2d-iphone
//
// it only scrolls on x-axis
//
- (void)scroll:(float)offset {
texOffset = ccpAdd(texOffset, CGPointMake(offset, 0.0f));
CGSize contentSize = texture_.contentSize;
if (texOffset.x > contentSize.width) texOffset.x -= contentSize.width;
if (texOffset.y > contentSize.height) texOffset.y -= contentSize.height;
if (texOffset.x < 0) texOffset.x += contentSize.width;
if (texOffset.y < 0) texOffset.y += contentSize.height;
}
- (void)scrollTest {
[self scroll:0.3f];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment