diff --git a/src/client/creature.cpp b/src/client/creature.cpp index 6a015452..3bfc45b4 100644 --- a/src/client/creature.cpp +++ b/src/client/creature.cpp @@ -128,7 +128,7 @@ void Creature::internalDrawOutfit(Point dest, float scaleFactor, bool animateWal dest -= datType->getDisplacement() * scaleFactor; datType->draw(dest, scaleFactor, 0, xPattern, 0, 0, animationPhase, lightView); dest += getDisplacement() * scaleFactor; - zPattern = std::min(1, getNumPatternZ() - 1); + zPattern = std::min(1, getNumPatternZ() - 1); } PointF jumpOffset = m_jumpOffset * scaleFactor; @@ -183,7 +183,7 @@ void Creature::internalDrawOutfit(Point dest, float scaleFactor, bool animateWal } if(m_outfit.getCategory() == ThingCategoryEffect) - animationPhase = std::min(animationPhase+1, animationPhases); + animationPhase = std::min(animationPhase+1, animationPhases); type->draw(dest - (getDisplacement() * scaleFactor), scaleFactor, 0, 0, 0, 0, animationPhase, lightView); } @@ -480,7 +480,7 @@ void Creature::updateWalkAnimation(int totalPixelsWalked) if(!self->m_walking || self->m_walkTimer.ticksElapsed() >= self->getStepDuration(true)) self->m_walkAnimationPhase = 0; self->m_walkFinishAnimEvent = nullptr; - }, std::min(footDelay, 200)); + }, std::min(footDelay, 200)); } } @@ -553,7 +553,7 @@ void Creature::nextWalkUpdate() void Creature::updateWalk() { float walkTicksPerPixel = getStepDuration(true) / 32; - int totalPixelsWalked = std::min(m_walkTimer.ticksElapsed() / walkTicksPerPixel, 32.0f); + int totalPixelsWalked = std::min(m_walkTimer.ticksElapsed() / walkTicksPerPixel, 32.0f); // needed for paralyze effect m_walkedPixels = std::max(m_walkedPixels, totalPixelsWalked); 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..b31e8ad2 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_maxZoomIn, m_maxZoomOut); 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/graphics/texture.cpp b/src/framework/graphics/texture.cpp index d4721a89..59923094 100644 --- a/src/framework/graphics/texture.cpp +++ b/src/framework/graphics/texture.cpp @@ -170,7 +170,7 @@ bool Texture::setupSize(const Size& size, bool forcePowerOfTwo) glSize = size; // checks texture max size - if(std::max(glSize.width(), glSize.height()) > g_graphics.getMaxTextureSize()) { + if(std::max(glSize.width(), glSize.height()) > g_graphics.getMaxTextureSize()) { g_logger.error(stdext::format("loading texture with size %dx%d failed, " "the maximum size allowed by the graphics card is %dx%d," "to prevent crashes the texture will be displayed as a blank texture", diff --git a/src/framework/stdext/math.h b/src/framework/stdext/math.h index 01c5f681..f7c237d6 100644 --- a/src/framework/stdext/math.h +++ b/src/framework/stdext/math.h @@ -23,6 +23,7 @@ #ifndef STDEXT_MATH_H #define STDEXT_MATH_H +#include #include "types.h" namespace stdext { @@ -45,6 +46,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/uihorizontallayout.cpp b/src/framework/ui/uihorizontallayout.cpp index 197f20f3..e8596066 100644 --- a/src/framework/ui/uihorizontallayout.cpp +++ b/src/framework/ui/uihorizontallayout.cpp @@ -66,10 +66,10 @@ bool UIHorizontalLayout::internalUpdate() pos.y = paddingRect.top() + widget->getMarginTop(); } else if(widget->getTextAlign() & Fw::AlignBottom) { pos.y = paddingRect.bottom() - widget->getHeight() - widget->getMarginBottom(); - pos.y = std::max(pos.y, paddingRect.top()); + pos.y = std::max(pos.y, paddingRect.top()); } else { // center it pos.y = paddingRect.top() + (paddingRect.height() - (widget->getMarginTop() + widget->getHeight() + widget->getMarginBottom()))/2; - pos.y = std::max(pos.y, paddingRect.top()); + pos.y = std::max(pos.y, paddingRect.top()); } } else { // expand height diff --git a/src/framework/ui/uilayout.h b/src/framework/ui/uilayout.h index 2ff95cd6..3aa48722 100644 --- a/src/framework/ui/uilayout.h +++ b/src/framework/ui/uilayout.h @@ -40,7 +40,7 @@ public: virtual void addWidget(const UIWidgetPtr& widget) { } virtual void removeWidget(const UIWidgetPtr& widget) { } void disableUpdates() { m_updateDisabled++; } - void enableUpdates() { m_updateDisabled = std::max(m_updateDisabled-1,0); } + void enableUpdates() { m_updateDisabled = std::max(m_updateDisabled-1,0); } void setParent(UIWidgetPtr parentWidget) { m_parentWidget = parentWidget; } UIWidgetPtr getParentWidget() { return m_parentWidget; } diff --git a/src/framework/ui/uitextedit.cpp b/src/framework/ui/uitextedit.cpp index f2c8f588..999a4e5e 100644 --- a/src/framework/ui/uitextedit.cpp +++ b/src/framework/ui/uitextedit.cpp @@ -181,15 +181,15 @@ void UITextEdit::update(bool focusCursor) if(!virtualRect.contains(glyphRect.topLeft()) || !virtualRect.contains(glyphRect.bottomRight())) { // calculate where is the first glyph visible Point startGlyphPos; - startGlyphPos.y = std::max(glyphRect.bottom() - virtualRect.height(), 0); - startGlyphPos.x = std::max(glyphRect.right() - virtualRect.width(), 0); + startGlyphPos.y = std::max(glyphRect.bottom() - virtualRect.height(), 0); + startGlyphPos.x = std::max(glyphRect.right() - virtualRect.width(), 0); // find that glyph for(pos = 0; pos < textLength; ++pos) { glyph = (uchar)text[pos]; glyphRect = Rect(glyphsPositions[pos], glyphsSize[glyph]); - glyphRect.setTop(std::max(glyphRect.top() - m_font->getYOffset() - m_font->getGlyphSpacing().height(), 0)); - glyphRect.setLeft(std::max(glyphRect.left() - m_font->getGlyphSpacing().width(), 0)); + glyphRect.setTop(std::max(glyphRect.top() - m_font->getYOffset() - m_font->getGlyphSpacing().height(), 0)); + glyphRect.setLeft(std::max(glyphRect.left() - m_font->getGlyphSpacing().width(), 0)); // first glyph entirely visible found if(glyphRect.topLeft() >= startGlyphPos) { @@ -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/uiverticallayout.cpp b/src/framework/ui/uiverticallayout.cpp index 4b9c1077..91fd8ef1 100644 --- a/src/framework/ui/uiverticallayout.cpp +++ b/src/framework/ui/uiverticallayout.cpp @@ -68,10 +68,10 @@ bool UIVerticalLayout::internalUpdate() pos.x = paddingRect.left() + widget->getMarginLeft(); } else if(widget->getTextAlign() & Fw::AlignLeft) { pos.x = paddingRect.bottom() - widget->getHeight() - widget->getMarginBottom(); - pos.x = std::max(pos.x, paddingRect.left()); + pos.x = std::max(pos.x, paddingRect.left()); } else { pos.x = paddingRect.left() + (paddingRect.width() - (widget->getMarginLeft() + widget->getWidth() + widget->getMarginRight()))/2; - pos.x = std::max(pos.x, paddingRect.left()); + pos.x = std::max(pos.x, paddingRect.left()); } } else { // expand width diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index f5571f59..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 a00eff0c..259cf68b 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(); } diff --git a/src/framework/util/rect.h b/src/framework/util/rect.h index f052e9f4..e56e7c40 100644 --- a/src/framework/util/rect.h +++ b/src/framework/util/rect.h @@ -221,10 +221,10 @@ public: TRect united(const TRect &r) const { TRect tmp; - tmp.x1 = std::min(x1, r.x1); - tmp.x2 = std::max(x2, r.x2); - tmp.y1 = std::min(y1, r.y1); - tmp.y2 = std::max(y2, r.y2); + tmp.x1 = std::min(x1, r.x1); + tmp.x2 = std::max(x2, r.x2); + tmp.y1 = std::min(y1, r.y1); + tmp.y2 = std::max(y2, r.y2); return tmp; } @@ -263,10 +263,10 @@ public: b2 = r.y2; TRect tmp; - tmp.x1 = std::max(l1, l2); - tmp.x2 = std::min(r1, r2); - tmp.y1 = std::max(t1, t2); - tmp.y2 = std::min(b1, b2); + tmp.x1 = std::max(l1, l2); + tmp.x2 = std::min(r1, r2); + tmp.y1 = std::max(t1, t2); + tmp.y2 = std::min(b1, b2); return tmp; } diff --git a/src/framework/util/size.h b/src/framework/util/size.h index 287ac73f..f2ffb822 100644 --- a/src/framework/util/size.h +++ b/src/framework/util/size.h @@ -70,8 +70,8 @@ public: bool operator==(const TSize& other) const { return other.wd==wd && other.ht==ht; } bool operator!=(const TSize& other) const { return other.wd!=wd || other.ht!=ht; } - TSize expandedTo(const TSize& other) const { return TSize(std::max(wd,other.wd), std::max(ht,other.ht)); } - TSize boundedTo(const TSize& other) const { return TSize(std::min(wd,other.wd), std::min(ht,other.ht)); } + TSize expandedTo(const TSize& other) const { return TSize(std::max(wd, other.wd), std::max(ht, other.ht)); } + TSize boundedTo(const TSize& other) const { return TSize(std::min(wd, other.wd), std::min(ht, other.ht)); } void scale(const TSize& s, Fw::AspectRatioMode mode) { if(mode == Fw::IgnoreAspectRatio || wd == 0 || ht == 0) {