Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _UTILS_FX16_8_HPP
- #define _UTILS_FX16_8_HPP
- #include <stdexcept>
- #include <SDL2/SDL.h>
- struct fx16_8; //forward declaration
- extern const fx16_8 fx_epsilon; // = 1/256
- extern const fx16_8 fx_1third; // = 1/3
- extern const fx16_8 fx_2thirds; // = 2/3
- extern const fx16_8 fx_half; // = 1/2
- extern const fx16_8 fx_sqrt2; // = sqrt(2)
- extern const fx16_8 fx_invpi; // = 1/pi
- extern const fx16_8 fx_pi4; // = pi/4
- extern const fx16_8 fx_pi2; // = pi/2
- extern const fx16_8 fx_pi; // = pi
- extern const fx16_8 fx_2pi; // = pi*2
- extern const fx16_8 fx_pi_180; // = pi/180
- extern const fx16_8 fx_180_pi; // = 180/pi
- extern const fx16_8 fx_invpi_180; // = 1/(pi/180)
- extern const fx16_8 fx_inv180_pi; // = 1/(180/pi)
- extern const Sint16 _fx16_8_sin_table[1608]; //lookup table for sin, cos, tan
- #define _FX_F32(_n) ( (int)(((_n)*256.0f)+0.5f) )
- #define _FX_F64(_n) ( (int)(((_n)*256.0)+0.5) )
- struct fx16_8 { //32-bit fixed point; 16-bits of integer, 8-bits of fraction
- Sint32 v = 0; //(highest 8-bits used for headroom between operations)
- int i32() const { return v>>8; }
- float f32() const { return (float)v/256; }
- double f64() const { return (double)v/256; }
- fx16_8(){}
- fx16_8(const int& n){ v = n<<8; }
- fx16_8(const float& n){ v = _FX_F32(n); }
- fx16_8(const double& n){ v = _FX_F64(n); }
- fx16_8(const fx16_8& n){ v = n.v; }
- fx16_8& operator++(){ v += 256; return *this; }
- fx16_8& operator--(){ v -= 256; return *this; }
- fx16_8& operator=(const int& n){ v = n<<8; return *this; }
- fx16_8& operator=(const float& n){ v = _FX_F32(n); return *this; }
- fx16_8& operator=(const double& n){ v = _FX_F64(n); return *this; }
- fx16_8& operator=(const fx16_8& n){ v = n.v; return *this; }
- fx16_8& operator+=(const int& n){ v += n<<8; return *this; }
- fx16_8& operator+=(const float& n){ v += _FX_F32(n); return *this; }
- fx16_8& operator+=(const double& n){ v += _FX_F64(n); return *this; }
- fx16_8& operator+=(const fx16_8& n){ v += n.v; return *this; }
- fx16_8& operator-=(const int& n){ v -= n<<8; return *this; }
- fx16_8& operator-=(const float& n){ v -= _FX_F32(n); return *this; }
- fx16_8& operator-=(const double& n){ v -= _FX_F64(n); return *this; }
- fx16_8& operator-=(const fx16_8& n){ v -= n.v; return *this; }
- fx16_8& operator*=(const int& n){ v = (v*(n<<8))>>8; return *this; }
- fx16_8& operator*=(const float& n){ v = (v*_FX_F32(n))>>8; return *this; }
- fx16_8& operator*=(const double& n){ v = (v*_FX_F64(n))>>8; return *this; }
- fx16_8& operator*=(const fx16_8& n){ v = (v*n.v)>>8; return *this; }
- fx16_8& operator/=(const int& n){ v = (v<<8)/(n<<8); return *this; }
- fx16_8& operator/=(const float& n){ v = (v<<8)/_FX_F32(n); return *this; }
- fx16_8& operator/=(const double& n){ v = (v<<8)/_FX_F64(n); return *this; }
- fx16_8& operator/=(const fx16_8& n){ v = (v<<8)/n.v; return *this; }
- fx16_8& operator%=(const int& n){ v = (v<<8)%(n<<8); return *this; }
- fx16_8& operator%=(const float& n){ v = (v<<8)%_FX_F32(n); return *this; }
- fx16_8& operator%=(const double& n){ v = (v<<8)%_FX_F64(n); return *this; }
- fx16_8& operator%=(const fx16_8& n){ v = (v<<8)%n.v; return *this; }
- fx16_8& operator<<=(const int& n){ v <<= n; return *this; }
- fx16_8& operator>>=(const int& n){ v >>= n; return *this; }
- fx16_8 operator+(const int& n) const { fx16_8 o; o.v = v+(n<<8); return o; }
- fx16_8 operator+(const float& n) const { fx16_8 o; o.v = v+_FX_F32(n); return o; }
- fx16_8 operator+(const double& n) const { fx16_8 o; o.v = v+_FX_F64(n); return o; }
- fx16_8 operator+(const fx16_8& n) const { fx16_8 o; o.v = v+n.v; return o; }
- fx16_8 operator-(const int& n) const { fx16_8 o; o.v = v-(n<<8); return o; }
- fx16_8 operator-(const float& n) const { fx16_8 o; o.v = v-_FX_F32(n); return o; }
- fx16_8 operator-(const double& n) const { fx16_8 o; o.v = v-_FX_F64(n); return o; }
- fx16_8 operator-(const fx16_8& n) const { fx16_8 o; o.v = v-n.v; return o; }
- fx16_8 operator*(const int& n) const { fx16_8 o; o.v = (v*(n<<8))>>8; return o; }
- fx16_8 operator*(const float& n) const { fx16_8 o; o.v = (v*_FX_F32(n))>>8; return o; }
- fx16_8 operator*(const double& n) const { fx16_8 o; o.v = (v*_FX_F64(n))>>8; return o; }
- fx16_8 operator*(const fx16_8& n) const { fx16_8 o; o.v = (v*n.v)>>8; return o; }
- fx16_8 operator/(const int& n) const { fx16_8 o; o.v = (v<<8)/(n<<8); return o; }
- fx16_8 operator/(const float& n) const { fx16_8 o; o.v = (v<<8)/_FX_F32(n); return o; }
- fx16_8 operator/(const double& n) const { fx16_8 o; o.v = (v<<8)/_FX_F64(n); return o; }
- fx16_8 operator/(const fx16_8& n) const { fx16_8 o; o.v = (v<<8)/n.v; return o; }
- fx16_8 operator%(const int& n) const { fx16_8 o; o.v = (v<<8)%(n<<8); return o; }
- fx16_8 operator%(const float& n) const { fx16_8 o; o.v = (v<<8)%_FX_F32(n); return o; }
- fx16_8 operator%(const double& n) const { fx16_8 o; o.v = (v<<8)%_FX_F64(n); return o; }
- fx16_8 operator%(const fx16_8& n) const { fx16_8 o; o.v = (v<<8)%n.v; return o; }
- fx16_8 operator<<(const int& n) const { fx16_8 o; o.v = v<<n; return o; }
- fx16_8 operator>>(const int& n) const { fx16_8 o; o.v = v>>n; return o; }
- fx16_8 operator-() const { fx16_8 o; o.v = -v; return o; }
- bool operator==(const int& n) const { return v == n<<8; }
- bool operator==(const float& n) const { return v == _FX_F32(n); }
- bool operator==(const double& n) const { return v == _FX_F64(n); }
- bool operator==(const fx16_8& n) const { return v == n.v; }
- bool operator!=(const int& n) const { return v != n<<8; }
- bool operator!=(const float& n) const { return v != _FX_F32(n); }
- bool operator!=(const double& n) const { return v != _FX_F64(n); }
- bool operator!=(const fx16_8& n) const { return v != n.v; }
- bool operator<(const int& n) const { return v < n<<8; }
- bool operator<(const float& n) const { return v < _FX_F32(n); }
- bool operator<(const double& n) const { return v < _FX_F64(n); }
- bool operator<(const fx16_8& n) const { return v < n.v; }
- bool operator>(const int& n) const { return v > n<<8; }
- bool operator>(const float& n) const { return v > _FX_F32(n); }
- bool operator>(const double& n) const { return v > _FX_F64(n); }
- bool operator>(const fx16_8& n) const { return v > n.v; }
- bool operator<=(const int& n) const { return v <= n<<8; }
- bool operator<=(const float& n) const { return v <= _FX_F32(n); }
- bool operator<=(const double& n) const { return v <= _FX_F64(n); }
- bool operator<=(const fx16_8& n) const { return v <= n.v; }
- bool operator>=(const int& n) const { return v >= n<<8; }
- bool operator>=(const float& n) const { return v >= _FX_F32(n); }
- bool operator>=(const double& n) const { return v >= _FX_F64(n); }
- bool operator>=(const fx16_8& n) const { return v >= n.v; }
- fx16_8 abs() const { fx16_8 o; o.v = std::abs(v); return o; }
- fx16_8 trunc() const { fx16_8 o; o.v = v&0xffffff00; return o; }
- fx16_8 round() const { fx16_8 o; o.v = (v+128)&0xffffff00; return o; }
- int iround() const { return (v+128)>>8; }
- fx16_8 sin() const {
- fx16_8 o; o.v = _fx16_8_sin_table[std::abs(v)%fx_2pi.v];
- return o;
- }
- fx16_8 cos() const {
- fx16_8 o; o.v = _fx16_8_sin_table[(std::abs(v)+fx_pi2.v)%fx_2pi.v];
- return o;
- }
- fx16_8 tan() const { return sin()/cos(); }
- //i'll come up with a more pure implementation of the following functions later
- fx16_8 asin() const {
- fx16_8 o; o.v = (std::asin((double)v/256)*256 + 0.5);
- return o;
- }
- fx16_8 acos() const {
- fx16_8 o; o.v = (std::acos((double)v/256)*256 + 0.5);
- return o;
- }
- fx16_8 atan() const {
- fx16_8 o; o.v = (std::atan((double)v/256)*256 + 0.5);
- return o;
- }
- fx16_8 sqrt() const {
- if(v < 0) throw std::domain_error("Cannot compute square root of a negative number");
- fx16_8 o; o.v = (std::sqrt((double)v/256)*256 + 0.5);
- return o;
- }
- };
- struct fx_vec2;
- struct fx_vec3;
- struct fx_vec2 {
- fx16_8 x;
- fx16_8 y;
- fx_vec2 operator+(const fx_vec2& n) const { fx_vec2 o; o.x = x+n.x, o.y = y+n.y; return o; }
- fx_vec2 operator-(const fx_vec2& n) const { fx_vec2 o; o.x = x-n.x, o.y = y-n.y; return o; }
- bool operator==(const fx_vec2& n){ return (x==n.x) && (y==n.y); }
- fx_vec2 trunc() const { fx_vec2 o; o.x = x.trunc(), o.y = y.trunc(); return o; }
- fx_vec2 round() const { fx_vec2 o; o.x = x.round(), o.y = y.round(); return o; }
- };
- struct fx_vec3 {
- fx16_8 x;
- fx16_8 y;
- fx16_8 z;
- fx_vec3() : x(0), y(0), z(0) {}
- fx_vec3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
- void rotateRad(fx16_8 roll, fx16_8 pitch, fx16_8 yaw);
- void rotateDeg(fx16_8 roll, fx16_8 pitch, fx16_8 yaw){
- rotateRad(roll/fx_invpi_180, pitch/fx_invpi_180, yaw/fx_invpi_180);
- }
- };
- static inline fx16_8 fx_vec2_cross(const fx_vec2& a,
- const fx_vec2& b,
- const fx_vec2& origin = {0,0})
- {
- const fx_vec2 oa = a-origin;
- const fx_vec2 ob = b-origin;
- return (oa.x*ob.y)-(oa.y*ob.x);
- }
- //static inline fx_vec3 fx_vec2_3(const fx_vec2& v2){ return { .x = v2.x, .y = v2.y, .z = 0 }; }
- static inline fx_vec2 fx_vec3_2(const fx_vec3& v3){ fx_vec2 o; o.x=v3.x, o.y=v3.y; return o; }
- #endif /* _UTILS_FX16_8_HPP */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement