Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support files for Accelerometer Simulation for iPhone Simulator an... #760

Merged
merged 1 commit into from Mar 9, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions cocos2dx/platform/ios/Simulation/AccelerometerSimulation.h
@@ -0,0 +1,59 @@
/*
* AccelerometerSimulation.h
* AccelerometerGraph
*
* Created by Otto Chrons on 9/26/08.
* Copyright 2008 Seastringo Oy. All rights reserved.
*
*/
#import <TargetConditionals.h>

// when compiling to ARM (iPhone device), hide everything and use system defaults
// if you wish to use simulation mode even on the device, remove the #if/#endif
#if !TARGET_CPU_ARM
#import <UIKit/UIKit.h>

// this is exactly the same as UIAcceleration, but we can modify the member variables
@interface UIAccelerationSimulation: NSObject
{
NSTimeInterval timestamp;
UIAccelerationValue x, y, z;
}
@property(nonatomic, readonly) NSTimeInterval timestamp;
@property(nonatomic, readonly) UIAccelerationValue x, y, z;

@end

// override UIAccelerometer behaviour
@interface UIAccelerometer (Simulation)
+ (UIAccelerometer *)sharedAccelerometer;
@end

// our own version of the Accelerometer
@interface AccelerometerSimulation : UIAccelerometer <NSMachPortDelegate>
{

//CFSocketRef udpSocket;
int udpSocket;
NSThread *thread;
BOOL isExiting;
id<UIAccelerometerDelegate> accelDelegate;
UIAccelerationSimulation *accObject;
// Threaded notification support
NSMutableArray *notifications;
NSThread *notificationThread;
NSLock *notificationLock;
NSMachPort *notificationPort;
}

@property(nonatomic, assign) id<UIAccelerometerDelegate> delegate;

- (void) setUpThreadingSupport;
- (void) handleMachMessage:(void *) msg;
- (void) processNotification:(NSNotification *) notification;
+ (AccelerometerSimulation *)getAccelerometer;
- (AccelerometerSimulation *)initialize;

@end

#endif
238 changes: 238 additions & 0 deletions cocos2dx/platform/ios/Simulation/AccelerometerSimulation.m
@@ -0,0 +1,238 @@
//
// AccelerometerSimulation.m
// AccelerometerGraph
//
// Created by Otto Chrons on 9/26/08.
// Copyright 2008 Seastringo Oy. All rights reserved.
//

#import "AccelerometerSimulation.h"

// when compiling to ARM (iPhone device), hide everything and use system defaults
// if you wish to use simulation mode even on the device, remove the #if/#endif
#if !TARGET_CPU_ARM

#import <netdb.h>

#define kAccelerometerSimulationPort 10552

@implementation UIAccelerationSimulation

@synthesize timestamp;
@synthesize x;
@synthesize y;
@synthesize z;

-(UIAccelerationSimulation*)initWithTimestamp:(NSTimeInterval)aTimeStamp
X:(UIAccelerationValue)ax
Y:(UIAccelerationValue)ay
Z:(UIAccelerationValue)az
{
timestamp = aTimeStamp;
x = ax;
y = ay;
z = az;

return self;
}
@end

@implementation UIAccelerometer (Simulation)

// override the static method and return our simulated version instead
+ (UIAccelerometer *)sharedAccelerometer
{
return [AccelerometerSimulation getAccelerometer];
}
@end
/*
// callback that never got called with CFSocket UDP...

void mySocketCallBack( CFSocketRef s,
CFSocketCallBackType callbackType,
CFDataRef address,
const void *data,
void *info)
{
AccelerometerSimulation *accSim = (AccelerometerSimulation*)info;

NSLog(@"Data %s received", (char*)data);
}
*/

// singleton
static AccelerometerSimulation *sharedAccelerometer = NULL;

@implementation AccelerometerSimulation

// this is straight from developer guide example for multi-threaded notifications
- (void) setUpThreadingSupport {
if ( notifications ) return;

notifications = [[NSMutableArray alloc] init];
notificationLock = [[NSLock alloc] init];
notificationThread = [[NSThread currentThread] retain];

notificationPort = [[NSMachPort alloc] init];
[notificationPort setDelegate:self];
[[NSRunLoop currentRunLoop] addPort:notificationPort
forMode:(NSString *) kCFRunLoopCommonModes];
}

