This source file includes following definitions.
- getseconds
- FpsInit
- FpsStep
#include <sys/time.h>
static inline double getseconds() {
static int first_call = 1;
static struct timeval start_tv;
static int start_tv_retv;
const double usec_to_sec = 0.000001;
if (first_call) {
first_call = 0;
start_tv_retv = gettimeofday(&start_tv, NULL);
}
struct timeval tv;
if ((0 == start_tv_retv) && (0 == gettimeofday(&tv, NULL)))
return (tv.tv_sec - start_tv.tv_sec) + tv.tv_usec * usec_to_sec;
return 0.0;
}
struct FpsState {
double last_time;
int frame_count;
};
inline void FpsInit(struct FpsState* state) {
state->last_time = getseconds();
state->frame_count = 0;
}
inline int FpsStep(struct FpsState* state, double* out_fps) {
const double kFpsUpdateSecs = 1.0f;
double current_time = getseconds();
state->frame_count++;
if (current_time < state->last_time + kFpsUpdateSecs)
return 0;
*out_fps = state->frame_count / (current_time - state->last_time);
state->last_time = current_time;
state->frame_count = 0;
return 1;
}