# arthurcarrell/craycast architecture

> Architecture diagram of the GitHub repository arthurcarrell/craycast, 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/arthurcarrell/craycast
- Repository: https://github.com/arthurcarrell/craycast

## Overview

1. This is a small SDL3/C raycasting application with two major runtime modes: a 3D-ish “World Mode” for movement and rendering, and a “Map Mode” for editing line geometry. The repository is organized around a lightweight game loop plus a separate map parser/saver pipeline.

2. The core application entrypoint is `src/main.c`, with `src/state.c` and `src/state.h` acting as the high-level mode/state coordinator. From the README and file layout, the state layer likely switches between title, world, and editor behavior rather than embedding that logic in one monolithic loop.

3. Platform and frame presentation are isolated in `src/sdl.c`, `src/framebuf.c`, and related headers. SDL3 is the only major external runtime dependency, so input polling, window creation, and presenting rendered pixels are probably centralized there instead of being spread through gameplay code.

4. Rendering is split into a software-style pipeline: `src/raycast.c` computes the view from the scene, `src/framebuf.c` manages the pixel buffer, `src/textures.c` holds image/texture data, and `src/font.c` supports text drawing. This suggests the engine draws into an internal buffer first and then hands that buffer to SDL for display.

5. World simulation is represented by `src/world.c`, `src/actor.c`, `src/sector.c`, and `src/line.c`. The presence of `actor` and `sector` indicates the scene is not just a flat list of walls; it is structured enough to support moving entities and region-based geometry, even if the README notes sectors/portals are still evolving.

6. Geometry appears to be line-centric. The README says Map Mode creates lines by clicking twice, and the `line` and `sector` modules likely define the map primitives used both for rendering boundaries and for collision/visibility tests. The sample `.map` files in `maps/` are the main scene assets for this geometry layer.

7. Input handling is separated in `src/input.c`, which likely translates keyboard/mouse state into commands consumed by the current state. That matters architecturally because movement in World Mode and point placement in Map Mode are different consumers of the same low-level input stream.

8. Map editing is its own subsystem in `src/editor.c`. Based on the README, it provides interactive line creation and possibly persistence hooks, but it is distinct from the runtime renderer so that editing and play mode can share map data without sharing UI logic.

9. Persistence is handled by `src/map_parser/parse.c` and `src/map_parser/save.c`. This is a clear boundary: map files are not hardcoded into world logic, but loaded/saved through a dedicated parser layer. That makes `.map` files the main interchange format between the editor and the game runtime.

10. `src/title.c` suggests there is a startup or menu state before entering the main modes. Combined with `state.c`, this implies a small state machine architecture: title screen → world mode or map mode, with the current state determining which update/render path is active.

11. `src/utils.c` and the headers around the project likely contain shared math/helpers used across raycasting, map parsing, and rendering. In a repo like this, those utilities are important because they prevent geometry math and file parsing from being duplicated in multiple subsystems.

12. The asset structure is simple: `textures/` holds visual resources, while `maps/` holds example levels and test cases such as portals, loops, corridors, and infinite hallway scenarios. Those map names strongly suggest the engine is being exercised against tricky geometric cases, especially for the raycaster and future portal/sector work.

