From 6b8af9cf83851c075c6c9514b1deaa931c2b19a4 Mon Sep 17 00:00:00 2001 From: Thomas Guillermo Albers Raviola Date: Fri, 16 Jan 2026 23:02:32 +0100 Subject: Initial commit --- 07-july/src/util/util_time.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 07-july/src/util/util_time.c (limited to '07-july/src/util/util_time.c') diff --git a/07-july/src/util/util_time.c b/07-july/src/util/util_time.c new file mode 100644 index 0000000..b601379 --- /dev/null +++ b/07-july/src/util/util_time.c @@ -0,0 +1,52 @@ +#include "util_time.h" + +static struct +{ + float max_ticks_per_frame; //< cuantos ticks (tiempo demora) un frame + Uint32 counted_frames; //< cuantos frames han pasado + Uint32 start_ticks; //< ticks al iniciar la iteracion del loop + Uint32 beg_ticks; //< ticks desde el inicio del juego + Uint32 time_per_frame; +} TIME = { + 1000.0f / 60.0f, + 0, 0, 0, 0 +}; + +void Time_Init() +{ + TIME.beg_ticks = SDL_GetTicks(); +} + +void Time_Begin() +{ + TIME.start_ticks = SDL_GetTicks(); +} + +float Time_End() +{ + TIME.counted_frames += 1; + float FPS = TIME.counted_frames / ( (float)(SDL_GetTicks() - TIME.beg_ticks) / 1000.f ); + + float frameTicks = (float)(SDL_GetTicks() - TIME.start_ticks); + TIME.time_per_frame = frameTicks; + if(frameTicks < TIME.max_ticks_per_frame){ + SDL_Delay( (Uint32)(TIME.max_ticks_per_frame - frameTicks) ); + } + + return FPS; +} + +float Time_GetFrameTime() +{ + return (float)TIME.time_per_frame / 1000.0f; +} + +void Time_SetMaxFramesPerSecond(Uint32 frames) +{ + TIME.max_ticks_per_frame = 1000.0f / (float)frames; +} + +Uint32 Time_GetCountedFrames() +{ + return TIME.counted_frames; +} -- cgit v1.2.3