src/joystick/iphoneos/SDLUIAccelerationDelegate.m
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 15 Aug 2008 00:46:58 +0000
branchgsoc2008_iphone
changeset 2427 32b9909db651
parent 2362 44fc2537ff9e
child 2439 78bddf0c0fa2
permissions -rw-r--r--
changed macro MAX_G_FORCE to SDL_IPHONE_MAX_GFORCE and moved it to the SDL_config_iphoneos.h file. This should allow users to convert between the Sint16 returned by polling the joystick and units of g-force, which better describe what is going on with the iPhone (and are what the iPhone OS actually gives you). This conversion wouldn't be necessary except that we'd need floating point to store everything as g-force.

//
//  SDLUIAccelerationDelegate.m
//  iPodSDL
//
//  Created by Holmes Futrell on 6/21/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SDLUIAccelerationDelegate.h"
#import "../../../include/SDL_config_iphoneos.h"

static SDLUIAccelerationDelegate *sharedDelegate=nil;

@implementation SDLUIAccelerationDelegate

+(SDLUIAccelerationDelegate *)sharedDelegate {
	if (sharedDelegate == nil) {
		sharedDelegate = [[SDLUIAccelerationDelegate alloc] init];
	}
	return sharedDelegate;
}

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
		
	x = acceleration.x;
	y = acceleration.y;
	z = acceleration.z;
	
	hasNewData = YES;
	//timestamp = acceleration.timestamp;
	
}

-(void)getLastOrientation:(Sint16 *)data {

	#define MAX_SINT16 0x7FFF	

	if (x > SDL_IPHONE_MAX_GFORCE) x = SDL_IPHONE_MAX_GFORCE;
	else if (x < -SDL_IPHONE_MAX_GFORCE) x = -SDL_IPHONE_MAX_GFORCE;

	if (y > SDL_IPHONE_MAX_GFORCE) y = SDL_IPHONE_MAX_GFORCE;
	else if (y < -SDL_IPHONE_MAX_GFORCE) y = -SDL_IPHONE_MAX_GFORCE;

	if (z > SDL_IPHONE_MAX_GFORCE) z = SDL_IPHONE_MAX_GFORCE;
	else if (z < -SDL_IPHONE_MAX_GFORCE) z = -SDL_IPHONE_MAX_GFORCE;
	
	data[0] = (x / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
	data[1] = (y / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
	data[2] = (z / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;

}

-(id)init {

	self = [super init];
	x = y = z = 0.0;
	return self;
	
}

-(void)dealloc {
	sharedDelegate = nil;
	[super dealloc];
}

-(void)startup {
	[UIAccelerometer sharedAccelerometer].delegate = self;
	isRunning = YES;
}

-(void)shutdown {
	[UIAccelerometer sharedAccelerometer].delegate = nil;
	isRunning = NO;
}

-(BOOL)isRunning {
	return isRunning;
}

-(BOOL)hasNewData {
	return hasNewData;
}

-(void)setHasNewData:(BOOL)value {
	hasNewData = value;
}

@end