diff options
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/system.cpp | 270 | ||||
-rw-r--r-- | src/app/system.h | 46 | ||||
-rw-r--r-- | src/app/system_linux.h | 101 | ||||
-rw-r--r-- | src/app/system_other.h | 146 | ||||
-rw-r--r-- | src/app/system_windows.h | 122 |
5 files changed, 498 insertions, 187 deletions
diff --git a/src/app/system.cpp b/src/app/system.cpp index a765e11..eb0321b 100644 --- a/src/app/system.cpp +++ b/src/app/system.cpp @@ -21,19 +21,21 @@ #include "common/config.h" + #if defined(PLATFORM_WINDOWS) -#include <windows.h> +#include "system_windows.h" + #elif defined(PLATFORM_LINUX) -#include <cstdlib> +#include "system_linux.h" + #else -#include <iostream> +#include "system_other.h" + #endif +#include <cassert> -SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message); -SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message); -SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message); /** * Displays a system dialog with info, error, question etc. message. @@ -45,209 +47,103 @@ SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& */ SystemDialogResult SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) { - #if defined(PLATFORM_WINDOWS) +#if defined(PLATFORM_WINDOWS) return SystemDialog_Windows(type, title, message); - #elif defined(PLATFORM_LINUX) +#elif defined(PLATFORM_LINUX) return SystemDialog_Linux(type, title, message); - #else +#else return SystemDialog_Other(type, title, message); - #endif +#endif } - - -#if defined(PLATFORM_WINDOWS) - -// Convert a wide Unicode string to an UTF8 string -std::string UTF8_Encode_Windows(const std::wstring &wstr) +SystemTimeStamp* CreateTimeStamp() { - int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); - std::string strTo(size_needed, 0); - WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); - return strTo; + return new SystemTimeStamp(); } -// Convert an UTF8 string to a wide Unicode String -std::wstring UTF8_Decode_Windows(const std::string &str) +void DestroyTimeStamp(SystemTimeStamp *stamp) { - int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); - std::wstring wstrTo(size_needed, 0); - MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); - return wstrTo; + delete stamp; } -SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message) +void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src) { - unsigned int windowsType = 0; - std::wstring windowsMessage = UTF8_Decode_Windows(message); - std::wstring windowsTitle = UTF8_Decode_Windows(title); - - switch (type) - { - case SDT_INFO: - default: - windowsType = MB_ICONINFORMATION|MB_OK; - break; - case SDT_WARNING: - windowsType = MB_ICONWARNING|MB_OK; - break; - case SDT_ERROR: - windowsType = MB_ICONERROR|MB_OK; - break; - case SDT_YES_NO: - windowsType = MB_ICONQUESTION|MB_YESNO; - break; - case SDT_OK_CANCEL: - windowsType = MB_ICONWARNING|MB_OKCANCEL; - break; - } - - switch (MessageBoxW(NULL, windowsMessage.c_str(), windowsTitle.c_str(), windowsType)) - { - case IDOK: - return SDR_OK; - case IDCANCEL: - return SDR_CANCEL; - case IDYES: - return SDR_YES; - case IDNO: - return SDR_NO; - default: - break; - } - - return SDR_OK; + *dst = *src; } +void GetCurrentTimeStamp(SystemTimeStamp *stamp) +{ +#if defined(PLATFORM_WINDOWS) + GetCurrentTimeStamp_Windows(stamp); #elif defined(PLATFORM_LINUX) + GetCurrentTimeStamp_Linux(stamp); +#else + GetCurrentTimeStamp_Other(stamp); +#endif +} -SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message) +float GetTimeStampResolution(SystemTimeUnit unit) { - std::string options = ""; - switch (type) - { - case SDT_INFO: - default: - options = "--info"; - break; - case SDT_WARNING: - options = "--warning"; - break; - case SDT_ERROR: - options = "--error"; - break; - case SDT_YES_NO: - options = "--question --ok-label=\"Yes\" --cancel-label=\"No\""; - break; - case SDT_OK_CANCEL: - options = "--question --ok-label=\"OK\" --cancel-label=\"Cancel\""; - break; - } - - std::string command = "zenity " + options + " --text=\"" + message + "\" --title=\"" + title + "\""; - int code = system(command.c_str()); - - SystemDialogResult result = SDR_OK; - switch (type) - { - case SDT_YES_NO: - result = code ? SDR_NO : SDR_YES; - break; - case SDT_OK_CANCEL: - result = code ? SDR_CANCEL : SDR_OK; - break; - default: - break; - } - + unsigned long long exact = 0; +#if defined(PLATFORM_WINDOWS) + exact = GetTimeStampExactResolution_Windows(); +#elif defined(PLATFORM_LINUX) + exact = GetTimeStampExactResolution_Linux(); +#else + exact = GetTimeStampExactResolution_Other(); +#endif + float result = 0.0f; + if (unit == STU_SEC) + result = exact * 1e-9; + else if (unit == STU_MSEC) + result = exact * 1e-6; + else if (unit == STU_USEC) + result = exact * 1e-3; + else + assert(false); return result; } +long long GetTimeStampExactResolution() +{ +#if defined(PLATFORM_WINDOWS) + return GetTimeStampExactResolution_Windows(); +#elif defined(PLATFORM_LINUX) + return GetTimeStampExactResolution_Linux(); #else + return GetTimeStampExactResolution_Other(); +#endif +} -SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message) +float TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit) { - switch (type) - { - case SDT_INFO: - std::cout << "INFO: "; - break; - case SDT_WARNING: - std::cout << "WARNING:"; - break; - case SDT_ERROR: - std::cout << "ERROR: "; - break; - case SDT_YES_NO: - case SDT_OK_CANCEL: - std::cout << "QUESTION: "; - break; - } - - std::cout << message << std::endl; - - std::string line; - - SystemDialogResult result = SDR_OK; - - bool done = false; - while (!done) - { - switch (type) - { - case SDT_INFO: - case SDT_WARNING: - case SDT_ERROR: - std::cout << "Press ENTER to continue"; - break; - - case SDT_YES_NO: - std::cout << "Type 'Y' for Yes or 'N' for No"; - break; - - case SDT_OK_CANCEL: - std::cout << "Type 'O' for OK or 'C' for Cancel"; - break; - } - - std::getline(std::cin, line); - - switch (type) - { - case SDT_INFO: - case SDT_WARNING: - case SDT_ERROR: - done = true; - break; - - case SDT_YES_NO: - if (line == "Y" || line == "y") - { - result = SDR_YES; - done = true; - } - else if (line == "N" || line == "n") - { - result = SDR_NO; - done = true; - } - break; - - case SDT_OK_CANCEL: - if (line == "O" || line == "o") - { - done = true; - result = SDR_OK; - } - else if (line == "C" || line == "c") - { - done = true; - result = SDR_CANCEL; - } - break; - } - } - + long long exact = 0; +#if defined(PLATFORM_WINDOWS) + exact = TimeStampExactDiff_Windows(before, after); +#elif defined(PLATFORM_LINUX) + exact = TimeStampExactDiff_Linux(before, after); +#else + exact = TimeStampExactDiff_Other(before, after); +#endif + float result = 0.0f; + if (unit == STU_SEC) + result = exact * 1e-9; + else if (unit == STU_MSEC) + result = exact * 1e-6; + else if (unit == STU_USEC) + result = exact * 1e-3; + else + assert(false); return result; } -#endif // if defined(PLATFORM_WINDOWS) + +long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) +{ +#if defined(PLATFORM_WINDOWS) + return TimeStampExactDiff_Windows(before, after); +#elif defined(PLATFORM_LINUX) + return TimeStampExactDiff_Linux(before, after); +#else + return TimeStampExactDiff_Other(before, after); +#endif +} diff --git a/src/app/system.h b/src/app/system.h index 3bf6457..3c04760 100644 --- a/src/app/system.h +++ b/src/app/system.h @@ -23,6 +23,8 @@ #include <string> +/* Dialog utils */ + /** * \enum SysDialogType * \brief Type of system dialog @@ -57,3 +59,47 @@ enum SystemDialogResult //! Displays a system dialog SystemDialogResult SystemDialog(SystemDialogType, const std::string &title, const std::string &message); + + +/* Time utils */ + +enum SystemTimeUnit +{ + //! seconds + STU_SEC, + //! milliseconds + STU_MSEC, + //! microseconds + STU_USEC +}; + +/* Forward declaration of time stamp struct + * SystemTimeStamp should be used in a pointer context. + * The implementation details are hidden because of platform dependence. */ +struct SystemTimeStamp; + +//! Creates a new time stamp object +SystemTimeStamp* CreateTimeStamp(); + +//! Destroys a time stamp object +void DestroyTimeStamp(SystemTimeStamp *stamp); + +//! Copies the time stamp from \a src to \a dst +void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src); + +//! Returns a time stamp associated with current time +void GetCurrentTimeStamp(SystemTimeStamp *stamp); + +//! Returns the platform's expected time stamp resolution +float GetTimeStampResolution(SystemTimeUnit unit = STU_SEC); + +//! Returns the platform's exact (in nanosecond units) expected time stamp resolution +long long GetTimeStampExactResolution(); + +//! Returns a difference between two timestamps in given time unit +/** The difference is \a after - \a before. */ +float TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit = STU_SEC); + +//! Returns the exact (in nanosecond units) difference between two timestamps +/** The difference is \a after - \a before. */ +long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after); diff --git a/src/app/system_linux.h b/src/app/system_linux.h new file mode 100644 index 0000000..f58c9a1 --- /dev/null +++ b/src/app/system_linux.h @@ -0,0 +1,101 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// system_linux.h + +/* This header contains Linux-specific code for system utils + from system.h. There is no separate .cpp module for simplicity.*/ + +#include <sys/time.h> +#include <time.h> +#include <stdlib.h> + + +SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message); + +void GetCurrentTimeStamp_Linux(SystemTimeStamp *stamp); +long long GetTimeStampExactResolution_Linux(); +long long TimeStampExactDiff_Linux(SystemTimeStamp *before, SystemTimeStamp *after); + +struct SystemTimeStamp +{ + timespec clockTime; + + SystemTimeStamp() + { + clockTime.tv_sec = clockTime.tv_nsec = 0; + } +}; + + +SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message) +{ + std::string options = ""; + switch (type) + { + case SDT_INFO: + default: + options = "--info"; + break; + case SDT_WARNING: + options = "--warning"; + break; + case SDT_ERROR: + options = "--error"; + break; + case SDT_YES_NO: + options = "--question --ok-label=\"Yes\" --cancel-label=\"No\""; + break; + case SDT_OK_CANCEL: + options = "--question --ok-label=\"OK\" --cancel-label=\"Cancel\""; + break; + } + + std::string command = "zenity " + options + " --text=\"" + message + "\" --title=\"" + title + "\""; + int code = system(command.c_str()); + + SystemDialogResult result = SDR_OK; + switch (type) + { + case SDT_YES_NO: + result = code ? SDR_NO : SDR_YES; + break; + case SDT_OK_CANCEL: + result = code ? SDR_CANCEL : SDR_OK; + break; + default: + break; + } + + return result; +} + +void GetCurrentTimeStamp_Linux(SystemTimeStamp *stamp) +{ + clock_gettime(CLOCK_MONOTONIC, &stamp->clockTime); +} + +long long GetTimeStampExactResolution_Linux() +{ + return 1ll; +} + +long long TimeStampExactDiff_Linux(SystemTimeStamp *before, SystemTimeStamp *after) +{ + return (after->clockTime.tv_nsec - before->clockTime.tv_nsec) + + (after->clockTime.tv_sec - before->clockTime.tv_sec) * 1000000000ll; +} diff --git a/src/app/system_other.h b/src/app/system_other.h new file mode 100644 index 0000000..9f13ffa --- /dev/null +++ b/src/app/system_other.h @@ -0,0 +1,146 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// system_other.h + +/* This header contains fallback code for other platforms for system utils + from system.h. There is no separate .cpp module for simplicity.*/ + +#include <SDL/SDL.h> + +#include <iostream> + + +SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message); + +void GetCurrentTimeStamp_Other(SystemTimeStamp *stamp); +long long GetTimeStampExactResolution_Other(); +long long TimeStampExactDiff_Other(SystemTimeStamp *before, SystemTimeStamp *after); + +struct SystemTimeStamp +{ + Uint32 sdlTicks; + + SystemTimeStamp() + { + sdlTicks = 0; + } +}; + + +SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message) +{ + switch (type) + { + case SDT_INFO: + std::cout << "INFO: "; + break; + case SDT_WARNING: + std::cout << "WARNING:"; + break; + case SDT_ERROR: + std::cout << "ERROR: "; + break; + case SDT_YES_NO: + case SDT_OK_CANCEL: + std::cout << "QUESTION: "; + break; + } + + std::cout << message << std::endl; + + std::string line; + + SystemDialogResult result = SDR_OK; + + bool done = false; + while (!done) + { + switch (type) + { + case SDT_INFO: + case SDT_WARNING: + case SDT_ERROR: + std::cout << "Press ENTER to continue"; + break; + + case SDT_YES_NO: + std::cout << "Type 'Y' for Yes or 'N' for No"; + break; + + case SDT_OK_CANCEL: + std::cout << "Type 'O' for OK or 'C' for Cancel"; + break; + } + + std::getline(std::cin, line); + + switch (type) + { + case SDT_INFO: + case SDT_WARNING: + case SDT_ERROR: + done = true; + break; + + case SDT_YES_NO: + if (line == "Y" || line == "y") + { + result = SDR_YES; + done = true; + } + else if (line == "N" || line == "n") + { + result = SDR_NO; + done = true; + } + break; + + case SDT_OK_CANCEL: + if (line == "O" || line == "o") + { + done = true; + result = SDR_OK; + } + else if (line == "C" || line == "c") + { + done = true; + result = SDR_CANCEL; + } + break; + } + } + + return result; +} + + + +void GetCurrentTimeStamp_Other(SystemTimeStamp *stamp) +{ + stamp->sdlTicks = SDL_GetTicks(); +} + +long long GetTimeStampExactResolution_Other() +{ + return 1000000ll; +} + +long long TimeStampExactDiff_Other(SystemTimeStamp *before, SystemTimeStamp *after) +{ + return (after->sdlTicks - before->sdlTicks) * 1000000ll; +} diff --git a/src/app/system_windows.h b/src/app/system_windows.h new file mode 100644 index 0000000..eb6beec --- /dev/null +++ b/src/app/system_windows.h @@ -0,0 +1,122 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// system_windows.h + +/* This header contains Windows-specific code for system utils + from system.h. There is no separate .cpp module for simplicity.*/ + +#include <windows.h> + + +std::string UTF8_Encode_Windows(const std::wstring &wstr); +std::wstring UTF8_Decode_Windows(const std::string &str); +SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message); + +void GetCurrentTimeStamp_Windows(SystemTimeStamp *stamp); +long long GetTimeStampExactResolution_Windows(); +long long TimeStampExactDiff_Windows(SystemTimeStamp *before, SystemTimeStamp *after); + +struct SystemTimeStamp +{ + FILETIME fileTime; + + SystemTimeStamp() + { + fileTime.dwHighDateTime = fileTime.dwLowDateTime = 0; + } +}; + + +// 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], (int)wstr.size(), NULL, 0, NULL, NULL); + std::string strTo(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (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) +{ + int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); + std::wstring wstrTo(size_needed, 0); + MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); + return wstrTo; +} + +SystemDialogResult SystemDialog_Windows(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); + + switch (type) + { + case SDT_INFO: + default: + windowsType = MB_ICONINFORMATION|MB_OK; + break; + case SDT_WARNING: + windowsType = MB_ICONWARNING|MB_OK; + break; + case SDT_ERROR: + windowsType = MB_ICONERROR|MB_OK; + break; + case SDT_YES_NO: + windowsType = MB_ICONQUESTION|MB_YESNO; + break; + case SDT_OK_CANCEL: + windowsType = MB_ICONWARNING|MB_OKCANCEL; + break; + } + + switch (MessageBoxW(NULL, windowsMessage.c_str(), windowsTitle.c_str(), windowsType)) + { + case IDOK: + return SDR_OK; + case IDCANCEL: + return SDR_CANCEL; + case IDYES: + return SDR_YES; + case IDNO: + return SDR_NO; + default: + break; + } + + return SDR_OK; +} + + +void GetCurrentTimeStamp_Windows(SystemTimeStamp *stamp) +{ + GetSystemTimeAsFileTime(&stamp->fileTime); +} + +long long GetTimeStampExactResolution_Windows() +{ + return 100ll; +} + +long long TimeStampExactDiff_Windows(SystemTimeStamp *before, SystemTimeStamp *after) +{ + long long tH = (1ll << 32) * (after->fileTime.dwHighDateTime - before->fileTime.dwHighDateTime); + long long tL = after->fileTime.dwLowDateTime - before->fileTime.dwLowDateTime; + return (tH + tL) * 100ll; +} |