]> git.localhorst.tv Git - l2e.git/commitdiff
fixed divide by zero bug in MapState
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 27 Dec 2012 16:01:29 +0000 (17:01 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 27 Dec 2012 16:01:29 +0000 (17:01 +0100)
and added more tests for the Fixed operators

src/map/MapState.cpp
src/math/Fixed.h
tests/math/FixedTest.cpp

index 51e99869f618588b2bb09b3b76dfdde1b9cbc687..baefdc5ef59c91e5095bc0999dcd14e95125549f 100644 (file)
@@ -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;
                        }
index a3982755dea86c8d147229fe0e19c33fb1caf2e4..7fe099cee57ea789f0cc631f9397c89da6165e94 100644 (file)
@@ -96,7 +96,8 @@ public:
                }
                return *this;
        }
-       Fixed &operator +=(int i) {
+       template<class Scalar>
+       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<class Scalar>
+       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<class Scalar>
+       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<class Scalar>
+       Fixed &operator /=(Scalar i) {
                Sint32 temp = SignedInt() / i;
                if (temp < 0) {
                        rep = (temp * -1) | SignMask();
index a81534b1d20c5c68aaae6b056efa376031bca83d..a86c40c98057cc4179595652dbfeb346a2c95322 100644 (file)
@@ -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() {