Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何确定固定时间步长?_C++_Time_Timer_Timestamp_Frame Rate - Fatal编程技术网

C++ 如何确定固定时间步长?

C++ 如何确定固定时间步长?,c++,time,timer,timestamp,frame-rate,C++,Time,Timer,Timestamp,Frame Rate,我尝试从一步一步地实现固定时间 在本教程中,他将delta值设置为0.01,这样它就不会改变。如果是,我的职位如何在不同的fps中更新?此外,当我打开游戏菜单时,输入速度变慢,一些输入(如回车)甚至不起作用 如何使我的循环在固定的时间步长内工作,并允许用户设置fps?Nobody?:C:C:C void Core::MainLoop() { float t = 0.0f; float dt = 0.01; float currentTime = SDL_GetTicks

我尝试从一步一步地实现固定时间

在本教程中,他将delta值设置为0.01,这样它就不会改变。如果是,我的职位如何在不同的fps中更新?此外,当我打开游戏菜单时,输入速度变慢,一些输入(如回车)甚至不起作用


如何使我的循环在固定的时间步长内工作,并允许用户设置fps?

Nobody?:C:C:C
void Core::MainLoop() {
    float t = 0.0f;
    float dt = 0.01;

    float currentTime = SDL_GetTicks();
    float accumulator = 0.0f;

    while (!quit) {

        float newTime = SDL_GetTicks();
        float frameT = newTime - currentTime;
        currentTime = newTime;

        accumulator += frameT;

        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
                exit(0);
            }

            MenuInput(event);
        }

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);

        while (accumulator >= dt) {
            Update(dt);
            t += dt;
            accumulator -= dt;
        }

        Draw();

        SDL_RenderPresent(renderer);
    }
}