How cieria/rusty-tetris works
Project Type and Purpose
• This is a Rust-based standalone game application (a Tetris clone) that uses SDL2 for rendering and input.
• It’s organized as a Cargo workspace of multiple crates (libraries) plus one binary crate (tetris).
Key Components
A. Core Math & Data Structures
• vecmath crate: 2D coordinate and rotation utilities.
• matrix crate: grid/matrix operations (collision, line clears, etc.).
B. Game Logic & Engine
• game crate: overall game state, rules, scoring, soft/hard drop mechanics.
• engine crate: defines a public interface to drive the game loop (frame timing, step/update).
C. Application Layer (Binary)
• tetris crate: the executable that ties everything together.
– Initializes SDL2 & SDL2_ttf
– Handles input (keyboard events)
– Invokes engine to update game state each frame
– Calls into matrix & pieces via game
– Renders current game state to the screen
D. External Services & Libraries
• SDL2 (graphics + input)
• SDL2_ttf (text rendering)
• rand crate (random piece generation)
Interactions and Data Flow
• At startup, tetris initializes external libraries (SDL2, SDL2_ttf).
• The main loop in tetris invokes the engine interface each tick:
→ Engine calls into game crate to advance game state.
→ game uses pieces (to spawn/rotate tetrominoes), matrix (to test collisions, clear lines), and vecmath (for coordinate math).
→ game returns updated state (grid contents, active piece, score).
• Back in tetris, the updated state is rendered via SDL2 calls.
• Keyboard events captured by SDL2 are mapped to game actions (move left/right, rotate, drop).
Architectural Patterns & Design Principles
• Layered architecture: low-level math → domain logic → engine interface → application UI/IO.
• Separation of concerns:
• Crates at the bottom only compute geometry/matrices.
• Game rules separated in game.
• I/O and rendering isolated in tetris.
• Dependency inversion in engine: the binary drives the engine, not the other way around.