Cleaned up code, added comments. gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 15 Aug 2008 19:16:01 +0000
branchgsoc2008_iphone
changeset 243978bddf0c0fa2
parent 2438 fe9d15725d96
child 2440 0a0205afed91
Cleaned up code, added comments.
src/joystick/iphoneos/SDLUIAccelerationDelegate.h
src/joystick/iphoneos/SDLUIAccelerationDelegate.m
     1.1 --- a/src/joystick/iphoneos/SDLUIAccelerationDelegate.h	Fri Aug 15 18:51:28 2008 +0000
     1.2 +++ b/src/joystick/iphoneos/SDLUIAccelerationDelegate.h	Fri Aug 15 19:16:01 2008 +0000
     1.3 @@ -1,18 +1,31 @@
     1.4 -//
     1.5 -//  SDLUIAccelerationDelegate.h
     1.6 -//  iPodSDL
     1.7 -//
     1.8 -//  Created by Holmes Futrell on 6/21/08.
     1.9 -//  Copyright 2008 __MyCompanyName__. All rights reserved.
    1.10 -//
    1.11 +/*
    1.12 + SDL - Simple DirectMedia Layer
    1.13 + Copyright (C) 1997-2006 Sam Lantinga
    1.14 + 
    1.15 + This library is free software; you can redistribute it and/or
    1.16 + modify it under the terms of the GNU Lesser General Public
    1.17 + License as published by the Free Software Foundation; either
    1.18 + version 2.1 of the License, or (at your option) any later version.
    1.19 + 
    1.20 + This library is distributed in the hope that it will be useful,
    1.21 + but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.22 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.23 + Lesser General Public License for more details.
    1.24 + 
    1.25 + You should have received a copy of the GNU Lesser General Public
    1.26 + License along with this library; if not, write to the Free Software
    1.27 + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.28 + 
    1.29 + Sam Lantinga
    1.30 + slouken@libsdl.org
    1.31 + */
    1.32  
    1.33  #import <UIKit/UIKit.h>
    1.34 -#import "SDL_stdinc.h"
    1.35 +#import <SDL_types.h>
    1.36  
    1.37  @interface SDLUIAccelerationDelegate: NSObject <UIAccelerometerDelegate> {
    1.38  
    1.39  	UIAccelerationValue x, y, z;
    1.40 -	//NSTimeInterval timestamp;
    1.41  	BOOL isRunning;
    1.42  	BOOL hasNewData;
    1.43  	
     2.1 --- a/src/joystick/iphoneos/SDLUIAccelerationDelegate.m	Fri Aug 15 18:51:28 2008 +0000
     2.2 +++ b/src/joystick/iphoneos/SDLUIAccelerationDelegate.m	Fri Aug 15 19:16:01 2008 +0000
     2.3 @@ -1,86 +1,140 @@
     2.4 -//
     2.5 -//  SDLUIAccelerationDelegate.m
     2.6 -//  iPodSDL
     2.7 -//
     2.8 -//  Created by Holmes Futrell on 6/21/08.
     2.9 -//  Copyright 2008 __MyCompanyName__. All rights reserved.
    2.10 -//
    2.11 +/*
    2.12 + SDL - Simple DirectMedia Layer
    2.13 + Copyright (C) 1997-2006 Sam Lantinga
    2.14 + 
    2.15 + This library is free software; you can redistribute it and/or
    2.16 + modify it under the terms of the GNU Lesser General Public
    2.17 + License as published by the Free Software Foundation; either
    2.18 + version 2.1 of the License, or (at your option) any later version.
    2.19 + 
    2.20 + This library is distributed in the hope that it will be useful,
    2.21 + but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.22 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.23 + Lesser General Public License for more details.
    2.24 + 
    2.25 + You should have received a copy of the GNU Lesser General Public
    2.26 + License along with this library; if not, write to the Free Software
    2.27 + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.28 + 
    2.29 + Sam Lantinga
    2.30 + slouken@libsdl.org
    2.31 + */
    2.32  
    2.33  #import "SDLUIAccelerationDelegate.h"
    2.34 +/* needed for SDL_IPHONE_MAX_GFORCE macro */
    2.35  #import "../../../include/SDL_config_iphoneos.h"
    2.36  
    2.37  static SDLUIAccelerationDelegate *sharedDelegate=nil;
    2.38  
    2.39  @implementation SDLUIAccelerationDelegate
    2.40  
    2.41 +/*
    2.42 +	Returns a shared instance of the SDLUIAccelerationDelegate, creating the shared delegate if it doesn't exist yet.
    2.43 +*/
    2.44  +(SDLUIAccelerationDelegate *)sharedDelegate {
    2.45  	if (sharedDelegate == nil) {
    2.46  		sharedDelegate = [[SDLUIAccelerationDelegate alloc] init];
    2.47  	}
    2.48  	return sharedDelegate;
    2.49  }
    2.50 -
    2.51 +/*
    2.52 +	UIAccelerometerDelegate delegate method.  Invoked by the UIAccelerometer instance when it has new data for us.
    2.53 +	We just take the data and mark that we have new data available so that the joystick system will pump it to the
    2.54 +	events system when SDL_SYS_JoystickUpdate is called.
    2.55 +*/	
    2.56  -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    2.57  		
    2.58  	x = acceleration.x;
    2.59  	y = acceleration.y;
    2.60  	z = acceleration.z;
    2.61  	
    2.62 -	hasNewData = YES;
    2.63 -	//timestamp = acceleration.timestamp;
    2.64 -	
    2.65 +	hasNewData = YES;	
    2.66  }
    2.67 -
    2.68 +/* 
    2.69 +	getLastOrientation -- put last obtained accelerometer data into Sint16 array
    2.70 + 
    2.71 +	Called from the joystick system when it needs the accelerometer data.
    2.72 +	Function grabs the last data sent to the accelerometer and converts it 
    2.73 +	from floating point to Sint16, which is what the joystick system expects.
    2.74 + 
    2.75 +	To do the conversion, the data is first clamped onto the interval
    2.76 +	[-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
    2.77 +	by MAX_SINT16 so that it is mapped to the full range of an Sint16.
    2.78 + 
    2.79 +	You can customize the clamped range of this function by modifying the 
    2.80 +	SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
    2.81 + 
    2.82 +	Once converted to Sint16, the accelerometer data no longer has coherent units.
    2.83 +	You can convert the data back to units of g-force by multiplying it 
    2.84 +	in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
    2.85 + */
    2.86  -(void)getLastOrientation:(Sint16 *)data {
    2.87  
    2.88  	#define MAX_SINT16 0x7FFF	
    2.89  
    2.90 +	/* clamp the data */
    2.91  	if (x > SDL_IPHONE_MAX_GFORCE) x = SDL_IPHONE_MAX_GFORCE;
    2.92  	else if (x < -SDL_IPHONE_MAX_GFORCE) x = -SDL_IPHONE_MAX_GFORCE;
    2.93 -
    2.94  	if (y > SDL_IPHONE_MAX_GFORCE) y = SDL_IPHONE_MAX_GFORCE;
    2.95  	else if (y < -SDL_IPHONE_MAX_GFORCE) y = -SDL_IPHONE_MAX_GFORCE;
    2.96 -
    2.97  	if (z > SDL_IPHONE_MAX_GFORCE) z = SDL_IPHONE_MAX_GFORCE;
    2.98  	else if (z < -SDL_IPHONE_MAX_GFORCE) z = -SDL_IPHONE_MAX_GFORCE;
    2.99  	
   2.100 +	/* pass in data mapped to range of SInt16 */
   2.101  	data[0] = (x / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
   2.102  	data[1] = (y / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
   2.103  	data[2] = (z / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
   2.104  
   2.105  }
   2.106  
   2.107 +/*
   2.108 +	Initialize SDLUIAccelerationDelegate.  Since we don't have any data yet,
   2.109 +	just set our last received data to zero, and indicate we don't have any;
   2.110 +*/
   2.111  -(id)init {
   2.112 -
   2.113  	self = [super init];
   2.114  	x = y = z = 0.0;
   2.115 +	hasNewData = NO;
   2.116  	return self;
   2.117 -	
   2.118  }
   2.119  
   2.120  -(void)dealloc {
   2.121  	sharedDelegate = nil;
   2.122 +	[self shutdown];
   2.123  	[super dealloc];
   2.124  }
   2.125  
   2.126 +/*
   2.127 +	Lets our delegate start receiving accelerometer updates.
   2.128 +*/
   2.129  -(void)startup {
   2.130  	[UIAccelerometer sharedAccelerometer].delegate = self;
   2.131  	isRunning = YES;
   2.132  }
   2.133 -
   2.134 +/*
   2.135 +	Stops our delegate from receiving accelerometer updates.
   2.136 +*/
   2.137  -(void)shutdown {
   2.138 -	[UIAccelerometer sharedAccelerometer].delegate = nil;
   2.139 +	if ([UIAccelerometer sharedAccelerometer].delegate == self) {
   2.140 +		[UIAccelerometer sharedAccelerometer].delegate = nil;
   2.141 +	}
   2.142  	isRunning = NO;
   2.143  }
   2.144 -
   2.145 +/*
   2.146 +	Our we currently receiving accelerometer updates?
   2.147 +*/
   2.148  -(BOOL)isRunning {
   2.149  	return isRunning;
   2.150  }
   2.151 -
   2.152 +/*
   2.153 +	Do we have any data that hasn't been pumped into SDL's event system?
   2.154 +*/
   2.155  -(BOOL)hasNewData {
   2.156  	return hasNewData;
   2.157  }
   2.158 -
   2.159 +/*
   2.160 +	When the joystick system grabs the new data, it sets this to NO.
   2.161 +*/
   2.162  -(void)setHasNewData:(BOOL)value {
   2.163  	hasNewData = value;
   2.164  }