diff options
author | Piotr Dziwinski <piotrdz@gmail.com> | 2013-03-24 00:03:37 +0100 |
---|---|---|
committer | Piotr Dziwinski <piotrdz@gmail.com> | 2013-03-24 12:00:12 +0100 |
commit | 195d6cded05f7ef5bde695ee047b341a0265eab3 (patch) | |
tree | af6ffa3622ae9bf7f2f5f065e269e86a019af854 /src/app/system_windows.cpp | |
parent | c211b001d2a4c9b36034a812650f1a2ac693ee54 (diff) | |
download | colobot-195d6cded05f7ef5bde695ee047b341a0265eab3.tar.gz colobot-195d6cded05f7ef5bde695ee047b341a0265eab3.tar.bz2 colobot-195d6cded05f7ef5bde695ee047b341a0265eab3.zip |
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
Diffstat (limited to 'src/app/system_windows.cpp')
-rw-r--r-- | src/app/system_windows.cpp | 64 |
1 files changed, 39 insertions, 25 deletions
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 <windows.h> -// 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<int>(wstr.size()), NULL, 0, NULL, NULL); - std::string strTo(size_needed, 0); - WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast<int>(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<int>(str.size()), NULL, 0); - std::wstring wstrTo(size_needed, 0); - MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast<int>(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<double>(after->counterValue - before->counterValue) * (1e9 / static_cast<double>(m_counterFrequency)); + return static_cast<long long>(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<int>(wstr.size()), NULL, 0, NULL, NULL); + std::string strTo(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast<int>(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<int>(str.size()), NULL, 0); + std::wstring wstrTo(size_needed, 0); + MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast<int>(str.size()), &wstrTo[0], size_needed); + return wstrTo; + } |