diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common.hpp | 32 | ||||
-rw-r--r-- | src/sandbox.cpp | 7 |
2 files changed, 37 insertions, 2 deletions
diff --git a/src/common.hpp b/src/common.hpp index e745ece..d35d1bc 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -13,6 +13,26 @@ struct Pt { Pt operator-(Pt o) const { return { x - o.x, y - o.y }; } Pt operator*(int k) const { return { x * k, y * k }; } Pt operator/(int k) const { return { x / k, y / k }; } + Pt& operator+=(Pt o) { + x += o.x; + y += o.y; + return *this; + } + Pt& operator-=(Pt o) { + x -= o.x; + y -= o.y; + return *this; + } + Pt& operator*=(Pt o) { + x *= o.x; + y *= o.y; + return *this; + } + Pt& operator/=(Pt o) { + x /= o.x; + y /= o.y; + return *this; + } bool operator==(const Pt&) const = default; }; @@ -25,8 +45,6 @@ inline Pt pt_min(Pt a, Pt b) { inline Pt pt_max(Pt a, Pt b) { return Pt{ MMAX(a.x, b.x), MMAX(a.y, b.y) }; } -#undef MMIN -#undef MMAX struct Rect { Pt bl, tr; @@ -46,3 +64,13 @@ inline Rect rect_union(Rect a, Rect b) { inline Rect rect_union(Rect a, Pt b) { return Rect(pt_min(a.bl, b), pt_max(a.tr, b)); } +inline Rect rect_intersect(Rect a, Rect b) { + // https://stackoverflow.com/a/19754915 + int x0 = MMAX(a.bl.x, b.bl.x); + int y0 = MMAX(a.bl.y, b.bl.y); + int x1 = MMIN(a.tr.x, b.tr.x); + int y1 = MMIN(a.tr.y, b.tr.y); + return Rect(x0, y0, x1, y1); +} +#undef MMIN +#undef MMAX diff --git a/src/sandbox.cpp b/src/sandbox.cpp index c6d0294..2c09d77 100644 --- a/src/sandbox.cpp +++ b/src/sandbox.cpp @@ -102,17 +102,24 @@ void Sandbox::simulate_step() { dirty_writeto = {}; const auto [x0, y0] = dirty_curr.bl; const auto [x1, y1] = dirty_curr.tr; + // Clear update bit for this cycle for (_y = y0; _y <= y1; ++_y) { for (_x = x0; _x <= x1; ++_x) { gs(_x, _y).updated = false; } } + + // Update for (_y = y0; _y <= y1; ++_y) { for (_x = x0; _x <= x1; ++_x) { simulate_sand_tile(*this, _x, _y); } } + dirty_writeto.bl -= Pt(1, 1); + dirty_writeto.tr += Pt(1, 1); + dirty_writeto = rect_intersect(dirty_writeto, Rect(0, 0, width - 1, height - 1)); + ++ncycle; } |