Hey, I just joined the list. This is my first post. I’ve been using
Allegro and I just decided to switch to SDL. Is there any way I can get
input from the keyboard without it flipping out on me? Like, here’s how
I would update the current key being pressed:
A few things.
bool keys[303];
Where’d you get that ‘303’? Huh? Punk!? ;^)
You should be using:
bool keys[SDLK_LAST];
if(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
keys[event.key.keysym.sym] = true;
break;
case SDL_KEYUP:
keys[event.key.keysym.sym] = false;
break;
}
}
What’s outside of this block? Are you throttling how fast your
loop is being executed?
Then, later in my program, to see if the right arrow is pressed:
if(keys[SDLK_RIGHT])
{
x++;
}
The only trouble is x gets ++ed like 20 times.
Did you happen to enable key repeat?
And are you certain it’s not just because of the fact that you
pushed the [Right] arrow down, then your loop iterated 20 times?
If so, your code is doing exactly what you want it to do. :^)
They keys are waay too sensitive.
I haven’t found this at all.
In Allegro I fixed this problem by using the readkey()
function instead of using their array of key states.
I’d need to know what “readkey()” is/does to be able to speak to this.
Is there any way I
can make the keys not too crazy sensitive, but just about as sensitive
as they are in all other programs without using SDL_Delay()? Thanks.
Ah - So you’re not using SDL_Delay(). My guess is that your main loop
is iterating like mad. (That is, very very quickly :^) )
Your “if (keys[SDLK_RIGHT])…” block is getting executed a LOT between
the time you push the button and the time you release it.
Have you tried tapping the arrow key very briefly? You’ll probably
find that “x++” doesn’t happen quite as many times. :^)
You can do what I do, simply throttle your main loop to
“no more than N iterations per second” (e.g., 60fps), in which case
your “x++” would only happy 20 times if you held down the arrow key
for 2/3rds of a second. :^)
Or, you can do it the harder, more accurate way, and only
increment “x” a certain fraction of a whole number, based on the
FPS your getting on the particular computer you’re on.
All of your math (movement, collision detection, etc.) gets a lot harder,
though. :^( And depending on how much more crap you throw on the screen,
your FPS may drop, which means your math changes.
This FPS-related stuff has all been discussed before on the SDL list
(at least twice), so I’d check the archives. I’m sure it comes up on
other game programming lists, as well.
Good luck!
-bill!On Thu, Dec 12, 2002 at 06:42:24PM -0500, Micah Lee wrote: