2019-12-01 15:41:14 +00:00
|
|
|
// Fixed-point format for expressing world-space coordinates, with 4 bits of
|
|
|
|
// fractional resolution.
|
|
|
|
|
2020-08-17 18:13:54 +00:00
|
|
|
#define PIXEL_NONE (-999)
|
|
|
|
|
2019-12-01 15:41:14 +00:00
|
|
|
typedef int subpixel_t;
|
|
|
|
|
2020-07-02 19:19:50 +00:00
|
|
|
#define TO_SP(v) \
|
|
|
|
(v << 4)
|
|
|
|
|
2019-12-01 15:41:14 +00:00
|
|
|
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
|
|
|
|
2020-07-02 19:19:50 +00:00
|
|
|
subpixel_t operator -(const SubpixelBase<T> &other) {
|
|
|
|
return (this->v - other.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
|
|
|
|
2020-06-28 13:38:55 +00:00
|
|
|
void operator =(const T &screen_v) {
|
2020-07-02 19:19:50 +00:00
|
|
|
v = TO_SP(screen_v);
|
2020-06-28 13:38:55 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 21:45:08 +00:00
|
|
|
T to_screen() const {
|
|
|
|
return v >> 4;
|
|
|
|
}
|
2020-08-17 18:13:54 +00:00
|
|
|
|
|
|
|
static T None() {
|
|
|
|
return to_sp(PIXEL_NONE);
|
|
|
|
}
|
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;
|
|
|
|
}
|
2020-06-28 13:38:55 +00:00
|
|
|
|
|
|
|
void set(const T &screen_x, const T &screen_y) {
|
|
|
|
x = screen_x;
|
|
|
|
y = screen_y;
|
|
|
|
}
|
2019-12-01 15:41:14 +00:00
|
|
|
};
|
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;
|