Strange glitch with mouse input and SDL (mouse button stuck down?)

hi,

i posted this the other day, but it seemed to disapear before i could reply back
to the guy who helped me. if your reading this, well, i dont have any globals
like that which mess with the mouse. anyway, heres the problem :

ive recently came across a strange glitch in my input system. its for my map
editor thats made w / SDL and OpenGL. anyway, for some reason, at some point,
the mouse gets screwed up, and its as if the mouse button is stuck down… IE, i
can just hover over a button and it will auto-click it for me, like as if my
left mouse button is stuck down. its pretty weird. i noticed i could stop the
glitch by left clicking somewhere in the screen. i also think im not able to
grab and drag the window while this is happening. if anyone has experianced
something like this before and can help, it would be appreciated. do you think
this bug could be because i have unicode enabled maybe? thanks for any help.

Don’t take any offense to this, but this is very very clearly a bug in
your code. Make no mistake, this bug has nothing to do with anything
but your code. If you want point us/me to the URL of your project then
I can probably tell you’re doing wrong.On Sep 15, 2004, at 4:26 AM, Drew Ferraro wrote:

hi,

i posted this the other day, but it seemed to disapear before i could
reply back
to the guy who helped me. if your reading this, well, i dont have any
globals
like that which mess with the mouse. anyway, heres the problem :

ive recently came across a strange glitch in my input system. its for
my map
editor thats made w / SDL and OpenGL. anyway, for some reason, at some
point,
the mouse gets screwed up, and its as if the mouse button is stuck
down… IE, i
can just hover over a button and it will auto-click it for me, like as
if my
left mouse button is stuck down. its pretty weird. i noticed i could
stop the
glitch by left clicking somewhere in the screen. i also think im not
able to
grab and drag the window while this is happening. if anyone has
experianced
something like this before and can help, it would be appreciated. do
you think
this bug could be because i have unicode enabled maybe? thanks for any
help.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

hey Donny,

no offence taken! i realize it probably is a stupid bug in my code. i was just
kind of hoping that i was some known SDL issue, but i should have known i
wouldnt be so lucky =).

well the thing is the project is rather large (not huge, but big enough), i dont
think you or anyone else would want to sift through it looking for a bug. its
the map editor for a 2D RPG im working on. the code is pretty sloppy, too, i
guess that would help explain the bug.

any clue what i might be doing though? i dont have a global “mouse button
pressed” variable. i also only get the mouse input directly and not when
Polling. i get it directly by doing:

if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1))
{
           do stuff()
    }

one thing that i DO do, is i do a little trick to make sure a mouse button is
only pressed one time and when it is presed. the reason i did this, was because
i was having problems in that when the user clicked the mouse, the above if
statement would hold true over the course of multiple frames. this isnt cool
because that do_stuff() might trigger other code which looks for left mouse
presses, so i would click once, but it would click multiple things since it
takes 1/10th of a second to click but im getting 30 FPS. anyway, the trick looks
like this:

     static bool lmbpressed = false;
if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1))
{

	if (!lmbpressed)  
        {
               DO STUFF HERE!!!!
            }
		
	
	lmbpressed = true;

}
else lmbpressed = false;

do you think this could be causing the bug? i use it ALOT all over my code. its
possible i might have used the same lmbpressed in 2 different actions. do you
think this could be the source of my problems? i really dont think so, but its
worth a shot.

thanks a lot for any more help!!!

Is there any real reason not to use events instead of getting state “directly”?
besides, that’s state is not direct, it is the last state the mouse button was in when SDLs event processing took place, as in when you last called SDL_PollEvent, SDL_WaitEvent, or SDL_PumpEvents.
not processing by events makes things act wonky like this…you’ll miss when a button was pressed or released, or that it was pressed multiple times…

-LIM-

Hey jon,

