How do you separate game logic from display?

后端 未结 8 2037
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 07:33

How can you make the display frames per second be independent from the game logic? That is so the game logic runs the same speed no matter how fast the video card can render

8条回答
  •  悲&欢浪女
    2020-12-24 08:15

    You could make your game loop look like:

    int lastTime = GetCurrentTime();
    while(1) {
        // how long is it since we last updated?
        int currentTime = GetCurrentTime();
        int dt = currentTime - lastTime;
        lastTime = currentTime;
    
        // now do the game logic
        Update(dt);
    
        // and you can render
        Draw();
    }
    

    Then you just have to write your Update() function to take into account the time differential; e.g., if you've got an object moving at some speed v, then update its position by v * dt every frame.

提交回复
热议问题