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)
|
|
|
|
|
2020-08-25 12:31:22 +00:00
|
|
|
inline subpixel_t to_sp(float pixel_v) {
|
|
|
|
return static_cast<subpixel_t>(pixel_v * 16.0f);
|
2019-12-01 15:41:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 12:31:22 +00:00
|
|
|
template <class SubpixelType, class PixelType> class SubpixelBase {
|
2019-12-01 15:41:14 +00:00
|
|
|
public:
|
2020-08-25 12:31:22 +00:00
|
|
|
typedef SubpixelBase<SubpixelType, PixelType> SelfType;
|
|
|
|
|
2019-12-01 15:41:14 +00:00
|
|
|
// Code generation will require direct access to v, if performing
|
|
|
|
// arithmetic with a local variable...
|
2020-08-25 12:31:22 +00:00
|
|
|
SubpixelType v;
|
2019-12-01 15:41:14 +00:00
|
|
|
|
2020-08-25 12:31:22 +00:00
|
|
|
SubpixelType operator -(const SelfType &other) {
|
2020-07-02 19:19:50 +00:00
|
|
|
return (this->v - other.v);
|
|
|
|
}
|
|
|
|
|
2020-08-25 12:31:22 +00:00
|
|
|
void operator +=(float pixel_v) {
|
|
|
|
this->v += static_cast<SubpixelType>(to_sp(pixel_v));
|
2019-12-01 15:41:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 12:31:22 +00:00
|
|
|
void operator -=(float pixel_v) {
|
|
|
|
this->v -= static_cast<SubpixelType>(to_sp(pixel_v));
|
2019-12-01 15:41:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 09:11:59 +00:00
|
|
|
// No overloads of `operator =()`, since the class needs to be trivially
|
|
|
|
// copyable.
|
2020-08-25 12:31:22 +00:00
|
|
|
void set(float pixel_v) {
|
|
|
|
v = static_cast<SubpixelType>(to_sp(pixel_v));
|
2019-12-01 15:41:14 +00:00
|
|
|
}
|
2019-12-03 21:45:08 +00:00
|
|
|
|
2020-08-25 12:31:22 +00:00
|
|
|
void set(const PixelType &pixel_v) {
|
|
|
|
v = static_cast<SubpixelType>(TO_SP(pixel_v));
|
2020-06-28 13:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 12:31:22 +00:00
|
|
|
PixelType to_pixel() const {
|
|
|
|
return static_cast<PixelType>(v >> 4);
|
2019-12-03 21:45:08 +00:00
|
|
|
}
|
2020-08-17 18:13:54 +00:00
|
|
|
|
2020-08-25 12:31:22 +00:00
|
|
|
operator SubpixelType() const {
|
2020-08-17 21:44:56 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2020-08-25 12:31:22 +00:00
|
|
|
static SubpixelType None() {
|
|
|
|
return static_cast<SubpixelType>(TO_SP(PIXEL_NONE));
|
2020-08-17 18:13:54 +00:00
|
|
|
}
|
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) {
|
2020-08-19 09:11:59 +00:00
|
|
|
x.set(screen_x);
|
|
|
|
y.set(screen_y);
|
2020-06-28 13:38:55 +00:00
|
|
|
}
|
2019-12-01 15:41:14 +00:00
|
|
|
};
|
2019-12-01 16:22:06 +00:00
|
|
|
|
|
|
|
// 16-bit (Q12.4)
|
2020-08-25 12:31:22 +00:00
|
|
|
typedef SubpixelBase<subpixel_t, pixel_t> Subpixel;
|
2019-12-01 16:22:06 +00:00
|
|
|
typedef SPPointBase<Subpixel> SPPoint;
|
|
|
|
// 8-bit (Q4.4)
|
2020-08-25 12:31:22 +00:00
|
|
|
typedef SubpixelBase<unsigned char, unsigned char> SubpixelLength8;
|
|
|
|
typedef SubpixelBase<char, char> Subpixel8;
|
2019-12-01 16:22:06 +00:00
|
|
|
typedef SPPointBase<Subpixel8> SPPoint8;
|