wow, i didnt think there was really any other way except getting it directly. i
mean, there are ALOT of different places, spread out all over the code, which
uses input. this means that i need to check for that input in those certain
places. for example, lets say for the players movement. inside the Player class,
i would check if a key was pressed directly, and if so, move the player. i
wouldnt want to do this where i take input, because that would break
encapsulation. then i have to make Set_Position() and Set_Velocity() functions
and stuff. it just seems ugly.

so you and others really take absolutely ALL input for the game in the polling
for input? doesnt this break encapsulation, and make it almost over-wealming to
look at?

also, you write, “you’ll miss when a button was pressed or
released, or that it was pressed multiple times…”

could you explain when / how this could happen exactly?

thanks a lot for all your help!!

Drew Ferraro wrote:

Hey jon,

wow, i didnt think there was really any other way except getting it directly. i
mean, there are ALOT of different places, spread out all over the code, which
uses input. this means that i need to check for that input in those certain
places. for example, lets say for the players movement. inside the Player class,
i would check if a key was pressed directly, and if so, move the player. i
wouldnt want to do this where i take input, because that would break
encapsulation. then i have to make Set_Position() and Set_Velocity() functions
and stuff. it just seems ugly.

what I’d do in your case is register all the objects with a central event handler
that would distribute the events. This is how gui’s work. The have one handler
that redistributes the events to the appropriate places. the registration may
also include an event mask for what events should be passed to the object, thus
avoiding calling handlers for events they don’t want.

so you and others really take absolutely ALL input for the game in the polling
for input? doesnt this break encapsulation, and make it almost over-wealming to
look at?

so with the event handler distribution method, the objects handle events
themselves. That should not break any encapsulation you are planning afaik.

also, you write, “you’ll miss when a button was pressed or
released, or that it was pressed multiple times…”

could you explain when / how this could happen exactly?

lets say that between calls to SDL_PumpEvents(), that the user presses and
releases a button a few times. After the events are processed, you would see no
buttin is being pressed currently, thus missing the fact that it was pressed a
few times…

thanks a lot for all your help!!

yep

-LIM-

hey jon, thanks a lot for your help.

i’m trying to figure out how important this is exactly. it seems like a serious
issue. so you think its sloppy to be taking input “directly” like this? im still
not sure i understand how it works exactly. so basically, it will only catch
input that happend during the while(PollEvents()) loop? any input before / after
this loop isnt caught? not only that, but it only tells me IF a key was pressed,
and not how many times the key is pressed and wheather it was up or down?

and how would it work using the message pump then? it would receive every single
message or something, like even if it was pressed multiple times or whatever? it
just seems like it would be a lot of work, i mean, theres a million places where
i check for input, at least in this map editor. but the game i care about coding
right, so i will do it if thats what is best for it. do you think its noticable
if im taking input wrongly like this?

thanks again!!

Drew Ferraro wrote:

hey jon, thanks a lot for your help.

i’m trying to figure out how important this is exactly. it seems like a serious
issue. so you think its sloppy to be taking input “directly” like this? im still
not sure i understand how it works exactly. so basically, it will only catch
input that happend during the while(PollEvents()) loop? any input before / after
this loop isnt caught? not only that, but it only tells me IF a key was pressed,
and not how many times the key is pressed and wheather it was up or down?

No, with event handling with SDL_PollEvent or SDL_WaitEvent, you get every mouse
press and release. you won’t miss anything. Whereas if you use SDL_PumpEvents
and then directly probe the state of keys and mouse buttons, you will only see
the state the mouse or keys were in after all pending events were processed.
So, with SDL_PollEvents/SDL_WaitEvents you get one event for each button press
and another for the corrosponding release, you just process each event
individually. the best event loop would poll for events until none are left,
then it would redraw the screen for any changes that occured during the event
processing, don’t redraw for every event, it’s a waste of time to do so, and
it will make the application less responsive. using events is the best way to
process input. Probing the state directly is not as useful.

and how would it work using the message pump then? it would receive every single
message or something, like even if it was pressed multiple times or whatever? it
just seems like it would be a lot of work, i mean, theres a million places where
i check for input, at least in this map editor. but the game i care about coding
right, so i will do it if thats what is best for it. do you think its noticable
if im taking input wrongly like this?

