SDL_GetTicks() accuracy below the millisecond level

后端 未结 2 1543
小蘑菇
小蘑菇 2021-01-18 16:13

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

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 16:35

    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 
    #include 
    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(t2 - t1).count()
                  << " nanoseconds" << std::endl;
    }
    

    Running this on ideone.com gave me:

    Delta t2-t1: 282 nanoseconds
    

提交回复
热议问题