Mercurial > sdl-ios-xcode
comparison src/joystick/iphoneos/SDLUIAccelerationDelegate.m @ 2439:78bddf0c0fa2 gsoc2008_iphone
Cleaned up code, added comments.
author | Holmes Futrell <hfutrell@umail.ucsb.edu> |
---|---|
date | Fri, 15 Aug 2008 19:16:01 +0000 |
parents | 32b9909db651 |
children |
comparison
equal
deleted
inserted
replaced
2438:fe9d15725d96 | 2439:78bddf0c0fa2 |
---|---|
1 // | 1 /* |
2 // SDLUIAccelerationDelegate.m | 2 SDL - Simple DirectMedia Layer |
3 // iPodSDL | 3 Copyright (C) 1997-2006 Sam Lantinga |
4 // | 4 |
5 // Created by Holmes Futrell on 6/21/08. | 5 This library is free software; you can redistribute it and/or |
6 // Copyright 2008 __MyCompanyName__. All rights reserved. | 6 modify it under the terms of the GNU Lesser General Public |
7 // | 7 License as published by the Free Software Foundation; either |
8 version 2.1 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Lesser General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Lesser General Public | |
16 License along with this library; if not, write to the Free Software | |
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
8 | 22 |
9 #import "SDLUIAccelerationDelegate.h" | 23 #import "SDLUIAccelerationDelegate.h" |
24 /* needed for SDL_IPHONE_MAX_GFORCE macro */ | |
10 #import "../../../include/SDL_config_iphoneos.h" | 25 #import "../../../include/SDL_config_iphoneos.h" |
11 | 26 |
12 static SDLUIAccelerationDelegate *sharedDelegate=nil; | 27 static SDLUIAccelerationDelegate *sharedDelegate=nil; |
13 | 28 |
14 @implementation SDLUIAccelerationDelegate | 29 @implementation SDLUIAccelerationDelegate |
15 | 30 |
31 /* | |
32 Returns a shared instance of the SDLUIAccelerationDelegate, creating the shared delegate if it doesn't exist yet. | |
33 */ | |
16 +(SDLUIAccelerationDelegate *)sharedDelegate { | 34 +(SDLUIAccelerationDelegate *)sharedDelegate { |
17 if (sharedDelegate == nil) { | 35 if (sharedDelegate == nil) { |
18 sharedDelegate = [[SDLUIAccelerationDelegate alloc] init]; | 36 sharedDelegate = [[SDLUIAccelerationDelegate alloc] init]; |
19 } | 37 } |
20 return sharedDelegate; | 38 return sharedDelegate; |
21 } | 39 } |
22 | 40 /* |
41 UIAccelerometerDelegate delegate method. Invoked by the UIAccelerometer instance when it has new data for us. | |
42 We just take the data and mark that we have new data available so that the joystick system will pump it to the | |
43 events system when SDL_SYS_JoystickUpdate is called. | |
44 */ | |
23 -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { | 45 -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { |
24 | 46 |
25 x = acceleration.x; | 47 x = acceleration.x; |
26 y = acceleration.y; | 48 y = acceleration.y; |
27 z = acceleration.z; | 49 z = acceleration.z; |
28 | 50 |
29 hasNewData = YES; | 51 hasNewData = YES; |
30 //timestamp = acceleration.timestamp; | |
31 | |
32 } | 52 } |
33 | 53 /* |
54 getLastOrientation -- put last obtained accelerometer data into Sint16 array | |
55 | |
56 Called from the joystick system when it needs the accelerometer data. | |
57 Function grabs the last data sent to the accelerometer and converts it | |
58 from floating point to Sint16, which is what the joystick system expects. | |
59 | |
60 To do the conversion, the data is first clamped onto the interval | |
61 [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied | |
62 by MAX_SINT16 so that it is mapped to the full range of an Sint16. | |
63 | |
64 You can customize the clamped range of this function by modifying the | |
65 SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h. | |
66 | |
67 Once converted to Sint16, the accelerometer data no longer has coherent units. | |
68 You can convert the data back to units of g-force by multiplying it | |
69 in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF. | |
70 */ | |
34 -(void)getLastOrientation:(Sint16 *)data { | 71 -(void)getLastOrientation:(Sint16 *)data { |
35 | 72 |
36 #define MAX_SINT16 0x7FFF | 73 #define MAX_SINT16 0x7FFF |
37 | 74 |
75 /* clamp the data */ | |
38 if (x > SDL_IPHONE_MAX_GFORCE) x = SDL_IPHONE_MAX_GFORCE; | 76 if (x > SDL_IPHONE_MAX_GFORCE) x = SDL_IPHONE_MAX_GFORCE; |
39 else if (x < -SDL_IPHONE_MAX_GFORCE) x = -SDL_IPHONE_MAX_GFORCE; | 77 else if (x < -SDL_IPHONE_MAX_GFORCE) x = -SDL_IPHONE_MAX_GFORCE; |
40 | |
41 if (y > SDL_IPHONE_MAX_GFORCE) y = SDL_IPHONE_MAX_GFORCE; | 78 if (y > SDL_IPHONE_MAX_GFORCE) y = SDL_IPHONE_MAX_GFORCE; |
42 else if (y < -SDL_IPHONE_MAX_GFORCE) y = -SDL_IPHONE_MAX_GFORCE; | 79 else if (y < -SDL_IPHONE_MAX_GFORCE) y = -SDL_IPHONE_MAX_GFORCE; |
43 | |
44 if (z > SDL_IPHONE_MAX_GFORCE) z = SDL_IPHONE_MAX_GFORCE; | 80 if (z > SDL_IPHONE_MAX_GFORCE) z = SDL_IPHONE_MAX_GFORCE; |
45 else if (z < -SDL_IPHONE_MAX_GFORCE) z = -SDL_IPHONE_MAX_GFORCE; | 81 else if (z < -SDL_IPHONE_MAX_GFORCE) z = -SDL_IPHONE_MAX_GFORCE; |
46 | 82 |
83 /* pass in data mapped to range of SInt16 */ | |
47 data[0] = (x / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16; | 84 data[0] = (x / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16; |
48 data[1] = (y / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16; | 85 data[1] = (y / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16; |
49 data[2] = (z / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16; | 86 data[2] = (z / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16; |
50 | 87 |
51 } | 88 } |
52 | 89 |
90 /* | |
91 Initialize SDLUIAccelerationDelegate. Since we don't have any data yet, | |
92 just set our last received data to zero, and indicate we don't have any; | |
93 */ | |
53 -(id)init { | 94 -(id)init { |
54 | |
55 self = [super init]; | 95 self = [super init]; |
56 x = y = z = 0.0; | 96 x = y = z = 0.0; |
97 hasNewData = NO; | |
57 return self; | 98 return self; |
58 | |
59 } | 99 } |
60 | 100 |
61 -(void)dealloc { | 101 -(void)dealloc { |
62 sharedDelegate = nil; | 102 sharedDelegate = nil; |
103 [self shutdown]; | |
63 [super dealloc]; | 104 [super dealloc]; |
64 } | 105 } |
65 | 106 |
107 /* | |
108 Lets our delegate start receiving accelerometer updates. | |
109 */ | |
66 -(void)startup { | 110 -(void)startup { |
67 [UIAccelerometer sharedAccelerometer].delegate = self; | 111 [UIAccelerometer sharedAccelerometer].delegate = self; |
68 isRunning = YES; | 112 isRunning = YES; |
69 } | 113 } |
70 | 114 /* |
115 Stops our delegate from receiving accelerometer updates. | |
116 */ | |
71 -(void)shutdown { | 117 -(void)shutdown { |
72 [UIAccelerometer sharedAccelerometer].delegate = nil; | 118 if ([UIAccelerometer sharedAccelerometer].delegate == self) { |
119 [UIAccelerometer sharedAccelerometer].delegate = nil; | |
120 } | |
73 isRunning = NO; | 121 isRunning = NO; |
74 } | 122 } |
75 | 123 /* |
124 Our we currently receiving accelerometer updates? | |
125 */ | |
76 -(BOOL)isRunning { | 126 -(BOOL)isRunning { |
77 return isRunning; | 127 return isRunning; |
78 } | 128 } |
79 | 129 /* |
130 Do we have any data that hasn't been pumped into SDL's event system? | |
131 */ | |
80 -(BOOL)hasNewData { | 132 -(BOOL)hasNewData { |
81 return hasNewData; | 133 return hasNewData; |
82 } | 134 } |
83 | 135 /* |
136 When the joystick system grabs the new data, it sets this to NO. | |
137 */ | |
84 -(void)setHasNewData:(BOOL)value { | 138 -(void)setHasNewData:(BOOL)value { |
85 hasNewData = value; | 139 hasNewData = value; |
86 } | 140 } |
87 | 141 |
88 @end | 142 @end |