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