SDL Time

I have a question about timers in SDL.

The code sample from this link:
https://wiki.libsdl.org/SDL_GetPerformanceFrequency

it seems like SDL_GetTicks() will return the time so if you do something
like

double start = SDL_GetTicks();
do some wofk
double finish = SDL_GetTicks();

double elapsed = finish - start;

elapsed should be in milliseconds but I am getting back pretty large
numbers, like 200, 250 or in that range.

How do I get the milliseconds when I use get SDL_GetTicks()?

The value you are seeing should be the number of milliseconds that have
elapsed from start to finish.

As the wiki page for SDL_GetTicks() [1] states,

Use this function to get the number of milliseconds since the SDL
library initialization.

Any time that you make that call, you are getting back the number of
milliseconds since SDL initialization (this value can become quite
large if the program is run for long enough).

Hope that helps.

[1] https://wiki.libsdl.org/SDL_GetTicksOn Sat, Nov 28, 2015 at 11:40 PM, Owen Alanzo Hogarth wrote:

I have a question about timers in SDL.

The code sample from this link:
https://wiki.libsdl.org/SDL_GetPerformanceFrequency

it seems like SDL_GetTicks() will return the time so if you do
something like

double start = SDL_GetTicks();
do some wofk
double finish = SDL_GetTicks();

double elapsed = finish - start;

elapsed should be in milliseconds but I am getting back pretty large
numbers, like 200, 250 or in that range.

How do I get the milliseconds when I use get SDL_GetTicks()?

the return type of SDL_GetTicks is an integer, not a double. Treating integers as doubles without performing a conversion is usually (unless you’re intentionally doing something highly irregular) wrong.------------------------
Nate Fries

Nathaniel J Fries wrote:

the return type of SDL_GetTicks is an integer, not a double. Treating integers as doubles without performing a conversion is usually (unless you’re intentionally doing something highly irregular) wrong.

C/C++ do implicit conversions of integer types to double. IIRC double can represent all whole numbers up to 2^53 which is quite a long time even in milliseconds so I doubt that is a problem here :slight_smile:

unless of course OP is doing something silly like doing a reinterpret_cast he should be fine.

That said, he almost certainly doesn’t have cause to use a double here and should use the appropriate integer type returned from SDL_GetTicks.