Sign in to follow this  

SDL_GetMouseState

This topic is 3501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't understand how this function is supposed to be used other than finding out the state of the mouse buttons. How do you retrieve the position of the mouse with this function? Uint8 SDL_GetMouseState( int *x, int *y ) The struct for SDL_MouseMotionEvent has Uint16 not ints, so I'm sure GetMouseState isn't giving you the mouse position... Lazyfoo uses event.motion.x and this works perfectly. So what is the purpose of the function above?

Share this post


Link to post
Share on other sites
You pass in the address of integers, receiving the x and y coordinates of the mouse's current state in them. SDL_GetMouseState has nothing to do with SDL_MouseMotionEvent.
int x, y;
Uint8 buttons = SDL_GetMouseState(&x, &y);
// x and y contain the mouse coordinates
// buttons contains bit values representing the state of the mouse buttons

Share this post


Link to post
Share on other sites
Quote:
Original post by ICUP
Uint8 SDL_GetMouseState( int *x, int *y )

The struct for SDL_MouseMotionEvent has Uint16 not ints, so I'm sure GetMouseState isn't giving you the mouse position...

Uint16s are ints, as are Uint8s, Uint32s, Sint16s, etc... They just offer hints as-to how the ints are going to be used, and have different minimum/maximum limits to them.

Quote:
Lazyfoo uses event.motion.x and this works perfectly. So what is the purpose of the function above?

The purpose of the function is to offer a alternative to continually polling events, or to allow the user to quickly and simply grab just the mouse info, without recording it himself.

Oluseyi already explained how to use it, so I wont repeat it, but I'll link you here to help you see how to parse the return value. (Read the 'Description' and 'Example' sections)

Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
Quote:
Original post by ICUP
Uint8 SDL_GetMouseState( int *x, int *y )

The struct for SDL_MouseMotionEvent has Uint16 not ints, so I'm sure GetMouseState isn't giving you the mouse position...

Uint16s are ints, as are Uint8s, Uint32s, Sint16s, etc... They just offer hints as-to how the ints are going to be used, and have different minimum/maximum limits to them.


No. A Uint8, Uint16, etc is not 'an int' in C++ terms. Unless they are a typedef for 'int', they can't be safely used interchangeably where an int is called for.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
No. A Uint8, Uint16, etc is not 'an int' in C++ terms. Unless they are a typedef for 'int', they can't be safely used interchangeably where an int is called for.

Aside from their the amount of memory they take, their minimum/maximum limits, and their unsigned nature, what is different about them? Obviously you can't pass a negative 'int' to a unsigned 'Uint', as you'd either get a 0 or possibly loop around to a really high number, but for positive ints it'd work fine, unless you go over the limit, and in the OP's case, SDL is returning positive ints instead of positive Uint16s.
Quote:
Uint8 SDL_GetMouseState( int *x, int *y )

The struct for SDL_MouseMotionEvent has Uint16 not ints, so I'm sure GetMouseState isn't giving you the mouse position...

Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
Quote:
Original post by Kylotan
No. A Uint8, Uint16, etc is not 'an int' in C++ terms. Unless they are a typedef for 'int', they can't be safely used interchangeably where an int is called for.

Aside from their the amount of memory they take, their minimum/maximum limits, and their unsigned nature, what is different about them?


You don't think those 3 factors make them different enough? They're completely different types, which just happen to have well-defined conversions between them for the most part. The annotations aren't just hints.

If you pass a pointer to an 8-bit variable (eg. uint8) and attempt to treat it as a 32-bit variable (eg. int, on most systems), then when you write into it you're trashing memory that exists outside of that variable.

Quote:
Obviously you can't pass a negative 'int' to a unsigned 'Uint', as you'd either get a 0 or possibly loop around to a really high number, but for positive ints it'd work fine, unless you go over the limit, and in the OP's case, SDL is returning positive ints instead of positive Uint16s.


No: if you attempt to write a 32 bit representation of the number 3 into an 8 bit slot, you are likely to run into problems, such as corrupt variables or even crashes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
If you pass a pointer to an 8-bit variable (eg. uint8) and attempt to treat it as a 32-bit variable (eg. int, on most systems), then when you write into it you're trashing memory that exists outside of that variable.

That's true, I wasn't thinking of pointers, and I can see what you mean. The pointer, if incremented by 32 bits, would skip 3 of the Uint8s, and only land on every fourth one. I wasn't thinking there.

Quote:
Quote:
Obviously you can't pass a negative 'int' to a unsigned 'Uint', as you'd either get a 0 or possibly loop around to a really high number, but for positive ints it'd work fine, unless you go over the limit, and in the OP's case, SDL is returning positive ints instead of positive Uint16s.


No: if you attempt to write a 32 bit representation of the number 3 into an 8 bit slot, you are likely to run into problems, such as corrupt variables or even crashes.

If you manually write it, yes, but if you just convert it from one int type to another, it wouldn't crash, it'd just loop around, or else hit the maximum and stay there. AFAIK, it wouldn't even get corrupted with some unknown/almost-random number. Granted, I'm still new to all this(directly writing memory, pointer manipulation, dynamic memory allocation), but I'm fairly sure a simply overflowing one int with another won't cause problems:
Uint32 largeInt = 1234567;
Uint8 smallInt = largeInt;


'smallInt' should either be 256, or else 135. (135 = 1234567 % 256)

Share this post


Link to post
Share on other sites
Converting a large number to a small one won't be dangerous (just difficult to predict). But in the original context of the pointers, it's dangerous. It's not just about incrementing or decrementing the pointers - if you write a value into a int*, that will alter 4 bytes (on most platforms), even if you originally got that pointer from the address of a uint8 or uint16.

Share this post


Link to post
Share on other sites

This topic is 3501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this