fixed level selector

This commit is contained in:
2025-08-16 21:58:19 +02:00
parent 5ca03fb689
commit eddd1a24b2
5 changed files with 163 additions and 74 deletions

View File

@ -3,10 +3,12 @@
This document lists recommended code, architecture, tooling, and runtime upgrades for the native SDL3 Tetris project. Items are grouped, prioritized, and mapped to target files and effort estimates so you can plan incremental work.
## Short plan
- Audit surface area and break tasks into small, testable PRs.
- Start with low-risk safety and build improvements, then refactors (Gravity/Scoring/Board), then tests/CI, then UX polish and optional features.
## Checklist (high level)
- [x] Make NES gravity table constant (constexpr) and move per-level multipliers out of a mutable global
- Note: FRAMES_TABLE is now immutable inside `GravityManager`; per-level multipliers are instance-scoped in `GravityManager`.
- [x] Extract GravityManager (SRP)
@ -27,6 +29,7 @@ This document lists recommended code, architecture, tooling, and runtime upgrade
---
## Rationale & Goals
- Improve maintainability: split `Game` responsibilities into focused classes (SRP) so logic is testable.
- Improve safety: remove mutable shared state (globals/`extern`) and minimize hidden side-effects.
- Improve tuning: per-level and global gravity tuning must be instance-local and debug-friendly.
@ -37,6 +40,7 @@ This document lists recommended code, architecture, tooling, and runtime upgrade
## Detailed tasks (prioritized)
### 1) Safety & small win (Low risk — 13 hours)
- Make the NES frames-per-cell table `constexpr` and immutable.
- File: `src/Game.cpp` (reverse any mutable anonymous namespace table changes)
- Why: avoids accidental global mutation; makes compute deterministic.
@ -48,18 +52,21 @@ This document lists recommended code, architecture, tooling, and runtime upgrade
Deliverable: small patch to `Game.h`/`Game.cpp` and a rebuild verification.
### 2) Replace ad-hoc logging (Low risk — 0.51 hour)
- Replace `printf` debug prints with `SDL_Log` or an injected `Logger` interface.
- Prefer a build-time `#ifdef DEBUG` or runtime verbosity flag so release builds are quiet.
Files: `src/Game.cpp`, any file using printf for debug.
### 3) Remove fragile globals / externs (Low risk — 12 hours)
- Ensure all textures, fonts and shared resources are passed via `StateContext& ctx`.
- Remove leftover `extern SDL_Texture* backgroundTex` or similar; make call-sites accept `SDL_Texture*` or read from `ctx`.
Files: `src/main.cpp`, `src/states/MenuState.cpp`, other states.
### 4) Extract GravityManager (Medium risk — 26 hours)
- Create `src/core/GravityManager.h|cpp` that encapsulates
- the `constexpr` NES frames table (read-only)
- per-instance multipliers
@ -69,6 +76,7 @@ Files: `src/main.cpp`, `src/states/MenuState.cpp`, other states.
Benefits: easier testing and isolated behavior change for future gravity models.
### 5) Extract Board/Scoring responsibilities (Medium — 48 hours)
- Split `Game` into smaller collaborators:
- `Board` — raw grid, collision, line detection/clear operations
- `Scorer` — scoring rules, combo handling, level progression thresholds
@ -78,6 +86,7 @@ Benefits: easier testing and isolated behavior change for future gravity models.
Files: `src/Board.*`, `src/Scorer.*`, `src/PieceController.*`, adjust `Game` to compose them.
### 6) Unit tests & test infrastructure (Medium — 36 hours)
- Add Catch2 or GoogleTest to `vcpkg.json` or as `FetchContent` in CMake.
- Add tests:
- Gravity conversion tests (frames→ms, per-level multipliers, global multiplier)
@ -86,6 +95,7 @@ Files: `src/Board.*`, `src/Scorer.*`, `src/PieceController.*`, adjust `Game` to
- Add a `tests/` CMake target and basic CI integration (see next section).
### 7) CI / Build checks (Medium — 24 hours)
- Add GitHub Actions to build Debug/Release on Windows and run tests.
- Add static analysis: clang-tidy/clang-format or cpplint configured for the project style.
- Add a Pre-commit hook to run format checks.
@ -93,6 +103,7 @@ Files: `src/Board.*`, `src/Scorer.*`, `src/PieceController.*`, adjust `Game` to
Files: `.github/workflows/build.yml`, `.clang-format`, optional `clang-tidy` config.
### 8) UX and input correctness (LowMedium — 13 hours)
- Update level popup hit-testing to use the same computed button rectangles used for drawing.
- Expose a function that returns vector<SDL_FRect> of button bounds from the popup draw logic.
- Ensure popup background texture is stretched to the logical viewport and that overlay alpha is constant across window sizes.
@ -101,6 +112,7 @@ Files: `.github/workflows/build.yml`, `.clang-format`, optional `clang-tidy` con
Files: `src/main.cpp`, `src/states/MenuState.cpp`.
### 9) Runtime debug knobs (Low — 1 hour)
- Add keys to increase/decrease `gravityGlobalMultiplier` (e.g., `[` and `]`) and reset to 1.0.
- Show `gravityGlobalMultiplier` and per-level effective ms on HUD (already partly implemented).
- Persist tuning to a small JSON file `settings/debug_tuning.json` (optional).
@ -108,6 +120,7 @@ Files: `src/main.cpp`, `src/states/MenuState.cpp`.
Files: `src/Game.h/cpp`, `src/main.cpp` HUD code.
### 10) Defensive & correctness improvements (Low — 24 hours)
- Add null checks for SDL_CreateTextureFromSurface and related APIs; log and fallback gracefully.
- Convert raw SDL_Texture* ownership to `std::unique_ptr` with custom deleter where appropriate, or centralize lifetime in a `ResourceManager`.
- Add `const` qualifiers to getters where possible.
@ -116,6 +129,7 @@ Files: `src/Game.h/cpp`, `src/main.cpp` HUD code.
Files: various (`src/*.cpp`, `src/*.h`).
### 11) Packaging & build improvements (Low — 12 hours)
- Verify `build-production.ps1` copies all required DLLs for SDL3 and SDL3_ttf from `vcpkg_installed` paths.
- Add an automated packaging job to CI that creates a ZIP artifact on release tags.
@ -124,6 +138,7 @@ Files: `build-production.ps1`, `.github/workflows/release.yml`.
---
## Suggested incremental PR plan
1. Small safety PR: make frames table `constexpr`, move multipliers into `Game` instance, clamp values, and add SDL_Log usage. (13 hours)
2. Global cleanup PR: remove `extern` textures and ensure all resource access goes through `StateContext`. (12 hours)
3. Add debug knobs & HUD display improvements. (1 hour)
@ -135,6 +150,7 @@ Files: `build-production.ps1`, `.github/workflows/release.yml`.
---
## Recommended quick wins (apply immediately)
- Convert NES frames table to `constexpr` and clamp gravity to >= 1ms.
- Replace `printf` with `SDL_Log` for debug output.
- Pass `SDL_Texture*` via `StateContext` instead of `extern` globals (you already did this; check for leftovers with `grep` for "extern .*backgroundTex").
@ -143,6 +159,7 @@ Files: `build-production.ps1`, `.github/workflows/release.yml`.
---
## Example: gravity computation (reference)
```cpp
// gravity constants
constexpr double NES_FPS = 60.0988;
@ -162,9 +179,11 @@ double GravityManager::msForLevel(int level) const {
---
## Estimated total effort
- Conservative: 1636 hours depending on how far you split `Game` and add tests/CI.
## Next steps I can take for you now
- Create a PR that converts the frames table to `constexpr` and moves multipliers into `Game` instance (small patch + build). (I can do this.)
- Add a unit-test harness using Catch2 and a small GravityManager test.