summaryrefslogtreecommitdiff
path: root/timer.c
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2016-09-07 18:22:41 +0300
committerOskari Timperi <oskari.timperi@iki.fi>2016-09-07 18:22:41 +0300
commit7a080b6d053fd0965641ee38dcd55fb7f015ebba (patch)
tree2b2e8a784cc729649b4bf2107305b3b788980618 /timer.c
downloadbreakout-7a080b6d053fd0965641ee38dcd55fb7f015ebba.tar.gz
breakout-7a080b6d053fd0965641ee38dcd55fb7f015ebba.zip
Initial commitHEADmaster
Diffstat (limited to 'timer.c')
-rw-r--r--timer.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/timer.c b/timer.c
new file mode 100644
index 0000000..e437397
--- /dev/null
+++ b/timer.c
@@ -0,0 +1,48 @@
+#include "timer.h"
+
+#include <time.h>
+
+#if defined(_WIN32)
+#include <windows.h>
+int64_t frequency;
+#endif
+
+void TimerInit()
+{
+#if defined(_WIN32)
+ QueryPerformanceFrequency((LARGE_INTEGER *) &frequency);
+#endif
+}
+
+int64_t TimerGetTimeMicro()
+{
+#if defined(_WIN32)
+ int64_t value;
+ QueryPerformanceCounter((LARGE_INTEGER *) &value);
+ value = value * 1000LL*1000LL;
+ return value / frequency;
+#else
+ struct timespec tp;
+ if (clock_gettime(CLOCK_MONOTONIC, &tp))
+ {
+ return 0;
+ }
+ return (((int64_t) tp.tv_sec) * 1000LL*1000LL*1000LL + ((int64_t) tp.tv_nsec)) / 1000LL;
+#endif
+}
+
+void TimerStart(Timer *timer)
+{
+ *timer = TimerGetTimeMicro();
+}
+
+int64_t TimerElapsedMicro(Timer *timer)
+{
+ int64_t now = TimerGetTimeMicro(timer);
+ return now - *timer;
+}
+
+int64_t TimerElapsedMilli(Timer *timer)
+{
+ return TimerElapsedMicro(timer) / 1000LL;
+}