Simplify implementations of sf::sleep

This commit is contained in:
Chris Thrasher 2024-11-09 10:02:28 -07:00
parent 58085efb29
commit e3de52a0c4
No known key found for this signature in database
GPG Key ID: 56FB686C9DFC8E2C
7 changed files with 22 additions and 162 deletions

View File

@ -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

View File

@ -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)

View File

@ -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 <SFML/System/Sleep.hpp>
#include <SFML/System/Time.hpp>
#if defined(SFML_SYSTEM_WINDOWS)
#include <SFML/System/Win32/SleepImpl.hpp>
#else
#include <SFML/System/Unix/SleepImpl.hpp>
#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

View File

@ -25,19 +25,22 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Sleep.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/System/Unix/SleepImpl.hpp>
#include <cerrno>
#include <ctime>
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

View File

@ -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 <SFML/Config.hpp>
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

View File

@ -25,17 +25,23 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Sleep.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/System/Win32/SleepImpl.hpp>
#include <SFML/System/Win32/WindowsHeader.hpp>
#include <mmsystem.h>
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<DWORD>(time.asMilliseconds()));
::Sleep(static_cast<DWORD>(duration.asMilliseconds()));
// Reset the timer resolution back to the system default
timeEndPeriod(periodMin);
}
} // namespace sf::priv
} // namespace sf

View File

@ -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 <SFML/Config.hpp>
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