// this is straight from developer guide example

- (void) processNotification:(NSNotification *) notification {
if( [NSThread currentThread] != notificationThread ) {
// Forward the notification to the correct thread, this is the socket thread
NSDate* date = [[NSDate alloc] init];
[notificationLock lock];
[notifications addObject:notification];
[notificationLock unlock];
[notificationPort sendBeforeDate:date
components:nil
from:nil
reserved:0];
[date release];
}
else {
// now we are in the main thread
// Process the notification here;
NSString *data = (NSString*)[notification object];

// parse the data, no error handling!
NSArray *components = [data componentsSeparatedByString:@","];

// create our own acceleration object
[accObject initWithTimestamp:[[components objectAtIndex:1] doubleValue]
X:[[components objectAtIndex:2] doubleValue]
Y:[[components objectAtIndex:3] doubleValue]
Z:[[components objectAtIndex:4] doubleValue]];
[accelDelegate accelerometer:self didAccelerate:(UIAcceleration*)accObject];
}
}

// this is straight from developer guide example
- (void) handleMachMessage:(void *) msg {
[notificationLock lock];
while ( [notifications count] ) {
NSNotification *notification = [[notifications objectAtIndex:0] retain];
[notifications removeObjectAtIndex:0];
[notificationLock unlock];
[self processNotification:notification];
[notification release];
[notificationLock lock];
};
[notificationLock unlock];
}

+ (AccelerometerSimulation *)getAccelerometer
{
if( sharedAccelerometer == NULL )
sharedAccelerometer = [[AccelerometerSimulation alloc] initialize];

return sharedAccelerometer;
}

- (void)threadLoop:(id)object
{
char buffer[1024];
// we never exit...
while(1) {
int count = recv( udpSocket, buffer, sizeof(buffer), 0 );
if( count > 0 )
{
// got data, let's pass it on
buffer[count] = 0;
NSString *str = [[NSString alloc] initWithUTF8String:buffer];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ThreadAccelNotification" object:str];
[str release];
}

}
}

// initialize our version of the accelerometer
- (AccelerometerSimulation *)initialize
{
accObject = [UIAccelerationSimulation alloc];
isExiting = false;

// couldn't get the CFSocket version to work with UDP and runloop, so used Berkely sockets and a thread instead

udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
// listen on all interfaces
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_len = sizeof(struct sockaddr_in);
sin.sin_family = AF_INET;
sin.sin_port = htons(kAccelerometerSimulationPort);

bind(udpSocket, (const struct sockaddr*)&sin, sizeof(sin));

// create a separate thread for receiving UDP packets
thread = [[NSThread alloc] initWithTarget:self
selector:@selector(threadLoop:)
object:nil];
[thread start];

// cross-thread communication setup
[self setUpThreadingSupport];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(processNotification:)
name:@"ThreadAccelNotification"
object:nil];
/*
// create and initialize a socket
CFSocketContext ctx;

ctx.info = self;
ctx.version = 0;
ctx.retain = NULL;
ctx.release = NULL;
ctx.copyDescription = NULL;
udpSocket = CFSocketCreate(NULL, PF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketDataCallBack | 0xF, mySocketCallBack, NULL);

CFRunLoopSourceRef source;
CFDataRef addr;
CFSocketError theErr;
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_len = sizeof(struct sockaddr_in);
sin.sin_family = AF_INET;
sin.sin_port = htons(10552);
addr = CFDataCreate(NULL, (unsigned char *)&sin, sizeof(sin));
theErr = CFSocketConnectToAddress(udpSocket, addr, 0);
switch (theErr) {
case kCFSocketSuccess:
NSLog(@"UDP Logged in");
source = CFSocketCreateRunLoopSource(NULL, udpSocket, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), source,
kCFRunLoopDefaultMode);
break;
case kCFSocketError:
NSLog(@"UDP Error");
break;
default:
NSLog(@"UDP Networking Error");
break;
}
*/
return self;
}

