src/joystick/iphoneos/SDLUIAccelerationDelegate.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #import "SDLUIAccelerationDelegate.h"
    23 /* needed for SDL_IPHONE_MAX_GFORCE macro */
    24 #import "../../../include/SDL_config_iphoneos.h"
    25 
    26 static SDLUIAccelerationDelegate *sharedDelegate=nil;
    27 
    28 @implementation SDLUIAccelerationDelegate
    29 
    30 /*
    31 	Returns a shared instance of the SDLUIAccelerationDelegate, creating the shared delegate if it doesn't exist yet.
    32 */
    33 +(SDLUIAccelerationDelegate *)sharedDelegate {
    34 	if (sharedDelegate == nil) {
    35 		sharedDelegate = [[SDLUIAccelerationDelegate alloc] init];
    36 	}
    37 	return sharedDelegate;
    38 }
    39 /*
    40 	UIAccelerometerDelegate delegate method.  Invoked by the UIAccelerometer instance when it has new data for us.
    41 	We just take the data and mark that we have new data available so that the joystick system will pump it to the
    42 	events system when SDL_SYS_JoystickUpdate is called.
    43 */	
    44 -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    45 		
    46 	x = acceleration.x;
    47 	y = acceleration.y;
    48 	z = acceleration.z;
    49 	
    50 	hasNewData = YES;	
    51 }
    52 /* 
    53 	getLastOrientation -- put last obtained accelerometer data into Sint16 array
    54  
    55 	Called from the joystick system when it needs the accelerometer data.
    56 	Function grabs the last data sent to the accelerometer and converts it 
    57 	from floating point to Sint16, which is what the joystick system expects.
    58  
    59 	To do the conversion, the data is first clamped onto the interval
    60 	[-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
    61 	by MAX_SINT16 so that it is mapped to the full range of an Sint16.
    62  
    63 	You can customize the clamped range of this function by modifying the 
    64 	SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
    65  
    66 	Once converted to Sint16, the accelerometer data no longer has coherent units.
    67 	You can convert the data back to units of g-force by multiplying it 
    68 	in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
    69  */
    70 -(void)getLastOrientation:(Sint16 *)data {
    71 
    72 	#define MAX_SINT16 0x7FFF	
    73 
    74 	/* clamp the data */
    75 	if (x > SDL_IPHONE_MAX_GFORCE) x = SDL_IPHONE_MAX_GFORCE;
    76 	else if (x < -SDL_IPHONE_MAX_GFORCE) x = -SDL_IPHONE_MAX_GFORCE;
    77 	if (y > SDL_IPHONE_MAX_GFORCE) y = SDL_IPHONE_MAX_GFORCE;
    78 	else if (y < -SDL_IPHONE_MAX_GFORCE) y = -SDL_IPHONE_MAX_GFORCE;
    79 	if (z > SDL_IPHONE_MAX_GFORCE) z = SDL_IPHONE_MAX_GFORCE;
    80 	else if (z < -SDL_IPHONE_MAX_GFORCE) z = -SDL_IPHONE_MAX_GFORCE;
    81 	
    82 	/* pass in data mapped to range of SInt16 */
    83 	data[0] = (x / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
    84 	data[1] = (y / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
    85 	data[2] = (z / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
    86 
    87 }
    88 
    89 /*
    90 	Initialize SDLUIAccelerationDelegate.  Since we don't have any data yet,
    91 	just set our last received data to zero, and indicate we don't have any;
    92 */
    93 -(id)init {
    94 	self = [super init];
    95 	x = y = z = 0.0;
    96 	hasNewData = NO;
    97 	return self;
    98 }
    99 
   100 -(void)dealloc {
   101 	sharedDelegate = nil;
   102 	[self shutdown];
   103 	[super dealloc];
   104 }
   105 
   106 /*
   107 	Lets our delegate start receiving accelerometer updates.
   108 */
   109 -(void)startup {
   110 	[UIAccelerometer sharedAccelerometer].delegate = self;
   111 	isRunning = YES;
   112 }
   113 /*
   114 	Stops our delegate from receiving accelerometer updates.
   115 */
   116 -(void)shutdown {
   117 	if ([UIAccelerometer sharedAccelerometer].delegate == self) {
   118 		[UIAccelerometer sharedAccelerometer].delegate = nil;
   119 	}
   120 	isRunning = NO;
   121 }
   122 /*
   123 	Our we currently receiving accelerometer updates?
   124 */
   125 -(BOOL)isRunning {
   126 	return isRunning;
   127 }
   128 /*
   129 	Do we have any data that hasn't been pumped into SDL's event system?
   130 */
   131 -(BOOL)hasNewData {
   132 	return hasNewData;
   133 }
   134 /*
   135 	When the joystick system grabs the new data, it sets this to NO.
   136 */
   137 -(void)setHasNewData:(BOOL)value {
   138 	hasNewData = value;
   139 }
   140 
   141 @end