From: Daniel Karbach Date: Thu, 27 Dec 2012 16:01:29 +0000 (+0100) Subject: fixed divide by zero bug in MapState X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=c49d46cbfbd7720627af2414e94963850fe8eed6;p=l2e.git fixed divide by zero bug in MapState and added more tests for the Fixed operators --- diff --git a/src/map/MapState.cpp b/src/map/MapState.cpp index 51e9986..baefdc5 100644 --- a/src/map/MapState.cpp +++ b/src/map/MapState.cpp @@ -139,7 +139,9 @@ void MapState::OnTileLock() { StopFollowers(*controlled); if (!moveTimer.Running()) { int tileSize((controlled->GetOrientation() % 2) ? map->Tileset()->Width() : map->Tileset()->Height()); - moveTimer = PhysicsTimers().StartInterval(tileSize/walkingSpeed.Int()); + Fixed<8> walkingInterval(tileSize); + walkingInterval /= walkingSpeed; + moveTimer = PhysicsTimers().StartInterval(walkingInterval.Int()); } pushed = 0; } diff --git a/src/math/Fixed.h b/src/math/Fixed.h index a398275..7fe099c 100644 --- a/src/math/Fixed.h +++ b/src/math/Fixed.h @@ -96,7 +96,8 @@ public: } return *this; } - Fixed &operator +=(int i) { + template + Fixed &operator +=(Scalar i) { Sint32 temp = SignedInt() + (i << IntShift()); if (temp < 0) { rep = (temp * -1) | SignMask(); @@ -114,7 +115,8 @@ public: } return *this; } - Fixed &operator -=(int i) { + template + Fixed &operator -=(Scalar i) { Sint32 temp = SignedInt() - (i << IntShift()); if (temp < 0) { rep = (temp * -1) | SignMask(); @@ -134,7 +136,8 @@ public: } return *this; } - Fixed &operator *=(int i) { + template + Fixed &operator *=(Scalar i) { Sint32 temp = SignedInt() * i; if (temp < 0) { rep = (temp * -1) | SignMask(); @@ -154,7 +157,8 @@ public: } return *this; } - Fixed &operator /=(int i) { + template + Fixed &operator /=(Scalar i) { Sint32 temp = SignedInt() / i; if (temp < 0) { rep = (temp * -1) | SignMask(); diff --git a/tests/math/FixedTest.cpp b/tests/math/FixedTest.cpp index a81534b..a86c40c 100644 --- a/tests/math/FixedTest.cpp +++ b/tests/math/FixedTest.cpp @@ -88,15 +88,27 @@ void FixedTest::testProduct() { CPPUNIT_ASSERT_EQUAL( Fixed(5), Fixed(2) * Fixed(2.5)); + CPPUNIT_ASSERT_EQUAL( + Fixed(8), + Fixed(2) * 4); CPPUNIT_ASSERT_EQUAL( Fixed(-1.7499999999), Fixed(3, 4) * Fixed(-7, 3)); + CPPUNIT_ASSERT_EQUAL( + Fixed(-1.7499999999), + Fixed(3, 4) * Fixed(-2.33333333)); CPPUNIT_ASSERT_EQUAL( Fixed(4), Fixed(2) / Fixed(0.5)); + CPPUNIT_ASSERT_EQUAL( + Fixed(4), + Fixed(2) / 0.5); CPPUNIT_ASSERT_EQUAL( Fixed(3), Fixed(15) / Fixed(5)); + CPPUNIT_ASSERT_EQUAL( + Fixed(3), + Fixed(15) / 5); } void FixedTest::testModulo() {