开发者

How to pause an animation with OpenGL / glut

开发者 https://www.devze.com 2023-04-09 21:50 出处:网络
To achieve an animation, i am just redrawing things on a loop. However, I need to be able to pause when a key is pressed. I know开发者_Go百科 the way i\'m doing it now its wrong because it eats all o

To achieve an animation, i am just redrawing things on a loop.

However, I need to be able to pause when a key is pressed. I know开发者_Go百科 the way i'm doing it now its wrong because it eats all of my cycles when the loop is going on.

Which way is better, and will allow for a key pause and resume?

I tried using a bool flag but obviously it didnt change the flag until the loop was done.


You have the correct very basic architecture sorted in that the everything needs to be updated in a loop, but you need to make your loop a lot smarter for a game (or other application requiring OpenGL animations).

However, I need to be able to pause when a key is pressed.

A basic way of doing this is to have a boolean value paused and to wrap the game into a loop.

while(!finished) {
    while(!paused) {
        update();
        render();
    }
}

Typically however you still want to do things such as look at your inventory, craft things, etc. while your game is paused, and many games still run their main loop while the game's paused, they just don't let the actors know any time has passed. For instance, it sounds like your animation frames simply have a number of game-frames to be visible for. This is a bad idea because if the animation speed increases or decreases on a different computer, the animation speed will look wrong on those computers. You can consider my answer here, and the linked samples to see how you can achieve framerate-independent animation by specifying animation frames in terms of millisecond duration and passing in the frame time in the update loop. For instance, your main game then changes to look like this:

float previousTime = 0.0f;
float thisTime = 0.0f;
float framePeriod = 0.0f;

while(!finished) {
    thisTime = getTimeInMilliseconds();
    framePeriod = previousTime - thisTime;

    update(framePeriod);
    render();

    previousTime = thisTime;
}

Now, everything in the game that gets updated will know how much time has passed since the previous frame. This is helpful for all your physics calculations as all of our physical formulae are in terms of time + starting factors + decay factors (for instance, the SUVAT equations). The same information can be used for your animations to make them framerate independent as I have described with some links to examples here.

To answer the next part of the question:

it eats all of my cycles when the loop is going on.

This is because you're using 100% of the CPU and never going to sleep. If we consider that we want for instance 30fps on the target device (and we know that this is possible) then we know the period of one frame is 1/30th of a second. We've just calculated the time it takes to update and render our game, so we can sleep for any of the spare time:

float previousTime = 0.0f;
float thisTime = 0.0f;
float framePeriod = 0.0f;
float availablePeriod = 1 / 30.0f;

while (!finished) {
    thisTime = getTimeInMilliseconds();
    framePeriod = previousTime - thisTime;

    update(framePeriod);
    render();

    previousTime = thisTime;

    if (framePeriod < availablePeriod)
        sleep(availablePeriod - framePeriod);
}

This technique is called framerate governance as you are manually controlling the rate at which you are rendering and updating.

0

精彩评论

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

关注公众号