World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
OurMath.h
См. документацию.
1 #pragma once
2 
3 #include <cassert>
4 #include <cmath>
5 #include <limits>
6 #include <cstdint>
7 
8 #define pi_double 3.14159265358979323846
9 
10 int64_t fixpoint_mul(int, int);
11 int64_t fixpoint_dot(int x1, int x2, int y1, int y2, int z1, int z2);
12 int64_t fixpoint_div(int, int);
14 int fixpoint_from_float(float value);
15 int fixpoint_from_int(int lhv, int rhv);
16 int integer_sqrt(int val);
17 int GetDiceResult(unsigned int uNumDice, unsigned int uDiceSides); // idb
18 inline int round_to_int(float x) { return (int)floor(x + 0.5f); }
19 
20 #pragma pack(push, 1)
21 struct fixed { // fixed-point decimal
22  inline fixed() : _internal(0) {}
23  explicit fixed(int _bits) : _internal(_bits) {}
24 
25  static fixed FromFloat(float f) {
27  }
28 
29  static fixed FromInt(int value) { return fixed::Raw(value << 16); }
30 
31  static fixed Raw(int value) { return fixed(value); }
32 
33  float GetFloatFraction() const {
34  return (float)((double)((unsigned int)this->_internal & 0xFFFF) /
35  65530.0);
36  }
37  float GetFloat() const {
38  return (float)this->GetInt() + this->GetFloatFraction();
39  }
40  int GetInt() const { return this->_internal >> 16; }
41  int GetUnsignedInt() const { return (unsigned int)this->_internal >> 16; }
42 
43  //----- (0042EBBE) --------------------------------------------------------
44  //----- (004453C0) mm6-----------------------------------------------------
45  //----- (004A1760) mm6_chinese---------------------------------------------
46  inline fixed operator*(const fixed &rhs) {
47  return fixed::Raw(((int64_t)this->_internal * (int64_t)rhs._internal) >> 16);
48  }
49 
50  inline fixed operator/(const fixed &rhs) {
51  return fixed::Raw(((int64_t)this->_internal << 16) / rhs._internal);
52  }
53 
54  inline fixed operator+(const fixed &rhs) {
55  return fixed::Raw(this->_internal + rhs._internal);
56  }
57  inline fixed operator-(const fixed &rhs) {
58  return fixed::Raw(this->_internal - rhs._internal);
59  }
60  inline bool operator>=(const fixed &rhs) {
61  return this->_internal >= rhs._internal;
62  }
63  inline bool operator<=(const fixed &rhs) {
64  return this->_internal <= rhs._internal;
65  }
66  inline bool operator>(const fixed &rhs) {
67  return this->_internal > rhs._internal;
68  }
69  inline bool operator<(const fixed &rhs) {
70  return this->_internal < rhs._internal;
71  }
72 
74 };
75 #pragma pack(pop)
76 
77 /* 186 */
78 #pragma pack(push, 1)
79 struct stru193_math {
80  stru193_math();
81  int Cos(int angle);
82  unsigned int Atan2(int x, int y);
83  int Sin(int angle);
84 
85  int pTanTable[520];
86  int pCosTable[520];
87  int pInvCosTable[520];
88  static const unsigned int uIntegerPi = 1024;
89  static const unsigned int uIntegerHalfPi = 512;
90  static const unsigned int uIntegerDoublePi = 2048;
91  static const unsigned int uDoublePiMask = 2047;
92  static const unsigned int uPiMask = 1023;
93  static const unsigned int uHalfPiMask = 511;
94 };
95 #pragma pack(pop)
96 
97 template <typename FloatType>
98 inline int bankersRounding(const FloatType &value) {
99  assert("Method unsupported for this type" && false);
100  return value;
101 }
102 
103 template <>
104 inline int bankersRounding<float>(const float &inValue) {
105  union Cast {
106  double d;
107  long l;
108  };
109  volatile Cast c;
110  c.d = inValue + 6755399441055744.0;
111  return c.l;
112 }
113 
114 #pragma push_macro("max")
115 #undef max
116 template <>
117 inline int bankersRounding<double>(const double &inValue) {
118  double maxValue = std::numeric_limits<int>::max();
119  assert(maxValue - 6755399441055744.0 >= inValue);
120  union Cast {
121  double d;
122  long l;
123  };
124  volatile Cast c;
125  c.d = inValue + 6755399441055744.0;
126  return c.l;
127 }
128 #pragma pop_macro("max")
129 
130 extern struct stru193_math *stru_5C6E00;
fixed::operator>
bool operator>(const fixed &rhs)
Definition: OurMath.h:66
fixed::FromFloat
static fixed FromFloat(float f)
Definition: OurMath.h:25
stru193_math::uIntegerDoublePi
static const unsigned int uIntegerDoublePi
Definition: OurMath.h:90
stru193_math::uDoublePiMask
static const unsigned int uDoublePiMask
Definition: OurMath.h:91
fixed::Raw
static fixed Raw(int value)
Definition: OurMath.h:31
fixpoint_dot
int64_t fixpoint_dot(int x1, int x2, int y1, int y2, int z1, int z2)
Definition: OurMath.cpp:142
stru193_math::uIntegerHalfPi
static const unsigned int uIntegerHalfPi
Definition: OurMath.h:89
stru_5C6E00
struct stru193_math * stru_5C6E00
Definition: mm7_data.cpp:19
round_to_int
int round_to_int(float x)
Definition: OurMath.h:18
stru193_math::pCosTable
int pCosTable[520]
Definition: OurMath.h:86
GetDiceResult
int GetDiceResult(unsigned int uNumDice, unsigned int uDiceSides)
Definition: OurMath.cpp:194
int64_t
__int64 int64_t
Definition: alext.h:31
bankersRounding< double >
int bankersRounding< double >(const double &inValue)
Definition: OurMath.h:117
fixpoint_sub_unknown
int64_t fixpoint_sub_unknown(int, int)
Definition: OurMath.cpp:149
x1
GLuint GLfloat GLfloat GLfloat x1
Definition: SDL_opengl_glext.h:8586
fixed::FromInt
static fixed FromInt(int value)
Definition: OurMath.h:29
y
EGLSurface EGLint EGLint y
Definition: SDL_egl.h:1596
bankersRounding
int bankersRounding(const FloatType &value)
Definition: OurMath.h:98
stru193_math::stru193_math
stru193_math()
Definition: OurMath.cpp:5
fixed::operator*
fixed operator*(const fixed &rhs)
Definition: OurMath.h:46
x
EGLSurface EGLint x
Definition: SDL_egl.h:1596
stru193_math::pTanTable
int pTanTable[520]
Definition: OurMath.h:85
fixed::operator<
bool operator<(const fixed &rhs)
Definition: OurMath.h:69
integer_sqrt
int integer_sqrt(int val)
Definition: OurMath.cpp:164
fixed::_internal
int32_t _internal
Definition: OurMath.h:73
int32_t
signed __int32 int32_t
Definition: SDL_config.h:38
f
GLfloat f
Definition: SDL_opengl_glext.h:1873
l
uint64_t l
Definition: intreadwrite.h:1
fixpoint_from_float
int fixpoint_from_float(float value)
Definition: OurMath.cpp:154
value
EGLSyncKHR EGLint EGLint * value
Definition: SDL_egl.h:899
fixed::operator/
fixed operator/(const fixed &rhs)
Definition: OurMath.h:50
fixed::GetFloatFraction
float GetFloatFraction() const
Definition: OurMath.h:33
bankersRounding< float >
int bankersRounding< float >(const float &inValue)
Definition: OurMath.h:104
stru193_math
Definition: OurMath.h:79
stru193_math::pInvCosTable
int pInvCosTable[520]
Definition: OurMath.h:87
fixed::operator<=
bool operator<=(const fixed &rhs)
Definition: OurMath.h:63
stru193_math::uPiMask
static const unsigned int uPiMask
Definition: OurMath.h:92
fixed::operator-
fixed operator-(const fixed &rhs)
Definition: OurMath.h:57
stru193_math::Atan2
unsigned int Atan2(int x, int y)
Definition: OurMath.cpp:46
val
GLuint GLfloat * val
Definition: SDL_opengl_glext.h:1495
x2
GLfixed GLfixed x2
Definition: SDL_opengl_glext.h:4586
fixpoint_from_int
int fixpoint_from_int(int lhv, int rhv)
Definition: OurMath.cpp:161
fixed::GetUnsignedInt
int GetUnsignedInt() const
Definition: OurMath.h:41
fixpoint_mul
int64_t fixpoint_mul(int, int)
Definition: OurMath.cpp:138
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11096
fixed::operator+
fixed operator+(const fixed &rhs)
Definition: OurMath.h:54
y1
GLfixed y1
Definition: SDL_opengl_glext.h:4586
fixed::operator>=
bool operator>=(const fixed &rhs)
Definition: OurMath.h:60
fixed::GetFloat
float GetFloat() const
Definition: OurMath.h:37
y2
GLfixed GLfixed GLfixed y2
Definition: SDL_opengl_glext.h:4586
stru193_math::Cos
int Cos(int angle)
Definition: OurMath.cpp:28
fixpoint_div
int64_t fixpoint_div(int, int)
Definition: OurMath.cpp:147
angle
GLfloat angle
Definition: SDL_opengl_glext.h:6100
fixed::fixed
fixed(int _bits)
Definition: OurMath.h:23
stru193_math::Sin
int Sin(int angle)
Definition: OurMath.cpp:133
fixed
Definition: OurMath.h:21
stru193_math::uIntegerPi
static const unsigned int uIntegerPi
Definition: OurMath.h:88
fixed::fixed
fixed()
Definition: OurMath.h:22
fixed::GetInt
int GetInt() const
Definition: OurMath.h:40
stru193_math::uHalfPiMask
static const unsigned int uHalfPiMask
Definition: OurMath.h:93