ScummVM API documentation
fixedint.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef STARTREK_FIXEDINT_H
23#define STARTREK_FIXEDINT_H
24
25#include "common/serializer.h"
26
27#include "startrek/common.h"
28
29namespace StarTrek {
30
34template<typename T, uint totalBits, uint decimalBits>
36 static const int max = (1 << (totalBits - decimalBits - 1)) - 1;
37 static const int min = -max - 1;
38
39 T val;
40
41public:
42 static TFixedInt fromRaw(T raw) {
43 TFixedInt ret;
44 ret.val = raw;
45 return ret;
46 }
47
48 TFixedInt() : val(0) {}
49 TFixedInt(double d) {
50 assert(d >= min && d <= max); // FIXME: downgrade this to a warning?
51 val = (T)(d * (1 << decimalBits));
52 }
53
57 template<typename T2, uint otherTB, uint otherDB>
59 int diff = otherDB - decimalBits;
60 if (otherDB >= decimalBits)
61 val = fi.raw() >> diff;
62 else
63 val = fi.raw() << (-diff);
64 }
65
66 T raw() const {
67 return val;
68 }
69
70 int16 toInt() const {
71 return val >> decimalBits;
72 }
73
74 double toDouble() const {
75 return ((double)val) / (1 << decimalBits);
76 }
77
78 TFixedInt operator-() const {
79 return fromRaw(-val);
80 }
81
86 int16 multToInt(int32 i) {
87 return ((int32)(val * i)) >> decimalBits;
88 }
89
93 TFixedInt operator*(int32 i) const {
94 return fromRaw(val * i);
95 }
99 TFixedInt operator*(const TFixedInt &f) const {
100 return fromRaw(((int32)(val * f.val)) >> decimalBits);
101 }
105 TFixedInt operator/(int32 i) const {
106 return fromRaw(val / i);
107 }
108 TFixedInt operator+(const TFixedInt &f) const {
109 return fromRaw(val + f.val);
110 }
111 TFixedInt operator-(const TFixedInt &f) const {
112 return fromRaw(val - f.val);
113 }
114
115 void operator+=(const TFixedInt &f) {
116 val += f.val;
117 }
118 void operator-=(const TFixedInt &f) {
119 val -= f.val;
120 }
121
122 bool operator==(double d) const {
123 return toDouble() == d;
124 }
125 bool operator!=(double d) const {
126 return toDouble() != d;
127 }
128 bool operator<(double d) const {
129 return toDouble() < d;
130 }
131 bool operator<=(double d) const {
132 return toDouble() <= d;
133 }
134 bool operator>(double d) const {
135 return toDouble() > d;
136 }
137 bool operator>=(double d) const {
138 return toDouble() >= d;
139 }
140
141 bool operator==(const TFixedInt &f) const {
142 return val == f.val;
143 }
144 bool operator!=(const TFixedInt &f) const {
145 return val != f.val;
146 }
147 bool operator<(const TFixedInt &f) const {
148 return val < f.val;
149 }
150 bool operator<=(const TFixedInt &f) const {
151 return val <= f.val;
152 }
153 bool operator>(const TFixedInt &f) const {
154 return val > f.val;
155 }
156 bool operator>=(const TFixedInt &f) const {
157 return val >= f.val;
158 }
159
160 void saveLoadWithSerializer(Common::Serializer &ser) override {
161 if (totalBits == 16)
162 ser.syncAsSint16LE(val);
163 else if (totalBits == 32)
164 ser.syncAsSint32LE(val);
165 else
166 error("Unsupported bit size for TFixedInt");
167 }
168};
169
170template<typename T, uint totalBits, uint decimalBits>
171int32 operator*(const int16 lhs, const TFixedInt<T, totalBits, decimalBits> &rhs) {
172 return rhs * lhs;
173}
174
175
176// Fixed-point (2.14) number (between -1 and 1)
177typedef TFixedInt<int16, 16, 14> Fixed14;
178
179// Fixed-point (8.8) number
180typedef TFixedInt<int16, 16, 8> Fixed8;
181
182// Fixed-point (16.16) number
183typedef TFixedInt<int32, 32, 16> Fixed16;
184
185typedef Fixed8 Angle;
186
187} // End of namespace StarTrek
188
189#endif
Definition: serializer.h:352
Definition: serializer.h:80
Definition: fixedint.h:35
TFixedInt operator*(const TFixedInt &f) const
Definition: fixedint.h:99
TFixedInt(const TFixedInt< T2, otherTB, otherDB > &fi)
Definition: fixedint.h:58
TFixedInt operator/(int32 i) const
Definition: fixedint.h:105
TFixedInt operator*(int32 i) const
Definition: fixedint.h:93
int16 multToInt(int32 i)
Definition: fixedint.h:86
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: action.h:27