summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/lib/osdlib_win32.c
diff options
context:
space:
mode:
author couriersud <couriersud@arcor.de>2015-01-10 00:47:56 +0100
committer couriersud <couriersud@arcor.de>2015-01-10 00:47:56 +0100
commitdfb34cdb00838ee0a3bdc03947ad25806127c980 (patch)
tree35076b368b04351b8e2b68e8cba96c7c05d457fa /src/osd/modules/lib/osdlib_win32.c
parenta96765a520c2cdd15f898e50d3910d8456d58965 (diff)
For unix and windows, moved osd_ticks() and friends to
src/osd/modules/lib. For mainline and SDL windows the implementations slightly differed. Dropped the SDL one in favour of mainline windows osd_ticks() implementation. (nw)
Diffstat (limited to 'src/osd/modules/lib/osdlib_win32.c')
-rw-r--r--src/osd/modules/lib/osdlib_win32.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/osd/modules/lib/osdlib_win32.c b/src/osd/modules/lib/osdlib_win32.c
index 2a222d4ce4f..c82293384ee 100644
--- a/src/osd/modules/lib/osdlib_win32.c
+++ b/src/osd/modules/lib/osdlib_win32.c
@@ -6,6 +6,8 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
+#include <mmsystem.h>
+
#include <stdlib.h>
#ifndef _MSC_VER
#include <unistd.h>
@@ -164,3 +166,97 @@ void osd_free(void *ptr)
}
#endif
}
+
+
+//============================================================
+// GLOBAL VARIABLES
+//============================================================
+
+static osd_ticks_t ticks_per_second = 0;
+static osd_ticks_t suspend_ticks = 0;
+static BOOL using_qpc = TRUE;
+
+
+
+//============================================================
+// osd_ticks
+//============================================================
+
+osd_ticks_t osd_ticks(void)
+{
+ LARGE_INTEGER performance_count;
+
+ // if we're suspended, just return that
+ if (suspend_ticks != 0)
+ return suspend_ticks;
+
+ // if we have a per second count, just go for it
+ if (ticks_per_second != 0)
+ {
+ // QueryPerformanceCounter if we can
+ if (using_qpc)
+ {
+ QueryPerformanceCounter(&performance_count);
+ return (osd_ticks_t)performance_count.QuadPart - suspend_ticks;
+ }
+
+ // otherwise, fall back to timeGetTime
+ else
+ return (osd_ticks_t)timeGetTime() - suspend_ticks;
+ }
+
+ // if not, we have to determine it
+ using_qpc = QueryPerformanceFrequency(&performance_count) && (performance_count.QuadPart != 0);
+ if (using_qpc)
+ ticks_per_second = (osd_ticks_t)performance_count.QuadPart;
+ else
+ ticks_per_second = 1000;
+
+ // call ourselves to get the first value
+ return osd_ticks();
+}
+
+
+//============================================================
+// osd_ticks_per_second
+//============================================================
+
+osd_ticks_t osd_ticks_per_second(void)
+{
+ if (ticks_per_second == 0)
+ osd_ticks();
+ return ticks_per_second;
+}
+
+
+//============================================================
+// osd_sleep
+//============================================================
+
+void osd_sleep(osd_ticks_t duration)
+{
+ DWORD msec;
+
+ // make sure we've computed ticks_per_second
+ if (ticks_per_second == 0)
+ (void)osd_ticks();
+
+ // convert to milliseconds, rounding down
+ msec = (DWORD)(duration * 1000 / ticks_per_second);
+
+ // only sleep if at least 2 full milliseconds
+ if (msec >= 2)
+ {
+ HANDLE current_thread = GetCurrentThread();
+ int old_priority = GetThreadPriority(current_thread);
+
+ // take a couple of msecs off the top for good measure
+ msec -= 2;
+
+ // bump our thread priority super high so that we get
+ // priority when we need it
+ SetThreadPriority(current_thread, THREAD_PRIORITY_TIME_CRITICAL);
+ Sleep(msec);
+ SetThreadPriority(current_thread, old_priority);
+ }
+}