aboutsummaryrefslogtreecommitdiff
path: root/07-july/src/util/util_time.c
blob: b6013794945dc00db7fba0fd59a52ae96ba84e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;
}