Index: source/main.cpp =================================================================== --- source/main.cpp +++ source/main.cpp @@ -126,6 +126,8 @@ #include +using namespace std::literals; + extern CStrW g_UniqueLogPostfix; // Determines the lifetime of the mainloop @@ -154,8 +156,6 @@ static int g_ResizedW; static int g_ResizedH; -static std::chrono::high_resolution_clock::time_point lastFrameTime; - bool IsQuitRequested() { return g_Shutdown == ShutdownType::Quit; @@ -176,8 +176,10 @@ g_Shutdown = ShutdownType::RestartAsAtlas; } +namespace +{ // main app message handler -static InReaction MainInputHandler(const SDL_Event_* ev) +InReaction MainInputHandler(const SDL_Event_* ev) { switch(ev->ev.type) { @@ -251,7 +253,7 @@ // dispatch all pending events to the various receivers. -static void PumpEvents() +void PumpEvents() { ScriptRequest rq(g_GUI->GetScriptInterface()); @@ -278,7 +280,7 @@ * Optionally throttle the render frequency in order to * prevent 100% workload of the currently used CPU core. */ -inline static void LimitFPS() +void LimitFPS(const std::chrono::steady_clock::time_point& frameStart) { if (g_VideoMode.IsVSyncEnabled()) return; @@ -290,17 +292,12 @@ if (fpsLimit < 20.0 || fpsLimit >= 360.0) return; - double wait = 1000.0 / fpsLimit - - std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - lastFrameTime).count() / 1000.0; + const auto earliestFrameStop = frameStart + 1.0s / fpsLimit; - if (wait > 0.0) - SDL_Delay(wait); - - lastFrameTime = std::chrono::high_resolution_clock::now(); + std::this_thread::sleep_until(earliestFrameStop); } -static int ProgressiveLoad() +int ProgressiveLoad() { PROFILE3("progressive load"); @@ -345,8 +342,7 @@ return 0; } - -static void RendererIncrementalLoad() +void RendererIncrementalLoad() { PROFILE3("renderer incremental load"); @@ -360,8 +356,9 @@ while (more && timer_Time() - startTime < maxTime); } -static void Frame(RL::Interface* rlInterface) +void Frame(RL::Interface* rlInterface) { + const std::chrono::steady_clock::time_point chronoTime{std::chrono::steady_clock::now()}; g_Profiler2.RecordFrameStart(); PROFILE2("frame"); g_Profiler2.IncrementFrameNumber(); @@ -451,10 +448,10 @@ g_Profiler.Frame(); - LimitFPS(); + LimitFPS(chronoTime); } -static void NonVisualFrame() +void NonVisualFrame() { g_Profiler2.RecordFrameStart(); PROFILE2("frame"); @@ -475,7 +472,7 @@ QuitEngine(); } -static void MainControllerInit() +void MainControllerInit() { // add additional input handlers only needed by this controller: @@ -483,12 +480,12 @@ in_add_handler(MainInputHandler); } -static void MainControllerShutdown() +void MainControllerShutdown() { in_reset_handlers(); } -static std::optional CreateRLInterface(const CmdLineArgs& args) +std::optional CreateRLInterface(const CmdLineArgs& args) { if (!args.Has("rl-interface")) return std::nullopt; @@ -505,7 +502,7 @@ // moved into a helper function to ensure args is destroyed before // exit(), which may result in a memory leak. -static void RunGameOrAtlas(const PS::span argv) +void RunGameOrAtlas(const PS::span argv) { const CmdLineArgs args(argv); @@ -725,6 +722,7 @@ Threading::TaskManager::Instance().ClearQueue(); CXeromyces::Terminate(); } +} // anonymous namespace #if OS_ANDROID // In Android we compile the engine as a shared library, not an executable,