Jump to content
  • Advertisement
Sign in to follow this  
Sykth

SDL & handling mouse events

This topic is 4140 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

Hey guys basically to cut a long story short, im finding it difficult to manage handling mouse events in my game. I know i can do the whole:
SDL_Event event;

while(SDL_Event(&event))
{


if(event.type == SDL_MOUSEBUTTONDOWN)
{
do blah
if(event.type == SDL_BUTTON(1))
{
rectangleColor(blah blah)
}}
But what im after is an alternative way to doing this without the need for an event poll as i already have one in my main game loop to move the camera positions. I'm trying to include a rectangle of a certain size which only draws when i select the left mouse button. I tried using 2 event polls like the one shown above but it makes the game very slow. I was told by my grad demonstrater that this rectangle should go in my draw method which is called in main later to load sprites and such. However I cant see to find a way to do this without having a SDL_Event poll to detect the mouse button presses ! Can i just use: if(SDL_MOUSEBUTTONDOWN) { blah.. } also im still having trouble calling some external functions from another class. 2 methods called Selection::update(); and Selection::render() exist within my selection.cpp file. But when i #include selection.h and create a pointer "Selection *theSelection" then call the methods like so: theSelection->render(); theSelection->update(); They compile ok but crash my game! Any ideas within any of my problems would be greatly appreciated! many thanks, Sykth

Share this post


Link to post
Share on other sites
Advertisement
You can use SDL_GetMouseState() to query the button state and the x and y position it has:

int x,y;
Uint8 button = SDL_GetMouseState(&x,&y);
if( button & SDL_BUTTON(1) )
{
// use x and y
}


As for the other code, how did you intialise your "theSelection" pointer?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!