开发者

OpenGL pausing problems

开发者 https://www.devze.com 2023-01-05 12:20 出处:网络
I have the following function that is used as the glutKeyboardFunc function parameter: void handleKeypres开发者_开发技巧s(unsigned char key, //The key that was pressed

I have the following function that is used as the glutKeyboardFunc function parameter:

void handleKeypres开发者_开发技巧s(unsigned char key, //The key that was pressed
                    int x, int y) {    //The current mouse coordinates
    switch (key) {
        case 27: //Escape key
            exit(0); //Exit the program
    }

    if (key == 'a')
    {
        moveCircleLeft(0);
    }
    if (key == 'w')
    {
        moveCircleUp(0);
    }
    if (key == 's')
    {
        moveCircleDown(0);
    }
    if (key == 'd')
    {
        moveCircleRight(0);
    }
}

Now I will show you moveCircleLeft as an example of how the moveCircle functions are written:

void moveCircleLeft(int x)
{
    characterX = characterX - 0.1;
    glutPostRedisplay();
    x++;
    if (x < 10)
    {
        glutTimerFunc(10, moveCircleLeft, x);
    }
}

The other moveCircle functions work similar and the only differences between the four functions is whether its plus of minus 0.1 or if it is characterY as opposed to characterX.

Now, here is the problem:

All the directions are moving in the right direction but, there is a delay/pause in movement of the character when I try to press keys. So, if I press just the d key (moveCircleRight) it moves right a little, stops a for a small amount of time, then moves across the screen in that direction at a constant speed with no pauses. Then if I change to a different key it pause for a little bit before changing directions then moves at a constant speed in that direction.

Any suggestion around this would be appreciated!


Create a boolean variable for each key (preferably an array). Then use KeyDown/KeyUp instead of KeyPress (i believe in GLUT its something like KeyboardUpFunc and KeyboardFunc, but cant remember now). On KeyDown, set the appropriate variable to true, on KeyUp, set it to false. Now you probably have an Idle function or something like that. Call there your "move" functions based on the boolean variables. There should be no pauses now. Beware though that you might need to keep track of the time elapsed between frames and scale the distance moved appropriately.

0

精彩评论

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