From 195d6cded05f7ef5bde695ee047b341a0265eab3 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 24 Mar 2013 00:03:37 +0100 Subject: Fixed timer functions on win32 * changed win32 implementation to QueryPerformaceTimer system function * refactored system utils code * proper tests for time utils and update event creation in application * should fix issue #134 --- src/app/system_windows.cpp | 64 ++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 25 deletions(-) (limited to 'src/app/system_windows.cpp') diff --git a/src/app/system_windows.cpp b/src/app/system_windows.cpp index 34fa57e..780afef 100644 --- a/src/app/system_windows.cpp +++ b/src/app/system_windows.cpp @@ -17,30 +17,25 @@ #include "app/system_windows.h" +#include "common/logger.h" + +#include -// Convert a wide Unicode string to an UTF8 string -std::string UTF8_Encode_Windows(const std::wstring &wstr) -{ - int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast(wstr.size()), NULL, 0, NULL, NULL); - std::string strTo(size_needed, 0); - WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast(wstr.size()), &strTo[0], size_needed, NULL, NULL); - return strTo; -} -// Convert an UTF8 string to a wide Unicode String -std::wstring UTF8_Decode_Windows(const std::string &str) +void CSystemUtilsWindows::Init() { - int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast(str.size()), NULL, 0); - std::wstring wstrTo(size_needed, 0); - MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast(str.size()), &wstrTo[0], size_needed); - return wstrTo; + LARGE_INTEGER freq; + QueryPerformanceFrequency(&freq); + m_counterFrequency = freq.QuadPart; + + assert(m_counterFrequency != 0); } -SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message) +SystemDialogResult CSystemUtilsWindows::SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) { unsigned int windowsType = 0; - std::wstring windowsMessage = UTF8_Decode_Windows(message); - std::wstring windowsTitle = UTF8_Decode_Windows(title); + std::wstring windowsMessage = UTF8_Decode(message); + std::wstring windowsTitle = UTF8_Decode(title); switch (type) { @@ -79,20 +74,39 @@ SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string return SDR_OK; } +void CSystemUtilsWindows::GetCurrentTimeStamp(SystemTimeStamp* stamp) +{ + LARGE_INTEGER value; + QueryPerformanceCounter(&value); + stamp->counterValue = value.QuadPart; +} + +long long int CSystemUtilsWindows::GetTimeStampExactResolution() +{ + return 1000000000ll / m_counterFrequency; +} -void GetCurrentTimeStamp_Windows(SystemTimeStamp *stamp) +long long int CSystemUtilsWindows::TimeStampExactDiff(SystemTimeStamp* before, SystemTimeStamp* after) { - GetSystemTimeAsFileTime(&stamp->fileTime); + float floatValue = static_cast(after->counterValue - before->counterValue) * (1e9 / static_cast(m_counterFrequency)); + return static_cast(floatValue); } -long long GetTimeStampExactResolution_Windows() +//! Converts a wide Unicode string to an UTF8 string +std::string CSystemUtilsWindows::UTF8_Encode(const std::wstring& wstr) { - return 100ll; + int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast(wstr.size()), NULL, 0, NULL, NULL); + std::string strTo(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast(wstr.size()), &strTo[0], size_needed, NULL, NULL); + return strTo; } -long long TimeStampExactDiff_Windows(SystemTimeStamp *before, SystemTimeStamp *after) +//! Converts an UTF8 string to a wide Unicode String +std::wstring CSystemUtilsWindows::UTF8_Decode(const std::string& str) { - long long tH = (1ll << 32) * (after->fileTime.dwHighDateTime - before->fileTime.dwHighDateTime); - long long tL = after->fileTime.dwLowDateTime - before->fileTime.dwLowDateTime; - return (tH + tL) * 100ll; + int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast(str.size()), NULL, 0); + std::wstring wstrTo(size_needed, 0); + MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast(str.size()), &wstrTo[0], size_needed); + return wstrTo; + } -- cgit v1.2.3-1-g7c22