# dragon01999/tetris architecture

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

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

## Overview

##### Project Type and Purpose
- This is a single‐process, terminal‐based Tetris clone written in C.
- It uses ncurses for rendering game state in the console and optionally SDL2/SDL2_mixer for background music.
- The game follows a classic game loop: initialize → input → update game logic → render → repeat until exit.

##### File Structure Analysis
Top-level modules (each pair of .c/.h):
- main.c            – Entry point; sets up subsystems and runs the game loop
- game_logic.c / game_logic.h – Core Tetris logic (piece generation, collision, line clearing, scoring)
- render.c / render.h – Draws the current state to the terminal via ncurses
- score.c / score.h – Tracks score, level, lines cleared
- music/            – Contains an MP3 file; module in Makefile toggles SDL2 mixer playback if available
- Makefile          – Build rules, conditional SDL2 flags
- img/              – Screenshot for README (not part of runtime)

##### README Insights
- Describes dependencies (ncurses assumed present, SDL2 optional).
- Outlines build (`make`) and run (`./tetris`) steps.

1. User / Terminal
2. Input Module
3. Game Logic Module
4. Render Module (ncurses)
5. Score Module
6. Music Module (SDL2/SDL2_mixer, optional)
7. Makefile / Build System (external to runtime; show on side)
- Terminal → Input Module: raw key events
- Input Module → Game Logic: normalized commands (move, rotate, drop, pause, quit)
- Game Logic → Score Module: line‐clear events
- Game Logic → Render Module: current board and piece positions each frame
- Render Module → Terminal: drawing updated frame
- Game Logic → Music Module: start / stop / pause background music (optional)
- Music Module → Audio Subsystem: streaming MP3
C. Architectural Patterns & Principles:
- Modular separation of concerns (I/O, logic, presentation, scoring, audio)
- Single main event loop coordinating modules
- Build‐time feature flag for SDL2 integration
D. Key Technologies & Dependencies (annotate near relevant components):
- ncurses (Render)
- SDL2 & SDL2_mixer (Music, optional)
- Standard C library (core modules)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External systems
    Terminal[/Terminal/]:::external
    Audio[(Audio Subsystem)]:::external

    %% Core Modules
    Main["Main Module"]:::core
    IM["Input Module"]:::core
    GL["Game Logic Module"]:::core
    RM["Render Module"]:::core
    SM["Score Module"]:::core

    %% Optional Module
    MuM["Music Module (optional)"]:::optional

    %% Build System & Docs
    subgraph "Build System"
        Make["Makefile"]:::build
        README["README.md"]:::build
        IMG["img/"]:::build
    end

    %% Game Loop
    subgraph "Game Loop"
        Main --> IM
        IM -->|"normalized commands"| GL
        GL -->|"state/frame"| RM
        RM -->|"draw frame"| Terminal
        GL -->|"line-clear events"| SM
        GL -->|"music control"| MuM
        MuM -->|"stream MP3"| Audio
    end

    %% Makefile conditional to Music Module
    Make -->|"With SDL2?"| MuM
    Make -.->|"Without SDL2?"| MuM

    %% Click Events
    click Main "https://github.com/dragon01999/tetris/blob/main/main.c"
    click IM "https://github.com/dragon01999/tetris/blob/main/input.c"
    click GL "https://github.com/dragon01999/tetris/blob/main/game_logic.c"
    click RM "https://github.com/dragon01999/tetris/blob/main/render.c"
    click SM "https://github.com/dragon01999/tetris/blob/main/score.c"
    click MuM "https://github.com/dragon01999/tetris/blob/main/music/tetris-1.mp3"
    click Make "https://github.com/dragon01999/tetris/tree/main/Makefile"
    click README "https://github.com/dragon01999/tetris/blob/main/README.md"
    click IMG "https://github.com/dragon01999/tetris/tree/main/img/"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085;
    classDef optional fill:#d4edda,stroke:#155724,stroke-dasharray: 5 5;
    classDef build fill:#e2e3e5,stroke:#6c757d;
    classDef external fill:#fff3cd,stroke:#856404;
```
