Use std::clamp
This commit is contained in:
parent
7884efc49e
commit
9bdf207818
@ -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;
|
||||
}
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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,
|
||||
swapchainExtent.width = std::clamp(window.getSize().x,
|
||||
surfaceCapabilities.minImageExtent.width,
|
||||
surfaceCapabilities.maxImageExtent.width);
|
||||
swapchainExtent.height = clamp<std::uint32_t>(window.getSize().y,
|
||||
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;
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user