1

I'm writing an Object Oriented OpenGL framework in perl and I'm running into a weird issue when I measure the DeltaTime between each frame. The delta time seems to go negative every once in a while (every ~.5 seconds). I'm not sure if this is gettimeofday's problem or if it's a problem with how GLUT calls my callbacks, but it's pretty annoying because it makes the movement of my sprite jump slightly every half a second.

Here's my glut main loop function:

# This is called everytime in the main glut loop
sub Tick
{
    my $this = shift;
    my ($now, $dt);

    $now = gettimeofday;
    $dt = $now - $this->{_oldTime};

    if ($dt < 0)
    {
        my $dterr;

        $dterr = $now - $this->{_oldErrorTime};

        print "WTF! We just encountered a time paradox! " . 
            "This function was last called $dt seconds ago...\n" .
            "Current time: $now, Last call: $this->{_oldTime}\n" .
            "We already encountered this error $dterr seconds ago.\n\n";

        $this->{_oldErrorTime} = $now;
    }

    $this->{_oldTime} = $now;
    $this->{FPS} = 1.0 / $dt;

    $this->Update($dt);
    $this->DrawFrame($dt);
}

And here's the output:

WTF! We just encountered a time paradox! This function was last called -0.017144 9184417725 seconds ago... Current time: 1340196716.27624, Last call: 1340196716.29339 We already encountered this error 0.482785940170288 seconds ago.

WTF! We just encountered a time paradox! This function was last called -0.013265 84815979 seconds ago... Current time: 1340196716.74632, Last call: 1340196716.75959 We already encountered this error 0.470081090927124 seconds ago.

WTF! We just encountered a time paradox! This function was last called -0.011317 9683685303 seconds ago... Current time: 1340196717.21836, Last call: 1340196717.22968 We already encountered this error 0.472035884857178 seconds ago.

WTF! We just encountered a time paradox! This function was last called -0.015201 0917663574 seconds ago... Current time: 1340196717.68649, Last call: 1340196717.70169 We already encountered this error 0.468127012252808 seconds ago.

Francesco Noferi
  • 482
  • 7
  • 13

2 Answers2

4

You need CLOCK_MONOTONIC, see Time::HiRes.

daxim
  • 39,270
  • 4
  • 65
  • 132
  • Thanks, but I need this to also work on win32, any other ideas other than creating my own time ticker thread? – Francesco Noferi Jun 20 '12 at 13:45
  • 2
    http://stackoverflow.com/questions/211257/does-windows-provide-a-monotonically-increasing-clock-to-applications – daxim Jun 20 '12 at 13:54
1

Thanks to you guys' answers, I came up with this cross-platform monotonic stopwatch function:

use constant WIN32 =>   $^O eq "MSWin32";

our $QueryPerformanceCounter = undef;
our $QueryPerformanceFrequency = undef;
our $qpf = undef;

sub GetTime
{
    # Windows
    if (WIN32)
    {
        my ($count, @unpacked_count);

        require Win32::API;
        Win32::API->import();

        if (!$QueryPerformanceCounter || !$QueryPerformanceFrequency || !$qpf)
        {
            my ($freq, @unpacked_freq);

            $QueryPerformanceCounter = new Win32::API(
                "Kernel32", "QueryPerformanceCounter", [qw(P)], 'I')
            or Carp::croak("GLPerl::Utils::GetTime(): Failed to get QueryPerformanceCounter: " .
                Win32::FormatMessage(Win32::GetLastError()));

            $QueryPerformanceFrequency = new Win32::API(
                "Kernel32", "QueryPerformanceFrequency", [qw(P)], 'I')
            or Carp::croak("GLPerl::Utils::GetTime(): Failed to get QueryPerformanceFrequency: " .
                Win32::FormatMessage(Win32::GetLastError()));

            $freq = pack 'I2', 0;

            Carp::croak("GLPerl::Utils::GetTime(): QueryPerformanceFrequency call failed: " .
                Win32::FormatMessage(Win32::GetLastError()))
                unless ($QueryPerformanceFrequency->Call($freq));

            @unpacked_freq = reverse unpack 'I2', $freq;
            $qpf = $unpacked_freq[0] * 2**32 + $unpacked_freq[1];
        }

        $count = pack 'I2', 0;

        Carp::croak("GLPerl::Utils::GetTime(): QueryPerformanceCounter call failed: " .
            Win32::FormatMessage(Win32::GetLastError()))
            unless ($QueryPerformanceCounter->Call($count));

        @unpacked_count = reverse unpack 'I2', $count;
        return ($unpacked_count[0] * 2**32 + $unpacked_count[1]) / $qpf;
    }

    # Linux
    require Time::HiRes;
    Time::HiRes->import(qw(clock_gettime));
    eval "return clock_gettime(CLOCK_MONOTONIC);";
}
Francesco Noferi
  • 482
  • 7
  • 13