Reputation: 1
I have created a game using openGL in C. The game features a slider that can be moved left or right using arrow keys. Code is as given below (for handling keyboard events).
When I press any key(left or right) the slider moves by one unit then waits for a certain time and then keeps continuously moving until I release the key.
It is natural as computer waits for us to release key so as to verify whether we want multiple key stroke to be fed as input or we were just slow to release the key. But this feature comes in the way of making slider responsive enough.
Is there any way to get rid of this? I have seen games in javascript where this problem doesn't arise. Thanks!!
void specialKeys(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
sliderX -= 10;
if (sliderX < sliderXMin)
sliderX = sliderXMin;
break;
case GLUT_KEY_RIGHT:
sliderX += 10;
if (sliderX > sliderXMax)
sliderX = sliderXMax;
break;
case GLUT_KEY_UP:
xSpeed *= 1.05;
ySpeed *= 1.05;
break;
case GLUT_KEY_DOWN:
xSpeed *= 0.95;
ySpeed *= 0.95;
break;
}
}
Upvotes: 0
Views: 388
Reputation: 162164
OpenGL does not deal with user input. In your case you're using GLUT, which is not part of OpenGL and this is doing the user input stuff for you.
With GLUT what you want to do is difficult to achieve, because the keyboard callback is just called for character code input. What you want is getting events if a key is pressed and later depressed to set a flag, which you use to update the position in a timed loop.
Fortunately GLUT is not the only framework around. There are many others, better suited for the development of games, because of their far finer grained user input control. Well suited for games are GLFW, SDL-2 and SFML. I suggest you use one of those.
Upvotes: 2