hi,
i seem to come across this common problem, which i "hack" my way around. im wondering if there is a general solution, or what im doing is totally wrong, or what.
anyway, im using SDL for input, and i have this problem. ill use the left mouse button as example, but it happends with all keys or mouse buttons.
-check directly if the left mouse button is pressed
-if so, do something
-the next frame comes, and that flag is still set that the mouse button is pressed, even though i only clicked it once, getting 30 or 800 FPS will register a mouse click over several frames
-the mouse state is still registerd as being pressed down, so all of a sudden im clicking something i shouldnt be clicking.
i found there are 2 ways around this. the first only allows the button to be pressed a single time, evne if its held down forever. the code looks like this:
static bool pressed = false;
if( some button is pushed)
{
if(!pressed)
DO_SOMETHING_HERE();
pressed = true;
}
else pressed = false;
this basically makes it so input is only taken that single time.
the other way i do it is using a delay. i mainly need this in 2 cases, 1) i want the user to be able to hold down the botton, just not so fast as SDL can take it, and 2) i have a situation where its taking the input too fast in 2 different pieces of code. basically it looks something like...
static Uint32 delay = SDL_GetTicks();
if(some button is pushed)
{
if(SDL_GetTicks() - delay > 200)
DO_SOMETHING_HERE();
delay = SDL_GetTicks();
}
this only takes the input every 200 MS.
anyway, these are both ugly hacks, and i dont like them. im having some weird input problems pop up in my code that i cant figure out and no one seems to have experianced before. mainly, for some reason, at some point, my mouse button gets stuck down. like its as if im pushing the mouse button, but im not. it doesnt happen alot, but it does happen. i notice that if i click somewhere, anywhere, it goes away. i also noticed that i cant click and drag the window while this is happening. pretty weird.
i posted about my input problem on the SDL newsgroup, and the guy was wondering why i take input directly. he says i should just take it while polling.
i dont get it! how could you take ALL the input for your entire game in your Polling() loop? how is that even possible? wouldnt it be a big sloppy mess? not to mention it would break encapsulation, like moving the Player for example, you would then need to move him from your input polling code, instead of inside the Player class somewhere. does anyone do this, or is this guy crazy?
thanks a lot for any help!