C++ 如何确定固定时间步长?
我尝试从一步一步地实现固定时间 在本教程中,他将delta值设置为0.01,这样它就不会改变。如果是,我的职位如何在不同的fps中更新?此外,当我打开游戏菜单时,输入速度变慢,一些输入(如回车)甚至不起作用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
如何使我的循环在固定的时间步长内工作,并允许用户设置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);
}
}