Skip to content

Instantly share code, notes, and snippets.

@pixelrevision
Created April 10, 2011 07:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelrevision/912119 to your computer and use it in GitHub Desktop.
Save pixelrevision/912119 to your computer and use it in GitHub Desktop.
SXPointSpriteGroup.h
#import <Foundation/Foundation.h>
#import "Sparrow.h"
typedef struct{
float x;
float y;
float size;
} SXPSData;
@interface SXPointSpriteGroup : SPSprite{
SPTexture *texture;
BOOL processing;
int numPointSprites;
SXPSData *pointSprites;
BOOL initialized;
}
@property (nonatomic, retain) SPTexture *texture;
- (id)initWithTexture:(SPTexture*)t andNumSprites:(int)amount;
- (void)start;
- (void)stop;
- (void)reset;
- (void)update:(SPEnterFrameEvent*)event;
@end
#import "SXPointSpriteGroup.h"
@implementation SXPointSpriteGroup
@synthesize texture;
- (id)initWithTexture:(SPTexture*)t andNumSprites:(int)amount{
self = [super init];
numPointSprites = amount;
self.texture = t;
[self reset];
return self;
}
- (void)start{
if(!processing){
[self addEventListener:@selector(update:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
processing = YES;
}
}
- (void)stop{
if(processing){
[self removeEventListener:@selector(update:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
processing = NO;
}
}
- (void)reset{
if(pointSprites != NULL){
free(pointSprites);
}
pointSprites = malloc(numPointSprites * sizeof(SXPSData));
for(int i=0; i<numPointSprites; i++){
SXPSData *ps = &pointSprites[i];
ps->x = 0;
ps->y = 0;
ps->size = 10; // TODO: NEED TO FIGURE THIS OUT
}
initialized = YES;
// override when resets to do things like set start position
}
- (void)update:(SPEnterFrameEvent*)event{
// override and process your pointSprites before they get rendered
// event.passedTime can be very hand here for accuracy.
}
- (void)render:(SPRenderSupport *)support{
[super render:support];
if(!processing) return;
if(!texture) return;
if(!initialized) [self reset];
[support bindTexture:texture]; // the class has a texture
glEnable(GL_POINT_SPRITE_OES);
glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
GLubyte alpha = self.alpha * 255;
if (support.usingPremultipliedAlpha) glColor4ub(alpha, alpha, alpha, alpha);
else glColor4ub(255, 255, 255, alpha);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, sizeof(SXPSData), pointSprites);
glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
glPointSizePointerOES(GL_FLOAT, sizeof(SXPSData), (void*)pointSprites +
offsetof(SXPSData, size));
glDrawArrays(GL_POINTS, 0, numPointSprites);
glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_POINT_SPRITE_OES);
}
- (void)dealloc{
[self stop];
self.texture = nil;
if(pointSprites != NULL){
free(pointSprites);
}
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment