diff --git a/.Jules/palette.md b/.Jules/palette.md index 01736b2..d134091 100644 --- a/.Jules/palette.md +++ b/.Jules/palette.md @@ -6,3 +6,7 @@ ## 2026-01-09 - Terminal I/O and Blocking **Learning:** Standard terminal I/O is line-buffered by default. For real-time games, it's essential to use non-canonical mode (raw mode) to capture keypresses immediately. Also, internal journals should be kept clean if they are to be included in the repo. + +## 2026-01-10 - Tactile Feedback in Terminal UIs +**Learning:** Terminal applications can feel sluggish if the UI only updates on timer ticks. Triggering a UI refresh immediately upon user input makes the application feel significantly more responsive and tactile. +**Action:** Decouple UI rendering from the main tick loop and ensure input events trigger an immediate redraw of relevant components. diff --git a/.github/workflows/rust.yml b/.github/workflows/ci.yml similarity index 52% rename from .github/workflows/rust.yml rename to .github/workflows/ci.yml index 96b3ef2..d80b155 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: Rust +name: C++ CI on: push: @@ -6,20 +6,15 @@ on: pull_request: branches: [ "main" ] -env: - CARGO_TERM_COLOR: always - jobs: build: - runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose + run: make - permissions: - contents: read + - name: Run Tests + run: make test diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index c9a3abc..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "code" -version = "0.1.0" -edition = "2021" - -[dependencies] -# Add your dependencies here - -[dev-dependencies] -# Add your dev dependencies here diff --git a/Makefile b/Makefile index 3bf8240..79ebf7a 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,13 @@ $(TARGET): $(SRCS) run: $(TARGET) ./$(TARGET) +# Test the game +test: $(TARGET) + echo "q" | ./$(TARGET) | grep "Thanks for playing!" + # Clean up build artifacts clean: rm -f $(TARGET) # Phony targets -.PHONY: all run clean +.PHONY: all run clean test diff --git a/src/main.cpp b/src/main.cpp index a70e887..ca9fd5c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,22 @@ #include #include -#include #include #include #include +#include + +// Colors +#define RESET "\033[0m" +#define BOLD_GREEN "\033[1;32m" +#define BOLD_RED "\033[1;31m" +#define BOLD_BLUE "\033[1;34m" +#define BOLD_YELLOW "\033[1;33m" + +void updateUI(int score, bool hardMode) { + std::cout << "\r" << BOLD_GREEN << "Score: " << score << RESET + << (hardMode ? BOLD_RED " [FAST] " RESET : BOLD_BLUE " [NORMAL] " RESET) + << " " << std::flush; +} int main() { struct termios oldt, newt; @@ -13,29 +26,36 @@ int main() { tcsetattr(STDIN_FILENO, TCSANOW, &newt); int score = 0; bool hardMode = false; char input; - 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 << BOLD_YELLOW << "==========================\n SPEED CLICKER\n==========================\n" << RESET + << BOLD_YELLOW << "Controls:\n" << RESET + << " [h] Toggle Hard Mode (10x Speed!)\n [q] Quit Game\n [Any key] Click!\n\n"; struct pollfd fds[1] = {{STDIN_FILENO, POLLIN, 0}}; auto last_tick = std::chrono::steady_clock::now(); + updateUI(score, hardMode); + while (true) { - int timeout = hardMode ? 100 : 1000; - if (poll(fds, 1, 0) > 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++; - } + int base_timeout = hardMode ? 100 : 1000; auto now = std::chrono::steady_clock::now(); auto elapsed = std::chrono::duration_cast(now - last_tick).count(); - if (elapsed >= timeout) { - score++; last_tick = now; - std::cout << "Score: " << score << (hardMode ? " [FAST] " : " [NORMAL] ") << "\r" << std::flush; + int remaining = std::max(0, static_cast(base_timeout - elapsed)); + + if (poll(fds, 1, remaining) > 0) { + if (read(STDIN_FILENO, &input, 1) <= 0 || input == 'q') break; + if (input == 'h') hardMode = !hardMode; + else score++; + updateUI(score, hardMode); + } + + now = std::chrono::steady_clock::now(); + elapsed = std::chrono::duration_cast(now - last_tick).count(); + if (elapsed >= base_timeout) { + score++; + last_tick = now; + updateUI(score, hardMode); } - std::this_thread::sleep_for(std::chrono::milliseconds(10)); } tcsetattr(STDIN_FILENO, TCSANOW, &oldt); - std::cout << "\nFinal Score: " << score << "\nThanks for playing!\n"; + std::cout << BOLD_GREEN << "\nFinal Score: " << score << RESET << "\nThanks for playing!\n"; return 0; }