diff --git a/th03/math/subpixel.hpp b/th03/math/subpixel.hpp new file mode 100644 index 00000000..3e0b5d1e --- /dev/null +++ b/th03/math/subpixel.hpp @@ -0,0 +1,36 @@ +// 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(screen_v * 16.0f); +} + +class Subpixel { +public: + // Code generation will require direct access to v, if performing + // arithmetic with a local variable... + subpixel_t v; + + void operator +=(float screen_v) { + this->v += to_sp(screen_v); + } + + void operator -=(float screen_v) { + this->v -= to_sp(screen_v); + } + + void operator =(float screen_v) { + v = to_sp(screen_v); + } +}; + +struct SPPoint { + Subpixel x, y; + + void set(float screen_x, float screen_y) { + x = screen_x; + y = screen_y; + } +}; diff --git a/th04/shared.hpp b/th04/shared.hpp index be13a4c5..5d461047 100644 --- a/th04/shared.hpp +++ b/th04/shared.hpp @@ -7,39 +7,7 @@ /// Math /// ---- -typedef int subpixel_t; - -inline subpixel_t to_sp(float screen_v) { - return static_cast(screen_v * 16.0f); -} - -class Subpixel { -public: - // Code generation will require direct access to v, if performing - // arithmetic with a local variable... - subpixel_t v; - - void operator +=(float screen_v) { - this->v += to_sp(screen_v); - } - - void operator -=(float screen_v) { - this->v -= to_sp(screen_v); - } - - void operator =(float screen_v) { - v = to_sp(screen_v); - } -}; - -struct SPPoint { - Subpixel x, y; - - void set(float screen_x, float screen_y) { - x = screen_x; - y = screen_y; - } -}; +#include "th03/math/subpixel.hpp" typedef struct { SPPoint cur;