Skip to content

Commit

Permalink
Fix DeltaTime must be positive ImGui assertion (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Oct 22, 2024
1 parent 1fc7132 commit d980e9c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/oscar/UI/ui_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ namespace
SDL_Window* Window = nullptr;
std::string ClipboardText;
bool WantUpdateMonitors = true;
std::optional<AppClock::time_point> LastFrameTime;

// Mouse handling
Uint32 MouseWindowID = 0;
Expand Down Expand Up @@ -468,7 +469,20 @@ static void ImGui_ImplOscar_NewFrame(App& app)
}

// Update `DeltaTime`
io.DeltaTime = static_cast<float>(app.frame_delta_since_last_frame().count());
{
auto t = app.frame_start_time(); // note: might not increase (#935)
if (bd.LastFrameTime and t <= *bd.LastFrameTime) {
// handle the case where the clock hasn't increased since the last frame by
// adding a very small amount of time, because ImGui doesn't accept a `DeltaTime`
// of zero (see: imgui/#6189, imgui/#6114, imgui/#3644)
static_assert(static_cast<float>(std::chrono::duration<AppClock::rep, std::nano>{1}.count()) > std::numeric_limits<float>::epsilon());
t = *bd.LastFrameTime + std::chrono::nanoseconds{1};
}
const auto delta = bd.LastFrameTime ? t - *bd.LastFrameTime : AppClock::duration{1.0/60.0};
io.DeltaTime = static_cast<float>(delta.count());
bd.LastFrameTime = t;

}

// Handle mouse leaving the window
if (bd.MouseLastLeaveFrame and (bd.MouseLastLeaveFrame >= ImGui::GetFrameCount()) and bd.MouseButtonsDown == 0) {
Expand Down

0 comments on commit d980e9c

Please sign in to comment.