From a9d4989597d32ce29f4a26c4e9a39428973c8535 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 29 Jan 2026 17:16:51 +0000 Subject: [PATCH 1/2] feat(game): optimize game loop with dynamic poll timeout Refactors the main game loop to use a single, blocking `poll()` call with a dynamic timeout. The previous implementation used an inefficient busy-wait pattern with a non-blocking `poll()` and a fixed `std::this_thread::sleep_for()`. This caused high CPU usage and unnecessary wakeups. This change calculates the remaining time until the next tick and uses it as the timeout for `poll()`, allowing the process to sleep efficiently. This significantly reduces CPU consumption and improves input responsiveness, enhancing the overall user experience. Co-authored-by: aidasofialily-cmd <247843425+aidasofialily-cmd@users.noreply.github.com> --- src/main.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a70e887..b4e0c69 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include int main() { struct termios oldt, newt; @@ -19,21 +20,27 @@ int main() { struct pollfd fds[1] = {{STDIN_FILENO, POLLIN, 0}}; auto last_tick = std::chrono::steady_clock::now(); while (true) { - int timeout = hardMode ? 100 : 1000; - if (poll(fds, 1, 0) > 0) { + int base_timeout = hardMode ? 100 : 1000; + auto now = std::chrono::steady_clock::now(); + auto elapsed = std::chrono::duration_cast(now - last_tick).count(); + + int timeout = std::max(0LL, static_cast(base_timeout - elapsed)); + + if (poll(fds, 1, timeout) > 0) { if (read(STDIN_FILENO, &input, 1) <= 0 || input == 'q') break; if (input == 'h') { hardMode = !hardMode; std::cout << (hardMode ? "\n[HARD MODE] Speed x10!\n" : "\n[NORMAL MODE]\n"); } else score++; } - auto now = std::chrono::steady_clock::now(); - auto elapsed = std::chrono::duration_cast(now - last_tick).count(); - if (elapsed >= timeout) { + + now = std::chrono::steady_clock::now(); + elapsed = std::chrono::duration_cast(now - last_tick).count(); + + if (elapsed >= base_timeout) { score++; last_tick = now; std::cout << "Score: " << score << (hardMode ? " [FAST] " : " [NORMAL] ") << "\r" << std::flush; } - std::this_thread::sleep_for(std::chrono::milliseconds(10)); } tcsetattr(STDIN_FILENO, TCSANOW, &oldt); std::cout << "\nFinal Score: " << score << "\nThanks for playing!\n"; From d88ebe10cc697c923d79d9a026eb7bb996f7751c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 29 Jan 2026 17:24:29 +0000 Subject: [PATCH 2/2] feat(game): Add start prompt and improve game loop - Adds a "Press any key to start..." prompt to improve the initial user experience. - Refactors the main game loop to use a more efficient and responsive `poll()`-based timer. - Fixes the CI workflow to correctly build and test the C++ project. Co-authored-by: aidasofialily-cmd <247843425+aidasofialily-cmd@users.noreply.github.com> --- .github/workflows/{rust.yml => build.yml} | 13 +++++++------ src/main.cpp | 3 +++ 2 files changed, 10 insertions(+), 6 deletions(-) rename .github/workflows/{rust.yml => build.yml} (51%) diff --git a/.github/workflows/rust.yml b/.github/workflows/build.yml similarity index 51% rename from .github/workflows/rust.yml rename to .github/workflows/build.yml index 9fd45e0..0e12c4e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ -name: Rust +name: Build and Test on: push: @@ -6,9 +6,6 @@ on: pull_request: branches: [ "main" ] -env: - CARGO_TERM_COLOR: always - jobs: build: @@ -17,6 +14,10 @@ jobs: steps: - uses: actions/checkout@v4 - name: Build - run: cargo build --verbose + run: make - name: Run tests - run: cargo test --verbose + run: | + echo "Running tests..." + output=$(echo "sccchcccq" | ./game) + echo "$output" + echo "$output" | grep "Final Score: 6" diff --git a/src/main.cpp b/src/main.cpp index b4e0c69..18fc010 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,9 @@ int main() { std::cout << "==========================\n SPEED CLICKER\n==========================\n" << "Controls:\n [h] Toggle Hard Mode (10x Speed!)\n [q] Quit Game\n [Any key] Click!\n\n"; + std::cout << "Press any key to start...\n"; + read(STDIN_FILENO, &input, 1); + struct pollfd fds[1] = {{STDIN_FILENO, POLLIN, 0}}; auto last_tick = std::chrono::steady_clock::now(); while (true) {