CGE2: Further fixes to FXP operators to fix pathfinding

This commit is contained in:
Paul Gilbert 2014-08-01 11:16:39 -04:00
parent 6d278157e1
commit 0d662566bd
2 changed files with 47 additions and 2 deletions

View File

@ -45,6 +45,51 @@ void V3D::sync(Common::Serializer &s) {
_z.sync(s);
}
FXP FXP::operator*(const FXP& x) const {
FXP y;
int32 t1 = (v >> 8) * x.v;
int32 t2 = (v & 0xFF) * x.v;
y.v = t1 + t2;
return y;
}
FXP FXP::operator/(const FXP& x) const {
FXP y;
if (x.v != 0) {
int32 v1 = this->v;
int32 v2 = x.v;
bool negFlag = false;
if (v1 < 0) {
v1 = -v1;
negFlag = true;
}
if (v2 < 0) {
v2 = -v2;
negFlag ^= true;
}
int32 v3 = v1 / v2;
v1 -= v3 * v2;
v3 <<= 8;
if (v1 < 0xFFFFFF) {
v1 <<= 8;
} else {
v2 >>= 8;
}
v3 += v1 / v2;
if (negFlag)
v3 = -v3;
y.v = v3;
}
return y;
}
void FXP::sync(Common::Serializer &s) {
s.syncAsSint32LE(v);
}

View File

@ -60,8 +60,8 @@ public:
FXP& operator=(const int& x) { v = x << 8; return *this; }
FXP operator+(const FXP& x) const { FXP y; y.v = v + x.v; return y; }
FXP operator-(const FXP& x) const { FXP y; y.v = v - x.v; return y; }
FXP operator*(const FXP& x) const { FXP y; y.v = v * x.v / 256; return y; }
FXP operator/(const FXP& x) const { FXP y; y.v = (x.v == 0) ? 0 : v * 256 / x.v; return y; }
FXP operator*(const FXP& x) const;
FXP operator/(const FXP& x) const;
//int& operator = (int& a, const FXP& b) { return a = b.i; }
friend int& operator+=(int& a, const FXP& b) { return a += b.trunc(); }