main loop improvements
This commit is contained in:
@@ -41,6 +41,12 @@ namespace OpenVulkano
|
||||
window->SetWindowHandler(this);
|
||||
inputManager = Input::InputManager::GetInstance();
|
||||
engineConfig = EngineConfiguration::GetEngineConfiguration();
|
||||
engineConfig->OnFpsCapChanged += EventHandler(this, &GraphicsAppManager::UpdateCappedFpsInfo);
|
||||
// set initial values
|
||||
if (engineConfig->GetFpsCap() > 0)
|
||||
{
|
||||
UpdateCappedFpsInfo(engineConfig->GetFpsCap());
|
||||
}
|
||||
}
|
||||
|
||||
GraphicsAppManager::GraphicsAppManager(IGraphicsApp* app, IWindow* window, RenderAPI::RenderApi renderApi)
|
||||
@@ -63,19 +69,38 @@ namespace OpenVulkano
|
||||
window->SetWindowHandler(this);
|
||||
inputManager = OpenVulkano::Input::InputManager::GetInstance();
|
||||
engineConfig = OpenVulkano::EngineConfiguration::GetEngineConfiguration();
|
||||
engineConfig->OnFpsCapChanged += EventHandler(this, &GraphicsAppManager::UpdateCappedFpsInfo);
|
||||
// set initial values
|
||||
if (engineConfig->GetFpsCap() > 0)
|
||||
{
|
||||
UpdateCappedFpsInfo(engineConfig->GetFpsCap());
|
||||
}
|
||||
}
|
||||
|
||||
void GraphicsAppManager::UpdateCappedFpsInfo(int32_t newFpsCap)
|
||||
{
|
||||
if (newFpsCap < 0)
|
||||
{
|
||||
cappedFrameTime = std::chrono::milliseconds(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
cappedFrameTime = std::chrono::milliseconds(1000 / newFpsCap);
|
||||
}
|
||||
}
|
||||
|
||||
void GraphicsAppManager::OnCappedFPS(const auto& frameStartTime)
|
||||
{
|
||||
int32_t fpsCap = engineConfig->GetFpsCap();
|
||||
fpsCapRemainder += 1000 % fpsCap;
|
||||
auto frameTime = std::chrono::milliseconds(1000 / fpsCap);
|
||||
auto expectedFrameTime = cappedFrameTime;
|
||||
if (fpsCapRemainder >= fpsCap)
|
||||
{
|
||||
frameTime += std::chrono::milliseconds(1);
|
||||
expectedFrameTime += std::chrono::milliseconds(1);
|
||||
fpsCapRemainder -= fpsCap;
|
||||
}
|
||||
while (clock::now() < frameStartTime + frameTime)
|
||||
auto frameEndTime = frameStartTime + expectedFrameTime;
|
||||
while (clock::now() < frameEndTime)
|
||||
{
|
||||
std::this_thread::yield();
|
||||
}
|
||||
@@ -84,6 +109,7 @@ namespace OpenVulkano
|
||||
GraphicsAppManager::~GraphicsAppManager() noexcept
|
||||
{
|
||||
if (windowTitleFormat.empty()) return;
|
||||
engineConfig->OnFpsCapChanged -= EventHandler(this, &GraphicsAppManager::UpdateCappedFpsInfo);
|
||||
ShutDown();
|
||||
}
|
||||
|
||||
@@ -165,7 +191,7 @@ namespace OpenVulkano
|
||||
inputManager->Tick();
|
||||
app->Tick();
|
||||
if (CURRENT_FRAME.needsRedraw) renderer->Tick();
|
||||
if (engineConfig->GetFpsCap() > 0 && !engineConfig->GetVSync())
|
||||
if (cappedFrameTime.count())
|
||||
{
|
||||
OnCappedFPS(start);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user