Created
August 30, 2013 13:27
-
-
Save Shilo/6389843 to your computer and use it in GitHub Desktop.
A Sparrow 2.X extension for polygon-shaped images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SHPolygon.h | |
// Sparrow 2.X | |
// | |
// Created by Shilo White on 7/28/13. | |
// | |
// Highly based on the following Sparrow extensions created by Daniel Sperl @ Gamua: | |
// Polygon: https://gist.github.com/PrimaryFeather/5352978 | |
// TexturedPolygon: https://gist.github.com/PrimaryFeather/5679832 | |
// | |
// Hit test code based on the following Starling extension, created by RomainThery: | |
// Polygon.as: https://gist.github.com/RomainThery/5366504/ | |
// | |
#import "SPDisplayObject.h" | |
@class SPBaseEffect; | |
@class SPVertexData; | |
@interface SHPolygon : SPDisplayObject | |
{ | |
SPBaseEffect *_baseEffect; | |
SPVertexData *_vertexData; | |
uint _vertexBufferName; | |
uint _indexBufferName; | |
ushort *_indexData; | |
} | |
- (id)initWithRadius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma; | |
- (id)initWithRadius:(float)radius numEdges:(uint)numEdges color:(uint)color; | |
- (id)initWithRadius:(float)radius numEdges:(uint)numEdges; | |
- (void)setColor:(uint)color ofVertex:(uint)vertexID; | |
- (uint)colorOfVertex:(uint)vertexID; | |
- (void)setAlpha:(float)alpha ofVertex:(uint)vertexID; | |
- (float)alphaOfVertex:(uint)vertexID; | |
- (void)update; | |
- (void)vertexDataDidChange; | |
+ (id)polygonWithRadius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma; | |
+ (id)polygonWithRadius:(float)radius numEdges:(uint)numEdges color:(uint)color; | |
+ (id)polygonWithRadius:(float)radius numEdges:(uint)numEdges; | |
+ (id)polygon; | |
@property (nonatomic, assign) float radius; | |
@property (nonatomic, assign) uint numEdges; | |
@property (nonatomic, assign) uint numDrawnEdges; | |
@property (nonatomic, assign) uint color; | |
@property (nonatomic, assign) BOOL requiresUpdate; | |
@property (nonatomic, assign) BOOL premultipliedAlpha; | |
@property (nonatomic, readonly) BOOL tinted; | |
@property (nonatomic, readonly) SPTexture *texture; | |
@property (nonatomic, readonly) uint centerVertexIndex; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SHPolygon.m | |
// Sparrow 2.X | |
// | |
// Created by Shilo White on 7/28/13. | |
// | |
// Highly based on the following Sparrow extensions created by Daniel Sperl @ Gamua: | |
// Polygon: https://gist.github.com/PrimaryFeather/5352978 | |
// TexturedPolygon: https://gist.github.com/PrimaryFeather/5679832 | |
// | |
// Hit test code based on the following Starling extension, created by RomainThery: | |
// Polygon.as: https://gist.github.com/RomainThery/5366504/ | |
// | |
#import "SHPolygon.h" | |
#import "SPBaseEffect.h" | |
#import "SPVertexData.h" | |
#import "SPPoint.h" | |
#import "SPMacros.h" | |
#import "SPRenderSupport.h" | |
#import "SPBlendMode.h" | |
#import "SPRectangle.h" | |
#import "SPMatrix.h" | |
@implementation SHPolygon | |
{ | |
float _radius; | |
uint _numDrawnEdges; | |
BOOL _requiresUpdate; | |
BOOL _tinted; | |
} | |
@synthesize radius = _radius; | |
@synthesize requiresUpdate = _requiresUpdate; | |
@synthesize numDrawnEdges = _numDrawnEdges; | |
- (id)initWithRadius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma | |
{ | |
if ((self = [super init])) | |
{ | |
_radius = radius; | |
_tinted = color != 0xffffff; | |
_numDrawnEdges = MAX(3, numEdges); | |
_baseEffect = [[SPBaseEffect alloc] init]; | |
_vertexData = [[SPVertexData alloc] initWithSize:_numDrawnEdges+1 premultipliedAlpha:pma]; | |
for (int i=0; i<_vertexData.numVertices; ++i) | |
_vertexData.vertices[i].color = SPVertexColorMakeWithColorAndAlpha(color, 1.0f); | |
_requiresUpdate = YES; | |
[self vertexDataDidChange]; | |
} | |
return self; | |
} | |
- (id)initWithRadius:(float)radius numEdges:(uint)numEdges color:(uint)color | |
{ | |
return [self initWithRadius:radius numEdges:numEdges color:color premultipliedAlpha:YES]; | |
} | |
- (id)initWithRadius:(float)radius numEdges:(uint)numEdges | |
{ | |
return [self initWithRadius:radius numEdges:numEdges color:SP_WHITE]; | |
} | |
- (id)init | |
{ | |
return [self initWithRadius:32.0f numEdges:5]; | |
} | |
- (void)dealloc | |
{ | |
free(_indexData); | |
} | |
- (void)update | |
{ | |
[self setupVertices]; | |
[self createBuffers]; | |
_requiresUpdate = NO; | |
} | |
- (void)setupVertices | |
{ | |
// set up vertex data | |
uint numEdges = _vertexData.numVertices-1; | |
for (int i=0; i<numEdges; ++i) | |
{ | |
float polarLength = (i<_numDrawnEdges)?_radius:0; | |
SPPoint *edge = [[SPPoint alloc] initWithPolarLength:polarLength angle:i*2*PI / numEdges]; | |
[_vertexData setPosition:edge atIndex:i]; | |
} | |
[_vertexData setPositionWithX:0.0f y:0.0f atIndex:numEdges]; // center vertex | |
// set up index data | |
int numIndices = numEdges * 3; | |
if (!_indexData) _indexData = malloc(sizeof(ushort) * numIndices); | |
else _indexData = realloc(_indexData, sizeof(ushort) * numIndices); | |
for (int i=0; i<numEdges; ++i) | |
{ | |
_indexData[i*3] = numEdges; | |
_indexData[i*3+1] = i; | |
_indexData[i*3+2] = (i+1) % numEdges; | |
} | |
} | |
- (void)createBuffers | |
{ | |
int numIndices = (_vertexData.numVertices - 1) * 3; | |
if (_vertexBufferName) glDeleteBuffers(1, &_vertexBufferName); | |
if (_indexBufferName) glDeleteBuffers(1, &_indexBufferName); | |
glGenBuffers(1, &_vertexBufferName); | |
glGenBuffers(1, &_indexBufferName); | |
if (!_vertexBufferName || !_indexBufferName) | |
[NSException raise:SP_EXC_OPERATION_FAILED format:@"could not create vertex buffers"]; | |
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferName); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(SPVertex) * _vertexData.numVertices, _vertexData.vertices, | |
GL_STATIC_DRAW); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferName); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ushort) * numIndices, _indexData, GL_STATIC_DRAW); | |
} | |
- (void)render:(SPRenderSupport *)support | |
{ | |
if (_requiresUpdate) [self update]; | |
[support finishQuadBatch]; // finish previously batched quads | |
[support addDrawCalls:1]; // update stats display | |
_baseEffect.mvpMatrix = support.mvpMatrix; | |
_baseEffect.alpha = support.alpha; | |
[_baseEffect prepareToDraw]; | |
[SPBlendMode applyBlendFactorsForBlendMode:support.blendMode | |
premultipliedAlpha:_vertexData.premultipliedAlpha]; | |
int attribPosition = _baseEffect.attribPosition; | |
int attribColor = _baseEffect.attribColor; | |
glEnableVertexAttribArray(attribPosition); | |
glEnableVertexAttribArray(attribColor); | |
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferName); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferName); | |
glVertexAttribPointer(attribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(SPVertex), | |
(void *)(offsetof(SPVertex, position))); | |
glVertexAttribPointer(attribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(SPVertex), | |
(void *)(offsetof(SPVertex, color))); | |
int numIndices = (_vertexData.numVertices - 1) * 3; | |
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0); | |
} | |
- (SPRectangle*)boundsInSpace:(SPDisplayObject*)targetSpace | |
{ | |
if (_requiresUpdate) [self update]; | |
if (!targetSpace) targetSpace = self; | |
SPMatrix *matrix = [self transformationMatrixToSpace:targetSpace]; | |
return [_vertexData boundsAfterTransformation:matrix]; | |
} | |
- (SPDisplayObject *)hitTestPoint:(SPPoint *)localPoint | |
{ | |
if ([super hitTestPoint:localPoint]) | |
{ | |
int i; | |
int j; | |
BOOL hit = NO; | |
for (i=0, j=_vertexData.numVertices-2; i<_vertexData.numVertices-1; j=i++) | |
{ | |
SPPoint *point1 = [_vertexData positionAtIndex:i]; | |
SPPoint *point2 = [_vertexData positionAtIndex:j]; | |
if (((point1.y > localPoint.y) != (point2.y > localPoint.y)) && (localPoint.x < (point2.x - point1.x) * (localPoint.y - point1.y) / (point2.y - point1.y) + point1.x)) | |
hit = !hit; | |
} | |
if (hit) return self; | |
else return nil; | |
} | |
else return nil; | |
} | |
- (void)setNumEdges:(uint)numEdges | |
{ | |
if (numEdges != _vertexData.numVertices-1) | |
{ | |
uint baseColor = self.color; | |
uint lastNumVertices = _vertexData.numVertices; | |
_numDrawnEdges = numEdges; | |
_vertexData.numVertices = numEdges + 1; | |
for (int i=lastNumVertices; i<_vertexData.numVertices; ++i) | |
_vertexData.vertices[i].color = SPVertexColorMakeWithColorAndAlpha(baseColor, 1.0f); | |
_requiresUpdate = YES; | |
} | |
} | |
- (uint)numEdges | |
{ | |
return _vertexData.numVertices-1; | |
} | |
- (void)setNumDrawnEdges:(uint)numDrawnEdges | |
{ | |
if (numDrawnEdges != _numDrawnEdges) | |
{ | |
_numDrawnEdges = MAX(3, MIN(numDrawnEdges, _vertexData.numVertices-1)); | |
_requiresUpdate = YES; | |
} | |
} | |
- (void)setRadius:(float)radius | |
{ | |
if (radius != _radius) | |
{ | |
_radius = radius; | |
_requiresUpdate = YES; | |
} | |
} | |
- (void)setColor:(uint)color ofVertex:(uint)vertexID | |
{ | |
[_vertexData setColor:color atIndex:vertexID]; | |
[self vertexDataDidChange]; | |
if (color != 0xffffff) _tinted = YES; | |
else _tinted = (self.alpha != 1.0f) || _vertexData.tinted; | |
_requiresUpdate = YES; | |
} | |
- (uint)colorOfVertex:(uint)vertexID | |
{ | |
return [_vertexData colorAtIndex:vertexID]; | |
} | |
- (void)setColor:(uint)color | |
{ | |
for (int i=0; i<_vertexData.numVertices; ++i) | |
[_vertexData setColor:color atIndex:i]; | |
[self vertexDataDidChange]; | |
if (color != 0xffffff) _tinted = YES; | |
else _tinted = (self.alpha != 1.0f) || _vertexData.tinted; | |
_requiresUpdate = YES; | |
} | |
- (uint)color | |
{ | |
return [self colorOfVertex:0]; | |
} | |
- (void)setAlpha:(float)alpha ofVertex:(uint)vertexID | |
{ | |
[_vertexData setAlpha:alpha atIndex:vertexID]; | |
[self vertexDataDidChange]; | |
if (alpha != 1.0) _tinted = true; | |
else _tinted = (self.alpha != 1.0f) || _vertexData.tinted; | |
_requiresUpdate = YES; | |
} | |
- (float)alphaOfVertex:(uint)vertexID | |
{ | |
return [_vertexData alphaAtIndex:vertexID]; | |
} | |
- (void)setAlpha:(float)alpha | |
{ | |
super.alpha = alpha; | |
if (self.alpha != 1.0f) _tinted = true; | |
else _tinted = _vertexData.tinted; | |
} | |
- (BOOL)premultipliedAlpha | |
{ | |
return _vertexData.premultipliedAlpha; | |
} | |
- (void)setPremultipliedAlpha:(BOOL)premultipliedAlpha | |
{ | |
if (premultipliedAlpha != self.premultipliedAlpha) | |
_vertexData.premultipliedAlpha = premultipliedAlpha; | |
} | |
- (BOOL)tinted | |
{ | |
return _tinted; | |
} | |
- (SPTexture *)texture | |
{ | |
return nil; | |
} | |
- (uint)centerVertexIndex | |
{ | |
return _vertexData.numVertices-1; | |
} | |
- (void)vertexDataDidChange | |
{ | |
// override in subclass | |
} | |
+ (id)polygonWithRadius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma | |
{ | |
return [[self alloc] initWithRadius:radius numEdges:numEdges color:color premultipliedAlpha:pma]; | |
} | |
+ (id)polygonWithRadius:(float)radius numEdges:(uint)numEdges color:(uint)color | |
{ | |
return [[self alloc] initWithRadius:radius numEdges:numEdges color:color]; | |
} | |
+ (id)polygonWithRadius:(float)radius numEdges:(uint)numEdges | |
{ | |
return [[self alloc] initWithRadius:radius numEdges:numEdges]; | |
} | |
+ (id)polygon | |
{ | |
return [[self alloc] init]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SHPolygonImage.h | |
// Sparrow 2.X | |
// | |
// Created by Shilo White on 7/28/13. | |
// | |
// Highly based on the following Sparrow extensions created by Daniel Sperl @ Gamua: | |
// Polygon: https://gist.github.com/PrimaryFeather/5352978 | |
// TexturedPolygon: https://gist.github.com/PrimaryFeather/5679832 | |
// | |
// Hit test code based on the following Starling extension, created by RomainThery: | |
// Polygon.as: https://gist.github.com/RomainThery/5366504/ | |
// | |
#import "SHPolygon.h" | |
@class SPTexture; | |
@class SPPoint; | |
@class SPRectangle; | |
@interface SHPolygonImage : SHPolygon | |
{ | |
SPTexture *_texture; | |
} | |
- (id)initWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma; | |
- (id)initWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges color:(uint)color; | |
- (id)initWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges; | |
- (id)initWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma generateMipmaps:(BOOL)mipmaps; | |
- (id)initWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma; | |
- (id)initWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color; | |
- (id)initWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges; | |
+ (id)polygonImageWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma; | |
+ (id)polygonImageWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges color:(uint)color; | |
+ (id)polygonImageWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges; | |
+ (id)polygonImageWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma generateMipmaps:(BOOL)mipmaps; | |
+ (id)polygonImageWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma; | |
+ (id)polygonImageWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color; | |
+ (id)polygonImageWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges; | |
@property (nonatomic, strong) SPTexture *texture; | |
@property (nonatomic, strong) SPRectangle *textureFrame; | |
@property (nonatomic, assign) float textureFrameX; | |
@property (nonatomic, assign) float textureFrameY; | |
@property (nonatomic, assign) float textureFrameWidth; | |
@property (nonatomic, assign) float textureFrameHeight; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SHPolygonImage.m | |
// Sparrow 2.X | |
// | |
// Created by Shilo White on 7/28/13. | |
// | |
// Highly based on the following Sparrow extensions created by Daniel Sperl @ Gamua: | |
// Polygon: https://gist.github.com/PrimaryFeather/5352978 | |
// TexturedPolygon: https://gist.github.com/PrimaryFeather/5679832 | |
// | |
// Hit test code based on the following Starling extension, created by RomainThery: | |
// Polygon.as: https://gist.github.com/RomainThery/5366504/ | |
// | |
#import "SHPolygonImage.h" | |
#import "SPPoint.h" | |
#import "SPTexture.h" | |
#import "SPGLTexture.h" | |
#import "SPRenderSupport.h" | |
#import "SPMacros.h" | |
#import "SPVertexData.h" | |
#import "SPRectangle.h" | |
@implementation SHPolygonImage | |
{ | |
SPRectangle *_textureFrame; | |
} | |
@synthesize texture = _texture; | |
@synthesize textureFrame = _textureFrame; | |
- (id)initWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma | |
{ | |
if ((self = [super initWithRadius:radius numEdges:numEdges color:color premultipliedAlpha:pma])) | |
{ | |
_texture = texture; | |
_textureFrame = nil; | |
} | |
return self; | |
} | |
- (id)initWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges color:(uint)color | |
{ | |
return [self initWithTexture:texture radius:radius numEdges:numEdges color:color premultipliedAlpha:YES]; | |
} | |
- (id)initWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges | |
{ | |
return [self initWithTexture:texture radius:radius numEdges:numEdges color:SP_WHITE]; | |
} | |
- (id)initWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma generateMipmaps:(BOOL)mipmaps | |
{ | |
return [self initWithTexture:[SPTexture textureWithContentsOfFile:path generateMipmaps:mipmaps] radius:radius numEdges:numEdges color:color premultipliedAlpha:pma]; | |
} | |
- (id)initWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma | |
{ | |
return [self initWithContentsOfFile:path radius:radius numEdges:numEdges color:color premultipliedAlpha:pma generateMipmaps:NO]; | |
} | |
- (id)initWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color | |
{ | |
return [self initWithContentsOfFile:path radius:radius numEdges:numEdges color:color premultipliedAlpha:YES]; | |
} | |
- (id)initWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges | |
{ | |
return [self initWithContentsOfFile:path radius:radius numEdges:numEdges color:SP_WHITE]; | |
} | |
- (void)setupVertices | |
{ | |
// set up vertex data | |
float diameter = self.radius*2; | |
float textureWidth = _textureFrame ? _textureFrame.width/diameter: 1.0f; | |
float textureHeight = _textureFrame ? _textureFrame.height/diameter : 1.0f; | |
float textureX = _textureFrame ? -(((_textureFrame.x/diameter)/textureWidth)+((1.0f-textureWidth)/2/-textureWidth)) : 0.0f; | |
float textureY = _textureFrame ? -(((_textureFrame.y/diameter)/textureHeight)+((1.0f-textureHeight)/2/-textureHeight)) : 0.0f; | |
uint numEdges = _vertexData.numVertices-1; | |
for (int i=0; i<numEdges; ++i) | |
{ | |
float polarLength = (i<self.numDrawnEdges)?self.radius:0; | |
SPPoint *edge = [[SPPoint alloc] initWithPolarLength:polarLength angle:i*2*PI / numEdges]; | |
[_vertexData setPosition:edge atIndex:i]; | |
if (_textureFrame) | |
{ | |
[_vertexData setTexCoordsWithX:0.5f + edge.x / (2 * polarLength * textureWidth) + textureX | |
y:0.5f + edge.y / (2 * polarLength * textureHeight) + textureY | |
atIndex:i]; | |
} | |
else | |
{ | |
[_vertexData setTexCoordsWithX:0.5f + edge.x / (2 * polarLength) | |
y:0.5f + edge.y / (2 * polarLength) | |
atIndex:i]; | |
} | |
} | |
// center vertex | |
[_vertexData setPositionWithX:0.0f y:0.0f atIndex:numEdges]; | |
[_vertexData setTexCoordsWithX:0.5f+textureX y:0.5f+textureY atIndex:numEdges]; | |
// above, we have inserted the "high level" vertex data, using a [0-1] range. The actual | |
// texture coordinates for OpenGL might differ; the following method will adjust them. | |
[_texture adjustVertexData:_vertexData atIndex:0 numVertices:numEdges+1]; | |
// set up index data | |
int numIndices = numEdges * 3; | |
if (!_indexData) _indexData = malloc(sizeof(ushort) * numIndices); | |
else _indexData = realloc(_indexData, sizeof(ushort) * numIndices); | |
for (int i=0; i<numEdges; ++i) | |
{ | |
_indexData[i*3] = numEdges; | |
_indexData[i*3+1] = i; | |
_indexData[i*3+2] = (i+1) % numEdges; | |
} | |
} | |
- (void)render:(SPRenderSupport *)support | |
{ | |
if (self.requiresUpdate) [self update]; | |
[support finishQuadBatch]; // finish previously batched quads | |
[support addDrawCalls:1]; // update stats display | |
_baseEffect.mvpMatrix = support.mvpMatrix; | |
_baseEffect.alpha = support.alpha; | |
_baseEffect.texture = _texture; | |
[_baseEffect prepareToDraw]; | |
[SPBlendMode applyBlendFactorsForBlendMode:support.blendMode | |
premultipliedAlpha:_vertexData.premultipliedAlpha]; | |
int attribPosition = _baseEffect.attribPosition; | |
int attribColor = _baseEffect.attribColor; | |
int attribTexCoords = _baseEffect.attribTexCoords; | |
glEnableVertexAttribArray(attribPosition); | |
glEnableVertexAttribArray(attribColor); | |
glEnableVertexAttribArray(attribTexCoords); | |
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferName); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferName); | |
glVertexAttribPointer(attribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(SPVertex), | |
(void *)(offsetof(SPVertex, position))); | |
glVertexAttribPointer(attribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(SPVertex), | |
(void *)(offsetof(SPVertex, color))); | |
if (self.texture) | |
glVertexAttribPointer(attribTexCoords, 2, GL_FLOAT, GL_FALSE, sizeof(SPVertex), | |
(void *)(offsetof(SPVertex, texCoords))); | |
int numIndices = (_vertexData.numVertices - 1) * 3; | |
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0); | |
} | |
- (void)setTexture:(SPTexture *)value | |
{ | |
if (value == nil) | |
{ | |
[NSException raise:SP_EXC_INVALID_OPERATION format:@"texture cannot be nil!"]; | |
} | |
else if (value != _texture) | |
{ | |
_texture = value; | |
self.requiresUpdate = YES; | |
[_vertexData setPremultipliedAlpha:_texture.premultipliedAlpha updateVertices:YES]; | |
[self vertexDataDidChange]; | |
} | |
} | |
- (void)setTextureFrame:(SPRectangle *)textureFrame | |
{ | |
if (textureFrame != _textureFrame) | |
{ | |
_textureFrame = textureFrame; | |
self.requiresUpdate = YES; | |
} | |
} | |
- (void)setTextureFrameX:(float)textureFrameX | |
{ | |
_textureFrame.x = textureFrameX; | |
self.requiresUpdate = YES; | |
} | |
- (void)setTextureFrameY:(float)textureFrameY | |
{ | |
_textureFrame.y = textureFrameY; | |
self.requiresUpdate = YES; | |
} | |
- (void)setTextureFrameWidth:(float)textureFrameWidth | |
{ | |
_textureFrame.width = textureFrameWidth; | |
self.requiresUpdate = YES; | |
} | |
- (void)setTextureFrameHeight:(float)textureFrameHeight | |
{ | |
_textureFrame.height = textureFrameHeight; | |
self.requiresUpdate = YES; | |
} | |
- (float)textureFrameX | |
{ | |
return _textureFrame.x; | |
} | |
- (float)textureFrameY | |
{ | |
return _textureFrame.y; | |
} | |
- (float)textureFrameWidth | |
{ | |
return _textureFrame.width; | |
} | |
- (float)textureFrameHeight | |
{ | |
return _textureFrame.height; | |
} | |
+ (id)polygonImageWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma | |
{ | |
return [[self alloc] initWithTexture:texture radius:radius numEdges:numEdges color:color premultipliedAlpha:pma]; | |
} | |
+ (id)polygonImageWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges color:(uint)color | |
{ | |
return [[self alloc] initWithTexture:texture radius:radius numEdges:numEdges color:color]; | |
} | |
+ (id)polygonImageWithTexture:(SPTexture *)texture radius:(float)radius numEdges:(uint)numEdges | |
{ | |
return [[self alloc] initWithTexture:texture radius:radius numEdges:numEdges]; | |
} | |
+ (id)polygonImageWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma generateMipmaps:(BOOL)mipmaps | |
{ | |
return [[self alloc] initWithContentsOfFile:path radius:radius numEdges:numEdges color:color premultipliedAlpha:pma generateMipmaps:mipmaps]; | |
} | |
+ (id)polygonImageWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color premultipliedAlpha:(BOOL)pma | |
{ | |
return [[self alloc] initWithContentsOfFile:path radius:radius numEdges:numEdges color:color premultipliedAlpha:pma]; | |
} | |
+ (id)polygonImageWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges color:(uint)color | |
{ | |
return [[self alloc] initWithContentsOfFile:path radius:radius numEdges:numEdges color:color]; | |
} | |
+ (id)polygonImageWithContentsOfFile:(NSString *)path radius:(float)radius numEdges:(uint)numEdges | |
{ | |
return [[self alloc] initWithContentsOfFile:path radius:radius numEdges:numEdges]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment