SDL_GetMouseState(3)

πŸ’– SDL_GetMouseState πŸ’–

okay bestie, welcome to the SDL3 FUNCTIONS corner of the man pages, where we're talking about SDL_GetMouseState(3) β€” yes, the mouse one, the one you've been googling at 2am. let's get into it 🐭✨


✨ NAME ✨

SDL_GetMouseState β€” it queries SDL's own little cache for the synchronous mouse button state plus the window-relative SDL-cursor position. no cap, that's literally the whole job πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <SDL3/SDL_mouse.h>

SDL_MouseButtonFlags SDL_GetMouseState(float *x, float *y);

that's it. two little float * besties and a return type that is not a vibe, it's a bitmask. iykyk 🌸


πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea: this function hands you back the cached synchronous state β€” as in, SDL's best understanding of things, from the last pump of the event queue. it's not asking the platform anything right now, it's reading SDL's notes πŸ“

if you want to interrogate the platform for the immediate asynchronous state, that's a different function, bestie β€” go say hi to SDL_GetGlobalMouseState.

if you pass non-NULL pointers into x or y, it'll write the destination with the respective x or y coordinates relative to the focused window. focused window. keep that in your head, it matters πŸ‘‘

and now the messy part, the drama: in Relative Mode, the SDL-cursor's position usually contradicts the platform-cursor's position β€” the one you'd manually calculate from SDL_GetGlobalMouseState() and SDL_GetWindowPosition. it's giving two sources of truth fighting in the group chat 😭 don't be surprised when they don't match.


🌈 FUNCTION PARAMETERS 🌈

  • x β€” a pointer to receive the SDL-cursor's x-position, measured from the focused window's top left corner. can be NULL if you're not using it, no hard feelings 🫢
  • y β€” a pointer to receive the SDL-cursor's y-position, measured from the focused window's top left corner. also can be NULL if unused. slay, optional parameters, we love to see it πŸ’œ

⭐ RETURN VALUE ⭐

it returns a 32-bit bitmask of the button state. and here's the move: you bitwise-compare it against the SDL_BUTTON_MASK(X) macro. that's the whole bitwise ritual, periodt πŸ”₯


πŸ’€ THREAD SAFETY πŸ’€

main thread only. this function should only be called on the main thread. do not take it to a worker thread, it will not survive the vibe check πŸ™ˆ

🧚 AVAILABILITY 🧚

this function has been available since SDL 3.2.0. if you're on anything older, you're on your own, bestie 😭


πŸ‘‘ SEE ALSO πŸ‘‘

go read those too, they're all in the same friend group 🌸


and that's the page!! brought to you by Simple Directmedia Layer, running SDL 3.4.18 β€” go forth and query your mouse state, you absolute icon πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from