Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

c++ - 了解游戏循环中的滴答插值(Understanding tick interpolation in game loop)

Sorry for bothering, I am just studying state management in game loops, and I need some help to understand the reason why the author does things this way.

(抱歉,我只是在游戏循环中研究状态管理,我需要一些帮助来了解作者采用这种方式进行操作的原因。)

void Game::run() {
    float newTime, frameTime, interpolation;
    float accumulator = 0.f;
    float currentTime = gameClock.getElapsedTime().asSeconds();

    while(data->renderWindow.isOpen()){
        data->stateManager.makeChanges();
        newTime = gameClock.getElapsedTime().asSeconds();
        frameTime = newTime - currentTime;
        if(frameTime > 0.2f)
            frameTime = 0.2f;       //why this if?
        currentTime = newTime;
        accumulator += frameTime;     //why don't he just make actions every dt time?
        while(accumulator >= dt){
            data->stateManager.getActiveState()->handleInput();
            data->stateManager.getActiveState()->update(dt);
            accumulator -= dt;
        }
        interpolation = accumulator/dt;
        data->stateManager.getActiveState()->draw(interpolation);    //really can't understand this :(
    }
}

I made comments in what I don't understand.

(我对我不了解的地方发表了评论。)

  ask by Lauro Rossi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...