Main entry points: [`src/world.c`](https://github.com/arthurcarrell/craycast/blob/main/src/world.c) (World), [`src/state.c`](https://github.com/arthurcarrell/craycast/blob/main/src/state.c) (State), [`src/line.c`](https://github.com/arthurcarrell/craycast/blob/main/src/line.c) (Line), [`src/raycast.c`](https://github.com/arthurcarrell/craycast/blob/main/src/raycast.c) (Raycast)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main["Main<br/>entrypoint<br/>[main.c]"]
  node_state["State<br/>mode router<br/>[state.c]"]
  node_title["Title<br/>menu state<br/>[title.c]"]
  node_input["Input<br/>event layer<br/>[input.c]"]
end

subgraph group_presentation["Presentation"]
  node_sdl["SDL<br/>platform layer<br/>[sdl.c]"]
  node_framebuf["Framebuf<br/>pixel buffer<br/>[framebuf.c]"]
  node_raycast["Raycast<br/>renderer<br/>[raycast.c]"]
  node_textures["Textures<br/>assets<br/>[textures.c]"]
  node_font["Font<br/>text renderer<br/>[font.c]"]
end

subgraph group_simulation["Simulation"]
  node_world["World<br/>simulation<br/>[world.c]"]
  node_actor["Actor<br/>entity<br/>[actor.c]"]
  node_sector{{"Sector<br/>region geometry<br/>[sector.c]"}}
  node_line["Line<br/>map primitive<br/>[line.c]"]
  node_utils["Utils<br/>shared helpers<br/>[utils.c]"]
end

subgraph group_editing["Editing"]
  node_editor["Editor<br/>[editor.c]"]
  node_parse["Parse<br/>map loader<br/>[parse.c]"]
  node_save["Save<br/>map writer<br/>[save.c]"]
end

subgraph group_assets["Assets"]
  node_maps[("Maps<br/>scene assets")]
  node_texture_dir[("Textures<br/>asset dir")]
end

node_main -->|"runs"| node_state
node_state -->|"starts at"| node_title
node_state -->|"enters"| node_world
node_state -->|"enters"| node_editor
node_input -->|"feeds"| node_state
node_sdl -->|"polls"| node_input
node_sdl -->|"presents"| node_framebuf
node_world -->|"renders via"| node_raycast
node_raycast -->|"draws into"| node_framebuf
node_textures -->|"provides"| node_raycast
node_font -->|"draws into"| node_framebuf
node_world -->|"updates"| node_actor
node_world -->|"uses"| node_sector
node_world -->|"uses"| node_line
node_actor -->|"collides with"| node_line
node_sector -->|"bounded by"| node_line
node_editor -->|"creates"| node_line
node_editor -->|"writes"| node_save
node_parse -->|"builds"| node_line
node_parse -->|"builds"| node_sector
node_save -->|"stores"| node_maps
node_parse -->|"loads"| node_maps
node_world -->|"shares helpers"| node_utils
node_raycast -->|"shares helpers"| node_utils

click node_main "https://github.com/arthurcarrell/craycast/blob/main/src/main.c"
click node_state "https://github.com/arthurcarrell/craycast/blob/main/src/state.c"
click node_title "https://github.com/arthurcarrell/craycast/blob/main/src/title.c"
click node_input "https://github.com/arthurcarrell/craycast/blob/main/src/input.c"
click node_sdl "https://github.com/arthurcarrell/craycast/blob/main/src/sdl.c"
click node_framebuf "https://github.com/arthurcarrell/craycast/blob/main/src/framebuf.c"
click node_raycast "https://github.com/arthurcarrell/craycast/blob/main/src/raycast.c"
click node_textures "https://github.com/arthurcarrell/craycast/blob/main/src/textures.c"
click node_font "https://github.com/arthurcarrell/craycast/blob/main/src/font.c"
click node_world "https://github.com/arthurcarrell/craycast/blob/main/src/world.c"
click node_actor "https://github.com/arthurcarrell/craycast/blob/main/src/actor.c"
click node_sector "https://github.com/arthurcarrell/craycast/blob/main/src/sector.c"
click node_line "https://github.com/arthurcarrell/craycast/blob/main/src/line.c"
click node_editor "https://github.com/arthurcarrell/craycast/blob/main/src/editor.c"
click node_parse "https://github.com/arthurcarrell/craycast/blob/main/src/map_parser/parse.c"
click node_save "https://github.com/arthurcarrell/craycast/blob/main/src/map_parser/save.c"
click node_utils "https://github.com/arthurcarrell/craycast/blob/main/src/utils.c"
click node_maps "https://github.com/arthurcarrell/craycast/tree/main/maps"
click node_texture_dir "https://github.com/arthurcarrell/craycast/tree/main/textures"

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_state,node_title,node_input toneBlue
class node_sdl,node_framebuf,node_raycast,node_textures,node_font toneAmber
class node_world,node_actor,node_sector,node_line,node_utils toneMint
class node_editor,node_parse,node_save toneRose
class node_maps,node_texture_dir toneIndigo
```

## Components

### Runtime

Application entry, state coordination, and mode switching

- [Main](https://github.com/arthurcarrell/craycast/blob/main/src/main.c): entrypoint · Program startup and top-level loop · `src/main.c`
- [State](https://github.com/arthurcarrell/craycast/blob/main/src/state.c): mode router · High-level state machine for title, world, and editor · `src/state.c`
- [Title](https://github.com/arthurcarrell/craycast/blob/main/src/title.c): menu state · Startup or menu screen before entering a mode · `src/title.c`
- [Input](https://github.com/arthurcarrell/craycast/blob/main/src/input.c): event layer · Keyboard and mouse polling translated into commands · `src/input.c`

### Presentation

SDL platform layer and software rendering pipeline

- [SDL](https://github.com/arthurcarrell/craycast/blob/main/src/sdl.c): platform layer · Windowing, event loop integration, and presenting frames · `src/sdl.c`
- [Framebuf](https://github.com/arthurcarrell/craycast/blob/main/src/framebuf.c): pixel buffer · Internal framebuffer for software rendering · `src/framebuf.c`
- [Raycast](https://github.com/arthurcarrell/craycast/blob/main/src/raycast.c): renderer · Computes the 3D-ish view from world geometry · `src/raycast.c`
- [Textures](https://github.com/arthurcarrell/craycast/blob/main/src/textures.c): assets · Texture and image data used by rendering · `src/textures.c`
- [Font](https://github.com/arthurcarrell/craycast/blob/main/src/font.c): text renderer · Bitmap text drawing for UI and debug output · `src/font.c`

### Simulation

World, geometry, and movement logic

- [World](https://github.com/arthurcarrell/craycast/blob/main/src/world.c): simulation · World update, movement, and scene coordination · `src/world.c`
- [Actor](https://github.com/arthurcarrell/craycast/blob/main/src/actor.c): entity · Moving entities and player-like actors · `src/actor.c`
- [Sector](https://github.com/arthurcarrell/craycast/blob/main/src/sector.c): region geometry · Region-based spatial structure for the scene · `src/sector.c`
- [Line](https://github.com/arthurcarrell/craycast/blob/main/src/line.c): map primitive · Line-segment geometry used for walls and collisions · `src/line.c`
- [Utils](https://github.com/arthurcarrell/craycast/blob/main/src/utils.c): shared helpers · Common math and helper routines reused across subsystems · `src/utils.c`

### Editing

Map authoring and persistence pipeline

- [Editor](https://github.com/arthurcarrell/craycast/blob/main/src/editor.c): map editor · Interactive line creation and map editing mode · `src/editor.c`
- [Parse](https://github.com/arthurcarrell/craycast/blob/main/src/map_parser/parse.c): map loader · Loads .map files into runtime geometry · `src/map_parser/parse.c`
- [Save](https://github.com/arthurcarrell/craycast/blob/main/src/map_parser/save.c): map writer · Serializes edited maps back to disk · `src/map_parser/save.c`

### Assets

Sample maps and texture resources

- [Maps](https://github.com/arthurcarrell/craycast/tree/main/maps): scene assets · Example and test map files for gameplay and editor · `maps`
- [Textures](https://github.com/arthurcarrell/craycast/tree/main/textures): asset dir · Texture resource directory · `textures`

## Connections

- Main → State: runs · Main initializes and drives the active state
- State → Title: starts at · Initial menu/title flow before other modes
- State → World: enters · Switches into gameplay/world mode
- State → Editor: enters · Switches into map editing mode
- Input → State: feeds · Low-level input is consumed by the current mode
- SDL → Input: polls · SDL provides keyboard and mouse events
- SDL → Framebuf: presents · Final pixel buffer is displayed through SDL
- World → Raycast: renders via · World state is converted into a view by raycasting
- Raycast → Framebuf: draws into · Renderer outputs into the internal framebuffer
- Textures → Raycast: provides · Texture data is sampled during rendering
- Font → Framebuf: draws into · Text is composited into the framebuffer
- World → Actor: updates · World simulation advances actors and movement
- World → Sector: uses · World logic relies on region geometry
- World → Line: uses · Geometry and collision boundaries are line-centric
- Actor → Line: collides with · Actors interact with map lines
- Sector → Line: bounded by · Sectors are defined from line geometry
- Editor → Line: creates · Map mode builds line geometry interactively
- Editor → Save: writes · Edited geometry is persisted through the save pipeline
- Parse → Line: builds · Parser turns map files into line geometry
- Parse → Sector: builds · Parser reconstructs sector structure when present
- Save → Maps: stores · Saved maps land in the scene asset directory
- Parse → Maps: loads · Runtime and editor consume .map scene files
- World → Utils: shares helpers · Shared math and helper routines support simulation
- Raycast → Utils: shares helpers · Rendering math uses common utilities
