# robloach/node-raylib architecture

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

- Interactive diagram: https://gitdiagram.com/robloach/node-raylib
- Repository: https://github.com/robloach/node-raylib

## Overview

##### Project Type and Purpose
- Project Type: This is a Node.js native binding module that connects Node.js to the raylib C library for game development. It uses node-addon-api for interfacing with native raylib functions.
- Purpose: It offers game developers the ability to build videogame applications with raylib through Node.js by exposing raylib’s API via JavaScript and TypeScript wrappers.

##### Major Components from the File Structure
- Native Binding Layer:
- Directories/Files: The “src/generated” folder contains generated native binding code (e.g., node-raylib.cc, node-raylib.js, node-raylib-drm.js, node-raylib.d.ts).
- Build system files such as CMakeLists.txt along with node-addon-api integration.
- Code Generation Tools:
- “tools/generate_templates” holds scripts that generate binding wrappers, definitions, and converters (e.g., node-raylib-bindings.js, node-raylib-definitions.js, node-raylib-wrapper.js).
- “tools/generate.js” may orchestrate parts of the generation process.
- Command-Line Interface (CLI):
- The “bin/node-raylib” script serves as the CLI tool to run node-raylib files directly.
- Documentation and Examples:
- “docs” directory includes detailed information on API usage, binding details, and code generation.
- “examples” provide sample usage across different raylib domains (core, audio, models, shaders, etc.).
- DRM Specific Module:
- The “drm” folder holds a specialized entry point (drm/index.js) for using raylib in DRM mode (targeting ARM devices like Raspberry Pi).
- Testing and Templates:
- “test” directory contains test suites.
- “templates” houses setup for sample projects (simple_game and typescript_game), showing how new projects can be scaffolded.
- Configuration, Build, and Linting:
- Files like .clang-format, .editorconfig, .npmrc, package.json, CMakeLists.txt, etc., are key for code quality, building, and packaging.

##### Important Relationships and Interactions
- The build process (via CMake and npm scripts) compiles the native binding code (from src/generated) into a Node.js addon.
- The code generation tools (in tools/generate_templates) use raylib’s API metadata (from a parsed JSON version of raylib headers) to produce wrapper and type definitions, which are then placed into the generated folder.
- The CLI (bin/node-raylib) and the examples (in various examples subdirectories) serve as consumers of the binding library, demonstrating how to initialize a window, render frames, load resources, etc.
- In DRM mode, alternate code paths are provided (drm/index.js) to switch rendering from X11 or window-based to direct DRM access on specific devices.

##### Architectural Patterns and Design Principles
- Layered architecture separating:
- Native binding (C/C++ code interfaced via node-addon-api)
- Code generation abstraction tools (runtime/script-based generation of bindings)
- Consumer/usage interface (CLI, examples, and templates)
- Clear separation between build time (CMake, code generation) and runtime components.
- Emphasis on automated code generation and testing to ensure bindings remain in sync with the underlying raylib API.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Dependencies
    K(("External: node-addon-api, raylib, Node.js")):::external

    %% Code Generation Layer
    subgraph "Code Generation Layer"
        A["tools/generate_templates: Binding Generators"]:::codegen
        B["tools/generate.js: Generation Orchestrator"]:::codegen
    end

    %% Native Bindings Layer
    subgraph "Native Bindings Layer"
        C["src/generated: Native Bindings"]:::native
        G["drm/index.js: DRM Module"]:::native
    end

    %% Build & Packaging / Configuration Layer
    subgraph "Build & Packaging / Config"
        D["Build & Packaging / Config (CMakeLists.txt, package.json, .clang-format, .editorconfig, .npmrc)"]:::build
    end

    %% Application and Consumer Layer
    subgraph "Application & Consumer Layer"
        E["bin/node-raylib: CLI Tool"]:::cli
        F["examples: Consumer Examples"]:::cli
    end

    %% Auxiliary Modules
    subgraph "Auxiliary Modules"
        H["docs: Documentation"]:::build
        I["test: Testing Suite"]:::build
        J["templates: Project Templates"]:::build
    end

    %% Connections
    A -->|"generates"| C
    B -->|"orchestrates"| C
    C -->|"compiled_by"| D
    D -->|"builds"| E
    E -->|"consumes"| F
    C -->|"alternate_path"| G
    K ---|"dependency"| C
    K ---|"runs_on"| E
    D -->|"includes"| H
    D -->|"includes"| I
    D -->|"includes"| J

    %% Click Events
    click A "https://github.com/robloach/node-raylib/tree/master/tools/generate_templates"
    click B "https://github.com/robloach/node-raylib/blob/master/tools/generate.js"
    click C "https://github.com/robloach/node-raylib/tree/master/src/generated"
    click D "https://github.com/robloach/node-raylib/blob/master/CMakeLists.txt"
    click E "https://github.com/robloach/node-raylib/tree/master/bin/node-raylib"
    click F "https://github.com/robloach/node-raylib/tree/master/examples"
    click G "https://github.com/robloach/node-raylib/blob/master/drm/index.js"
    click H "https://github.com/robloach/node-raylib/tree/master/docs"
    click I "https://github.com/robloach/node-raylib/tree/master/test"
    click J "https://github.com/robloach/node-raylib/tree/master/templates"

    %% Styles
    classDef native fill:#ADD8E6,stroke:#00008B,stroke-width:2px;
    classDef codegen fill:#90EE90,stroke:#006400,stroke-width:2px;
    classDef cli fill:#FFA500,stroke:#FF8C00,stroke-width:2px;
    classDef build fill:#D3D3D3,stroke:#696969,stroke-width:2px;
    classDef external fill:#FFFF99,stroke:#DAA520,stroke-width:2px;
```
