Search code examples
c++sdl-2

Get SDL2 Mouse Position


How would I get the position of the mouse in c++ SDL2? I found this wiki however I'm not really sure what it means and how would I get the x and y in int form? https://wiki.libsdl.org/SDL_GetMouseState


Solution

  • Call the function described at the link you found; with pointers to the two int variables which you want to receive the coordinates.

    Simplified, the function works like this one:

    void SetXto5(int* x)
    {*x = 5;}
    

    i.e. the variable which your parameter points to will receive a value. (This skips the check for NULL pointer, which is implied in the documentation.)

    This does of course require the SDL environment to be correctly set up and initialised. I assume from your comments that you do not ask about that background part.