Is it possible to replace SDL_GetTicks with std :: chrono :: high_resolution_clock?

Checking out new things with C ++, I found the std :: chrono library.

I am wondering if std :: chrono :: high_resolution_clock can be a good replacement for SDL_GetTicks?

+4
source share
2 answers

The advantage of switching from std::chrono::high_resolution_clock is that you do not store time points and time durations in Uint32 . The std::chrono comes with a wide range of std::chrono::duration , which you should use instead. This will make the code more readable and less ambiguous:

 Uint32 t0 = SDL_GetTicks(); // ... Uint32 t1 = SDL_GetTicks(); // ... // Is t1 a time point or time duration? Uint32 d = t1 -t0; // What units does d have? 

vs

 using namespace std::chrono; typedef high_resolution_clock Clock; Clock::time_point t0 = Clock::now(); // ... Clock::time_point t1 = Clock::now(); // ... // Is t1 has type time_point. It can't be mistaken for a time duration. milliseconds d = t1 - t0; // d has type milliseconds 

The typed system for holding points in time and time has no overhead in relation to simple storage of things in Uint32 . In addition, things will be stored in Int64 . But even if you really would like to customize,

 typedef duration<Uint32, milli> my_millisecond; 

You can check the accuracy of high_resolution_clock with

 cout << high_resolution_clock::period::num << '/' << high_resolution_clock::period::den << '\n'; 
+9
source

SDL_GetTicks returns milliseconds, so it’s quite possible to use std :: chrono, but remember to convert units. It may not be as simple as SDL_GetTicks. In addition, the starting point will not be the same.

+2
source

All Articles