delta_time is now calculating by converting timespec to seconds
parent
fcb9645dbe
commit
d69b92a464
22
src/engine.c
22
src/engine.c
|
@ -5,11 +5,27 @@
|
||||||
#include "corelib/input.h"
|
#include "corelib/input.h"
|
||||||
#include "time.h"
|
#include "time.h"
|
||||||
|
|
||||||
static float _delta_time = 0;
|
static double _delta_time = 0;
|
||||||
|
static double _min_frame_interval = 0;
|
||||||
static struct timespec start_last_frame;
|
static struct timespec start_last_frame;
|
||||||
|
|
||||||
|
#define CURRENT_TIME(__out) { struct timespect ts; timespec_get(&ts, TIME_UTC); __out = ts.tv_sec + tv.nsec * 1E-09; }
|
||||||
|
|
||||||
|
inline static
|
||||||
|
double timespec_to_sec(struct timespec spec) {
|
||||||
|
return spec.tv_sec + spec.tv_nsec;
|
||||||
|
}
|
||||||
|
|
||||||
inline float delta_time() {
|
inline float delta_time() {
|
||||||
return _delta_time;
|
return (float)_delta_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_frame_interval(double frame_interval) {
|
||||||
|
_min_frame_interval = frame_interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_frame_rate_limit(int fps) {
|
||||||
|
_min_frame_interval = 1.0/(double)fps;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
|
@ -58,7 +74,7 @@ int _engine_run() {
|
||||||
struct timespec next_time;
|
struct timespec next_time;
|
||||||
while(g_context.running) {
|
while(g_context.running) {
|
||||||
timespec_get(&next_time, TIME_UTC);
|
timespec_get(&next_time, TIME_UTC);
|
||||||
_delta_time = (next_time.tv_nsec - start_last_frame.tv_nsec) * 1E-9;
|
_delta_time = timespec_to_sec(next_time) - timespec_to_sec(start_last_frame);
|
||||||
if(next_time.tv_nsec < start_last_frame.tv_nsec) _delta_time = 0;
|
if(next_time.tv_nsec < start_last_frame.tv_nsec) _delta_time = 0;
|
||||||
start_last_frame = next_time;
|
start_last_frame = next_time;
|
||||||
_handle_events();
|
_handle_events();
|
||||||
|
|
|
@ -6,6 +6,8 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern float delta_time();
|
extern float delta_time();
|
||||||
|
extern void set_frame_interval(double frame_interval);
|
||||||
|
extern void set_frame_rate_limit(int fps);
|
||||||
|
|
||||||
/* TO BE DEFINED IN GAME */
|
/* TO BE DEFINED IN GAME */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue