aboutsummaryrefslogtreecommitdiff
path: root/08-august/src/util/util_time.c
diff options
context:
space:
mode:
Diffstat (limited to '08-august/src/util/util_time.c')
-rw-r--r--08-august/src/util/util_time.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/08-august/src/util/util_time.c b/08-august/src/util/util_time.c
new file mode 100644
index 0000000..b601379
--- /dev/null
+++ b/08-august/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;
+}