diff --git a/src/client/lightview.cpp b/src/client/lightview.cpp index b347e8c8..dccac0e3 100644 --- a/src/client/lightview.cpp +++ b/src/client/lightview.cpp @@ -50,7 +50,7 @@ TexturePtr LightView::generateLightBubble(float centerFactor) for(int x = 0; x < bubbleDiameter; x++) { for(int y = 0; y < bubbleDiameter; y++) { float radius = std::sqrt((bubbleRadius - x)*(bubbleRadius - x) + (bubbleRadius - y)*(bubbleRadius - y)); - float intensity = std::max(std::min((bubbleRadius-radius)/(float)(bubbleRadius-centerRadius), 1.0f), 0.0f); + float intensity = stdext::clamp((bubbleRadius - radius) / (float)(bubbleRadius - centerRadius), 0.0f, 1.0f); // light intensity varies inversely with the square of the distance intensity = intensity * intensity; diff --git a/src/client/mapview.cpp b/src/client/mapview.cpp index 36246a0b..39bc80a7 100644 --- a/src/client/mapview.cpp +++ b/src/client/mapview.cpp @@ -644,7 +644,7 @@ int MapView::calcFirstVisibleFloor() } // just ensure the that the floor is in the valid range - z = std::min(std::max(z, 0), (int)Otc::MAX_Z); + z = stdext::clamp(z, 0, (int)Otc::MAX_Z); return z; } @@ -669,7 +669,7 @@ int MapView::calcLastVisibleFloor() z = std::max(m_lockedFirstVisibleFloor, z); // just ensure the that the floor is in the valid range - z = std::min(std::max(z, 0), (int)Otc::MAX_Z); + z = stdext::clamp(z, 0, (int)Otc::MAX_Z); return z; } diff --git a/src/client/uimap.cpp b/src/client/uimap.cpp index a961e4af..e7086111 100644 --- a/src/client/uimap.cpp +++ b/src/client/uimap.cpp @@ -77,7 +77,7 @@ void UIMap::movePixels(int x, int y) bool UIMap::setZoom(int zoom) { - m_zoom = std::min(std::max(zoom, m_maxZoomIn), m_maxZoomOut); + m_zoom = stdext::clamp(zoom, m_maxZoomOut, m_maxZoomIn); updateVisibleDimension(); return false; } diff --git a/src/client/uiprogressrect.cpp b/src/client/uiprogressrect.cpp index 1187f1ca..f1abfd6c 100644 --- a/src/client/uiprogressrect.cpp +++ b/src/client/uiprogressrect.cpp @@ -84,7 +84,7 @@ void UIProgressRect::drawSelf(Fw::DrawPane drawPane) void UIProgressRect::setPercent(float percent) { - m_percent = std::max(std::min((double)percent, 100.0), 0.0); + m_percent = stdext::clamp((double)percent, 0.0, 100.0); } void UIProgressRect::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode) diff --git a/src/framework/core/adaptativeframecounter.cpp b/src/framework/core/adaptativeframecounter.cpp index ca1f40c6..bab8e09c 100644 --- a/src/framework/core/adaptativeframecounter.cpp +++ b/src/framework/core/adaptativeframecounter.cpp @@ -89,7 +89,7 @@ bool AdaptativeFrameCounter::update() void AdaptativeFrameCounter::setMaxFps(int maxFps) { - maxFps = std::max(std::min(maxFps, 1000), 0); + maxFps = stdext::clamp(maxFps, 0, 1000); if(maxFps != 0) { m_bestFrameDelay = 1000000 / maxFps; diff --git a/src/framework/stdext/math.h b/src/framework/stdext/math.h index 01c5f681..6de3a04f 100644 --- a/src/framework/stdext/math.h +++ b/src/framework/stdext/math.h @@ -45,6 +45,9 @@ float random_range(float min, float max); double round(double r); +template +T clamp(T x, T min, T max) { return std::max(min, std::min(x, max)); } + } #endif diff --git a/src/framework/ui/uitextedit.cpp b/src/framework/ui/uitextedit.cpp index 2f3f1ebb..999a4e5e 100644 --- a/src/framework/ui/uitextedit.cpp +++ b/src/framework/ui/uitextedit.cpp @@ -358,8 +358,8 @@ void UITextEdit::setSelection(int start, int end) if(end == -1) end = m_text.length(); - m_selectionStart = std::min(std::max(start, 0), (int)m_text.length()); - m_selectionEnd = std::min(std::max(end, 0), (int)m_text.length()); + m_selectionStart = stdext::clamp(start, 0, (int)m_text.length()); + m_selectionEnd = stdext::clamp(end, 0, (int)m_text.length()); } void UITextEdit::setTextHidden(bool hidden) diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index 94ce52df..4a6b84b0 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -192,7 +192,7 @@ void UIWidget::insertChild(int index, const UIWidgetPtr& child) if(!(index >= 0 && (uint)index <= m_children.size())) { //g_logger.traceWarning("attempt to insert a child UIWidget into an invalid index, using nearest index..."); - index = std::min(std::max(index, 0), (int)m_children.size()); + index = stdext::clamp(index, 0, (int)m_children.size()); } // retrieve child by index diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h index 2cb495ef..42e1e059 100644 --- a/src/framework/ui/uiwidget.h +++ b/src/framework/ui/uiwidget.h @@ -346,7 +346,7 @@ public: void setPaddingRight(int padding) { m_padding.right = padding; updateLayout(); } void setPaddingBottom(int padding) { m_padding.bottom = padding; updateLayout(); } void setPaddingLeft(int padding) { m_padding.left = padding; updateLayout(); } - void setOpacity(float opacity) { m_opacity = std::min(std::max(opacity, 0.0f), 1.0f); } + void setOpacity(float opacity) { m_opacity = stdext::clamp(opacity, 0.0f, 1.0f); } void setRotation(float degrees) { m_rotation = degrees; } int getX() { return m_rect.x(); }