joy(3alleg4)

πŸ’– joy πŸ’–

okay bestie, so this is a page from the Allegro game programming library, and it's about joy β€” the global array where all your joystick tea gets spilled. ✨


🌸 SYNOPSIS 🌸

#include <allegro.h>

extern JOYSTICK_INFO joy[n];

πŸŽ€ DESCRIPTION πŸŽ€

So joy is the global array of joystick state information, and it gets refreshed by the poll_joystick() function. πŸ’… Heads up though: only the first num_joysticks elements actually have anything meaningful in them. The rest are just… vibes. πŸ™ˆ

The JOYSTICK_INFO structure is defined like this, no cap:

   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;

The button status lives in this lil structure right here:

   typedef struct JOYSTICK_BUTTON_INFO
   {
      int b;                           - boolean on/off flag
      char *name;                      - description of this button
   } JOYSTICK_BUTTON_INFO;

You might wanna display those button names as part of an input configuration screen, so your user can pick which game function each button does β€” very main character of them honestly. 🫢 But if you're keeping it simple, you can safely assume that the first two elements in the button array will always be the main trigger controls. Slay.

Each joystick is gonna give you one or more stick inputs, and they come in varying types, like a whole spectrum bestie. 🌈 They can be digital controls that snap to specific positions β€” think a gamepad controller, the coolie hat on a Flightstick Pro or Wingman Extreme, or a normal joystick that hasn't been calibrated yet. Or they can be full analogue inputs with a smooth range of motion. Sticks can also have different numbers of axes β€” a normal directional control has two, but the Flightstick Pro throttle is only a single axis, and it's possible the system could be extended in the future to support full 3d controllers. (future-proofing, we love to see it πŸ‘‘)

A stick input is described by this structure:

   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;

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. periodt.

Info about each of the stick axis is stored in the substructure:

   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;

So this gives you both analogue input in the pos field β€” ranging from -128 to 128 or from 0 to 255, depending on the type of the control β€” <em>and</em> digital values in the d1 and d2 fields. ✨

For example: when describing the X-axis position, the pos field holds the horizontal position of the joystick, d1 gets set if it's moved left, and d2 gets set if it's moved right. Allegro fills in all these values regardless of whether it's using a digital or analogue joystick β€” it emulates the pos field for digital inputs by snapping it to the min, middle, and maximum positions, and it emulates the d1 and d2 values for an analogue stick by comparing the current position with the centre point. iconic behaviour fr fr. πŸ’…


✨ the flags, bestie ✨

The joystick flags field may contain any combination of these 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 it's not doing that at the moment. 😭
  • JOYFLAG_CALIB_ANALOGUE β€” This control will be capable of providing analogue input once it has been calibrated, but it's not doing that 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. no judgement, we support you either way. 🫢

πŸ”₯ SEE ALSO πŸ”₯


and that's the whole page, bestie!! πŸ’–βœ¨πŸŽ€ this one came from Allegro, version 4.4.3, and it's the joy(3alleg4) page β€” go forth and poll those joysticks, you absolute legend. πŸ‘‘πŸ¦‹πŸŒΈ