mouse_x(3)

πŸ“¦ version 4.4.3.1, in fedora 43, fedora 44

✨ mouse_x β€” bestie edition ✨

okay so this is the mouse_x(3) page from the Allegro manual, and it's literally just the global variables that hold your mouse's position and button vibes. iconic. let's get into it πŸ­πŸ’–


🌸 NAME 🌸

mouse_x, mouse_y, mouse_z, mouse_w, mouse_b, mouse_pos β€” global variables with the mouse position / button state. it's giving Allegro game programming library energy, fr fr.


πŸ¦‹ 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;

like. six little extern volatile int besties. that's the whole vibe check. πŸ’…


πŸ’– DESCRIPTION πŸ’–

so these are the global variables holding the current mouse position and button state. obsessed.

wherever possible, these values get updated asynchronously β€” like, they update themselves, main character energy, they don't need you. but (and this is the important part, no cap) if mouse_needs_poll() returns TRUE, you have to manually call poll_mouse() to update them with the current input state. so basically you gotta do the work yourself in that case. πŸ™ˆ

here's the breakdown, bestie:

  • mouse_x and mouse_y β€” the positions, as integers ranging from zero to the bottom right corner of the screen.
  • mouse_z and mouse_w β€” hold the current vertical and horizontal wheel position, when you're using an input driver that supports wheel mice.
  • mouse_b β€” a bitfield indicating the state of each button:
    • bit 0 = the left button
    • bit 1 = the right button
    • bit 2 = the middle button
    • additional non standard mouse buttons might be available as higher bits in this variable πŸ‘‘

usage example, because we love a demo:

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

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

see how bit 1 in mouse_b & 1 = left pressed, and !(mouse_b & 2) = right not pressed. yes queen, bitwise. πŸ”₯


🌈 the mouse_pos situation 🌈

the mouse_pos variable has the current X coordinate in the upper 16 bits and the Y in the lower 16 bits. it's giving two-for-one, two slays in one int.

this may be useful in tight polling loops, where a mouse interrupt could occur between your reading of the two separate variables β€” because you can copy this value into a local variable with a single instruction and then split it up at your leisure. atomic-ish behavior, we stan. ✨

example, and yes you're gonna wanna screenshot this:

int pos, x, y;

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

x from the top bits, y from the bottom bits, 0x0000ffff doing the masking. periodt. πŸ’…

lowkey important: if you read mouse_x and mouse_y separately in a tight loop, an interrupt could sneak in between them. mouse_pos is the way around that. iykyk.

πŸ‘‘ SEE ALSO πŸ‘‘

go read those too bestie, they're all connected, it's a whole cinematic universe πŸŽ€


Allegro, version 4.4.3, and this page is mouse_x(3) β€” mwah, that's a wrap, hope your mouse always knows where it is πŸ«ΆπŸ’œπŸŒΈ

πŸ“¦ where this page comes from