Adds Sparrow Extras.
authordsc <david.schoonover@gmail.com>
Tue, 24 May 2011 17:15:15 +0000 (10:15 -0700)
committerdsc <david.schoonover@gmail.com>
Tue, 24 May 2011 17:15:15 +0000 (10:15 -0700)
libs/sparrow/src/Extras/BEParallaxSprite.h [new file with mode: 0644]
libs/sparrow/src/Extras/BEParallaxSprite.m [new file with mode: 0644]
libs/sparrow/src/Extras/SHCircle.h [new file with mode: 0644]
libs/sparrow/src/Extras/SHCircle.m [new file with mode: 0644]
libs/sparrow/src/Extras/SHLine.h [new file with mode: 0644]
libs/sparrow/src/Extras/SHLine.m [new file with mode: 0644]
libs/sparrow/src/Extras/SHPolygon.h [new file with mode: 0644]
libs/sparrow/src/Extras/SHPolygon.m [new file with mode: 0644]
libs/sparrow/src/Extras/SXFPSMeter.h [new file with mode: 0644]
libs/sparrow/src/Extras/SXFPSMeter.m [new file with mode: 0644]
libs/sparrow/src/Extras/SparrowExtras.h [new file with mode: 0644]

diff --git a/libs/sparrow/src/Extras/BEParallaxSprite.h b/libs/sparrow/src/Extras/BEParallaxSprite.h
new file mode 100644 (file)
index 0000000..b25c72d
--- /dev/null
@@ -0,0 +1,37 @@
+//
+//  BEParallaxSprite.h
+//
+//  Created by Brian Ensor on 1/29/11.
+//  Copyright 2011 Brian Ensor Apps. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#define BE_PARALLAX_DIRECTION_LEFT 1
+#define BE_PARALLAX_DIRECTION_RIGHT 2
+#define BE_PARALLAX_DIRECTION_UP 3
+#define BE_PARALLAX_DIRECTION_DOWN 4
+
+@interface BEParallaxSprite : SPSprite {
+       SPImage *mImage1;
+       SPImage *mImage2;
+       float mSpeed;
+       int mDirection;
+       float mCurStep;
+       bool mRunning;
+}
+
+@property (nonatomic, assign) bool running;
+@property (nonatomic, assign) float speed;
+
+- (void)onEnterFrame:(SPEnterFrameEvent *)event;
+- (void)start;
+- (void)stop;
+- (id)initWithTexture:(SPTexture *)texture;
+- (id)initWithTexture:(SPTexture *)texture speed:(float)speed;
+- (id)initWithTexture:(SPTexture *)texture speed:(float)speed direction:(int)direction;
++ (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture;
++ (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture speed:(float)speed;
++ (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture speed:(float)speed direction:(int)direction;
+
+@end
diff --git a/libs/sparrow/src/Extras/BEParallaxSprite.m b/libs/sparrow/src/Extras/BEParallaxSprite.m
new file mode 100644 (file)
index 0000000..7c01729
--- /dev/null
@@ -0,0 +1,134 @@
+//
+//  BEParallaxSprite.m
+//
+//  Created by Brian Ensor on 1/29/11.
+//  Copyright 2011 Brian Ensor Apps. All rights reserved.
+//
+
+#import "BEParallaxSprite.h"
+
+@implementation BEParallaxSprite
+
+@synthesize speed = mSpeed;
+@synthesize running = mRunning;
+
+- (id)initWithTexture:(SPTexture *)texture {
+       if (self = [super init]) {
+               [self initWithTexture:texture speed:1 direction:BE_PARALLAX_DIRECTION_LEFT];
+       }
+       return self;
+}
+
+- (id)initWithTexture:(SPTexture *)texture speed:(float)speed {
+       if (self = [super init]) {
+               [self initWithTexture:texture speed:speed direction:BE_PARALLAX_DIRECTION_LEFT];
+       }
+       return self;
+}
+
+- (id)initWithTexture:(SPTexture *)texture speed:(float)speed direction:(int)direction {
+       if (self = [super init]) {
+               mRunning = YES;
+               mDirection = direction;
+               if (direction < 1 || direction > 4) {
+                       mDirection = BE_PARALLAX_DIRECTION_LEFT;
+               }
+               mSpeed = speed;
+               mImage1 = [SPImage imageWithTexture:texture];
+               [self addChild:mImage1];
+               mImage2 = [SPImage imageWithTexture:texture];
+               if (mDirection == BE_PARALLAX_DIRECTION_DOWN) {
+                       mImage2.y = mImage1.y-mImage2.height;
+               }
+               if (mDirection == BE_PARALLAX_DIRECTION_UP) {
+                       mImage2.y = mImage1.y+mImage1.height;
+               }
+               if (mDirection == BE_PARALLAX_DIRECTION_RIGHT) {
+                       mImage2.x = mImage1.x-mImage2.width;
+               }
+               if (mDirection == BE_PARALLAX_DIRECTION_LEFT) {
+                       mImage2.x = mImage1.x+mImage1.width;
+               }
+               [self addChild:mImage2];
+               [self addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
+       }
+    return self;
+}
+
++ (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture {
+       return [[[BEParallaxSprite alloc] initWithTexture:texture speed:1 direction:BE_PARALLAX_DIRECTION_LEFT] autorelease];
+}
+
++ (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture speed:(float)speed {
+       return [[[BEParallaxSprite alloc] initWithTexture:texture speed:speed direction:BE_PARALLAX_DIRECTION_LEFT] autorelease];
+}
+
++ (BEParallaxSprite *)parallexSpriteWithTexture:(SPTexture *)texture speed:(float)speed direction:(int)direction {
+       return [[[BEParallaxSprite alloc] initWithTexture:texture speed:speed direction:direction] autorelease];
+}
+
+- (void)start {
+       if (mRunning != YES) {
+               mRunning = YES;
+       }
+}
+
+- (void)stop {
+       if (mRunning != NO) {
+               mRunning = NO;
+       }
+}
+
+- (void)onEnterFrame:(SPEnterFrameEvent *)event {
+       if (mRunning == YES) {
+               mCurStep += mSpeed;
+               if (mCurStep < 1) return;
+               if (mDirection == BE_PARALLAX_DIRECTION_DOWN) {
+                       mImage1.y += floor(mCurStep);
+                       mImage2.y += floor(mCurStep);
+                       if (mImage1.y >= mImage1.height) {
+                               mImage1.y = mImage2.y-mImage2.height;
+                       }
+                       if (mImage2.y >= mImage2.height) {
+                               mImage2.y = mImage1.y-mImage1.height;
+                       }
+               }
+               if (mDirection == BE_PARALLAX_DIRECTION_UP) {
+                       mImage1.y -= floor(mCurStep);
+                       mImage2.y -= floor(mCurStep);
+                       if (mImage1.y <= -mImage1.height) {
+                               mImage1.y = mImage2.y+mImage2.height;
+                       }
+                       if (mImage2.y <= -mImage2.height) {
+                               mImage2.y = mImage1.y+mImage1.height;
+                       }
+               }
+               if (mDirection == BE_PARALLAX_DIRECTION_RIGHT) {
+                       mImage1.x += floor(mCurStep);
+                       mImage2.x += floor(mCurStep);
+                       if (mImage1.x >= mImage1.width) {
+                               mImage1.x = mImage2.x-mImage2.width;
+                       }
+                       if (mImage2.x >= mImage2.width) {
+                               mImage2.x = mImage1.x-mImage1.width;
+                       }
+               }
+               if (mDirection == BE_PARALLAX_DIRECTION_LEFT) {
+                       mImage1.x -= floor(mCurStep);
+                       mImage2.x -= floor(mCurStep);
+                       if (mImage1.x <= -mImage1.width) {
+                               mImage1.x = mImage2.x+mImage2.width;
+                       }
+                       if (mImage2.x <= -mImage2.width) {
+                               mImage2.x = mImage1.x+mImage1.width;
+                       }
+               }
+               mCurStep -= floor(mCurStep);
+       }
+}
+
+- (void)dealloc {
+       [super dealloc];
+}
+
+@end
diff --git a/libs/sparrow/src/Extras/SHCircle.h b/libs/sparrow/src/Extras/SHCircle.h
new file mode 100644 (file)
index 0000000..8324612
--- /dev/null
@@ -0,0 +1,50 @@
+//
+//  SHCircle.h
+//  Sparrow
+//
+//  Created by Shilo White on 1/27/11.
+//  Copyright 2011 Shilocity Productions. All rights reserved.
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the Simplified BSD License.
+//
+
+#import "SPDisplayObject.h"
+
+@interface SHCircle : SPDisplayObject {
+  @protected
+       float mVertexCoords[724];
+       float mBorderVertexCoords[724];
+       float mWidth;
+       float mHeight;
+       BOOL mFill;
+       uint mInnerColor;
+       uint mOuterColor;
+       BOOL mBorder;
+    uint mBorderColor;
+       float mBorderWidth;
+       int mDegrees;
+       float mCenterRotation;
+}
+
+@property (nonatomic, assign) float centerX;
+@property (nonatomic, assign) float centerY;
+@property (nonatomic, assign) float radiusX;
+@property (nonatomic, assign) float radiusY;
+@property (nonatomic, assign) BOOL fill;
+@property (nonatomic, assign) uint color;
+@property (nonatomic, assign) uint innerColor;
+@property (nonatomic, assign) uint outerColor;
+@property (nonatomic, assign) BOOL border;
+@property (nonatomic, assign) uint borderColor;
+@property (nonatomic, assign) float borderWidth;
+@property (nonatomic, assign) int degrees;
+@property (nonatomic, assign) float centerRotation;
+
+- (id)initWithWidth:(float)width height:(float)height;
++ (SHCircle *)circle;
++ (SHCircle *)circleWithWidth:(float)width height:(float)height;
+- (void)drawVertices;
+- (void)renderFill:(SPRenderSupport *)support alpha:(float)alpha;
+- (void)renderBorder:(SPRenderSupport *)support alpha:(float)alpha;
+@end
\ No newline at end of file
diff --git a/libs/sparrow/src/Extras/SHCircle.m b/libs/sparrow/src/Extras/SHCircle.m
new file mode 100644 (file)
index 0000000..86c01a5
--- /dev/null
@@ -0,0 +1,224 @@
+//
+//  SHCircle.m
+//  Sparrow
+//
+//  Created by Shilo White on 1/27/11.
+//  Copyright 2011 Shilocity Productions. All rights reserved.
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the Simplified BSD License.
+//
+
+#import "SHCircle.h"
+#import "SPMacros.h"
+#import "SPRenderSupport.h"
+
+#import <OpenGLES/EAGL.h>
+#import <OpenGLES/ES1/gl.h>
+#import <OpenGLES/ES1/glext.h>
+
+@implementation SHCircle
+
+@synthesize fill = mFill;
+@synthesize innerColor = mInnerColor;
+@synthesize outerColor = mOuterColor;
+@synthesize border = mBorder;
+@synthesize borderColor = mBorderColor;
+@synthesize borderWidth = mBorderWidth;
+
+- (id)init {
+       return [self initWithWidth:32 height:32];
+}
+
+- (id)initWithWidth:(float)width height:(float)height {
+       if (self = [super init]) {
+               mWidth = width;
+               mHeight = height;
+               
+               mFill = YES;
+               self.color = SP_WHITE;
+               mBorder = NO;
+               mBorderColor = SP_WHITE;
+               mBorderWidth = 1.0f;
+               mDegrees = 360;
+               mCenterRotation = 0;
+               
+               [self drawVertices];
+    }
+    return self;
+}
+
++ (SHCircle *)circle {
+       return [[[SHCircle alloc] initWithWidth:32 height:32] autorelease];
+}
+
++ (SHCircle *)circleWithWidth:(float)width height:(float)height {
+       return [[[SHCircle alloc] initWithWidth:width height:height] autorelease];
+}
+
+- (void)drawVertices {
+       float widthRadius = mWidth/2;
+       float heightRadius = mHeight/2;
+       
+       mVertexCoords[0] = widthRadius;
+       mVertexCoords[1] = heightRadius;
+       for (int i=0; i<mDegrees*2+1; i+=2) {
+               mVertexCoords[i+2] = mBorderVertexCoords[i+2] = cos(SP_D2R(i/2+mCenterRotation)) * widthRadius + widthRadius;
+               mVertexCoords[i+3] = mBorderVertexCoords[i+3] = sin(SP_D2R(i/2+mCenterRotation)) * heightRadius + heightRadius;
+       }
+       if (mDegrees < 360) {
+               mBorderVertexCoords[0] = widthRadius;
+               mBorderVertexCoords[1] = heightRadius;
+       } else {
+               mBorderVertexCoords[0] = mBorderVertexCoords[2];
+               mBorderVertexCoords[1] = mBorderVertexCoords[3];
+       }
+}
+
+- (void)render:(SPRenderSupport *)support {
+       float alpha = self.alpha;
+       
+       glEnableClientState(GL_VERTEX_ARRAY);
+       glEnableClientState(GL_COLOR_ARRAY);
+       
+       [support bindTexture:nil];
+       
+       [self renderFill:support alpha:alpha];
+    [self renderBorder:support alpha:alpha];
+       
+    glDisableClientState(GL_VERTEX_ARRAY);
+       glDisableClientState(GL_COLOR_ARRAY);
+}
+
+- (void)renderFill:(SPRenderSupport *)support alpha:(float)alpha {
+       if (!mFill || !mDegrees) return;
+       uint colors[mDegrees+2];
+       
+       for (int i=0; i<mDegrees+2; i++) {
+               if (i==0) colors[i] = [support convertColor:mInnerColor alpha:alpha];
+               else colors[i] = [support convertColor:mOuterColor alpha:alpha];
+       }
+       
+       glVertexPointer(2, GL_FLOAT, 0, mVertexCoords);
+       glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
+       glDrawArrays(GL_TRIANGLE_FAN, 0, mDegrees+2);
+}
+
+- (void)renderBorder:(SPRenderSupport *)support alpha:(float)alpha {
+       if (!mBorder || !mDegrees) return;
+       uint colors[mDegrees+2];
+       
+       for (int i=0; i<mDegrees+2; i++) colors[i] = [support convertColor:mBorderColor alpha:alpha];
+       
+       glVertexPointer(2, GL_FLOAT, 0, mBorderVertexCoords);
+       glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
+       glLineWidth(mBorderWidth);
+       glDrawArrays(GL_LINE_LOOP, 0, mDegrees+2);
+}
+
+- (SPRectangle *)boundsInSpace:(SPDisplayObject *)targetCoordinateSpace {
+       if (targetCoordinateSpace == self)
+        return [SPRectangle rectangleWithX:0 y:0 width:mWidth height:mHeight];
+    
+    SPMatrix *transformationMatrix = [self transformationMatrixToSpace:targetCoordinateSpace];
+    SPPoint *point = [[SPPoint alloc] init];
+    
+       float tempVertexCoords[8];
+       tempVertexCoords[2] = mWidth;
+       tempVertexCoords[5] = mHeight;
+       tempVertexCoords[6] = mWidth;
+       tempVertexCoords[7] = mHeight;
+       
+    float minX = FLT_MAX, maxX = -FLT_MAX, minY = FLT_MAX, maxY = -FLT_MAX;
+    for (int i=0; i<4; ++i) {
+        point.x = tempVertexCoords[2*i];
+        point.y = tempVertexCoords[2*i+1];
+        SPPoint *transformedPoint = [transformationMatrix transformPoint:point];
+        float tfX = transformedPoint.x; 
+        float tfY = transformedPoint.y;
+        minX = MIN(minX, tfX);
+        maxX = MAX(maxX, tfX);
+        minY = MIN(minY, tfY);
+        maxY = MAX(maxY, tfY);
+    }
+    [point release];
+    return [SPRectangle rectangleWithX:minX y:minY width:maxX-minX height:maxY-minY];    
+}
+
+- (void)setCenterX:(float)centerX {
+       self.x = centerX - (mWidth/2);
+}
+
+- (float)centerX {
+       return self.x + (mWidth/2);
+}
+
+- (void)setCenterY:(float)centerY {
+       self.y = centerY - (mHeight/2);
+}
+
+- (float)centerY {
+       return self.y + (mHeight/2);
+}
+
+- (void)setRadiusX:(float)radiusX {
+       self.width = radiusX*2;
+}
+
+- (float)radiusX {
+       return mWidth/2;
+}
+
+- (void)setRadiusY:(float)radiusY {
+       self.height = radiusY*2;
+}
+
+- (float)radiusY {
+       return mHeight/2;
+}
+
+- (void)setWidth:(float)width {
+       mWidth = width;
+       [self drawVertices];
+       [super setWidth:mWidth];
+}
+
+- (void)setHeight:(float)height {
+       mHeight = height;
+       [self drawVertices];
+       [super setHeight:mHeight];
+}
+
+- (void)setColor:(uint)color {
+       mInnerColor = mOuterColor = color;
+}
+
+- (uint)color {
+       return mInnerColor;
+}
+
+- (void)setDegrees:(int)degrees {
+       if (degrees < 0 || degrees > 360)
+               [NSException raise:NSInvalidArgumentException format:@"Degrees must have an integer value between 0 and 360.", NSStringFromSelector(_cmd)];
+       
+       if (degrees != mDegrees) {
+               mDegrees = degrees;
+               [self drawVertices];
+       }
+}
+
+- (int)degrees {
+       return mDegrees;
+}
+
+- (void)setCenterRotation:(float)centerRotation {
+       if (centerRotation != mCenterRotation) {
+               mCenterRotation = centerRotation;
+               [self drawVertices];
+       }
+}
+
+- (float)centerRotation {
+       return mCenterRotation;
+}
+@end
\ No newline at end of file
diff --git a/libs/sparrow/src/Extras/SHLine.h b/libs/sparrow/src/Extras/SHLine.h
new file mode 100644 (file)
index 0000000..2ccc960
--- /dev/null
@@ -0,0 +1,46 @@
+//
+//  SHLine.h
+//  Sparrow
+//
+//  Created by Shilo White on 1/11/11.
+//  Copyright 2011 Shilocity Productions. All rights reserved.
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the Simplified BSD License.
+//
+
+#import <Foundation/Foundation.h>
+#import "SPDisplayObject.h"
+#import "SPRenderSupport.h"
+
+#import <OpenGLES/EAGL.h>
+#import <OpenGLES/ES1/gl.h>
+#import <OpenGLES/ES1/glext.h>
+
+@interface SHLine : SPDisplayObject {
+  @protected
+       float mVertexCoords[4];
+    uint mVertexColors[2];
+       float mVertexAlpha[2];
+       float mThickness;
+}
+
+@property (nonatomic, assign) float x2;
+@property (nonatomic, assign) float y2;
+@property (nonatomic, assign) uint color;
+@property (nonatomic, assign) uint startColor;
+@property (nonatomic, assign) uint endColor;
+@property (nonatomic, assign) float startAlpha;
+@property (nonatomic, assign) float endAlpha;
+@property (nonatomic, assign) float thickness;
+
+- (SHLine *)initWithSize:(float)length;
+- (SHLine *)initWithLength:(float)length andThickness:(float)thickness;
+- (SHLine *)initWithCoords:(float)x1 :(float)y1 :(float)x2 :(float)y2;
+- (SHLine *)initWithCoords:(float)x1 :(float)y1 :(float)x2 :(float)y2 andThickness:(float)thickness;
+
++ (SHLine *)lineWithLength:(float)length;
++ (SHLine *)lineWithLength:(float)length andThickness:(float)thickness;
++ (SHLine *)lineWithCoords:(float)x1 :(float)y1 :(float)x2 :(float)y2;
++ (SHLine *)lineWithCoords:(float)x1 :(float)y1 :(float)x2 :(float)y2 andThickness:(float)thickness;
+@end
diff --git a/libs/sparrow/src/Extras/SHLine.m b/libs/sparrow/src/Extras/SHLine.m
new file mode 100644 (file)
index 0000000..cb9e8b1
--- /dev/null
@@ -0,0 +1,171 @@
+//
+//  SHLine.h
+//  Sparrow
+//
+//  Created by Shilo White on 1/11/11.
+//  Copyright 2011 Shilocity Productions. All rights reserved.
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the Simplified BSD License.
+//
+
+#import "SHLine.h"
+
+@implementation SHLine
+
+@synthesize thickness = mThickness;
+
+- (SHLine *)init {
+       return [self initWithCoords:0:0:50.0f:0 andThickness:1.0f];
+}
+
+- (SHLine *)initWithSize:(float)length {
+       return [self initWithCoords:0:0:length:0 andThickness:1.0f];
+}
+
+- (SHLine *)initWithLength:(float)length andThickness:(float)thickness {
+       return [self initWithCoords:0:0:length:0 andThickness:thickness];
+}
+
+- (SHLine *)initWithCoords:(float)x1 :(float)y1 :(float)x2 :(float)y2 {
+       return [self initWithCoords:x1:y1:x2:y2 andThickness:1.0f];
+}
+
+- (SHLine *)initWithCoords:(float)x1 :(float)y1 :(float)x2 :(float)y2 andThickness:(float)thickness {
+       if (self = [super init]) {
+               self.x = x1;
+               self.y = y1;
+               self.color = 0xffffff;
+               mVertexCoords[2] = x2;
+               mVertexCoords[3] = y2;
+               mVertexAlpha[0] = 1.0f;
+               mVertexAlpha[1] = 1.0f;
+               mThickness = thickness;
+       }
+       return self;
+}
+
++ (SHLine *)lineWithLength:(float)length {
+       return [[[SHLine alloc] initWithSize:length] autorelease];
+}
+
++ (SHLine *)lineWithLength:(float)length andThickness:(float)thickness {
+       return [[[SHLine alloc] initWithLength:length andThickness:thickness] autorelease];
+}
+
++ (SHLine *)lineWithCoords:(float)x1 :(float)y1 :(float)x2 :(float)y2 {
+       return [[[SHLine alloc] initWithCoords:x1:y1:x2:y2] autorelease];
+}
+
++ (SHLine *)lineWithCoords:(float)x1 :(float)y1 :(float)x2 :(float)y2 andThickness:(float)thickness {
+       return [[[SHLine alloc] initWithCoords:x1:y1:x2:y2 andThickness:thickness] autorelease];
+}
+
+- (void)render:(SPRenderSupport *)support {
+    uint colors[2];
+    float alpha = self.alpha;
+    
+    [support bindTexture:nil];
+    
+       for (int i=0; i<2; i++) colors[i] = [support convertColor:mVertexColors[i] alpha:mVertexAlpha[i]*alpha];
+       
+    glEnableClientState(GL_VERTEX_ARRAY);
+    glEnableClientState(GL_COLOR_ARRAY);    
+    
+       glVertexPointer(2, GL_FLOAT, 0, mVertexCoords);
+       glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
+       glLineWidth(mThickness);
+       glDrawArrays(GL_LINES, 0, 2);
+    
+    glDisableClientState(GL_VERTEX_ARRAY);
+    glDisableClientState(GL_COLOR_ARRAY);
+}
+
+- (SPRectangle *)boundsInSpace:(SPDisplayObject *)targetCoordinateSpace
+{
+    if (targetCoordinateSpace == self)
+        return [SPRectangle rectangleWithX:0 y:0 width:mVertexCoords[2] height:mVertexCoords[3]];
+    
+    SPMatrix *transformationMatrix = [self transformationMatrixToSpace:targetCoordinateSpace];
+    SPPoint *point = [[SPPoint alloc] init];
+    
+    float minX = FLT_MAX, maxX = -FLT_MAX, minY = FLT_MAX, maxY = -FLT_MAX;
+    for (int i=0; i<2; i++) {
+        point.x = mVertexCoords[2*i];
+        point.y = mVertexCoords[2*i+1];
+        SPPoint *transformedPoint = [transformationMatrix transformPoint:point];
+        float tfX = transformedPoint.x; 
+        float tfY = transformedPoint.y;
+        minX = MIN(minX, tfX);
+        maxX = MAX(maxX, tfX);
+        minY = MIN(minY, tfY);
+        maxY = MAX(maxY, tfY);
+    }
+    [point release];
+    return [SPRectangle rectangleWithX:minX y:minY width:maxX-minX height:maxY-minY];    
+}
+
+- (void)setX2:(float)x2 {
+       mVertexCoords[2] = x2;
+}
+
+- (void)setY2:(float)y2 {
+       mVertexCoords[3] = y2;
+}
+
+- (void)setColor:(uint)color {
+    for (int i=0; i<2; i++) {
+               mVertexColors[i] = color;
+       }
+}
+
+- (void)setStartColor:(uint)color {
+       mVertexColors[0] = color;
+}
+
+- (void)setEndColor:(uint)color {
+       mVertexColors[1] = color;
+}
+
+- (void)setStartAlpha:(float)alpha {
+       if (alpha < 0) alpha = 0;
+       if (alpha > 1.0f) alpha = 1.0f;
+       
+       mVertexAlpha[0] = alpha;
+}
+
+- (void)setEndAlpha:(float)alpha {
+       if (alpha < 0) alpha = 0;
+       if (alpha > 1.0f) alpha = 1.0f;
+       
+       mVertexAlpha[1] = alpha;
+}
+
+- (float)x2 {
+       return mVertexCoords[2];
+}
+
+- (float)y2 {
+       return mVertexCoords[3];
+}
+
+- (uint)color {
+       return mVertexColors[0];
+}
+
+- (uint)startColor {
+       return mVertexColors[0];
+}
+
+- (uint)endColor {
+       return mVertexColors[1];
+}
+
+- (float)startAlpha {
+       return mVertexAlpha[0];
+}
+
+- (float)endAlpha {
+       return mVertexAlpha[1];
+}
+@end
diff --git a/libs/sparrow/src/Extras/SHPolygon.h b/libs/sparrow/src/Extras/SHPolygon.h
new file mode 100644 (file)
index 0000000..da8408a
--- /dev/null
@@ -0,0 +1,52 @@
+//
+//  SHPolygon.h
+//  Sparrow
+//
+//  Created by Shilo White on 2/6/11.
+//  Copyright 2011 Shilocity Productions. All rights reserved.
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the Simplified BSD License.
+//
+
+#import "SPDisplayObject.h"
+@class SPRenderSupport;
+
+@interface SHPolygon : SPDisplayObject {
+  @protected
+       float mVertexCoords[724];
+       float mBorderVertexCoords[724];
+       float mWidth;
+       float mHeight;
+       int mSides;
+       BOOL mFill;
+       uint mInnerColor;
+       uint mOuterColor;
+       BOOL mBorder;
+        uint mBorderColor;
+       float mBorderWidth;
+       float mCenterRotation;
+}
+
+@property (nonatomic, assign) int sides;
+@property (nonatomic, assign) float centerX;
+@property (nonatomic, assign) float centerY;
+@property (nonatomic, assign) float radiusX;
+@property (nonatomic, assign) float radiusY;
+@property (nonatomic, assign) BOOL fill;
+@property (nonatomic, assign) uint color;
+@property (nonatomic, assign) uint innerColor;
+@property (nonatomic, assign) uint outerColor;
+@property (nonatomic, assign) BOOL border;
+@property (nonatomic, assign) uint borderColor;
+@property (nonatomic, assign) float borderWidth;
+@property (nonatomic, assign) float centerRotation;
+
+- (id)initWithWidth:(float)width height:(float)height;
++ (SHPolygon *)polygon;
++ (SHPolygon *)polygonWithWidth:(float)width height:(float)height;
+- (void)drawVertices;
+- (float)setRotationOffset;
+- (void)renderFill:(SPRenderSupport *)support alpha:(float)alpha;
+- (void)renderBorder:(SPRenderSupport *)support alpha:(float)alpha;
+@end
\ No newline at end of file
diff --git a/libs/sparrow/src/Extras/SHPolygon.m b/libs/sparrow/src/Extras/SHPolygon.m
new file mode 100644 (file)
index 0000000..e12d9fe
--- /dev/null
@@ -0,0 +1,264 @@
+//
+//  SHPolygon.m
+//  Sparrow
+//
+//  Created by Shilo White on 2/6/11.
+//  Copyright 2011 Shilocity Productions. All rights reserved.
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the Simplified BSD License.
+//
+
+#import "SHPolygon.h"
+#import "SPMacros.h"
+#import "SPRenderSupport.h"
+#import "SPPoint.h"
+
+#import <OpenGLES/EAGL.h>
+#import <OpenGLES/ES1/gl.h>
+#import <OpenGLES/ES1/glext.h>
+
+@implementation SHPolygon
+
+@synthesize sides = mSides;
+@synthesize fill = mFill;
+@synthesize innerColor = mInnerColor;
+@synthesize outerColor = mOuterColor;
+@synthesize border = mBorder;
+@synthesize borderColor = mBorderColor;
+@synthesize borderWidth = mBorderWidth;
+
+- (id)init {
+       return [self initWithWidth:32 height:32];
+}
+
+- (id)initWithWidth:(float)width height:(float)height {
+       if (self = [super init]) {
+               mWidth = width;
+               mHeight = height;
+               
+               mSides = 3;
+               mFill = YES;
+               self.color = SP_WHITE;
+               mBorder = NO;
+               mBorderColor = SP_WHITE;
+               mBorderWidth = 1.0f;
+               mCenterRotation = 0;
+               
+               [self drawVertices];
+    }
+    return self;
+}
+
++ (SHPolygon *)polygon {
+       return [[[SHPolygon alloc] initWithWidth:32 height:32] autorelease];
+}
+
++ (SHPolygon *)polygonWithWidth:(float)width height:(float)height {
+       return [[[SHPolygon alloc] initWithWidth:width height:height] autorelease];
+}
+
+- (void)drawVertices {
+       float widthRadius = mWidth/2;
+       float heightRadius = mHeight/2;
+       float rotationOffset = [self setRotationOffset];
+       
+       mVertexCoords[0] = widthRadius;
+       mVertexCoords[1] = heightRadius;
+       
+       int count=2;
+       for (GLfloat i = 0; i < 360.0f; i+=360.0f/mSides)
+       {
+               if (count == 2) {
+                       mVertexCoords[mSides*2+2] = mBorderVertexCoords[mSides*2+2] = cos(SP_D2R(i+mCenterRotation+rotationOffset)) * widthRadius + widthRadius;
+                       mVertexCoords[mSides*2+3] = mBorderVertexCoords[mSides*2+3] = sin(SP_D2R(i+mCenterRotation+rotationOffset)) * heightRadius + heightRadius;
+               }
+               mVertexCoords[count] = mBorderVertexCoords[count++] = cos(SP_D2R(i+mCenterRotation+rotationOffset)) * widthRadius + widthRadius;
+               mVertexCoords[count] = mBorderVertexCoords[count++] = sin(SP_D2R(i+mCenterRotation+rotationOffset)) * heightRadius + heightRadius;
+       }
+       
+       mBorderVertexCoords[0] = mBorderVertexCoords[2];
+       mBorderVertexCoords[1] = mBorderVertexCoords[3];
+}
+
+- (float)setRotationOffset {
+       switch (mSides) {
+               case 3:
+                       return 30;
+                       break;
+               case 5:
+                       return -18;
+                       break;
+               case 7:
+                       return 13;
+                       break;
+               case 9:
+                       return -10;
+                       break;
+               case 11:
+                       return 8.2;
+                       break;
+               case 13:
+                       return -7;
+                       break;
+               case 15:
+                       return 6;
+                       break;
+               case 17:
+                       return -5;
+                       break;
+               case 19:
+                       return 4;
+                       break;
+               default:
+                       return 0;
+       }
+}
+
+- (void)render:(SPRenderSupport *)support {
+       float alpha = self.alpha;
+       
+       glEnableClientState(GL_VERTEX_ARRAY);
+       glEnableClientState(GL_COLOR_ARRAY);
+       
+       [support bindTexture:nil];
+       
+       [self renderFill:support alpha:alpha];
+    [self renderBorder:support alpha:alpha];
+       
+    glDisableClientState(GL_VERTEX_ARRAY);
+       glDisableClientState(GL_COLOR_ARRAY);
+}
+
+- (void)renderFill:(SPRenderSupport *)support alpha:(float)alpha {
+       if (!mFill) return;
+       uint colors[mSides+2];
+       
+       for (int i=0; i<mSides+2; i++) {
+               if (i==0) colors[i] = [support convertColor:mInnerColor alpha:alpha];
+               else colors[i] = [support convertColor:mOuterColor alpha:alpha];
+       }
+       
+       glVertexPointer(2, GL_FLOAT, 0, mVertexCoords);
+       glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
+       glDrawArrays(GL_TRIANGLE_FAN, 0, mSides+2);
+}
+
+- (void)renderBorder:(SPRenderSupport *)support alpha:(float)alpha {
+       if (!mBorder) return;
+       uint colors[mSides+2];
+       
+       for (int i=0; i<mSides+2; i++) colors[i] = [support convertColor:mBorderColor alpha:alpha];
+       
+       glVertexPointer(2, GL_FLOAT, 0, mBorderVertexCoords);
+       glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
+       glLineWidth(mBorderWidth);
+       glDrawArrays(GL_LINE_LOOP, 0, mSides+2);
+}
+
+- (SPRectangle *)boundsInSpace:(SPDisplayObject *)targetCoordinateSpace {
+       if (targetCoordinateSpace == self)
+        return [SPRectangle rectangleWithX:0 y:0 width:mWidth height:mHeight];
+    
+    SPMatrix *transformationMatrix = [self transformationMatrixToSpace:targetCoordinateSpace];
+    SPPoint *point = [[SPPoint alloc] init];
+    
+       float tempVertexCoords[8];
+       tempVertexCoords[2] = mWidth;
+       tempVertexCoords[5] = mHeight;
+       tempVertexCoords[6] = mWidth;
+       tempVertexCoords[7] = mHeight;
+       
+    float minX = FLT_MAX, maxX = -FLT_MAX, minY = FLT_MAX, maxY = -FLT_MAX;
+    for (int i=0; i<4; ++i) {
+        point.x = tempVertexCoords[2*i];
+        point.y = tempVertexCoords[2*i+1];
+        SPPoint *transformedPoint = [transformationMatrix transformPoint:point];
+        float tfX = transformedPoint.x; 
+        float tfY = transformedPoint.y;
+        minX = MIN(minX, tfX);
+        maxX = MAX(maxX, tfX);
+        minY = MIN(minY, tfY);
+        maxY = MAX(maxY, tfY);
+    }
+    [point release];
+    return [SPRectangle rectangleWithX:minX y:minY width:maxX-minX height:maxY-minY];    
+}
+
+- (void)setCenterX:(float)centerX {
+       self.x = centerX - (mWidth/2);
+}
+
+- (float)centerX {
+       return self.x + (mWidth/2);
+}
+
+- (void)setCenterY:(float)centerY {
+       self.y = centerY - (mHeight/2);
+}
+
+- (float)centerY {
+       return self.y + (mHeight/2);
+}
+
+- (void)setRadiusX:(float)radiusX {
+       self.width = radiusX*2;
+}
+
+- (float)radiusX {
+       return mWidth/2;
+}
+
+- (void)setRadiusY:(float)radiusY {
+       self.height = radiusY*2;
+}
+
+- (float)radiusY {
+       return mHeight/2;
+}
+
+- (void)setWidth:(float)width {
+       mWidth = width;
+       [self drawVertices];
+       [super setWidth:mWidth];
+}
+
+- (void)setHeight:(float)height {
+       mHeight = height;
+       [self drawVertices];
+       [super setHeight:mHeight];
+}
+
+- (void)setColor:(uint)color {
+       mInnerColor = mOuterColor = color;
+}
+
+- (uint)color {
+       return mInnerColor;
+}
+
+- (void)setSides:(int)sides {
+       if (sides < 3 || sides > 360)
+               [NSException raise:NSInvalidArgumentException format:@"Sides must have an integer value between 3 and 360.", NSStringFromSelector(_cmd)];
+       
+       if (sides != mSides) {
+               mSides = sides;
+               [self drawVertices];
+       }
+}
+
+- (int)sides {
+       return mSides;
+}
+
+- (void)setCenterRotation:(float)centerRotation {
+       if (centerRotation != mCenterRotation) {
+               mCenterRotation = centerRotation;
+               [self drawVertices];
+       }
+}
+
+- (float)centerRotation {
+       return mCenterRotation;
+}
+@end
\ No newline at end of file
diff --git a/libs/sparrow/src/Extras/SXFPSMeter.h b/libs/sparrow/src/Extras/SXFPSMeter.h
new file mode 100644 (file)
index 0000000..e4fbf07
--- /dev/null
@@ -0,0 +1,8 @@
+#import <Foundation/Foundation.h>
+#import "Sparrow.h"
+
+@interface SXFPSMeter : SPTextField{
+}
+- (void)update:(SPEnterFrameEvent*)event;
+
+@end
\ No newline at end of file
diff --git a/libs/sparrow/src/Extras/SXFPSMeter.m b/libs/sparrow/src/Extras/SXFPSMeter.m
new file mode 100644 (file)
index 0000000..9d7ce74
--- /dev/null
@@ -0,0 +1,30 @@
+#import "SXFPSMeter.h"
+
+@implementation SXFPSMeter
+
+- (id)initWithText:(NSString *)text{
+       self = [super initWithText:text];
+       self.hAlign = SPHAlignLeft;
+       self.vAlign = SPVAlignTop;
+       self.fontSize = 16;
+       self.color = 0xFF0000;
+       [self addEventListener:@selector(update:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
+       return self;
+}
+
+- (void)update:(SPEnterFrameEvent*)event{
+       static int frameCount = 0;
+       static double totalTime = 0;
+       totalTime += event.passedTime;
+       if (++frameCount % 60 == 0){
+               self.text = [NSString stringWithFormat:@"FPS: %f", frameCount/totalTime];
+               frameCount = totalTime = 0;
+       }
+}
+
+- (void)dealloc{
+       [self removeEventListener:@selector(update:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
+       [super dealloc];
+}
+
+@end
diff --git a/libs/sparrow/src/Extras/SparrowExtras.h b/libs/sparrow/src/Extras/SparrowExtras.h
new file mode 100644 (file)
index 0000000..a597333
--- /dev/null
@@ -0,0 +1,8 @@
+#import "Sparrow.h"
+
+#import "SHCircle.h"
+#import "SHLine.h"
+#import "SHPolygon.h"
+#import "BEParallaxSprite.h"
+#import "SXFPSMeter.h"
+