# gbalatro/balatro-gba architecture

> Architecture diagram of the GitHub repository gbalatro/balatro-gba, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gbalatro/balatro-gba
- Repository: https://github.com/gbalatro/balatro-gba

## Overview

1. **What this repo is**
   - A self-contained Game Boy Advance homebrew ROM: a demake of Balatro.
   - There is no backend, network layer, or persistent service dependency; the whole app runs on the GBA runtime and local assets.

2. **Build and delivery boundary**
   - The project is built with `make` using the devkitPro/devkitARM toolchain, with Docker and GitHub Actions used to standardize builds.
   - The final artifact is a `.gba` ROM in `build/`.
   - CI also runs formatting, unit tests, and Doxygen docs, and there are release workflows for tagged/nightly builds.

3. **Program entry and top-level control**
   - `source/main.c` is the entrypoint.
   - The runtime is organized as a game loop/state-machine architecture centered around `game.c` and `include/game.h`, with state metadata in `def_state_info_table.h`.
   - `splash_screen.c` handles startup before the main game flow.

4. **Core game domain**
   - The gameplay model is split into focused domain modules:
     - `card.c` for playing cards and card state
     - `blind.c` for round/blind progression
     - `joker.c` and `joker_effects.c` for joker inventory and effect resolution
     - `hand_analysis.c` for evaluating poker hands and scoring logic
     - `game_variables.h` and related headers for global game state/configuration
   - This is the main simulation layer that drives round/shop progression and score calculation.

5. **Input and selection/UI flow**
   - Player input comes from GBA buttons and is handled through UI-oriented modules rather than a generic event system.
   - `button.c` abstracts button behavior, while `selection_grid.c` and `include/selection_grid.h` manage focus, cursor movement, and selection across cards/jokers/menu items.
   - `source/game/common_ui.c` and its header provide shared UI behavior used across shop/round screens.
   - The README’s controls map directly onto these modules: D-pad navigation, A/B selection, L/R actions, and hold-A swap behavior.

6. **Rendering pipeline**
   - Rendering is split into reusable graphics primitives and screen-specific assets:
     - `sprite.c`, `graphic_utils.c`, and `affine_background.c` provide low-level drawing helpers
     - `font.c` manages text rendering
     - Backgrounds and cards/jokers are stored as preprocessed assets under `graphics/`
   - The `.grit` files indicate assets are converted into GBA-friendly sprite/tile data during the build.
   - `def_joker_gfx_table.h` and `joker_gfx.h` suggest table-driven mapping from joker IDs to art assets.

7. **Audio subsystem**
   - `audio_utils.c` is the audio runtime boundary.
   - Audio assets live in `audio/` and include short WAV effects plus the `main_theme.xm` tracker music.
   - Sound effects and music are treated as local ROM assets, likely streamed or played through the GBA audio hardware via helper code rather than an external middleware stack.

8. **Shared utilities and data structures**
   - The repo includes custom low-level utilities instead of relying on libc-heavy abstractions:
     - `bitset.c` / `bitset.h`
     - `list.c` / `list.h`
     - `pool.c` / `pool.h`
     - `util.c`
   - `def_balatro_mempool.h` and the test-specific mempool headers indicate a custom fixed-memory allocation strategy, which is important on GBA hardware with tight RAM limits.

9. **Game object composition**
   - Game entities are structured around reusable types in headers such as `card.h`, `joker.h`, `blind.h`, `button.h`, and `sprite.h`.
   - These are likely composed by the higher-level game state rather than managed by a heavyweight engine.
   - The architecture is object-like in C, but still hardware-oriented and memory-conscious.

10. **Asset and code generation pipeline**
   - `scripts/generate_font.py` suggests font assets are generated rather than hand-authored.
   - `scripts/get_hash.py`, `scripts/get_memory_map.sh`, and `scripts/save_build.sh` support build metadata, reproducibility, and artifact handling.
   - The `graphics/` and `audio/` folders are source assets; the build transforms them into ROM-ready data.

