Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


SDL_GetMouseState


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Bigfatmeany   Members   -  Reputation: 137

Like
0Likes
Like

Posted 17 April 2013 - 10:14 PM

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.



Sponsor:

#2 Kylotan   Moderators   -  Reputation: 3338

Like
0Likes
Like

Posted 18 April 2013 - 07:03 AM

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
}
 


#3 Bigfatmeany   Members   -  Reputation: 137

Like
0Likes
Like

Posted 18 April 2013 - 02:01 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS