I program currently something with SDL2.
All works fine, but I have a problem with the SDL_GetTicks()
method.
Normally it should return the total application ti
Well, of course, you need to actually wait until >=1ms has passed before updating your last tick count
void Application::Update()
{
Uint32 current_time = SDL_GetTicks();
Uint32 delta_time = current_time - last_update_time;
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
{
should_close = true;
}
break;
default:
break;
}
}
if (delta_time >= 1)
{
// Update game objects with delta_time
last_update_time = current_time;
}
}
You cannot use SDL_GetTicks() if you want higher precision but there are many other alternatives. If you want to be platform independant you need to be careful though, but here is a portable C++11 example that will get you started:
#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;
int main()
{
auto t1 = Clock::now();
auto t2 = Clock::now();
std::cout << "Delta t2-t1: "
<< std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count()
<< " nanoseconds" << std::endl;
}
Running this on ideone.com gave me:
Delta t2-t1: 282 nanoseconds