diff --git a/photon/tests/test_timer.py b/photon/tests/test_timer.py new file mode 100644 index 0000000..3bd0c15 --- /dev/null +++ b/photon/tests/test_timer.py @@ -0,0 +1,24 @@ +from .. import init, InitFlags, timer +import time + +def init_timer(): + init(InitFlags.TIMER) + +def test_performance_counter(): + init_timer() + first = timer.get_performance_counter() + time.sleep(1.1) + second = timer.get_performance_counter() + freq = timer.get_performance_frequency() + assert second > first + assert second/freq > first/freq + +def test_get_ticks(): + init_timer() + first = timer.get_ticks() + time.sleep(0.1) + second = timer.get_ticks() + assert second > first + # delay should safely put it in this range + assert 50 < second - first < 500 + diff --git a/photon/timer.py b/photon/timer.py new file mode 100644 index 0000000..08f8c08 --- /dev/null +++ b/photon/timer.py @@ -0,0 +1,17 @@ +import ctypes +from .internal import _SDL + +_SDL.SDL_GetPerformanceCounter.restype = ctypes.c_uint64 +_SDL.SDL_GetPerformanceFrequency.restype = ctypes.c_uint64 +_SDL.SDL_GetTicks.restype = ctypes.c_uint32 + + +def get_performance_counter(): + return _SDL.SDL_GetPerformanceCounter() + +def get_performance_frequency(): + return _SDL.SDL_GetPerformanceFrequency() + +def get_ticks(): + return _SDL.SDL_GetTicks() +