diff --git a/include/CSFML/System.h b/include/CSFML/System.h index 7050c01..6da5a17 100644 --- a/include/CSFML/System.h +++ b/include/CSFML/System.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include diff --git a/include/CSFML/System/TimeoutWithPredicate.h b/include/CSFML/System/TimeoutWithPredicate.h new file mode 100644 index 0000000..d555e38 --- /dev/null +++ b/include/CSFML/System/TimeoutWithPredicate.h @@ -0,0 +1,66 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2026 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 + +#include + + +//////////////////////////////////////////////////////////// +/// \brief Predicate that returns true to continue or false to time out +/// +/// \param userData User data stored in the sfTimeoutWithPredicate +/// +/// \return True to continue, false to time out +/// +//////////////////////////////////////////////////////////// +typedef bool (*sfTimeoutPredicate)(void* userData); + +//////////////////////////////////////////////////////////// +/// \brief Hybrid of a timeout and a continuation predicate +/// +/// Functions taking a timeout parameter that is specified +/// solely by a time cannot be easily interrupted. A predicate +/// allows interrupting the operation at any time, e.g. when +/// the user cancels it. +/// +/// If \a predicate is NULL, the operation times out after +/// \a timeout (0 meaning no timeout). If \a predicate is set, +/// \a timeout is ignored and the operation times out once the +/// predicate returns false. The predicate is checked every +/// \a period, a period of 0 checks it every millisecond. +/// +//////////////////////////////////////////////////////////// +typedef struct +{ + sfTime timeout; ///< Time to time out after, if no predicate is set + sfTimeoutPredicate predicate; ///< Predicate that returns true to continue or false to time out, can be NULL + void* userData; ///< User data passed to the predicate + sfTime period; ///< The period between checks of the predicate +} sfTimeoutWithPredicate; diff --git a/src/CSFML/System/CMakeLists.txt b/src/CSFML/System/CMakeLists.txt index 0178d6c..4954d33 100644 --- a/src/CSFML/System/CMakeLists.txt +++ b/src/CSFML/System/CMakeLists.txt @@ -20,6 +20,8 @@ set(SRC ${INCROOT}/Sleep.h ${SRCROOT}/Time.cpp ${INCROOT}/Time.h + ${SRCROOT}/ConvertTimeoutWithPredicate.hpp + ${INCROOT}/TimeoutWithPredicate.h ${INCROOT}/Types.h ${INCROOT}/Vector2.h ${INCROOT}/Vector3.h diff --git a/src/CSFML/System/ConvertTimeoutWithPredicate.hpp b/src/CSFML/System/ConvertTimeoutWithPredicate.hpp new file mode 100644 index 0000000..d0433e7 --- /dev/null +++ b/src/CSFML/System/ConvertTimeoutWithPredicate.hpp @@ -0,0 +1,46 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2026 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 + +#include + + +//////////////////////////////////////////////////////////// +// Convert sfTimeoutWithPredicate to sf::TimeoutWithPredicate +//////////////////////////////////////////////////////////// +[[nodiscard]] inline sf::TimeoutWithPredicate convertTimeoutWithPredicate(const sfTimeoutWithPredicate& timeout) +{ + if (!timeout.predicate) + return {sf::microseconds(timeout.timeout.microseconds)}; + + const auto period = timeout.period.microseconds > 0 ? sf::microseconds(timeout.period.microseconds) + : sf::milliseconds(1); + return {[predicate = timeout.predicate, userData = timeout.userData] { return predicate(userData); }, period}; +}