I was saying DON’T PUMP… Poll or Wait instead. It’s work that should’ve been
designed into the application in the first place perhaps, and that’s why it’s a
haassle to do it now, but it is worth it if you do it right. I’d suggest fixing
both applications of course. Unless you don’t care what people think about the
editor. The editor is perhaps just as important if you are depending on other
people to enjoy their experience enough to want to create maps for your game.
It’s sad when a game is released with a free editor that has too many quirks.
The buggy editors drive people away from even dealing with them, so the game
dies off too, since noone wants to deal with the bugs.

-LIM-

hi jon,

i appreciate your help a lot. im still fairly new to programming (10 months), so
its hard for my to understand all of this. but i appreciate you helping me here.

I dont use SDL_PumpEvents. Instead, i do SDL_PollEvents(), but, to be honest,
inside this loop i dont do much, since most of the stuff i grab directly. For
example, my loop looks like this:

SDL_Event event;
while(SDL_PollEvents(&event)

Inside this while loop, i dont do much. I only check for an SDL_QUIT message and
an SDL_UserEvent message. All other input is taken directly. After the loop, i do

keys = SDL_GetKeyState(NULL);

keys is a Uint8*. then, everywhere else in the game, i just do something like

if(keys[SDLK_somekey])
{
somekey was pressed!
}

this is doing the same thing as PumpEvents then, no? im pretty sure calling
PollEvents() is the equivalent of calling PumpEvents(). so im missing input…
im still not sure on how to avoid / fix this. could you please, if you dont
mind, explain or give a small example on how it would work then? it seems pretty
complicated. it doesnt help that my game is 5 months in development, 65+ files
and 10k + lines of code, either =(.

thanks again!

hi jon,

i appreciate your help a lot. im still fairly new to programming (10 months), so
its hard for my to understand all of this. but i appreciate you helping me here.

I dont use SDL_PumpEvents. Instead, i do SDL_PollEvents(), but, to be honest,
inside this loop i dont do much, since most of the stuff i grab directly. For
example, my loop looks like this:

SDL_Event event;
while(SDL_PollEvents(&event)

Inside this while loop, i dont do much. I only check for an SDL_QUIT message and
an SDL_UserEvent message. All other input is taken directly. After the loop, i do

keys = SDL_GetKeyState(NULL);

keys is a Uint8*. then, everywhere else in the game, i just do something like

if(keys[SDLK_somekey])
{
somekey was pressed!
}

Nope, it does not mean that a key was pressed. It means a key is
currently being pressed. The way you are handling events can be OK for
mouse movement, you probably only want to know where it is, not where it
has been. But, and it is a very big but, doing what you are doing will
almost certainly cause you to miss key presses and mouse clicks. And,
the odds of missing them will depend on the frame rate of your game.
(I’m assuming you are doing your loop once per frame.) That means it
might work just fine on a fast machine and be flaky as all get out on a
slightly slower machine.

this is doing the same thing as PumpEvents then, no? im pretty sure calling
PollEvents() is the equivalent of calling PumpEvents(). so im missing input…

The way you are doing things the events are getting handled, and then
thrown away in your while loop.

im still not sure on how to avoid / fix this. could you please, if you dont
mind, explain or give a small example on how it would work then? it seems pretty
complicated. it doesnt help that my game is 5 months in development, 65+ files
and 10k + lines of code, either =(.

Sure, go to
http://www.linuxdevcenter.com/pub/a/linux/2003/05/15/sdl_anim.html and
read the article. It covers the basics of how to write an event loop
along with a bunch of other stuff.

You are going to have to rewrite a lot of that 10K of code. Don’t get
discouraged, we all have had to that at one time or another.

	Bob PendletonOn Fri, 2004-09-17 at 15:31, Drew Ferraro wrote:

thanks again!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±-------------------------------------+

hi bob,

thanks a lot for your reply. i went to the link you provided and read the
article. it pretty much didnt tell me anything i didnt already know. in fact,
the way they handle events is exactly how i do it. however, like i said, in that
while(PollEvent) loop, i only check for SDL_QUIT or SDL_USEREVENT. so basically
all input is taken using something like

if(keys[SDLK_xxx])
xxx was pressed!

same with mouse, only i use SDL_GetMouseState. so, AM i doing it totally wrong,
or not?

thanks a lot for all your help everyone!

PS - cant seem to “quote” anyone using the

< your text here

it gives me an error about top posting. even if i put this above / below my
post, it doesnt matter.

thanks again!

hi bob,

thanks a lot for your reply. i went to the link you provided and read the
article. it pretty much didnt tell me anything i didnt already know. in fact,
the way they handle events is exactly how i do it.

OK, you need to think carefully about what you are saying here. You do
it like I do it in my article…

however, like i said, in that
while(PollEvent) loop, i only check for SDL_QUIT or SDL_USEREVENT. so basically
all input is taken using something like

if(keys[SDLK_xxx])
xxx was pressed!

Except, that you do it completely differently :slight_smile: As you said, you use
SDL_GetMouseState and the keys array to check for input. Which is not
what I do in my article.

Are you getting the key state using SDL_GetKeyState() or are you keeping
track of it yourself?

same with mouse, only i use SDL_GetMouseState. so, AM i doing it totally wrong,
or not?

I would say you are doing it totally wrong, but others would disagree
with me. I doubt that what you are doing is what you think you are
doing, but I could be completely wrong. :slight_smile:

SDL_GetMouseState and SDL_GetKeyState are only updated when you call
SDL_PumpEvents. In between times all sorts of things can be happening to
the mouse and keys and nothing gets changed until the next time that
SDL_PumpEvents gets called. That means that mouse buttons and keys can
be clicked and pressed and your program will never notice that the
events occurred. Why? Because they happened when you weren’t looking. If
a key is pressed and released between two calls to SDL_PumpEvents and
you are counting on SDL_GetKeyState to tell you about it, you will miss
the key press.

If you really need to know that a key was pressed, then you have to
notice that it was pressed in your event loop. If you need to know that
it was pressed more than once, then count the key presses in the event
loop.

	Bob PendletonOn Sat, 2004-09-18 at 15:12, Drew Ferraro wrote:

thanks a lot for all your help everyone!

PS - cant seem to “quote” anyone using the

< your text here

it gives me an error about top posting. even if i put this above / below my
post, it doesnt matter.

thanks again!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±-------------------------------------+

Hi there

Sorry for my delay.

Talking about OO, you can have a main game class, having a EvendHandling class wich would decide to whom send the event, like:

(Sorry if I made some C++ mistake. From 2 years I have only wrote things for java, not C++ anymore)

class Main {
private:
EventHandler * evt;
public:

void handleEvent();
};

void Main::handleEvent(Event & event ) {
if ( is related do player ) {
player.handleEvent(event);
} else if ( is related to something else) {

  whomhavetohandle.handeEvent(event);
} else {
    //nothing
}

}

I have not tested it, Hope the Idea helped someway.On Fri, 17 Sep 2004 01:51:10 +0000 (UTC), Drew Ferraro wrote:

Hey jon,

wow, i didnt think there was really any other way except getting it directly. i
mean, there are ALOT of different places, spread out all over the code, which
uses input. this means that i need to check for that input in those certain
places. for example, lets say for the players movement. inside the Player class,
i would check if a key was pressed directly, and if so, move the player. i
wouldnt want to do this where i take input, because that would break
encapsulation. then i have to make Set_Position() and Set_Velocity() functions
and stuff. it just seems ugly.

so you and others really take absolutely ALL input for the game in the polling
for input? doesnt this break encapsulation, and make it almost over-wealming to
look at?

also, you write, “you’ll miss when a button was pressed or
released, or that it was pressed multiple times…”

could you explain when / how this could happen exactly?

thanks a lot for all your help!!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Jos? In?cio
Programador J2EE / j2SE
Extreme Softwares