Smooth keyboard input with GLUT

August 20, 2010 at 4:09 pm (Gaming, Programming/Development)

Recently, I’ve been learning OpenGL in order to ramp up for the still-in-progress Android game mentioned in previous posts.

In my Jedi training I came across a rather annoying behavior of GLUT – slow response to keyboard input. Specifically, response to keys that are held down. The repeat time is like that of a standard Windows app, i.e. in Notepad, press a letter key an hold it, the letter is displayed, there is a short delay, then the letter begins to repeat.
Now, this is all fine and dandy for most every application because immediate responsiveness is usually not necessary. Although, in games, this is awful and just plain unacceptable.

When doing some research, I tended to find three solutions to the problem:
1. Switch to normal Win32 development and use that message loop to handle input
Ick… Nein! I simply want to learn OpenGL, not Win32 programming.
2. That’s just a limitation of GLUT, live with it until you are ready to switch to #1.
Again, I say: nein! 容認できない!
3. Use DirectInput, it’s much better for handling Input on Windows
Ok, granted. BUT, again, I want to learn OpenGL here… for an Android app, I’ll come back to DirectX when the project necessitates it.

Fortunately, there’s actually a very simple solution that allows those learning OpenGL to continue to use GLUT for its inherent benefits all while receiving silky-smooth keyboard input with the added bonus of handling multiple-key input:

Use a flag variable on key-down to signal a press, swap the flag on key-up, update based upon that flag

Here’s an example:

In accessible scope

GLboolean upPressed = false;

In our glutSpecialFunc()

void SpecialKeys(int key, int x, int y)
{
   // ...
   // Stuff
   // ...
   if (GLUT_KEY_UP == key)
      upPressed = true;

   // More stuff ...
}

In our glutSpecialUpFunc()

void SpecialKeysUp(int key, int x, int y)
{
   // ...
   // Stuff
   // ...
   if (GLUT_KEY_UP == key)
      upPressed = false;

   // More stuff ...
}

In our glutIdleFunc()

void Update()
{
   // ...
   // Stuff
   // ...
   if (upPressed)
      // Movement kung-fu!

   // More stuff ...
}

That’s it! Not too bad, right? Not only is the input smoothed out, but now you can handle multiple, simultaneous key presses! This works rather nicely and I’m sure it can be applied to any key (not just “special” keys) or mouse button.

Take that Win32!

About these ads

2 Comments

  1. Qwerty said,

    Thank you, that’s what I was searching. :)

    The only think I would suggest to use along this is glutTimerFunc to slow the movement, but I am not sure what will it do with other objects on scene which need to be animated without delay.

  2. Unreal said,

    Thanks man!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: