# Input Handling - SDL

## Recommended Posts

Input Handling Hi, I have written a tetris clone, which is running fine...ie logic wise. I am howerver facing problems with handling the input. The earlier system i used was in the pollevent.Thrown right into game loop.Basically something like . if(Pollevent(&event)) { if(event.key.keysym.sym == SDL_KLEFT) //check collision move right } But here the problem was in case the user wanted to move a brick to the right he had to hit the right key once per unit movement.Then i went for this class:
class Input
{
private:
bool keys[343];
bool closed;
public:
bool *ReturnKeys();
bool Closed();
Input();
};


Guess the only function important here is ReadInput:
void Input::ReadInput()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
keys[event.key.keysym.sym]=true;
}

if(event.type == SDL_KEYUP)
{
keys[event.key.keysym.sym]=false;
}

if(event.type == SDL_QUIT)
closed=true;
}
}


So currently my program looks something like
main()
{

while(run)
{
//check framerate
//handle input
//draw world
}
}


here is what handle input looks like.
void HandleInput(Input obj)
{
bool *keys = obj->returnKeys();
if(keys[SDL_KLEFT])
//check collision & move left

if(keys[SDL_KUP]
//check collisoin & rotate

if(keys[SDL_KDOWN]
//if collision add new block to the world & create a new block to fall.

}


now the input is read 30 times per-second ie equal to the framerate.Hence i am getting amazing rotation speeds.Which i would like to reduce.If i add a little delay before the read & draw , the input is sluggish.Not to mention the collision detection is off the charts too as the collision is not detected at all.eg:- i am getting values such as Element pushed at value y 135268352.

##### Share on other sites
Actually your first version will work fine if you call SDL_EnableKeyRepeat. With this, you can specify the delay before you start getting repeats and the interval between them. So when the user presses and holds 'UP' it will delay for a bit and then keep generating SDL_KEYDOWN events.

Hope that helps.

##### Share on other sites
Thanks Simian Man, that got the job done.Just so that i understand the paramerters correctly, delay = how long does a key need to be pressed , interval = interval to poll for the event.Please correct me if i am wrong.Funny thing ,everything seems to be running fine when i use the above function , a change of the class and collision detection goes akward.Can anyone volunteer to go through my code, i sure would like to know whats is that i am doing wrong.

##### Share on other sites
Here is how it works, when the user presses and holds a key. Each X represents an SDL_KEYDOWN event being generated.
X----------X--X--X--X--X--X--X

The first interval duration (the long one) is the delay. This is set with the first parameter. You can use SDL_DEFAULT_REPEAT_DELAY as a default. The smaller durations (--) are the repeat interval. These are set with the second parameter. Here you can use SDL_DEFAULT_REPEAT_INTERVAL as a default.

I don't understand your second question, but if you post your code, I'll look at it.

##### Share on other sites
The best way to fix this is to calculate your rotations in degrees per second, instead of degrees per frame.

Basically, you calculate the elapsed time (in seconds) since the previous frame, and use that to scale your object rotations (and movements). This way, no matter what your framerate is, or how much it fluctuates between frames, you'll see "smooth", correct motion.

##### Share on other sites
arudson , i think you misunderstood. What i am having problem is accepting a continious input.Basically since i am catching input per frame (30 times a second) if i hold the UP key , the block does a amazingly fast rotation , hence the user doesnot have a control over the orientation.

Got it simian man.What i meant to say was that i have confirmed that the logic is doing fine ie works perfectly with the function you mentioned, but when i try to handle the input with the above Input class , i am facing major collision detection problems.Hence i wanted some one to go through my source and tell me what was off,i have spent a good deal of time staring at it in vain, though this is an elegant solution , i would like to know what was i doing wrong.

Edit 1:posting code in a while.
Edit 2:traced the problem, funny pointers :
[Edited by - Nokturnal on July 21, 2006 5:01:17 AM]

##### Share on other sites
actually, you should not handle movement logic by game cycle, but by game tick.

a game tick is a logical measurement of a game's execution cycles per second, the best tick is normally one 60th of a second, though you can have tick of 1/30 seconds with no problems.
now, in tetris i remember the input works like this; holding a button gives you a steady movement witha certain vel, let's assume one block displacement per 1/3 of a second and tapping the keys gives you a movement equal to the positive edge of the key pressing.

##### Share on other sites
I am not sure if this helps, but I am currently trying to pick up SDL using the tutorials on lazyfooproductions.com. So far it has been going well, although some of the implementation decisions are questionable. The tutorials take a different approach to movement that seems to work okay, although I am probably too noob to say for sure. Instead of doing repeats, the tutorial has a velocity variable that is increased whenever a key is pressed. Maybe this version would be more straightforward for implementing an input class. I hope this helps.

JJ

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978515

• 10
• 10
• 12
• 22
• 13