SDL_GetMouseState

Started by
1 comment, last by Bigfatmeany 11 years ago

Note: I'm wrather NEW to these forums, sorry if I post this in the wrong section/Don't post something right.

I'v been working with SDL a bit. Decided to do this before I head off to Digipen for a Pre-college Program. After following Lazyfoo's tuts, I decided to attempt a new way at making a "button". Anything clickable that would give me some sort of result on the screen. I want to use the SDL_GetMouseState to tell whenever the cursor is over the button/If a mouse button is pressed . I realize that all this does is Get the mouse state, but is there any way i'm able to output the mouse states, to trigger something that pops up only while the cursor is in a certain region on the screen/when a button is pressed in a certain area.
For example,

If I were to put the mouse near the top left corner, a box should pop up somewhere else.

I've been up for too long and need some sleep, so i'm very sorry if what i just wrote seems like a jumbled mess.

Advertisement

SDL's rendering is like painting a watercolour - it doesn't keep track of each individual brush stroke, just the resulting colours on the canvas. So, if you want to see if the mouse cursor is over a certain part of the screen where you previously drew a button, you have to keep track of those positions yourself and compare them to the current mouse position.

If you define a region of the screen as an SDL_Rect, you can check to see if the mouse is within it with a simple rectangle bounds check like this:


SDL_Rect theRegion; // this must be filled in with the correct screen coordinates
int x, y;
SDL_GetMouseState(&x, &y);
if (x >= theRegion.x && x < theRegion.x + theRegion.w &&
    y >= theRegion.y && y < theRegion.y + theRegion.h)
{
    // the mouse is in the region
}
else
{
    // the mouse is outside the region
}
 

If you define a region of the screen as an SDL_Rect, you can check to see if the mouse is within it with a simple rectangle bounds check like this:


SDL_Rect theRegion; // this must be filled in with the correct screen coordinates
int x, y;
SDL_GetMouseState(&x, &y);
if (x >= theRegion.x && x < theRegion.x + theRegion.w &&
    y >= theRegion.y && y < theRegion.y + theRegion.h)
{
    // the mouse is in the region
}
else
{
    // the mouse is outside the region
}
 

Ah I see. I'm not sure why this didn't come to me last night, as it seems a tad obvious :P. Thanks a bunch for helping me out!

This topic is closed to new replies.

Advertisement