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

  • SEARCH
  • Home
  • 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 6544485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:28:15+00:00 2026-05-25T11:28:15+00:00

Trying to understand the concepts of setting constant speed on game loop. My head

  • 0

Trying to understand the concepts of setting constant speed on game loop. My head hurts. I read the deWiTTERS page, but I can’t see the why/how…when I get it…it slips.

while(true)
{
      player->update() ;
      player->draw() ;
}

This will run as fast as possible depending on how fast a processor is…I get that.

To run at the same speed on all computers, the logic is what I don’t get. If I am trying to run at 60fps, then it means for every 16ms the objects move by a frame, yeah? What I don’t get is how the update() or draw() may be too slow.

deWiTTERS example (I used 60):

const int FRAMES_PER_SECOND = 60;
const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;

DWORD next_game_tick = GetTickCount();
// GetTickCount() returns the current number of milliseconds
// that have elapsed since the system was started

int sleep_time = 0;

bool game_is_running = true;

while( game_is_running ) {
    update_game();
    display_game();

    next_game_tick += SKIP_TICKS;
    sleep_time = next_game_tick - GetTickCount();
    if( sleep_time >= 0 ) {
        Sleep( sleep_time );
    }
    else {
        // Shit, we are running behind!
    }
}

I don’t understand why he gets the current time before the loop starts. And when he increments by SKIP_TICKS I understand he increments to the next 16ms interval. But I don’t understand this part as well:

sleep_time = nextgametick  - GetTickCount() 

What does Sleep(sleep_time) mean? The processor leaves the loop and does something else? How does it achieve running 60fps?

  • 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-25T11:28:16+00:00Added an answer on May 25, 2026 at 11:28 am

    In cases where the update_game() and display_game() functions complete in less time than a single frame interval at 60FPs, the loop tries to ensure that the next frame is not processed until that interval is up, by sleeping (blocking the thread) off the excess frame time. Seems like it is trying to ensure that the frame rate is capped to 60FPS, and no higher.

    The processor does not ‘leave the loop’ but rather the thread in which your loop is running is blocked (prevented from continuing execution of your code) until the sleep time is up. Then it continues onto the next frame. In a multi-threaded game engine, sleeping the thread of the main game loop like this gives the CPU time to execute code in other threads, which may be managing physics, AI, audio mixing etc, depending on set up.

    Why is GetTickCount() called before the loop starts?
    We know from the comment in your code that GetTickCount() returns the milliseconds since system boot.

    So lets say that the system has been running for 30 seconds (30,000ms) when you start your program,
    and let’s say that we didn’t call GetTickCount() before entering the loop,
    but instead initialized next_game_tick to 0.

    We do the update and draw calls (as an example, they take 6ms) and then:

    next_game_tick += SKIP_TICKS;  // next_game_tick is now 16
    sleep_time = next_game_tick - GetTickCount();   
    
    // GetTickCount() returns 30000!
    // So sleep_time is now 16 - 30000 = -29984 !!!
    

    Since we (sensibly) only sleep when sleep_time is positive,
    the game loop would run as fast as possible (potentially faster than 60FPS),
    which is not what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand the concepts behind DDD, but I find it hard to
I am trying to better understand basic concepts in OOP. What are static and
I'm trying to understand the main concepts of graph theory and the algorithms within
I am trying understand ViewModels deeper and I have read many articles and blogs
I am trying to understand concepts of windows authentication, forms authentication and their differences.
Hai, I am trying to understand few concepts in JavaScript. Consider the following code:
I am trying to understand basic OS concepts Want to know if my understanding
I'm trying to understand OOP and having some trouble with a couple of concepts
I am trying to understand concepts (similarities and differences) between the .NET remoting (and
I am a newbie and trying to understand concepts of inheritance and design patterns.

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.