// we grab the delegate setting action
- (void)setDelegate:(id<UIAccelerometerDelegate>)delegate
{
accelDelegate = delegate;
}

- (id<UIAccelerometerDelegate>)delegate
{
return accelDelegate;
}
@end

#endif
3 changes: 3 additions & 0 deletions tests/test.ios/main.m
Expand Up @@ -8,6 +8,9 @@

#import <UIKit/UIKit.h>

// Under iOS and the Simulator, we can use an alternate Accelerometer interface
#import "cocos2dx/platform/ios/Simulation/AccelerometerSimulation.h"

int main(int argc, char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Expand Down
14 changes: 14 additions & 0 deletions tests/test.ios/test.xcodeproj/project.pbxproj
Expand Up @@ -12,6 +12,7 @@
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; };
288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 288765A40DF7441C002DB57D /* CoreGraphics.framework */; };
7ECAE73914F52FB100A57185 /* AccelerometerSimulation.m in Sources */ = {isa = PBXBuildFile; fileRef = 7ECAE73814F52FB100A57185 /* AccelerometerSimulation.m */; };
78891AAE14EA1DAE0072482D /* Icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 78891AAD14EA1DAE0072482D /* Icon.png */; };
78891AB214EA1DC30072482D /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = 78891AB114EA1DC30072482D /* Default.png */; };
789B545214EBA942008E7E29 /* animations in Resources */ = {isa = PBXBuildFile; fileRef = 789B545114EBA942008E7E29 /* animations */; };
Expand Down Expand Up @@ -570,6 +571,8 @@
288765A40DF7441C002DB57D /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = ../main.m; sourceTree = "<group>"; };
32CA4F630368D1EE00C91783 /* iphone_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = iphone_Prefix.pch; path = ../iphone_Prefix.pch; sourceTree = "<group>"; };
7ECAE73714F52FB100A57185 /* AccelerometerSimulation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AccelerometerSimulation.h; sourceTree = "<group>"; };
7ECAE73814F52FB100A57185 /* AccelerometerSimulation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AccelerometerSimulation.m; sourceTree = "<group>"; };
78891AAD14EA1DAE0072482D /* Icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = Icon.png; path = ../test.ios/Icon.png; sourceTree = "<group>"; };
78891AB114EA1DC30072482D /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = Default.png; path = ../test.ios/Default.png; sourceTree = "<group>"; };
789B545114EBA942008E7E29 /* animations */ = {isa = PBXFileReference; lastKnownFileType = folder; path = animations; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1310,6 +1313,15 @@
name = Frameworks;
sourceTree = "<group>";
};
7ECAE73614F52FB100A57185 /* Simulation */ = {
isa = PBXGroup;
children = (
7ECAE73714F52FB100A57185 /* AccelerometerSimulation.h */,
7ECAE73814F52FB100A57185 /* AccelerometerSimulation.m */,
);
path = Simulation;
sourceTree = "<group>";
};
78C7DDAA14EBA5050085D0C2 /* Resources */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2489,6 +2501,7 @@
BF83594513276C7700F3C033 /* ios */ = {
isa = PBXGroup;
children = (
7ECAE73614F52FB100A57185 /* Simulation */,
AD7C5EB514750E2E008E5647 /* CCThread_ios.mm */,
BF1E7A4813E2637200C2D68B /* FontLabel */,
BFA00A1E134426C600289DC3 /* CCCommon_ios.mm */,
Expand Down Expand Up @@ -3128,6 +3141,7 @@
BF1E7BC513E273DF00C2D68B /* FontTest.cpp in Sources */,
BF1B49F013FE04C300838E41 /* CurrentLanguageTest.cpp in Sources */,
ADF1707D1474F632004E9212 /* TextureCacheTest.cpp in Sources */,
7ECAE73914F52FB100A57185 /* AccelerometerSimulation.m in Sources */,
78C7E02A14EBA5580085D0C2 /* ExtensionsTest.cpp in Sources */,
78C7E02B14EBA5580085D0C2 /* NotificationCenterTest.cpp in Sources */,
);
Expand Down