# aaronalam1987/tetris architecture

> Architecture diagram of the GitHub repository aaronalam1987/tetris, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aaronalam1987/tetris
- Repository: https://github.com/aaronalam1987/tetris

## Overview

##### Project Type and Purpose
- Type: Single‐player desktop game application (Tetris clone).
- Purpose: Real‐time interactive puzzle game with rendering, audio, input handling, game state management, and persistence of high scores.

##### File Structure
- Top‐level code files:
- main.cpp / main.h: application entry point, game loop orchestration
- inputMonitor.*: captures and normalizes player input
- graphics.*: rendering board, blocks, UI (start screen, next‐piece preview, game over)
- audio.*: playing SFX and music via SFML audio module
- highScores.*: reading/writing scores.json using nlohmann::json
- assets/: all multimedia assets (sprites, sound files)
- nlohmann/json.hpp: JSON parsing library for high-score persistence

##### README Insights
- Uses SFML 2.5.1 for windowing, graphics, audio, and event handling.
- Makefile produces statically‐linked executable.
- Controls, features (start screen, dynamic speed, next‐block preview, high scores).

##### Main Components & Interactions
a. Components to include:
1. Application Entry (main) & Game Loop
2. Input Subsystem (InputMonitor)
3. Game Logic Subsystem (PieceManager + Pieces + Global State)
4. Rendering Subsystem (Graphics)
5. Audio Subsystem (Audio)
6. Persistence Subsystem (HighScores + JSON)
7. Asset Store (assets/ folder)
8. External Libraries: SFML (Graphics, Window, Audio, System), nlohmann::json
b. Relationships:
- The Game Loop invokes:
- InputMonitor → collects SFML events → dispatches commands to Game Logic
- Game Logic → updates board state, score, next piece
- Graphics → reads board state, UI state → draws to SFML window
- Audio → plays BGM and SFX based on Game Logic events
- HighScores → on game over, reads/writes scores.json
- Asset Store → loaded by Graphics and Audio at startup
- SFML libs → underlying calls for window creation, drawing, audio playback, event polling
c. Architectural Patterns & Principles:
- Modular separation of concerns: input, logic, rendering, audio, persistence
- Single responsibility per module
- Resource management via central asset folder
- Game loop pattern (init → process input → update → render → repeat)
d. Key Technologies & Libraries:
- C++17 (or later)
- SFML 2.5.1 (Graphics, Window, Audio, System)
- nlohmann/json for JSON I/O
- Makefile / Visual Studio Code configuration for build

##### Elements
- Clear labels: e.g., “InputMonitor Module,” “PieceManager Module,” “Graphics Module,” etc.
- Input → Game Logic
- Game Logic → Rendering & Audio & Persistence (on game over)
- Rendering/Audio → SFML Library
- Persistence ↔ JSON Library & scores.json
- Graphics/Audio ↔ Asset Store

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Modules
    GameLoop["Game Loop"]:::core
    InputMonitor["InputMonitor Module"]:::core
    PieceManager["PieceManager Module"]:::core
    Pieces["Pieces Module"]:::core
    Graphics["Graphics Module"]:::core
    Audio["Audio Module"]:::core
    HighScores["HighScores Module"]:::core
    GlobalState["Global State"]:::core

    %% Data Stores
    Assets["Asset Store"]:::datastore
    ScoresJSON["scores.json"]:::datastore

    %% External Dependencies
    SFML["SFML Library"]:::external
    JSONLib["nlohmann::json Library"]:::external

    %% Connections
    InputMonitor -->|player events| GameLoop
    GameLoop -->|update state| PieceManager
    PieceManager -->|new board state| GameLoop
    GameLoop -->|render frame| Graphics
    GameLoop -->|play sound| Audio
    GameLoop -->|load/save| HighScores

    Graphics -->|draw calls| SFML
    Audio -->|audio calls| SFML
    InputMonitor -->|event polling| SFML

    HighScores -->|read/write JSON| JSONLib
    HighScores -->|file I/O| ScoresJSON

    Graphics -->|load assets| Assets
    Audio -->|load assets| Assets

    PieceManager -->|state definitions| GlobalState

    %% Click Events
    click GameLoop "https://github.com/aaronalam1987/tetris/blob/main/main.cpp"
    click InputMonitor "https://github.com/aaronalam1987/tetris/blob/main/inputMonitor.cpp"
    click PieceManager "https://github.com/aaronalam1987/tetris/blob/main/pieceManager.cpp"
    click Pieces "https://github.com/aaronalam1987/tetris/blob/main/pieces.cpp"
    click GlobalState "https://github.com/aaronalam1987/tetris/blob/main/global.h"
    click Graphics "https://github.com/aaronalam1987/tetris/blob/main/graphics.cpp"
    click Audio "https://github.com/aaronalam1987/tetris/blob/main/audio.cpp"
    click HighScores "https://github.com/aaronalam1987/tetris/blob/main/highScores.cpp"
    click ScoresJSON "https://github.com/aaronalam1987/tetris/blob/main/scores.json"
    click JSONLib "https://github.com/aaronalam1987/tetris/blob/main/nlohmann/json.hpp"
    click Assets "https://github.com/aaronalam1987/tetris/tree/main/assets/"

    %% Styles
    classDef core fill:#AEDFF7,stroke:#333,stroke-width:1px
    classDef external fill:#F7DFAE,stroke:#333,stroke-width:1px
    classDef datastore fill:#DFF7AE,stroke:#333,stroke-width:1px
```
