From ec5c6b2becdc2232ec83a2c7c5a672cc534c1001 Mon Sep 17 00:00:00 2001 From: shylie Date: Sat, 18 Apr 2026 12:21:04 -0400 Subject: [PATCH] Fix bug in update --- src/main.cpp | 48 ++++++++++++++++++++++++++++++++++-------------- src/sand.cpp | 2 +- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 512a981..f6ed8a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,29 +7,46 @@ #include #include -constexpr uint16_t WIDTH = 128; -constexpr uint16_t HEIGHT = 128; +constexpr uint16_t WIDTH = 256; +constexpr uint16_t HEIGHT = 256; constexpr int WINDOW_WIDTH = 1024; constexpr int WINDOW_HEIGHT = 1024; uint8_t lookup[][3] - = { { 0, 0, 0 }, { 50, 0, 0 }, { 100, 100, 100 }, { 200, 200, 200 } }; + = { { 0, 0, 0 }, { 50, 0, 0 }, { 100, 100, 100 }, { 100, 100, 100 } }; int main(int argc, char** argv) { auto rb = sand::TypeBuilder() .add_type("offgrid") .add_type("air") - .add_type("stone1") - .add_type("stone2") + .add_type("stone1", "stone2") + .add_type("stone2", "stone1") .finish(); - rb.add_rule("air", "stone1") + rb.add_rule("air", "stone2") .top_middle([](const auto& t) { return t == "stone1"; }); + rb.add_rule("air", "stone1") + .top_middle([](const auto& t) { return t == "stone2"; }); + rb.add_rule("air", "stone2") + .top_right([](const auto& t) { return t == "stone1"; }) + .middle_right([](const auto& t) { return t != "air"; }); + rb.add_rule("air", "stone1") + .top_left([](const auto& t) { return t == "stone2"; }) + .middle_left([](const auto& t) { return t != "air"; }); rb.add_rule("stone1", "air") .bottom_middle([](const auto& t) { return t == "air"; }); + rb.add_rule("stone1", "air") + .bottom_left([](const auto& t) { return t == "air"; }) + .bottom_middle([](const auto& t) { return t != "air"; }); + + rb.add_rule("stone2", "air") + .bottom_middle([](const auto& t) { return t == "air"; }); + rb.add_rule("stone2", "air") + .bottom_right([](const auto& t) { return t == "air"; }) + .bottom_middle([](const auto& t) { return t != "air"; }); auto s = rb.build(WIDTH, HEIGHT, "air"); @@ -60,15 +77,20 @@ int main(int argc, char** argv) float x, y; int state; - if (time > 0.008 && (state = SDL_GetMouseState(&x, &y))) + if (time > 1 / 1000.0f) { - int ix = x * WIDTH / WINDOW_WIDTH; - int iy = y * HEIGHT / WINDOW_HEIGHT; + time = 0; + s.update(); - if (ix >= 0 && ix < WIDTH && iy >= 0 && iy < HEIGHT) + if ((state = SDL_GetMouseState(&x, &y))) { - time = 0; - s.set(ix, iy, state == 1 ? "stone1" : "stone2"); + int ix = x * WIDTH / WINDOW_WIDTH; + int iy = y * HEIGHT / WINDOW_HEIGHT; + + if (ix >= 0 && ix < WIDTH && iy >= 0 && iy < HEIGHT) + { + s.set(ix, iy, state == 1 ? "stone1" : "stone2"); + } } } @@ -85,8 +107,6 @@ int main(int argc, char** argv) SDL_BlitSurfaceScaled(surface, nullptr, SDL_GetWindowSurface(window), nullptr, SDL_SCALEMODE_NEAREST); SDL_UpdateWindowSurface(window); - - s.update(); } SDL_DestroySurface(surface); diff --git a/src/sand.cpp b/src/sand.cpp index 7a382eb..1da5270 100644 --- a/src/sand.cpp +++ b/src/sand.cpp @@ -291,7 +291,7 @@ void Sand::update() if (i < width - 1 && j > 0) { - neighbors += get(i + 1, j); + neighbors += get(i + 1, j - 1); } neighbors <<= 16;