Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // TextureCutoutShape.mm
- // DemoBros
- //
- // Created by et1337 on 6/29/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import "TextureCutoutShape.h"
- #import "CCNode+Extensions.h"
- @implementation TextureCutoutShape
- @synthesize vertexCount;
- @synthesize textureScale;
- -(CCTexture2D*) texture
- {
- return texture;
- }
- -(void) setTexture:(CCTexture2D*)tex
- {
- texture = tex;
- ccTexParams params = { GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT };
- [texture setTexParameters:¶ms];
- }
- -(id) init
- {
- NSAssert(false, @"Incorrect constructor for TextureCutoutShape.");
- return NULL;
- }
- -(id) initWithFile:(NSString*)file andVertexCount:(int)vertices
- {
- if ((self = [self initWithTexture:[[CCTextureCache sharedTextureCache] addImage:file] andVertexCount:vertices]))
- {
- }
- return self;
- }
- -(id) initWithTexture:(CCTexture2D*)tex andVertexCount:(int)vertices
- {
- if ((self = [super init]))
- {
- self.texture = tex;
- vertexCount = vertices;
- points = (CGPoint*)malloc(sizeof(CGPoint) * vertexCount);
- coords = (CGPoint*)malloc(sizeof(CGPoint) * vertexCount);
- colors = (ccColor4B*)malloc(sizeof(ccColor4B) * vertexCount);
- self.color = ccc3(255, 255, 255);
- self.opacity = 255;
- textureScale = 0.0025f;
- }
- return self;
- }
- -(ccColor3B) color
- {
- return ccc3(colors[0].r, colors[0].g, colors[0].b);
- }
- -(void) setColor:(ccColor3B)color
- {
- for (int i = 0; i < vertexCount; i++)
- colors[i] = ccc4(color.r, color.g, color.b, colors[i].a);
- }
- -(GLubyte) opacity
- {
- return colors[0].a;
- }
- -(void) setOpacity:(GLubyte)opacity
- {
- for (int i = 0; i < vertexCount; i++)
- colors[i].a = opacity;
- }
- -(void) dealloc
- {
- [texture release];
- free(points);
- free(coords);
- free(colors);
- [super dealloc];
- }
- -(void) draw
- {
- #ifdef DEBUG
- [super draw];
- #endif
- for (int i = 0; i < vertexCount; i++)
- coords[i] = ccpMult([self.layer convertToNodeSpace:[self convertToWorldSpace:points[i]]], textureScale);
- glBindTexture(GL_TEXTURE_2D, texture.name);
- glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
- glTexCoordPointer(2, GL_FLOAT, 0, coords);
- glVertexPointer(2, GL_FLOAT, 0, points);
- glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount);
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment