Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:58:13+00:00 2026-05-25T21:58:13+00:00

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

  • 0

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 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:58:13+00:00Added an answer on May 25, 2026 at 9:58 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that you can force GPU acceleration to achieve smooth animation of elements
Is there a way to achieve a hanging animation of an element bouncing up
http://www.apple.com/uk/iphone/ Any idea on the best way to achieve this kind of animation with
How can I achieve a cool looking photo gallery page? I don't know Flash,
I've got a simple storyboard animation that looks like this: <Window.Resources> <Storyboard x:Key=fader> <DoubleAnimationUsingKeyFrames
I just want to do a simple animation (for example in C++ using OpenGL)
Is there a way to achieve the iTunes style modal view controller chain (see
I am unable to achieve any animation with the following code: if (self.segmentControl.selectedSegmentIndex ==
I'm interested in doing some more flash-like animation in either, or a combination of
I have trying to achieve SVG element's animation while adding dynamic DOMs for its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.