summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2011-02-10 20:54:46 +0000
committerrubidium <rubidium@openttd.org>2011-02-10 20:54:46 +0000
commit66a8c324dbff3b387ae8167147a86e199a4e3661 (patch)
tree9f811e677b16b60265a039ac590e14d693fd2f19
parent24983ec62947ad374f7b7453ab2f0d479b3e55a5 (diff)
downloadopenttd-66a8c324dbff3b387ae8167147a86e199a4e3661.tar.xz
(svn r22054) -Codechange: support for rounding the converted units to their closest integral value instead of flooring
-rw-r--r--src/strings.cpp17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/strings.cpp b/src/strings.cpp
index f9f562f49..aef219881 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -551,21 +551,23 @@ struct UnitConversion {
/**
* Convert value from OpenTTD's internal unit into the displayed value.
* @param input The input to convert.
+ * @param round Whether to round the value or not.
* @return The converted value.
*/
- int64 ToDisplay(int64 input) const
+ int64 ToDisplay(int64 input, bool round = true) const
{
- return (input * this->multiplier) >> this->shift;
+ return ((input * this->multiplier) + (round && this->shift != 0 ? 1 << (this->shift - 1) : 0)) >> this->shift;
}
/**
* Convert the displayed value back into a value of OpenTTD's internal unit.
* @param input The input to convert.
+ * @param round Whether to round the value or not.
* @return The converted value.
*/
- int64 FromDisplay(int64 input) const
+ int64 FromDisplay(int64 input, bool round = true) const
{
- return ((input << this->shift) + this->multiplier / 2) / this->multiplier;
+ return ((input << this->shift) + (round ? this->multiplier / 2 : 0)) / this->multiplier;
}
};
@@ -594,7 +596,7 @@ static const Units _units[] = {
{ 1, 0}, STR_UNITS_WEIGHT_SHORT_METRIC, STR_UNITS_WEIGHT_LONG_METRIC,
{1000, 0}, STR_UNITS_VOLUME_SHORT_METRIC, STR_UNITS_VOLUME_LONG_METRIC,
{ 1, 0}, STR_UNITS_FORCE_SI,
- { 3, 0}, STR_UNITS_HEIGHT_IMPERIAL,
+ { 3, 0}, STR_UNITS_HEIGHT_IMPERIAL, // "Wrong" conversion factor for more nicer GUI values
},
{ // Metric (km/h, hp, metric ton, litre, kN, metre)
{ 103, 6}, STR_UNITS_VELOCITY_METRIC,
@@ -621,7 +623,10 @@ static const Units _units[] = {
*/
uint ConvertSpeedToDisplaySpeed(uint speed)
{
- return _units[_settings_game.locale.units].c_velocity.ToDisplay(speed);
+ /* For historical reasons we don't want to mess with the
+ * conversion for speed. So, don't round it and keep the
+ * original conversion factors instead of the real ones. */
+ return _units[_settings_game.locale.units].c_velocity.ToDisplay(speed, false);
}
/**