Skip to content

Commit

Permalink
(x): modernized Objective-C code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Panajev committed Oct 21, 2012
1 parent e09ff70 commit 2eaf95c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 62 deletions.
5 changes: 1 addition & 4 deletions CCBlade.h
Expand Up @@ -37,14 +37,10 @@ inline void CGPointSet(CGPoint *v, float x, float y);
inline void f1(CGPoint p1, CGPoint p2, float d, CGPoint *o1, CGPoint *o2);

@interface CCBlade : CCNode {
NSMutableArray *path;
unsigned int pointLimit;
int count;
CGPoint *vertices;
CGPoint *coordinates;
BOOL reset;
CCTexture2D *_texture;
float width;
BOOL _finish;
BOOL _willPop;

Expand All @@ -55,6 +51,7 @@ inline void f1(CGPoint p1, CGPoint p2, float d, CGPoint *o1, CGPoint *o2);
@property(strong) CCTexture2D *texture;
@property(nonatomic) float width;
@property (nonatomic) BOOL autoDim;
@property(nonatomic,strong)NSMutableArray *path;

+ (id) bladeWithMaximumPoint:(int) limit;
- (id) initWithMaximumPoint:(int) limit;
Expand Down
85 changes: 40 additions & 45 deletions CCBlade.m
Expand Up @@ -60,10 +60,6 @@ inline void CGPointSet(CGPoint *v, float x, float y){
}

@implementation CCBlade
@synthesize texture = _texture;
@synthesize pointLimit;
@synthesize width;
@synthesize autoDim;

+ (id) bladeWithMaximumPoint:(int) limit{
return [[self alloc] initWithMaximumPoint:limit];
Expand All @@ -74,16 +70,16 @@ + (id) bladeWithMaximumPoint:(int) limit{
- (id) initWithMaximumPoint:(int) limit{
self = [super init];

pointLimit = limit;
self.width = 5;
_pointLimit = limit;
_width = 5;

vertices = (CGPoint *)calloc(2*limit+5, sizeof(vertices[0]));
coordinates = (CGPoint *)calloc(2*limit+5, sizeof(coordinates[0]));

CGPointSet(coordinates+0, 0.00, 0.5);
reset = NO;

path = [[NSMutableArray alloc] init];
_path = [[NSMutableArray alloc] init];

#if USE_UPDATE_FOR_POP
popTimeInterval = POP_TIME_INTERVAL;
Expand All @@ -92,6 +88,8 @@ - (id) initWithMaximumPoint:(int) limit{
[self scheduleUpdateWithPriority:0];
#endif

self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTexture];

return self;
}

Expand All @@ -102,40 +100,40 @@ - (void) dealloc{
}

- (void) populateVertices{
vertices[0] = [[path objectAtIndex:0] CGPointValue];
vertices[0] = [[_path objectAtIndex:0] CGPointValue];
CGPoint pre = vertices[0];

unsigned int i = 0;
CGPoint it = [[path objectAtIndex:1] CGPointValue];
float dd = width / [path count];
while (i < [path count] - 2){
f1(pre, it, width - i * dd , vertices+2*i+1, vertices+2*i+2);
CGPoint it = [[_path objectAtIndex:1] CGPointValue];
float dd = _width / [_path count];
while (i < [_path count] - 2){
f1(pre, it, _width - i * dd , vertices+2*i+1, vertices+2*i+2);
CGPointSet(coordinates+2*i+1, .5, 1.0);
CGPointSet(coordinates+2*i+2, .5, 0.0);

i++;
pre = it;

it = [[path objectAtIndex:i+1] CGPointValue];
it = [[_path objectAtIndex:i+1] CGPointValue];
}

CGPointSet(coordinates+1, 0.25, 1.0);
CGPointSet(coordinates+2, 0.25, 0.0);

vertices[2*[path count]-3] = it;
CGPointSet(coordinates+2*[path count]-3, 0.75, 0.5);
vertices[2*[_path count]-3] = it;
CGPointSet(coordinates+2*[_path count]-3, 0.75, 0.5);
}

- (void) shift{
int index = 2 * pointLimit - 1;
int index = 2 * _pointLimit - 1;
for (int i = index; i > 3; i -= 2) {
vertices[i] = vertices[i-2];
vertices[i-1] = vertices[i-3];
}
}

- (void) setWidth:(float)width_{
width = width_ ;//* CC_CONTENT_SCALE_FACTOR();
- (void) set_width:(float)newWidth{
_width = newWidth ;//* CC_CONTENT_SCALE_FACTOR();
}

#define DISTANCE_TO_INTERPOLATE 10
Expand All @@ -146,37 +144,34 @@ - (void) push:(CGPoint) v{
if (reset) {
return;
}
if (CC_CONTENT_SCALE_FACTOR() != 1.0f) {
//v = ccpMult(v, CC_CONTENT_SCALE_FACTOR());
}

#if USE_LAGRANGE

if ([path count] == 0) {
[path insertObject:[NSValue valueWithCGPoint:v] atIndex:0];
if ([_path count] == 0) {
[_path insertObject:[NSValue valueWithCGPoint:v] atIndex:0];
return;
}

CGPoint first = [[path objectAtIndex:0] CGPointValue];
CGPoint first = [[_path objectAtIndex:0] CGPointValue];
if (ccpDistance(v, first) < DISTANCE_TO_INTERPOLATE) {
[path insertObject:[NSValue valueWithCGPoint:v] atIndex:0];
if ([path count] > pointLimit) {
[path removeLastObject];
[_path insertObject:[NSValue valueWithCGPoint:v] atIndex:0];
if ([_path count] > _pointLimit) {
[_path removeLastObject];
}
}else{
int num = ccpDistance(v, first) / DISTANCE_TO_INTERPOLATE;
CGPoint iv = ccpMult(ccpSub(v, first), (float)1./(num + 1));
for (int i = 1; i <= num + 1; i++) {
[path insertObject:[NSValue valueWithCGPoint:ccpAdd(first, ccpMult(iv, i))] atIndex:0];
[_path insertObject:[NSValue valueWithCGPoint:ccpAdd(first, ccpMult(iv, i))] atIndex:0];
}
while ([path count] > pointLimit) {
[path removeLastObject];
while ([_path count] > _pointLimit) {
[_path removeLastObject];
}
}
#else // !USE_LAGRANGE
path.push_front(v);
if (path.size() > pointLimit) {
path.pop_back();
_path.push_front(v);
if (_path.size() > pointLimit) {
_path.pop_back();
}
#endif // !USE_LAGRANGE

Expand All @@ -185,18 +180,18 @@ - (void) push:(CGPoint) v{
}

- (void) pop:(int) n{
while ([path count] > 0 && n > 0) {
[path removeLastObject];
while ([_path count] > 0 && n > 0) {
[_path removeLastObject];
n--;
}

if ([path count] > 2) {
if ([_path count] > 2) {
[self populateVertices];
}
}

- (void) clear{
[path removeAllObjects];
[_path removeAllObjects];
reset = NO;
if (_finish)
[self removeFromParentAndCleanup:YES];
Expand All @@ -222,9 +217,9 @@ - (void) update:(ccTime)dt {

for (int pop = 0; pop < numberOfPops; pop++) {

if ((reset && [path count] > 0) || (self.autoDim && _willPop)) {
if ((reset && [_path count] > 0) || (self.autoDim && _willPop)) {
[self pop:1];
if ([path count] < 3) {
if ([_path count] < 3) {
[self clear];
if (_finish) {
return; // if we continue self will have been deallocated
Expand All @@ -238,9 +233,9 @@ - (void) update:(ccTime)dt {
- (void) draw{

#if !USE_UPDATE_FOR_POP
if ((reset && [path count] > 0) || (self.autoDim && _willPop)) {
if ((reset && [_path count] > 0) || (self.autoDim && _willPop)) {
[self pop:1];
if ([path count] < 3) {
if ([_path count] < 3) {
[self clear];
if (_finish) {
return; // if we continue self will have been deallocated
Expand All @@ -249,14 +244,14 @@ - (void) draw{
}
#endif

if(path == nil)
if(_path == nil)
return;

if ([path count] < 3) {
if ([_path count] < 3) {
return;
}

willPop = YES;
_willPop = YES;
CC_NODE_DRAW_SETUP();
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords);

Expand All @@ -267,7 +262,7 @@ - (void) draw{
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, coordinates);


glDrawArrays(GL_TRIANGLE_STRIP, 0, 2*[path count]-2);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 2*[_path count]-2);

CC_INCREMENT_GL_DRAWS(1);
}
Expand Down
20 changes: 8 additions & 12 deletions Example/Example.xcodeproj/project.pbxproj
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 45;
objectVersion = 46;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -218,8 +218,11 @@
/* Begin PBXProject section */
29B97313FDCFA39411CA2CEA /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0450;
};
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "Example" */;
compatibilityVersion = "Xcode 3.1";
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 1;
knownRegions = (
Expand Down Expand Up @@ -290,19 +293,17 @@
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_FIX_AND_CONTINUE = NO;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = Example_Prefix.pch;
GCC_TREAT_WARNINGS_AS_ERRORS = NO;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
HEADER_SEARCH_PATHS = "~/Programming/SharedLibs/Cocos/include/**";
INFOPLIST_FILE = Resources/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 3.1;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
LIBRARY_SEARCH_PATHS = "~/Programming/SharedLibs/Cocos/lib/**";
ONLY_ACTIVE_ARCH = YES;
ONLY_ACTIVE_ARCH = NO;
OTHER_LDFLAGS = "-lCocos";
PREBINDING = NO;
PRODUCT_NAME = CCBlade;
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "9FC23DC7-CB28-4671-9494-EB7B1F3ABCBE";
TARGETED_DEVICE_FAMILY = 1;
Expand All @@ -318,18 +319,15 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer: Duc Hiep Ngo (36Q6CZ7F37)";
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_ENABLE_FIX_AND_CONTINUE = NO;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = Example_Prefix.pch;
GCC_TREAT_WARNINGS_AS_ERRORS = NO;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
HEADER_SEARCH_PATHS = "~/Programming/SharedLibs/Cocos/include/**";
INFOPLIST_FILE = Resources/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 3.1;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
LIBRARY_SEARCH_PATHS = "~/Programming/SharedLibs/Cocos/lib/**";
ONLY_ACTIVE_ARCH = NO;
OTHER_LDFLAGS = "-lCocos";
PREBINDING = NO;
PRODUCT_NAME = CCBlade;
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "9FC23DC7-CB28-4671-9494-EB7B1F3ABCBE";
TARGETED_DEVICE_FAMILY = 1;
Expand All @@ -356,7 +354,6 @@
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 3.0;
ONLY_ACTIVE_ARCH = YES;
PREBINDING = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand All @@ -376,7 +373,6 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 3.0;
PREBINDING = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand Down
Binary file not shown.
5 changes: 5 additions & 0 deletions Example/Resources/Info.plist
Expand Up @@ -44,5 +44,10 @@
</dict>
<key>UIStatusBarHidden</key>
<true/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
</dict>
</plist>
1 change: 0 additions & 1 deletion Example/libs/README

This file was deleted.

0 comments on commit 2eaf95c

Please sign in to comment.