开发者

GLUT Keyboard callback issues with C

I'm currently coding a version of breakout as a quick lea开发者_开发百科rning 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?


A common thing in games is a keyboard array. Therefore you will be also able to press several buttons at a time.

You have an array where you keep state of keys (you put 1 when you get pressed, set 0 when released). And you process game in each frame by taking information just from array, not directly from input.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