src/joystick/iphoneos/SDLUIAccelerationDelegate.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 7191 75360622e65f
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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@7191
    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@7191
    34
    if (sharedDelegate == nil) {
slouken@7191
    35
        sharedDelegate = [[SDLUIAccelerationDelegate alloc] init];
slouken@7191
    36
    }
slouken@7191
    37
    return sharedDelegate;
slouken@2765
    38
}
slouken@2765
    39
/*
slouken@7191
    40
    UIAccelerometerDelegate delegate method.  Invoked by the UIAccelerometer instance when it has new data for us.
slouken@7191
    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@7191
    42
    events system when SDL_SYS_JoystickUpdate is called.
slouken@7191
    43
*/
slouken@2765
    44
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
slouken@7191
    45
slouken@7191
    46
    x = acceleration.x;
slouken@7191
    47
    y = acceleration.y;
slouken@7191
    48
    z = acceleration.z;
slouken@7191
    49
slouken@7191
    50
    hasNewData = YES;
slouken@2765
    51
}
slouken@7191
    52
/*
slouken@7191
    53
    getLastOrientation -- put last obtained accelerometer data into Sint16 array
slouken@7191
    54
slouken@7191
    55
    Called from the joystick system when it needs the accelerometer data.
slouken@7191
    56
    Function grabs the last data sent to the accelerometer and converts it
slouken@7191
    57
    from floating point to Sint16, which is what the joystick system expects.
slouken@7191
    58
slouken@7191
    59
    To do the conversion, the data is first clamped onto the interval
slouken@7191
    60
    [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
slouken@7191
    61
    by MAX_SINT16 so that it is mapped to the full range of an Sint16.
slouken@7191
    62
slouken@7191
    63
    You can customize the clamped range of this function by modifying the
slouken@7191
    64
    SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
slouken@7191
    65
slouken@7191
    66
    Once converted to Sint16, the accelerometer data no longer has coherent units.
slouken@7191
    67
    You can convert the data back to units of g-force by multiplying it
slouken@7191
    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@7191
    72
    #define MAX_SINT16 0x7FFF
slouken@2765
    73
slouken@7191
    74
    /* clamp the data */
slouken@7191
    75
    if (x > SDL_IPHONE_MAX_GFORCE) x = SDL_IPHONE_MAX_GFORCE;
slouken@7191
    76
    else if (x < -SDL_IPHONE_MAX_GFORCE) x = -SDL_IPHONE_MAX_GFORCE;
slouken@7191
    77
    if (y > SDL_IPHONE_MAX_GFORCE) y = SDL_IPHONE_MAX_GFORCE;
slouken@7191
    78
    else if (y < -SDL_IPHONE_MAX_GFORCE) y = -SDL_IPHONE_MAX_GFORCE;
slouken@7191
    79
    if (z > SDL_IPHONE_MAX_GFORCE) z = SDL_IPHONE_MAX_GFORCE;
slouken@7191
    80
    else if (z < -SDL_IPHONE_MAX_GFORCE) z = -SDL_IPHONE_MAX_GFORCE;
slouken@7191
    81
slouken@7191
    82
    /* pass in data mapped to range of SInt16 */
slouken@7191
    83
    data[0] = (x / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
slouken@7191
    84
    data[1] = (y / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
slouken@7191
    85
    data[2] = (z / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
slouken@2765
    86
slouken@2765
    87
}
slouken@2765
    88
slouken@2765
    89
/*
slouken@7191
    90
    Initialize SDLUIAccelerationDelegate.  Since we don't have any data yet,
slouken@7191
    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@7191
    94
    self = [super init];
slouken@7191
    95
    x = y = z = 0.0;
slouken@7191
    96
    hasNewData = NO;
slouken@7191
    97
    return self;
slouken@2765
    98
}
slouken@2765
    99
slouken@2765
   100
-(void)dealloc {
slouken@7191
   101
    sharedDelegate = nil;
slouken@7191
   102
    [self shutdown];
slouken@7191
   103
    [super dealloc];
slouken@2765
   104
}
slouken@2765
   105
slouken@2765
   106
/*
slouken@7191
   107
    Lets our delegate start receiving accelerometer updates.
slouken@2765
   108
*/
slouken@2765
   109
-(void)startup {
slouken@7191
   110
    [UIAccelerometer sharedAccelerometer].delegate = self;
slouken@7191
   111
    isRunning = YES;
slouken@2765
   112
}
slouken@2765
   113
/*
slouken@7191
   114
    Stops our delegate from receiving accelerometer updates.
slouken@2765
   115
*/
slouken@2765
   116
-(void)shutdown {
slouken@7191
   117
    if ([UIAccelerometer sharedAccelerometer].delegate == self) {
slouken@7191
   118
        [UIAccelerometer sharedAccelerometer].delegate = nil;
slouken@7191
   119
    }
slouken@7191
   120
    isRunning = NO;
slouken@2765
   121
}
slouken@2765
   122
/*
slouken@7191
   123
    Our we currently receiving accelerometer updates?
slouken@2765
   124
*/
slouken@2765
   125
-(BOOL)isRunning {
slouken@7191
   126
    return isRunning;
slouken@2765
   127
}
slouken@2765
   128
/*
slouken@7191
   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@7191
   132
    return hasNewData;
slouken@2765
   133
}
slouken@2765
   134
/*
slouken@7191
   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@7191
   138
    hasNewData = value;
slouken@2765
   139
}
slouken@2765
   140
slouken@2765
   141
@end