Way to get time between frames

Tue Sep 21, 2021 2:18 pm

I want smoothly move sprite on camera screen using render overlay. For this I need to know exact time that passed between frames.
Problem is, time that Gzdoom sends into render overlay event are time passed between frames inside one iteration of main game update loop. In other words it cuts time passed from actual previous frame, that happened in previous step, and counts it from current update step.

What is a "correct" way to get time passed between frames?
I managed to find a way
Code:
    private ui double last_tick;

    private ui double frame_time(double cur_tick)
    {
        double tick_ = cur_tick > last_tick ? cur_tick - last_tick : cur_tick;
        last_tick = cur_tick;
        return tick_;
    }

    override void uitick()
    {
        last_tick = 0;
       
    }

    override void renderoverlay(renderevent e)
    {
        double time_passed = frame_time(e.FracTic);
        //advanced logic here
    }

but it feels like a hack, because it use both ui tick, which have nothing to do with render overlay.

Re: Way to get time between frames

Wed Sep 22, 2021 4:01 am

Okay I managed to squish it into one function
Code:
    ui double last_tick;

    ui double time_passed;

    ui bool st;
    ui int frame_count;

    private ui bool is_rendering_active(double frame_time)
    {
        //opening console, menu or pausing game stops rendering
        if(frame_time == 1.0)
            return false;
        else
            return true;
    }

    private ui double frame_time(double cur_tick)
    {
        double tick_ = cur_tick > last_tick ? cur_tick - last_tick : cur_tick + 1 - last_tick;
        last_tick = cur_tick;
        return tick_;
    }

    override void renderoverlay(renderevent e)
    {
        if(st) return;
        double tick_frac = frame_time(e.FracTic);
        time_passed += tick_frac * 1/thinker.ticrate;

        console.printf("passed %f, sum %f", tick_frac, time_passed);
        frame_count++;

        if(time_passed >= 3)//seconds
        {
            st = true;
            console.printf("\n framecount %i, mid fps %f", frame_count, double(time_passed/frame_count) );
        }
    }

It works like a swiss watch in all situations, except on fps values that are approximately equal to the native Doom fps.
On small values, on any smaller than 40 fps on my machine, time between frames become too "unstable" comparing to the time of game update loop. Is there are any way to prevent it? You can change fps amount by typing "vid_maxfps number" into console.