mouse input problem

leggo
Unregistered
 
Post: #1
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 Rasp

so I've tried to add a timer like this

Code:
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 Rasp

thank you
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
You want to do this:
Code:
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.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #3
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 and SDL_MouseButtonEvent in the SDL documentation. 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.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
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() 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.
Quote this message in a reply
leggo
Unregistered
 
Post: #5
Skorche Wrote: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() 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 Grin

I'm using SDL_PollEvents

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

Code:
        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
Quote this message in a reply
leggo
Unregistered
 
Post: #6
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 Rasp

link
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #7
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:

Code:
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.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
leggo
Unregistered
 
Post: #8
NCarter Wrote: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:

Code:
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 Sad
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 ?
Quote this message in a reply
Moderator
Posts: 704
Joined: 2002.04
Post: #9
Quote: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.

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #10
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.
Quote this message in a reply
leggo
Unregistered
 
Post: #11
Skorche Wrote: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 Huh

however the other problem is more urgent right now Wacko
plz help me Ninja
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #12
leggo Wrote:it works perfectly this way... the only problem is that I'll need a lot of global variables to handle everything inside SDL_PollEvent Sad
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() 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. Wink

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....

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
leggo
Unregistered
 
Post: #13
NCarter Wrote: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() 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. Wink

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 Rasp
now I want to finish it, as it would be my first full game Wink
Quote this message in a reply
Member
Posts: 233
Joined: 2003.05
Post: #14
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.

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  annoying mouse clicks and GLUT problem WhatMeWorry 1 2,537 Nov 6, 2005 01:12 AM
Last Post: OneSadCookie
  sdl key input problem leggo 6 3,969 Aug 18, 2004 04:48 PM
Last Post: aaronsullivan