11. **Tests and quality boundaries**
   - Tests are separate, small, module-level executables under `tests/`.
   - Current coverage focuses on foundational utilities: `bitset`, `list`, `pool`, and `util`.
   - That matches the repo’s architecture: verify core data structures independently of the full GBA runtime.

12. **Documentation and developer tooling**
   - Doxygen is a first-class part of the repo, with custom styling under `doxygen/` and an automated doc deployment workflow.
   - Formatting is enforced via clang-format CI.
   - The repository is therefore structured to support both embedded build correctness and readable API documentation.

13. **Practical diagram shape**
   - A good architecture diagram would show:
     - **Input**: GBA buttons
     - **Game logic**: state machine in `game.c`, domain rules in card/joker/blind/hand-analysis modules
     - **UI/rendering**: selection grid, common UI, sprites, backgrounds, font
     - **Audio**: effect/music playback helpers
     - **Assets**: compiled graphics/audio/font data feeding render/audio subsystems
     - **Support systems**: pools/lists/bitsets/utilities underneath game logic
     - **Build/test/docs**: Makefile, Docker, CI, unit tests, Doxygen as external development infrastructure

Main entry points: [`source/game.c`](https://github.com/gbalatro/balatro-gba/blob/main/source/game.c) (Game Loop), [`source/main.c`](https://github.com/gbalatro/balatro-gba/blob/main/source/main.c) (Main), [`source/joker.c`](https://github.com/gbalatro/balatro-gba/blob/main/source/joker.c) (Jokers), [`tests/run_tests.sh`](https://github.com/gbalatro/balatro-gba/blob/main/tests/run_tests.sh) (Unit Tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Game Runtime"]
  node_main["Main<br/>entrypoint<br/>[main.c]"]
  node_game["Game Loop<br/>state machine<br/>[game.c]"]
  node_state_info["State Table<br/>state metadata"]
  node_splash["Splash Screen<br/>startup ui<br/>[splash_screen.c]"]
  node_input["Button Input<br/>input layer<br/>[button.c]"]
  node_selection["Selection Grid<br/>cursor ui<br/>[selection_grid.c]"]
  node_common_ui["Common UI<br/>shared ui<br/>[common_ui.c]"]
  node_cards["Cards<br/>domain model<br/>[card.c]"]
  node_blinds["Blinds<br/>progression<br/>[blind.c]"]
  node_jokers["Jokers<br/>inventory model<br/>[joker.c]"]
  node_joker_fx["Joker Effects<br/>rules engine<br/>[joker_effects.c]"]
  node_hands["Hand Analysis<br/>scoring<br/>[hand_analysis.c]"]
end

subgraph group_assets["Assets"]
  node_gfx["Graphics<br/>render helpers<br/>[sprite.c]"]
  node_font["Font<br/>text rendering<br/>[font.c]"]
  node_audio["Audio<br/>sound runtime<br/>[audio_utils.c]"]
end

subgraph group_support["Core Support"]
  node_support["Utilities<br/>data structures<br/>[util.c]"]
end

subgraph group_tooling["Build & Quality"]
  node_build["Build System<br/>tooling"]
  node_tests["Unit Tests<br/>verification<br/>[run_tests.sh]"]
  node_docs_ci["CI & Docs<br/>automation"]
end

node_main -->|"starts"| node_game
node_main -->|"shows"| node_splash
node_game -->|"uses"| node_state_info
node_game -->|"reads"| node_input
node_game -->|"drives"| node_selection
node_game -->|"shares"| node_common_ui
node_game -->|"updates"| node_cards
node_game -->|"advances"| node_blinds
node_game -->|"manages"| node_jokers
node_jokers -->|"triggers"| node_joker_fx
node_game -->|"scores"| node_hands
node_game -->|"renders"| node_gfx
node_game -->|"prints"| node_font
node_game -->|"plays"| node_audio
node_game -->|"depends on"| node_support
node_build -->|"automates"| node_docs_ci
node_tests -->|"verifies"| node_support
node_docs_ci -->|"runs"| node_tests

click node_main "https://github.com/gbalatro/balatro-gba/blob/main/source/main.c"
click node_game "https://github.com/gbalatro/balatro-gba/blob/main/source/game.c"
click node_state_info "https://github.com/gbalatro/balatro-gba/blob/main/include/def_state_info_table.h"
click node_splash "https://github.com/gbalatro/balatro-gba/blob/main/source/splash_screen.c"
click node_input "https://github.com/gbalatro/balatro-gba/blob/main/source/button.c"
click node_selection "https://github.com/gbalatro/balatro-gba/blob/main/source/selection_grid.c"
click node_common_ui "https://github.com/gbalatro/balatro-gba/blob/main/source/game/common_ui.c"
click node_cards "https://github.com/gbalatro/balatro-gba/blob/main/source/card.c"
click node_blinds "https://github.com/gbalatro/balatro-gba/blob/main/source/blind.c"
click node_jokers "https://github.com/gbalatro/balatro-gba/blob/main/source/joker.c"
click node_joker_fx "https://github.com/gbalatro/balatro-gba/blob/main/source/joker_effects.c"
click node_hands "https://github.com/gbalatro/balatro-gba/blob/main/source/hand_analysis.c"
click node_gfx "https://github.com/gbalatro/balatro-gba/blob/main/source/sprite.c"
click node_font "https://github.com/gbalatro/balatro-gba/blob/main/source/font.c"
click node_audio "https://github.com/gbalatro/balatro-gba/blob/main/source/audio_utils.c"
click node_support "https://github.com/gbalatro/balatro-gba/blob/main/source/util.c"
click node_build "https://github.com/gbalatro/balatro-gba/tree/main/Makefile"
click node_tests "https://github.com/gbalatro/balatro-gba/blob/main/tests/run_tests.sh"
click node_docs_ci "https://github.com/gbalatro/balatro-gba/blob/main/.github/workflows/build_ci_workflow.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_game,node_state_info,node_splash,node_input,node_selection,node_common_ui,node_cards,node_blinds,node_jokers,node_joker_fx,node_hands toneBlue
class node_gfx,node_font,node_audio toneAmber
class node_support toneMint
class node_build,node_tests,node_docs_ci toneRose
```

## Components

### Game Runtime

GBA entrypoint, state machine, core gameplay, input, UI, and rendering.

- [Main](https://github.com/gbalatro/balatro-gba/blob/main/source/main.c): entrypoint · ROM entry and startup handoff into the game flow. · `source/main.c`
- [Game Loop](https://github.com/gbalatro/balatro-gba/blob/main/source/game.c): state machine · Top-level runtime control and state transitions. · `source/game.c`
- [State Table](https://github.com/gbalatro/balatro-gba/blob/main/include/def_state_info_table.h): state metadata · Maps game states to their handlers and metadata. · `include/def_state_info_table.h`
- [Splash Screen](https://github.com/gbalatro/balatro-gba/blob/main/source/splash_screen.c): startup ui · Startup presentation before entering the main game loop. · `source/splash_screen.c`
- [Button Input](https://github.com/gbalatro/balatro-gba/blob/main/source/button.c): input layer · Button behavior and action handling for GBA controls. · `source/button.c`
- [Selection Grid](https://github.com/gbalatro/balatro-gba/blob/main/source/selection_grid.c): cursor ui · Focus, movement, and selection across cards and menu items. · `source/selection_grid.c`
- [Common UI](https://github.com/gbalatro/balatro-gba/blob/main/source/game/common_ui.c): shared ui · Reusable UI behavior shared across shop and round screens. · `source/game/common_ui.c`
- [Cards](https://github.com/gbalatro/balatro-gba/blob/main/source/card.c): domain model · Playing card state and card-level behavior. · `source/card.c`
- [Blinds](https://github.com/gbalatro/balatro-gba/blob/main/source/blind.c): progression · Round and blind progression logic. · `source/blind.c`
- [Jokers](https://github.com/gbalatro/balatro-gba/blob/main/source/joker.c): inventory model · Joker collection and joker state management. · `source/joker.c`
- [Joker Effects](https://github.com/gbalatro/balatro-gba/blob/main/source/joker_effects.c): rules engine · Resolves joker-triggered effects during scoring and play. · `source/joker_effects.c`
- [Hand Analysis](https://github.com/gbalatro/balatro-gba/blob/main/source/hand_analysis.c): scoring · Evaluates poker hands and computes score logic. · `source/hand_analysis.c`

### Assets

Source art, audio, and generated font data packed into the ROM.

- [Graphics](https://github.com/gbalatro/balatro-gba/blob/main/source/sprite.c): render helpers · Sprite, tile, affine background, and drawing helpers. · `source/sprite.c`
- [Font](https://github.com/gbalatro/balatro-gba/blob/main/source/font.c): text rendering · Text rendering and generated font data integration. · `source/font.c`
- [Audio](https://github.com/gbalatro/balatro-gba/blob/main/source/audio_utils.c): sound runtime · GBA audio boundary for effects and tracker music playback. · `source/audio_utils.c`

### Core Support

Shared utilities and memory structures used by the simulation and UI.

- [Utilities](https://github.com/gbalatro/balatro-gba/blob/main/source/util.c): data structures · Bitsets, lists, pools, and utility helpers for memory-conscious code. · `source/util.c`

### Build & Quality

Build, CI, tests, docs, and release automation around the ROM.

- [Build System](https://github.com/gbalatro/balatro-gba/tree/main/Makefile): tooling · Make/Docker-based ROM build and release packaging. · `Makefile`
- [Unit Tests](https://github.com/gbalatro/balatro-gba/blob/main/tests/run_tests.sh): verification · Module-level tests for core data structures and utilities. · `tests/run_tests.sh`
- [CI & Docs](https://github.com/gbalatro/balatro-gba/blob/main/.github/workflows/build_ci_workflow.yml): automation · GitHub Actions, clang-format, Doxygen, and release workflows. · `.github/workflows/build_ci_workflow.yml`

## Connections

- Main → Game Loop: starts · Program entry hands off to the main runtime.
- Main → Splash Screen: shows · Startup flow enters the splash screen before gameplay.
- Game Loop → State Table: uses · State machine consults state metadata for dispatch.
- Game Loop → Button Input: reads · Main loop consumes button actions.
- Game Loop → Selection Grid: drives · Gameplay UI uses cursor and focus navigation.
- Game Loop → Common UI: shares · Screens reuse common UI behaviors.
- Game Loop → Cards: updates · Game flow mutates card state during play.
- Game Loop → Blinds: advances · Round progression is controlled through blind logic.
- Game Loop → Jokers: manages · Game state owns joker inventory.
- Jokers → Joker Effects: triggers · Joker inventory feeds effect resolution.
- Game Loop → Hand Analysis: scores · Scoring and hand evaluation drive outcomes.
- Game Loop → Graphics: renders · Gameplay and UI draw through the graphics helpers.
- Game Loop → Font: prints · Screens render text via the font subsystem.
- Game Loop → Audio: plays · Events trigger effects and music playback.
- Game Loop → Utilities: depends on · Core runtime relies on shared low-level utilities.
- Build System → CI & Docs: automates · Build tooling is exercised and packaged by CI and docs workflows.
- Unit Tests → Utilities: verifies · Tests cover foundational utility modules.
- CI & Docs → Unit Tests: runs · CI includes unit testing as part of quality checks.
