Use std::clamp

This commit is contained in:
Shiv 2022-12-28 05:28:49 +05:30 committed by GitHub
parent 7884efc49e
commit 9bdf207818
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 20 deletions

View File

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

View File

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

View File

@ -9,6 +9,7 @@
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
@ -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 <typename T>
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<std::uint32_t>(window.getSize().x,
surfaceCapabilities.minImageExtent.width,
surfaceCapabilities.maxImageExtent.width);
swapchainExtent.height = clamp<std::uint32_t>(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<std::uint32_t>(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;

View File

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