HATPC/engine/timing.c

69 lines
1.6 KiB
C

#include "timing.h"
#include "assert.h"
bool set_counter_period(ManualCounter* counter, float period)
{
if (period < 0.0f) return false;
counter->period = period;
return true;
}
void elapse_counter_time(ManualCounter* counter, float delta)
{
counter->fractional += delta;
if (counter->fractional > counter->period)
{
counter->fractional -= counter->period;
counter->count++;
}
}
void reset_counter_count(ManualCounter* counter)
{
counter->count = 0;
}
void reset_counter(ManualCounter* counter)
{
counter->count = 0;
counter->fractional = 0.0f;
}
bool set_frame_timings(FrameTimer* timer, float frame_time, int count_period, int max_count)
{
if (!set_counter_period((ManualCounter*)timer, frame_time)) return false;
if (count_period < 0 || max_count < 0) return false;
timer->frame_count_period = count_period;
timer->frame_max_count = max_count;
return true;
}
void elapse_frame_time(FrameTimer* timer, float delta)
{
timer->has_elapsed = false;
if (timer->frame_max_count == 0) return;
elapse_counter_time((ManualCounter *)timer, delta);
if (timer->counter.count >= timer->frame_count_period)
{
reset_counter_count((ManualCounter*)timer);
if (!timer->pause)
{
timer->frame_count++;
timer->frame_count %= timer->frame_max_count;
timer->has_elapsed = true;
}
}
}
void reset_frame_count(FrameTimer* timer)
{
reset_counter_count((ManualCounter*)timer);
timer->frame_count = 0;
}
void reset_frame_timer(FrameTimer* timer)
{
reset_counter((ManualCounter*)timer);
timer->frame_count = 0;
}