view mde/input/joystick.d @ 9:1885a9080f2a

Joystick button input now works with config. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 30 Jan 2008 11:33:56 +0000
parents
children 4c3575400769
line wrap: on
line source

/** Opens SDL joysticks ready for use.
* May be extended later to include other input devices and remap devices as per config.
*/
module mde.input.joystick;

import tango.util.log.Log : Log, Logger;

import derelict.sdl.joystick;

private Logger logger;
static this() {
    logger = Log.getLogger ("mde.input.config");
}
private SDL_Joystick*[] joysticks;	// pointers to all joystick structs, whether successfully opened or not

/** Open joysticks ready for use.
*
* This is simply required for SDL to handle joystick events. It can fail, but won't affect anything
* else, except for the controller not working.
*
* closeJoysticks must be run to cleanup afterwards.
*/
void openJoysticks () {
    joysticks = new SDL_Joystick*[SDL_NumJoysticks ()];
    char tmp[128] = void;
    
    for (int i = 0; i < joysticks.length; ++i) {
        if ((joysticks[i] = SDL_JoystickOpen (i)) is null) {	// null on failure
            logger.warn (logger.format (tmp, "Unable to open joystick {} via SDL", i));
        }
    }
    
    logger.info (logger.format (tmp, "Opened {} joysticks via SDL, succesfully unless preceding warnings say otherwise.", joysticks.length));
}

/// Cleanup fct.
void closeJoysticks () {
    foreach (js; joysticks) {
        if(js) SDL_JoystickClose(js);	// only close if successfully opened
    }
}