From 9bdf2078181954e06d88d84a0dbe7bbd5652f4fc Mon Sep 17 00:00:00 2001 From: Shiv <91686009+HelloShiv@users.noreply.github.com> Date: Wed, 28 Dec 2022 05:28:49 +0530 Subject: [PATCH] Use `std::clamp` --- examples/island/Island.cpp | 2 +- examples/joystick/Joystick.cpp | 2 +- examples/vulkan/Vulkan.cpp | 26 +++++++++-------------- src/SFML/Window/OSX/SFOpenGLView+mouse.mm | 4 ++-- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/examples/island/Island.cpp b/examples/island/Island.cpp index ad21348a..2032c479 100644 --- a/examples/island/Island.cpp +++ b/examples/island/Island.cpp @@ -307,7 +307,7 @@ float getElevation(float x, float y) float distance = 2.0f * std::sqrt(x * x + y * y); elevation = (elevation + heightBase) * (1.0f - edgeFactor * std::pow(distance, edgeDropoffExponent)); - elevation = std::min(std::max(elevation, 0.0f), 1.0f); + elevation = std::clamp(elevation, 0.0f, 1.0f); return elevation; } diff --git a/examples/joystick/Joystick.cpp b/examples/joystick/Joystick.cpp index 6d313f08..479e41f5 100644 --- a/examples/joystick/Joystick.cpp +++ b/examples/joystick/Joystick.cpp @@ -210,7 +210,7 @@ int main() if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) newThreshold -= 0.1f; - newThreshold = std::min(std::max(newThreshold, 0.1f), 100.0f); + newThreshold = std::clamp(newThreshold, 0.1f, 100.0f); if (newThreshold != threshold) { diff --git a/examples/vulkan/Vulkan.cpp b/examples/vulkan/Vulkan.cpp index c58a6422..a23b45b4 100644 --- a/examples/vulkan/Vulkan.cpp +++ b/examples/vulkan/Vulkan.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -171,13 +172,6 @@ void matrixPerspective(Matrix& result, sf::Angle fov, float aspect, float nearPl result[3][3] = 0.f; } -// Clamp a value between low and high values -template -T clamp(T value, T low, T high) -{ - return (value <= low) ? low : ((value >= high) ? high : value); -} - // Helper function we pass to GLAD to load Vulkan functions via SFML GLADapiproc getVulkanFunction(const char* name) { @@ -871,14 +865,14 @@ public: return; } - swapchainExtent.width = clamp(window.getSize().x, - surfaceCapabilities.minImageExtent.width, - surfaceCapabilities.maxImageExtent.width); - swapchainExtent.height = clamp(window.getSize().y, - surfaceCapabilities.minImageExtent.height, - surfaceCapabilities.maxImageExtent.height); + swapchainExtent.width = std::clamp(window.getSize().x, + surfaceCapabilities.minImageExtent.width, + surfaceCapabilities.maxImageExtent.width); + swapchainExtent.height = std::clamp(window.getSize().y, + surfaceCapabilities.minImageExtent.height, + surfaceCapabilities.maxImageExtent.height); - auto imageCount = clamp(2, surfaceCapabilities.minImageCount, surfaceCapabilities.maxImageCount); + auto imageCount = std::clamp(2u, surfaceCapabilities.minImageCount, surfaceCapabilities.maxImageCount); VkSwapchainCreateInfoKHR swapchainCreateInfo = VkSwapchainCreateInfoKHR(); swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; @@ -2434,8 +2428,8 @@ public: // Translate the model based on the mouse position sf::Vector2f mousePosition = sf::Vector2f(sf::Mouse::getPosition(window)); sf::Vector2f windowSize = sf::Vector2f(window.getSize()); - float x = clamp(mousePosition.x * 2.f / windowSize.x - 1.f, -1.0f, 1.0f) * 2.0f; - float y = clamp(-mousePosition.y * 2.f / windowSize.y + 1.f, -1.0f, 1.0f) * 1.5f; + float x = std::clamp(mousePosition.x * 2.f / windowSize.x - 1.f, -1.0f, 1.0f) * 2.0f; + float y = std::clamp(-mousePosition.y * 2.f / windowSize.y + 1.f, -1.0f, 1.0f) * 1.5f; model[3][0] -= x; model[3][2] += y; diff --git a/src/SFML/Window/OSX/SFOpenGLView+mouse.mm b/src/SFML/Window/OSX/SFOpenGLView+mouse.mm index 9fc390da..93e8a01b 100644 --- a/src/SFML/Window/OSX/SFOpenGLView+mouse.mm +++ b/src/SFML/Window/OSX/SFOpenGLView+mouse.mm @@ -384,8 +384,8 @@ NSSize size = [self frame].size; NSPoint origin = [self frame].origin; NSPoint oldPos = rawPos; - rawPos.x = std::min(std::max(origin.x, rawPos.x), origin.x + size.width - 1); - rawPos.y = std::min(std::max(origin.y + 1, rawPos.y), origin.y + size.height); + rawPos.x = std::clamp(rawPos.x, origin.x, origin.x + size.width - 1); + rawPos.y = std::clamp(rawPos.y, origin.y + 1, origin.y + size.height); // Note: the `-1` and `+1` on the two lines above prevent the user to click // on the left or below the window, repectively, and therefore prevent the // application to lose focus by accident. The sign of this offset is determinded