2019-12-01 15:41:14 +00:00
|
|
|
// Fixed-point format for expressing world-space coordinates, with 4 bits of
|
|
|
|
// fractional resolution.
|
|
|
|
|
|
|
|
typedef int subpixel_t;
|
|
|
|
|
|
|
|
inline subpixel_t to_sp(float screen_v) {
|
|
|
|
return static_cast<subpixel_t>(screen_v * 16.0f);
|
|
|
|
}
|
|
|
|
|
2019-12-01 16:22:06 +00:00
|
|
|
template <class T> class SubpixelBase {
|
2019-12-01 15:41:14 +00:00
|
|
|
public:
|
|
|
|
// Code generation will require direct access to v, if performing
|
|
|
|
// arithmetic with a local variable...
|
2019-12-01 16:22:06 +00:00
|
|
|
T v;
|
2019-12-01 15:41:14 +00:00
|
|
|
|
|
|
|
void operator +=(float screen_v) {
|
2019-12-01 16:22:06 +00:00
|
|
|
this->v += static_cast<T>(to_sp(screen_v));
|
2019-12-01 15:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator -=(float screen_v) {
|
2019-12-01 16:22:06 +00:00
|
|
|
this->v -= static_cast<T>(to_sp(screen_v));
|
2019-12-01 15:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator =(float screen_v) {
|
2019-12-01 16:22:06 +00:00
|
|
|
v = static_cast<T>(to_sp(screen_v));
|
2019-12-01 15:41:14 +00:00
|
|
|
}
|
2019-12-03 21:45:08 +00:00
|
|
|
|
|
|
|
T to_screen() const {
|
|
|
|
return v >> 4;
|
|
|
|
}
|
2019-12-01 15:41:14 +00:00
|
|
|
};
|
|
|
|
|
2019-12-01 16:22:06 +00:00
|
|
|
template <class T> struct SPPointBase {
|
|
|
|
T x, y;
|
2019-12-01 15:41:14 +00:00
|
|
|
|
|
|
|
void set(float screen_x, float screen_y) {
|
|
|
|
x = screen_x;
|
|
|
|
y = screen_y;
|
|
|
|
}
|
|
|
|
};
|
2019-12-01 16:22:06 +00:00
|
|
|
|
|
|
|
// 16-bit (Q12.4)
|
|
|
|
typedef SubpixelBase<subpixel_t> Subpixel;
|
|
|
|
typedef SPPointBase<Subpixel> SPPoint;
|
|
|
|
// 8-bit (Q4.4)
|
2020-02-03 19:32:56 +00:00
|
|
|
typedef SubpixelBase<unsigned char> SubpixelLength8;
|
2019-12-01 16:22:06 +00:00
|
|
|
typedef SubpixelBase<char> Subpixel8;
|
|
|
|
typedef SPPointBase<Subpixel8> SPPoint8;
|