mouse_x(3alleg4)

✨ mouse_x β€” bestie, it's the mouse page ✨

okay so real talk, this is from the Allegro game programming library manual, and it's covering a whole lil squad of global variables: mouse_x, mouse_y, mouse_z, mouse_w, mouse_b, and mouse_pos. they all just hang out in global scope holding the mouse's position and button state. no cap. πŸ­πŸ’–


🌸 SYNOPSIS 🌸

#include <allegro.h>

extern volatile int mouse_x;

extern volatile int mouse_y;

extern volatile int mouse_z;

extern volatile int mouse_w;

extern volatile int mouse_b;

extern volatile int mouse_pos;

πŸŽ€ DESCRIPTION πŸŽ€

So these are global variables and they hold the current mouse position plus the button state, okay? and here's the thing β€” wherever it's possible, these values get updated asynchronously, like they just do it themselves, main character energy, no help needed. ✨

BUT. if mouse_needs_poll() returns TRUE, bestie, you have to manually call poll_mouse() to refresh them with the current input state. it's giving "you gotta do it yourself", no shortcuts. πŸ’…

Here's the breakdown of who holds what:

  • mouse_x and mouse_y β€” these are integers ranging from zero all the way to the bottom right corner of the screen. iconic.
  • mouse_z and mouse_w β€” these hold the current vertical and horizontal wheel position, but only when you're using an input driver that supports wheel mice. if your driver doesn't, well, not them being useful πŸ’€
  • mouse_b β€” this one's a bitfield showing the state of each button:
    • bit 0 β†’ the left button
    • bit 1 β†’ the right button
    • bit 2 β†’ the middle button
    • higher bits β†’ additional non standard mouse buttons might show up there, so like, extra buttons can absolutely be lurking up in there 🫢

Usage example, and it's literally this:

if (mouse_b & 1)
   printf("Left button is pressed\n");

if (!(mouse_b & 2))
   printf("Right button is not pressed\n");

Now mouse_pos, she's the clever one. 🌈 She's got the current X coordinate in the upper 16 bits and the Y in the lower 16 bits. why do you care? because this is lowkey perfect for tight polling loops β€” imagine a mouse interrupt pops off between you reading the two separate variables, that's a whole vibe check failure 😭. but with mouse_pos you copy the value into a local variable with a single instruction and then split it up at your leisure. obsessed.

Example, verbatim:

int pos, x, y;

pos = mouse_pos;
x = pos >> 16;
y = pos & 0x0000ffff;

πŸ’œ SEE ALSO πŸ’œ

go read these too bestie, they're the whole friend group:


and that's the page, periodt. πŸŽ€ this was the Allegro manual, version 4.4.3, and yeah the page is literally named mouse_x(3alleg4). go press some buttons, bestie, you've got this πŸ’…βœ¨πŸ¦‹