Roberto Sepúlveda Bravo Roberto Sepúlveda Bravo - 2 years ago 104
C Question

How to use SDL_MOUSEBUTTONDOWN correctly?

I'm trying to use x and y coordinates from mouse for a game, but i can not get the coordinates.

This is the event i've been reading:

typedef struct{
Uint8 type;
Uint8 button;
Uint8 state;
Uint16 x, y;
} SDL_MouseButtonEvent;

I need something like:

if(SDL_MouseButtonEvent.x >= 100)
//do something

I think the sintax is very different because i saw some examples using SDL_MOUSEBUTTONDOWN.


I apologize, know this question might not be clear, but Peter's answer was just what I needed, thanks for your time.

Answer Source

If you want to intercept a click in an specific area first you need to be polling the events and check for a SDL_MouseButtonEvent.

    while(SDL_PollEvent(&event)) // check to see if an event has happened
            case SDL_QUIT:
                running = false;
            case SDL_MOUSEBUTTONDOWN: // if the event is mouse click
                if(event.mouse.x >= 100)  // check if it is in the desired area
                    //do something    

I hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download