PDA

View Full Version : mouse input problem


leggo
2004.09.30, 02:59 PM
I have to make a grid that appears/disappears when i right-click with my mouse
I have two booleans (left_mouse_pressed, and right) and I make a check

if(right_mouse_button) drawgrid();

the problem is that it acts as if I keep the button pressed even if I click as fast as I can :p

so I've tried to add a timer like this


if mouse button down
if x ms have elapsed
mouse_button = true;

I haven't put a timer in the button up part

if mouse button up
mouse_button = false;


but the problem is still there (altough less evident): the grid flashes for some ms and then randomly choose to show up or not :cry:

I hope I was enough clear :P

thank you

Skorche
2004.09.30, 11:38 PM
You want to do this: if( SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(**the button number**))
Substituting in the button number as necessary, 1-3 for left, middle, right. 4 and 5 for scroll up/down.

NCarter
2004.10.01, 04:04 AM
I'm not sure I understand exactly what your pseudocode is doing, but it looks like you're checking the mouse state (SDL_GetMouseState(), as Skorche described) when you should be getting mousedown and mouseup events. Look up event handling (http://sdldoc.csn.ul.ie/guideeventexamples.php) and SDL_MouseButtonEvent (http://sdldoc.csn.ul.ie/sdlmousebuttonevent.php) in the SDL documentation (http://sdldoc.csn.ul.ie/). You shouldn't need a timer for this purpose.

Once you're using mouse button events, you simply reverse the value of the grid boolean when you get a mousedown event.

Skorche
2004.10.01, 05:07 AM
Come to think of it, I think I misunderstood what you were asking too.

What you really want to do is let SDL take care of all the events for you. SDL_PollEvents() (http://sdldoc.csn.ul.ie/sdlpollevent.php) If you do things the event way, SDL will only tell you once that the mouse was pushed, and once when it's let back up.

leggo
2004.10.01, 07:33 AM
Come to think of it, I think I misunderstood what you were asking too.

What you really want to do is let SDL take care of all the events for you. SDL_PollEvents() (http://sdldoc.csn.ul.ie/sdlpollevent.php) If you do things the event way, SDL will only tell you once that the mouse was pushed, and once when it's let back up.

yeah I wasn't very clear :D

I'm using SDL_PollEvents

this for mouse up (and similar code for mouse down)

case SDL_MOUSEBUTTONUP: {
SDL_MouseButtonEvent * e = (SDL_MouseButtonEvent*)&event;

if(e->button == SDL_BUTTON_LEFT)
left_mouse_pressed = false;


if(e->button == SDL_BUTTON_RIGHT)
right_mouse_pressed = false;

break;
}

when I push a mouse button the grid flashes as if it was pressed many times

I've also tried using switch(event.button.button) instead of the pointer

leggo
2004.10.01, 08:23 AM
ok here's the program

on my mac there is a strange problem: the mouse y is inverted :/
it was all ok on my pc...

left click to go past the splash screen, and then right click to toggle the grid


edit:
forgot the link :P

link (http://legomania.web.ctonet.it/picto.zip)

NCarter
2004.10.01, 09:26 AM
Your problem, I think, is that you're effectively converting the events back into state variables, and that's not helpful in this instance. You need to do something like this instead:

case SDL_MOUSEBUTTONUP:
{
SDL_MouseButtonEvent * e = (SDL_MouseButtonEvent*)&event;

if(e->button == SDL_BUTTON_RIGHT)
show_grid_boolean = !show_grid_boolean; // Flip the grid on or off

break;
}
Put the code directly in the event handler (or in a function called from the event handler) so that you can react to it immediately, instead of trying to record its value and reacting to it somewhere else.

leggo
2004.10.01, 09:57 AM
Your problem, I think, is that you're effectively converting the events back into state variables, and that's not helpful in this instance. You need to do something like this instead:

case SDL_MOUSEBUTTONUP:
{
SDL_MouseButtonEvent * e = (SDL_MouseButtonEvent*)&event;

if(e->button == SDL_BUTTON_RIGHT)
show_grid_boolean = !show_grid_boolean; // Flip the grid on or off

break;
}
Put the code directly in the event handler (or in a function called from the event handler) so that you can react to it immediately, instead of trying to record its value and reacting to it somewhere else.


umm I'll try, thank you
the fact is that I need the buttons to do different actions depending on the level/position/etc...
so I wanted to make each function check if the button is pressed and react accordingly


EDIT:
it works perfectly this way... the only problem is that I'll need a lot of global variables to handle everything inside SDL_PollEvent :(
anyone can suggest me a better solution?

p.s. any idea why the mouse motion tracking works ok in win and is flipped in mac os x ?

sealfin
2004.10.01, 10:42 AM
p.s. any idea why the mouse motion tracking works ok in win and is flipped in mac os x ?
This is a known issue, although I can't remember the cause or the fix; I think it also only affects OS X 10.2 - I've never encountered the problem in 10.1 or 10.3.

Skorche
2004.10.01, 12:15 PM
I have, and it only seems to occur in when openGL is active and full screen. I simply just flip the y coordinate if a boolean for fullscreen is set.

leggo
2004.10.01, 01:50 PM
I have, and it only seems to occur in when openGL is active and full screen. I simply just flip the y coordinate if a boolean for fullscreen is set.

my app is not fullscreen :???:

however the other problem is more urgent right now :wacko:
plz help me :ninja:

NCarter
2004.10.01, 04:37 PM
it works perfectly this way... the only problem is that I'll need a lot of global variables to handle everything inside SDL_PollEvent :(
anyone can suggest me a better solution?
Why would that require lots of global variables? Can't you have your event handler use a single (global?) variable to detect which mode you're in, then call an appropriate function to inform the associated part of the program about the mouse up event?

Failing that, you could try using SDL_GetMouseState() (http://sdldoc.csn.ul.ie/sdlgetmousestate.php) to detect the current mouse button state and write a routine which prevents it from toggling the grid more than once until it's released. I could write some example code but I'm too tired right now... try it yourself first, and I'll try to help later if you can't crack it. ;)

By the way, it would be better if you did it the first way, because otherwise you're redundantly turning events into state and then back into events again, which is silly. If you can't implement it directly with events without requiring loads of global variables you may have a deeper design problem....

leggo
2004.10.05, 07:15 AM
Why would that require lots of global variables? Can't you have your event handler use a single (global?) variable to detect which mode you're in, then call an appropriate function to inform the associated part of the program about the mouse up event?

Failing that, you could try using SDL_GetMouseState() (http://sdldoc.csn.ul.ie/sdlgetmousestate.php) to detect the current mouse button state and write a routine which prevents it from toggling the grid more than once until it's released. I could write some example code but I'm too tired right now... try it yourself first, and I'll try to help later if you can't crack it. ;)

By the way, it would be better if you did it the first way, because otherwise you're redundantly turning events into state and then back into events again, which is silly. If you can't implement it directly with events without requiring loads of global variables you may have a deeper design problem....


ok I did as you suggested and it's all ok now!
this project has a LOT of design problems... in fact I started it almost randomly :P
now I want to finish it, as it would be my first full game ;)

aaronsullivan
2004.10.05, 10:10 AM
Inverted y is fixed in the CVS version of SDL. Next major update will include the fix if you want to wait for a stable release.