Search code examples
copenglglutbreakout

GLUT Keyboard callback issues with C


I'm currently coding a version of breakout as a quick learning experience of C and OpenGL.

Im having some issues with moving the paddle. I've set a keyboard callback so that when the left arrow is pressed, it subtracts 1 from the x value on the paddle, and adds 1 to the x value when pressing the right arrow.

With this in mind, the paddle moves incredibly slow when I hold either key. I can change this by increasing the amount the x value is changed to 10 for example. When I do this the paddle seems to stutter across the screen because it's jumping 10 at a time. It does of course move faster along the screen now but doesn't look smooth.

I'm using GLUT for windowing on OSX.

Is there a way of speeding this up and keeping it looking smooth?


Solution

  • Here is some code from one of my projects:

    bool keyDown[256];
    
    ...
    
    //Called when a key is pressed
    void handleKeypress(unsigned char key, int x, int y) {  
        keyDown[key] = true;
    }
    
    void handleKeyUp(unsigned char key, int x, int y){
        keyDown[key] = false;
    }
    

    This essentially keeps an array of the states of each key, so you can just check them each time. Then you don't have to depend on the callbacks coming in that frequently.