diff options
author | rtk0c <[email protected]> | 2025-05-17 18:03:44 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2025-05-17 18:03:44 -0700 |
commit | e041b2a3ea3835a6b3a59dfbd1f24a86fd104396 (patch) | |
tree | 876f22bcc7710864d32ea0d61150bfc6c3a8ad17 /src/sandbox.cpp | |
parent | 152580faf7e7665a04be69b4a0e0538cf39c975c (diff) |
Dirty rect & UI features
- dirty rectangle (and the math library thereof)
- drawing it
- no more std::vector in Sandbox
- single step
- cycle counter display
Diffstat (limited to 'src/sandbox.cpp')
-rw-r--r-- | src/sandbox.cpp | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/src/sandbox.cpp b/src/sandbox.cpp index d0d918c..efc2bd0 100644 --- a/src/sandbox.cpp +++ b/src/sandbox.cpp @@ -25,23 +25,25 @@ uint32_t Tile::get_color() const { return material_color_lut[std::to_underlying(so)]; } -Sandbox::Sandbox(int w, int h) - : _bitmap(w * h) - , _a(w * h) - // TODO random seed - , _rand() +Sandbox::Sandbox(int w, int h, RandomState rand) + : bitmap(new uint32_t[w * h]) + , tiles(new Tile[w * h]) + , _rand(std::move(rand)) , _wall_tile{ Tile::ROCK } , width{ w } , height{ h } // { - memset(_bitmap.data(), 0xff, _bitmap.size() * sizeof(_bitmap[0])); + memset(bitmap, 0xff, (w * h) * sizeof(bitmap[0])); +} + +Sandbox::~Sandbox() { + delete[] bitmap; + delete[] tiles; } static void simulate_sand_tile(Sandbox& self, int x, int y) { const auto at0 = self.gs(x, y); if (at0.updated) { - // Clear update bit for next cycle - self.gs(x, y).updated = false; return; } @@ -91,13 +93,20 @@ static void simulate_sand_tile(Sandbox& self, int x, int y) { } void Sandbox::simulate_step() { - _x = 0; - _y = 0; - for (; _y < height; ++_y) { - for (; _x < width; ++_x) { + dirty_curr = dirty_writeto; + 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; + } + } + for (_y = y0; _y <= y1; ++_y) { + for (_x = x0; _x <= x1; ++_x) { simulate_sand_tile(*this, _x, _y); } - _x = 0; } ++ncycle; } @@ -105,16 +114,20 @@ void Sandbox::simulate_step() { Tile& Sandbox::gs(int x, int y) { if (x < 0 || x >= width || y < 0 || y >= height) return _wall_tile; - return _a[y * width + x]; + return tiles[y * width + x]; } void Sandbox::set_sand(int x, int y, Tile sand) { - auto& target = _a[y * width + x]; + auto& target = tiles[y * width + x]; target = sand; // Set update bit if the target is after cursor if (y < _y || x > _x) target.updated = true; - _bitmap[y * width + x] = sand.get_color(); + bitmap[y * width + x] = sand.get_color(); + if (dirty_writeto == Rect()) + dirty_writeto = Rect(x, y, x, y); + else + dirty_writeto = rect_union(dirty_writeto, Pt(x, y)); } // std::vector<uint32_t> Sandbox::to_bitmap() const { |