cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_sysjoystick.m (5935B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 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#include "../../SDL_internal.h"
     22
     23/* This is the iOS implementation of the SDL joystick API */
     24
     25#include "SDL_joystick.h"
     26#include "SDL_stdinc.h"
     27#include "../SDL_sysjoystick.h"
     28#include "../SDL_joystick_c.h"
     29
     30#import <CoreMotion/CoreMotion.h>
     31
     32/* needed for SDL_IPHONE_MAX_GFORCE macro */
     33#import "SDL_config_iphoneos.h"
     34
     35const char *accelerometerName = "iOS accelerometer";
     36
     37static CMMotionManager *motionManager = nil;
     38
     39/* Function to scan the system for joysticks.
     40 * This function should set SDL_numjoysticks to the number of available
     41 * joysticks.  Joystick 0 should be the system default joystick.
     42 * It should return 0, or -1 on an unrecoverable fatal error.
     43 */
     44int
     45SDL_SYS_JoystickInit(void)
     46{
     47    return (1);
     48}
     49
     50int SDL_SYS_NumJoysticks()
     51{
     52    return 1;
     53}
     54
     55void SDL_SYS_JoystickDetect()
     56{
     57}
     58
     59/* Function to get the device-dependent name of a joystick */
     60const char *
     61SDL_SYS_JoystickNameForDeviceIndex(int device_index)
     62{
     63    return accelerometerName;
     64}
     65
     66/* Function to perform the mapping from device index to the instance id for this index */
     67SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
     68{
     69    return device_index;
     70}
     71
     72/* Function to open a joystick for use.
     73   The joystick to open is specified by the index field of the joystick.
     74   This should fill the nbuttons and naxes fields of the joystick structure.
     75   It returns 0, or -1 if there is an error.
     76 */
     77int
     78SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
     79{
     80    joystick->naxes = 3;
     81    joystick->nhats = 0;
     82    joystick->nballs = 0;
     83    joystick->nbuttons = 0;
     84
     85    if (motionManager == nil) {
     86        motionManager = [[CMMotionManager alloc] init];
     87    }
     88
     89    /* Shorter times between updates can significantly increase CPU usage. */
     90    motionManager.accelerometerUpdateInterval = 0.1;
     91    [motionManager startAccelerometerUpdates];
     92
     93    return 0;
     94}
     95
     96/* Function to determine is this joystick is attached to the system right now */
     97SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
     98{
     99    return SDL_TRUE;
    100}
    101
    102static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
    103{
    104    const float maxgforce = SDL_IPHONE_MAX_GFORCE;
    105    const SInt16 maxsint16 = 0x7FFF;
    106    CMAcceleration accel;
    107
    108    if (!motionManager.accelerometerActive) {
    109        return;
    110    }
    111
    112    accel = [[motionManager accelerometerData] acceleration];
    113
    114    /*
    115     Convert accelerometer data from floating point to Sint16, which is what
    116     the joystick system expects.
    117
    118     To do the conversion, the data is first clamped onto the interval
    119     [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
    120     by MAX_SINT16 so that it is mapped to the full range of an Sint16.
    121
    122     You can customize the clamped range of this function by modifying the
    123     SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
    124
    125     Once converted to Sint16, the accelerometer data no longer has coherent
    126     units. You can convert the data back to units of g-force by multiplying
    127     it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
    128     */
    129
    130    /* clamp the data */
    131    accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
    132    accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
    133    accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
    134
    135    /* pass in data mapped to range of SInt16 */
    136    SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
    137    SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
    138    SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
    139}
    140
    141/* Function to update the state of a joystick - called as a device poll.
    142 * This function shouldn't update the joystick structure directly,
    143 * but instead should call SDL_PrivateJoystick*() to deliver events
    144 * and update joystick device state.
    145 */
    146void
    147SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
    148{
    149    SDL_SYS_AccelerometerUpdate(joystick);
    150}
    151
    152/* Function to close a joystick after use */
    153void
    154SDL_SYS_JoystickClose(SDL_Joystick * joystick)
    155{
    156    [motionManager stopAccelerometerUpdates];
    157    joystick->closed = 1;
    158}
    159
    160/* Function to perform any system-specific joystick related cleanup */
    161void
    162SDL_SYS_JoystickQuit(void)
    163{
    164    if (motionManager != nil) {
    165        [motionManager release];
    166        motionManager = nil;
    167    }
    168}
    169
    170SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
    171{
    172    SDL_JoystickGUID guid;
    173    /* the GUID is just the first 16 chars of the name for now */
    174    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
    175    SDL_zero( guid );
    176    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
    177    return guid;
    178}
    179
    180SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
    181{
    182    SDL_JoystickGUID guid;
    183    /* the GUID is just the first 16 chars of the name for now */
    184    const char *name = joystick->name;
    185    SDL_zero( guid );
    186    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
    187    return guid;
    188}
    189
    190/* vi: set ts=4 sw=4 expandtab: */