Unlike keyboard or mouse input, which are usually read through hardware interrupts by Allegro, joystick input functions have to be polled because there are no hardware interrupts for them on most platforms. This doesn't mean that you have to poll the joysticks on each line of code you want to read their values, but you should make sure to poll them at least once per frame in your game loop. Otherwise you face the possibility of reading stale incorrect data.
textout_centre_ex(screen, font, "Center the joystick and press a key", SCREEN_W/2, SCREEN_H/2, red_color, -1); readkey(); if (install_joystick(JOY_TYPE_AUTODETECT) != 0) abort_on_error("Error initialising joystick!");
Return value: Returns zero on success. As soon as you have installed the joystick module, you will be able to read the button state and digital (on/off toggle) direction information, which may be enough for some games. If you want to get full analogue input, though, you need to use the calibrate_joystick() functions to measure the exact range of the inputs: see below.
See also: remove_joystick, num_joysticks, load_joystick_data, calibrate_joystick, calibrate_joystick_name, poll_joystick, Standard config variables, JOY_TYPE_*/DOS, JOY_TYPE_*/Windows, JOY_TYPE_*/Linux.
Examples using this: exjoy.
See also: install_joystick, allegro_exit.
do { /* Get joystick input */ poll_joystick(); /* Process input for the first joystick */ if (joy[0].button[0].b) first_button_pressed(); if (joy[0].button[1].b) second_button_pressed(); ... } while(!done);
Return value: Returns zero on success or a negative number on failure (usually because no joystick driver was installed).
See also: install_joystick, joy, num_joysticks.
Examples using this: exjoy.
See also: install_joystick, joy.
Examples using this: exjoy.
The button status is stored in the structure:typedef struct JOYSTICK_INFO { int flags; - status flags for this joystick int num_sticks; - how many stick inputs? int num_buttons; - how many buttons? JOYSTICK_STICK_INFO stick[n]; - stick state information JOYSTICK_BUTTON_INFO button[n]; - button state information } JOYSTICK_INFO;
You may wish to display the button names as part of an input configuration screen to let the user choose what game function will be performed by each button, but in simpler situations you can safely assume that the first two elements in the button array will always be the main trigger controls.typedef struct JOYSTICK_BUTTON_INFO { int b; - boolean on/off flag char *name; - description of this button } JOYSTICK_BUTTON_INFO;
Each joystick will provide one or more stick inputs, of varying types. These can be digital controls which snap to specific positions (eg. a gamepad controller, the coolie hat on a Flightstick Pro or Wingman Extreme, or a normal joystick which hasn't yet been calibrated), or they can be full analogue inputs with a smooth range of motion. Sticks may also have different numbers of axes, for example a normal directional control has two, but the Flightstick Pro throttle is only a single axis, and it is possible that the system could be extended in the future to support full 3d controllers. A stick input is described by the structure:
A single joystick may provide several different stick inputs, but you can safely assume that the first element in the stick array will always be the main directional controller.typedef struct JOYSTICK_STICK_INFO { int flags; - status flags for this input int num_axis; - how many axes do we have? (note the misspelling) JOYSTICK_AXIS_INFO axis[n]; - axis state information char *name; - description of this input } JOYSTICK_STICK_INFO;
Information about each of the stick axis is stored in the substructure:
This provides both analogue input in the pos field (ranging from -128 to 128 or from 0 to 255, depending on the type of the control), and digital values in the d1 and d2 fields. For example, when describing the X-axis position, the pos field will hold the horizontal position of the joystick, d1 will be set if it is moved left, and d2 will be set if it is moved right. Allegro will fill in all these values regardless of whether it is using a digital or analogue joystick, emulating the pos field for digital inputs by snapping it to the min, middle, and maximum positions, and emulating the d1 and d2 values for an analogue stick by comparing the current position with the centre point.typedef struct JOYSTICK_AXIS_INFO { int pos; - analogue axis position int d1, d2; - digital axis position char *name; - description of this axis } JOYSTICK_AXIS_INFO;
The joystick flags field may contain any combination of the bit flags:
JOYFLAG_DIGITAL
This control is currently providing digital input.
JOYFLAG_ANALOGUE
This control is currently providing analogue input.
JOYFLAG_CALIB_DIGITAL
This control will be capable of providing digital input once it has
been calibrated, but is not doing this at the moment.
JOYFLAG_CALIB_ANALOGUE
This control will be capable of providing analogue input once it has
been calibrated, but is not doing this at the moment.
JOYFLAG_CALIBRATE
Indicates that this control needs to be calibrated. Many devices
require multiple calibration steps, so you should call the
calibrate_joystick() function from a loop until this flag is cleared.
JOYFLAG_SIGNED
Indicates that the analogue axis position is in signed format, ranging
from -128 to 128. This is the case for all 2d directional controls.
JOYFLAG_UNSIGNED
Indicates that the analogue axis position is in unsigned format,
ranging from 0 to 255. This is the case for all 1d throttle controls.
Note for people who spell funny: in case you don't like having to type "analogue", there are some #define aliases in allegro/joystick.h that will allow you to write "analog" instead.
See also: install_joystick, poll_joystick, num_joysticks, calibrate_joystick, calibrate_joystick_name.
Examples using this: exjoy.
Return value: Returns a text description for the next type of calibration that will be done on the specified joystick, or NULL if no more calibration is required.
See also: install_joystick, calibrate_joystick, joy, num_joysticks.
Examples using this: exjoy.
int i; for (i=0; i<;num_joysticks; i++) { while (joy[i].flags & JOYFLAG_CALIBRATE) { char *msg = calibrate_joystick_name(i); textprintf_ex(..., "%s, and press a key\n", msg); readkey(); if (calibrate_joystick(i) != 0) { textprintf_ex(..., "oops!\n"); readkey(); exit(1); } } }
Return value: Returns zero on success, non-zero if the calibration could not be performed successfully.
See also: install_joystick, calibrate_joystick_name, joy, num_joysticks.
Examples using this: exjoy.
Return value: Returns zero on success, non-zero if the data could not be saved.
See also: load_joystick_data, set_config_file.
Return value: Returns zero on success: if it fails the joystick state is undefined and you must reinitialise it from scratch.
See also: install_joystick, save_joystick_data, set_config_file.
See also: install_joystick.