summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbtzy <bernard.14916@gmail.com>2019-01-12 16:28:43 +0800
committerCharles Pigott <charlespigott@googlemail.com>2019-01-20 19:43:56 +0000
commit9d75600ac0137907d5c5ec4f0ff475e2efa2f596 (patch)
tree049f914029ac9a58510a37bfd1a50e8b587056f2
parent5ad73e402908fb80a7d6b99f8f4146ac204764dd (diff)
downloadopenttd-9d75600ac0137907d5c5ec4f0ff475e2efa2f596.tar.xz
Fix: Round up deltas for smooth scrolling, so target will be reached
-rw-r--r--src/core/math_func.hpp17
-rw-r--r--src/viewport.cpp4
2 files changed, 19 insertions, 2 deletions
diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp
index df9142462..4a19033f4 100644
--- a/src/core/math_func.hpp
+++ b/src/core/math_func.hpp
@@ -346,6 +346,23 @@ static inline int RoundDivSU(int a, uint b)
}
}
+/**
+ * Computes (a / b) rounded away from zero.
+ * @param a Numerator
+ * @param b Denominator
+ * @return Quotient, rounded away from zero
+ */
+static inline int DivAwayFromZero(int a, uint b)
+{
+ const int _b = static_cast<int>(b);
+ if (a > 0) {
+ return (a + _b - 1) / _b;
+ } else {
+ /* Note: Behaviour of negative numerator division is truncation toward zero. */
+ return (a - _b + 1) / _b;
+ }
+}
+
uint32 IntSqrt(uint32 num);
#endif /* MATH_FUNC_HPP */
diff --git a/src/viewport.cpp b/src/viewport.cpp
index da4ed2660..5e72aaa23 100644
--- a/src/viewport.cpp
+++ b/src/viewport.cpp
@@ -1813,8 +1813,8 @@ void UpdateViewportPosition(Window *w)
if (_settings_client.gui.smooth_scroll) {
int max_scroll = ScaleByMapSize1D(512 * ZOOM_LVL_BASE);
/* Not at our desired position yet... */
- w->viewport->scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll);
- w->viewport->scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll);
+ w->viewport->scrollpos_x += Clamp(DivAwayFromZero(delta_x, 4), -max_scroll, max_scroll);
+ w->viewport->scrollpos_y += Clamp(DivAwayFromZero(delta_y, 4), -max_scroll, max_scroll);
} else {
w->viewport->scrollpos_x = w->viewport->dest_scrollpos_x;
w->viewport->scrollpos_y = w->viewport->dest_scrollpos_y;