From e3de52a0c407c389e307acffe1374749e43e9f5a Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sat, 9 Nov 2024 10:02:28 -0700 Subject: [PATCH] Simplify implementations of `sf::sleep` --- include/SFML/System/Sleep.hpp | 2 +- src/SFML/System/CMakeLists.txt | 7 +-- src/SFML/System/Sleep.cpp | 50 ------------------- .../System/Unix/{SleepImpl.cpp => Sleep.cpp} | 13 +++-- src/SFML/System/Unix/SleepImpl.hpp | 48 ------------------ .../System/Win32/{SleepImpl.cpp => Sleep.cpp} | 16 ++++-- src/SFML/System/Win32/SleepImpl.hpp | 48 ------------------ 7 files changed, 22 insertions(+), 162 deletions(-) delete mode 100644 src/SFML/System/Sleep.cpp rename src/SFML/System/Unix/{SleepImpl.cpp => Sleep.cpp} (90%) delete mode 100644 src/SFML/System/Unix/SleepImpl.hpp rename src/SFML/System/Win32/{SleepImpl.cpp => Sleep.cpp} (84%) delete mode 100644 src/SFML/System/Win32/SleepImpl.hpp diff --git a/include/SFML/System/Sleep.hpp b/include/SFML/System/Sleep.hpp index c5d1962a0..ed566a82b 100644 --- a/include/SFML/System/Sleep.hpp +++ b/include/SFML/System/Sleep.hpp @@ -52,6 +52,6 @@ class Time; /// \param duration Time to sleep /// //////////////////////////////////////////////////////////// -void SFML_SYSTEM_API sleep(Time duration); +SFML_SYSTEM_API void sleep(Time duration); } // namespace sf diff --git a/src/SFML/System/CMakeLists.txt b/src/SFML/System/CMakeLists.txt index 3f313f6aa..3a28a9304 100644 --- a/src/SFML/System/CMakeLists.txt +++ b/src/SFML/System/CMakeLists.txt @@ -14,7 +14,6 @@ set(SRC ${INCROOT}/Export.hpp ${INCROOT}/InputStream.hpp ${INCROOT}/NativeActivity.hpp - ${SRCROOT}/Sleep.cpp ${INCROOT}/Sleep.hpp ${SRCROOT}/String.cpp ${INCROOT}/String.hpp @@ -42,14 +41,12 @@ source_group("" FILES ${SRC}) # add platform specific sources if(SFML_OS_WINDOWS) set(PLATFORM_SRC - ${SRCROOT}/Win32/SleepImpl.cpp - ${SRCROOT}/Win32/SleepImpl.hpp + ${SRCROOT}/Win32/Sleep.cpp ) source_group("windows" FILES ${PLATFORM_SRC}) else() set(PLATFORM_SRC - ${SRCROOT}/Unix/SleepImpl.cpp - ${SRCROOT}/Unix/SleepImpl.hpp + ${SRCROOT}/Unix/Sleep.cpp ) if(SFML_OS_ANDROID) diff --git a/src/SFML/System/Sleep.cpp b/src/SFML/System/Sleep.cpp deleted file mode 100644 index 68dbc2e89..000000000 --- a/src/SFML/System/Sleep.cpp +++ /dev/null @@ -1,50 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include - -#if defined(SFML_SYSTEM_WINDOWS) -#include -#else -#include -#endif - - -namespace sf -{ -//////////////////////////////////////////////////////////// -void sleep(Time duration) -{ - // Note that 'std::this_thread::sleep_for' is intentionally not used here - // as it results in inconsistent sleeping times under MinGW-w64. - - if (duration >= Time::Zero) - priv::sleepImpl(duration); -} - -} // namespace sf diff --git a/src/SFML/System/Unix/SleepImpl.cpp b/src/SFML/System/Unix/Sleep.cpp similarity index 90% rename from src/SFML/System/Unix/SleepImpl.cpp rename to src/SFML/System/Unix/Sleep.cpp index a4320b85a..cdfde2553 100644 --- a/src/SFML/System/Unix/SleepImpl.cpp +++ b/src/SFML/System/Unix/Sleep.cpp @@ -25,19 +25,22 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// +#include #include -#include #include #include -namespace sf::priv +namespace sf { //////////////////////////////////////////////////////////// -void sleepImpl(Time time) +void sleep(Time duration) { - const std::int64_t usecs = time.asMicroseconds(); + if (duration == Time::Zero) + return; + + const std::int64_t usecs = duration.asMicroseconds(); // Construct the time to wait timespec ti{}; @@ -54,4 +57,4 @@ void sleepImpl(Time time) } } -} // namespace sf::priv +} // namespace sf diff --git a/src/SFML/System/Unix/SleepImpl.hpp b/src/SFML/System/Unix/SleepImpl.hpp deleted file mode 100644 index 6eac6923e..000000000 --- a/src/SFML/System/Unix/SleepImpl.hpp +++ /dev/null @@ -1,48 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#pragma once - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -namespace sf -{ -class Time; -} - -namespace sf::priv -{ -//////////////////////////////////////////////////////////// -/// \brief Unix implementation of sf::Sleep -/// -/// \param time Time to sleep -/// -//////////////////////////////////////////////////////////// -void sleepImpl(Time time); - -} // namespace sf::priv diff --git a/src/SFML/System/Win32/SleepImpl.cpp b/src/SFML/System/Win32/Sleep.cpp similarity index 84% rename from src/SFML/System/Win32/SleepImpl.cpp rename to src/SFML/System/Win32/Sleep.cpp index 625633d07..c3118dfc9 100644 --- a/src/SFML/System/Win32/SleepImpl.cpp +++ b/src/SFML/System/Win32/Sleep.cpp @@ -25,17 +25,23 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// +#include #include -#include #include #include -namespace sf::priv +namespace sf { //////////////////////////////////////////////////////////// -void sleepImpl(Time time) +void sleep(Time duration) { + // Note that 'std::this_thread::sleep_for' is intentionally not used here + // as it results in inconsistent sleeping times under MinGW-w64. + + if (duration == Time::Zero) + return; + // Get the minimum supported timer resolution on this system static const UINT periodMin = [] { @@ -48,10 +54,10 @@ void sleepImpl(Time time) timeBeginPeriod(periodMin); // Wait... - ::Sleep(static_cast(time.asMilliseconds())); + ::Sleep(static_cast(duration.asMilliseconds())); // Reset the timer resolution back to the system default timeEndPeriod(periodMin); } -} // namespace sf::priv +} // namespace sf diff --git a/src/SFML/System/Win32/SleepImpl.hpp b/src/SFML/System/Win32/SleepImpl.hpp deleted file mode 100644 index e10a01b0c..000000000 --- a/src/SFML/System/Win32/SleepImpl.hpp +++ /dev/null @@ -1,48 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#pragma once - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - -namespace sf -{ -class Time; -} - -namespace sf::priv -{ - -//////////////////////////////////////////////////////////// -/// \brief Windows implementation of sf::Sleep -/// -/// \param time Time to sleep -/// -//////////////////////////////////////////////////////////// -void sleepImpl(Time time); - -} // namespace sf::priv