mirror of https://github.com/OpenTTD/OpenTTD
(svn r19671) -Fix (r19670): RoundDiv() needs to deal with signed numerators.
parent
2e90f7f8b9
commit
af7051178d
|
@ -330,14 +330,20 @@ static FORCEINLINE uint CeilDiv(uint a, uint b)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes round(a / b) for non-negative a and b.
|
* Computes round(a / b) for signed a and unsigned b.
|
||||||
* @param a Numerator
|
* @param a Numerator
|
||||||
* @param b Denominator
|
* @param b Denominator
|
||||||
* @return Quotient, rounded to nearest
|
* @return Quotient, rounded to nearest
|
||||||
*/
|
*/
|
||||||
static FORCEINLINE uint RoundDiv(uint a, uint b)
|
static FORCEINLINE int RoundDivSU(int a, uint b)
|
||||||
{
|
{
|
||||||
return (a + b / 2) / b;
|
if (a > 0) {
|
||||||
|
/* 0.5 is rounded to 1 */
|
||||||
|
return (a + (int)b / 2) / (int)b;
|
||||||
|
} else {
|
||||||
|
/* -0.5 is rounded to 0 */
|
||||||
|
return (a - ((int)b - 1) / 2) / (int)b;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MATH_FUNC_HPP */
|
#endif /* MATH_FUNC_HPP */
|
||||||
|
|
|
@ -550,7 +550,7 @@ static int DrawString(int left, int right, int top, char *str, const char *last,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SA_CENTER:
|
case SA_CENTER:
|
||||||
left = RoundDiv(initial_right + 1 + initial_left - w, 2);
|
left = RoundDivSU(initial_right + 1 + initial_left - w, 2);
|
||||||
/* right + 1 = left + w */
|
/* right + 1 = left + w */
|
||||||
right = left + w - 1;
|
right = left + w - 1;
|
||||||
break;
|
break;
|
||||||
|
@ -823,7 +823,7 @@ int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str,
|
||||||
total_height = (num + 1) * mt;
|
total_height = (num + 1) * mt;
|
||||||
}
|
}
|
||||||
|
|
||||||
int y = (align == SA_CENTER) ? RoundDiv(bottom + top - total_height, 2) : top;
|
int y = (align == SA_CENTER) ? RoundDivSU(bottom + top - total_height, 2) : top;
|
||||||
const char *src = buffer;
|
const char *src = buffer;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
|
@ -649,8 +649,8 @@ struct ScenarioEditorLandscapeGenerationWindow : Window {
|
||||||
{
|
{
|
||||||
if (widget != ETTW_DOTS) return;
|
if (widget != ETTW_DOTS) return;
|
||||||
|
|
||||||
int center_x = RoundDiv(r.left + r.right, 2);
|
int center_x = RoundDivSU(r.left + r.right, 2);
|
||||||
int center_y = RoundDiv(r.top + r.bottom, 2);
|
int center_y = RoundDivSU(r.top + r.bottom, 2);
|
||||||
|
|
||||||
int n = _terraform_size * _terraform_size;
|
int n = _terraform_size * _terraform_size;
|
||||||
const int8 *coords = &_multi_terraform_coords[0][0];
|
const int8 *coords = &_multi_terraform_coords[0][0];
|
||||||
|
|
Loading…
Reference in New Issue