Reintroduce 'SleepImpl' sleep implementation
This commit is contained in:
parent
da6a226941
commit
a880122836
@ -39,7 +39,10 @@ namespace sf
|
|||||||
/// \brief Make the current thread sleep for a given duration
|
/// \brief Make the current thread sleep for a given duration
|
||||||
///
|
///
|
||||||
/// sf::sleep is the best way to block a program or one of its
|
/// sf::sleep is the best way to block a program or one of its
|
||||||
/// threads, as it doesn't consume any CPU power.
|
/// threads, as it doesn't consume any CPU power. Compared to
|
||||||
|
/// the standard std::this_thread::sleep_for function, this
|
||||||
|
/// one provides more accurate sleeping time thanks to some
|
||||||
|
/// platform-specific tweaks.
|
||||||
///
|
///
|
||||||
/// \param duration Time to sleep
|
/// \param duration Time to sleep
|
||||||
///
|
///
|
||||||
|
@ -34,17 +34,31 @@ set(SRC
|
|||||||
source_group("" FILES ${SRC})
|
source_group("" FILES ${SRC})
|
||||||
|
|
||||||
# add platform specific sources
|
# add platform specific sources
|
||||||
if(SFML_OS_ANDROID)
|
if(SFML_OS_WINDOWS)
|
||||||
set(PLATFORM_SRC ${PLATFORM_SRC}
|
set(PLATFORM_SRC
|
||||||
${SRCROOT}/Android/Activity.hpp
|
${SRCROOT}/Win32/SleepImpl.cpp
|
||||||
${SRCROOT}/Android/Activity.cpp
|
${SRCROOT}/Win32/SleepImpl.hpp
|
||||||
${SRCROOT}/Android/NativeActivity.cpp
|
|
||||||
${SRCROOT}/Android/ResourceStream.cpp
|
|
||||||
${SRCROOT}/Android/ResourceStream.cpp
|
|
||||||
${SRCROOT}/Android/SuspendAwareClock.cpp
|
|
||||||
)
|
)
|
||||||
|
source_group("windows" FILES ${PLATFORM_SRC})
|
||||||
|
else()
|
||||||
|
set(PLATFORM_SRC
|
||||||
|
${SRCROOT}/Unix/SleepImpl.cpp
|
||||||
|
${SRCROOT}/Unix/SleepImpl.hpp
|
||||||
|
)
|
||||||
|
|
||||||
|
if(SFML_OS_ANDROID)
|
||||||
|
set(PLATFORM_SRC ${PLATFORM_SRC}
|
||||||
|
${SRCROOT}/Android/Activity.hpp
|
||||||
|
${SRCROOT}/Android/Activity.cpp
|
||||||
|
${SRCROOT}/Android/NativeActivity.cpp
|
||||||
|
${SRCROOT}/Android/ResourceStream.cpp
|
||||||
|
${SRCROOT}/Android/ResourceStream.cpp
|
||||||
|
${SRCROOT}/Android/SuspendAwareClock.cpp
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
source_group("unix" FILES ${PLATFORM_SRC})
|
||||||
endif()
|
endif()
|
||||||
source_group("unix" FILES ${PLATFORM_SRC})
|
|
||||||
|
|
||||||
# define the sfml-system target
|
# define the sfml-system target
|
||||||
sfml_add_library(System
|
sfml_add_library(System
|
||||||
|
@ -26,8 +26,12 @@
|
|||||||
// Headers
|
// Headers
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
#include <SFML/System/Sleep.hpp>
|
#include <SFML/System/Sleep.hpp>
|
||||||
#include <chrono>
|
|
||||||
#include <thread>
|
#if defined(SFML_SYSTEM_WINDOWS)
|
||||||
|
#include <SFML/System/Win32/SleepImpl.hpp>
|
||||||
|
#else
|
||||||
|
#include <SFML/System/Unix/SleepImpl.hpp>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
namespace sf
|
namespace sf
|
||||||
@ -35,8 +39,11 @@ namespace sf
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void sleep(Time duration)
|
void sleep(Time duration)
|
||||||
{
|
{
|
||||||
const auto time = std::chrono::duration<Int64, std::micro>(duration.asMicroseconds());
|
// Note that 'std::this_thread::sleep_for' is intentionally not used here
|
||||||
std::this_thread::sleep_for(time);
|
// as it results in inconsistent sleeping times under MinGW-w64.
|
||||||
|
|
||||||
|
if (duration >= Time::Zero)
|
||||||
|
priv::sleepImpl(duration);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sf
|
} // namespace sf
|
||||||
|
55
src/SFML/System/Unix/SleepImpl.cpp
Normal file
55
src/SFML/System/Unix/SleepImpl.cpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SFML - Simple and Fast Multimedia Library
|
||||||
|
// Copyright (C) 2007-2022 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 <SFML/System/Unix/SleepImpl.hpp>
|
||||||
|
#include <cerrno>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
|
||||||
|
namespace sf::priv
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
void sleepImpl(Time time)
|
||||||
|
{
|
||||||
|
const Int64 usecs = time.asMicroseconds();
|
||||||
|
|
||||||
|
// Construct the time to wait
|
||||||
|
timespec ti;
|
||||||
|
ti.tv_sec = static_cast<time_t>(usecs / 1000000);
|
||||||
|
ti.tv_nsec = static_cast<long>((usecs % 1000000) * 1000);
|
||||||
|
|
||||||
|
// Wait...
|
||||||
|
// If nanosleep returns -1, we check errno. If it is EINTR
|
||||||
|
// nanosleep was interrupted and has set ti to the remaining
|
||||||
|
// duration. We continue sleeping until the complete duration
|
||||||
|
// has passed. We stop sleeping if it was due to an error.
|
||||||
|
while ((nanosleep(&ti, &ti) == -1) && (errno == EINTR))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace sf::priv
|
48
src/SFML/System/Unix/SleepImpl.hpp
Normal file
48
src/SFML/System/Unix/SleepImpl.hpp
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SFML - Simple and Fast Multimedia Library
|
||||||
|
// Copyright (C) 2007-2022 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.
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifndef SFML_SLEEPIMPLUNIX_HPP
|
||||||
|
#define SFML_SLEEPIMPLUNIX_HPP
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
// Headers
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
#include <SFML/Config.hpp>
|
||||||
|
#include <SFML/System/Time.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
namespace sf::priv
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
/// \brief Unix implementation of sf::Sleep
|
||||||
|
///
|
||||||
|
/// \param time Time to sleep
|
||||||
|
///
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
void sleepImpl(Time time);
|
||||||
|
|
||||||
|
} // namespace sf::priv
|
||||||
|
|
||||||
|
|
||||||
|
#endif // SFML_SLEEPIMPLUNIX_HPP
|
55
src/SFML/System/Win32/SleepImpl.cpp
Normal file
55
src/SFML/System/Win32/SleepImpl.cpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SFML - Simple and Fast Multimedia Library
|
||||||
|
// Copyright (C) 2007-2022 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 <SFML/System/Win32/SleepImpl.hpp>
|
||||||
|
#include <SFML/System/Win32/WindowsHeader.hpp>
|
||||||
|
#include <mmsystem.h>
|
||||||
|
|
||||||
|
namespace sf::priv
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
void sleepImpl(Time time)
|
||||||
|
{
|
||||||
|
// Get the minimum supported timer resolution on this system
|
||||||
|
static const UINT periodMin = []
|
||||||
|
{
|
||||||
|
TIMECAPS tc;
|
||||||
|
timeGetDevCaps(&tc, sizeof(TIMECAPS));
|
||||||
|
return tc.wPeriodMin;
|
||||||
|
}();
|
||||||
|
|
||||||
|
// Set the timer resolution to the minimum for the Sleep call
|
||||||
|
timeBeginPeriod(periodMin);
|
||||||
|
|
||||||
|
// Wait...
|
||||||
|
::Sleep(static_cast<DWORD>(time.asMilliseconds()));
|
||||||
|
|
||||||
|
// Reset the timer resolution back to the system default
|
||||||
|
timeEndPeriod(periodMin);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace sf::priv
|
48
src/SFML/System/Win32/SleepImpl.hpp
Normal file
48
src/SFML/System/Win32/SleepImpl.hpp
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// SFML - Simple and Fast Multimedia Library
|
||||||
|
// Copyright (C) 2007-2022 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.
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifndef SFML_SLEEPIMPLWIN32_HPP
|
||||||
|
#define SFML_SLEEPIMPLWIN32_HPP
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
// Headers
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
#include <SFML/Config.hpp>
|
||||||
|
#include <SFML/System/Time.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
namespace sf::priv
|
||||||
|
{
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
/// \brief Windows implementation of sf::Sleep
|
||||||
|
///
|
||||||
|
/// \param time Time to sleep
|
||||||
|
///
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
void sleepImpl(Time time);
|
||||||
|
|
||||||
|
} // namespace sf::priv
|
||||||
|
|
||||||
|
|
||||||
|
#endif // SFML_SLEEPIMPLWIN32_HPP
|
Loading…
Reference in New Issue
Block a user