Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Issue 4557. CFAbsoluteTimeGetCurrent can jump backwards. Use mach_abs… #4563

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions backends/imgui_impl_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "imgui.h"
#include "imgui_impl_osx.h"
#import <Cocoa/Cocoa.h>
#include <mach/mach_time.h>

// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
Expand All @@ -37,7 +38,8 @@
@class ImFocusObserver;

// Data
static CFAbsoluteTime g_Time = 0.0;
static double g_HostClockPeriod;
static double g_Time = 0.0;
static NSCursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {};
static bool g_MouseCursorHidden = false;
static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {};
Expand All @@ -52,6 +54,19 @@ + (id)_windowResizeNorthSouthCursor;
+ (id)_windowResizeEastWestCursor;
@end

static void InitHostClockPeriod()
{
struct mach_timebase_info info;
mach_timebase_info(&info);
// Period is the reciprocal of frequency.
g_HostClockPeriod = 1e-9 * ((double)info.denom / (double)info.numer);
}

static double GetMachAbsoluteTimeInSeconds()
{
return (double)mach_absolute_time() * g_HostClockPeriod;
}

static void resetKeys()
{
ImGuiIO& io = ImGui::GetIO();
Expand Down Expand Up @@ -231,9 +246,11 @@ void ImGui_ImplOSX_NewFrame(NSView* view)
}

// Setup time step
if (g_Time == 0.0)
g_Time = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime current_time = CFAbsoluteTimeGetCurrent();
if (g_Time == 0.0) {
InitHostClockPeriod();
g_Time = GetMachAbsoluteTimeInSeconds();
}
double current_time = GetMachAbsoluteTimeInSeconds();
io.DeltaTime = (float)(current_time - g_Time);
g_Time = current_time;

Expand Down