# ading2210/doompdf architecture

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

## Overview

##### Project Type and Overall Purpose
- The project is a source port of the classic Doom game designed to run within a PDF file via embedded JavaScript.
- Its main challenges are compiling C code (using Emscripten targeting asm.js) and interfacing with the very limited IO provided by PDF’s JavaScript, particularly for rendering output (using rows of text fields for the framebuffer) and processing user inputs.

##### Key Components/Modules Identified from the File Structure
A. Core Game Engine (doomgeneric directory):
- Contains all the C source files for the Doom engine such as game logic (d_main.c, d_loop.c, etc.), graphics rendering (r_draw.c, r_main.c, etc.), sound and input (i_input.c, i_video.c, i_sound.c), and many utility and subsystem files.
- Multiple Makefiles (Makefile.emscripten, Makefile.sdl, and others) indicate support for different build targets/platforms.

B. Build and Compilation System:
- build.sh: Script that automates the build process on Linux. This downloads the older Emscripten version and compiles the engine (targeting asm.js for PDF compatibility).
- Python scripts (embed_file.py, generate.py): Likely help with embedding assets (WAD files) into the PDF or generating necessary code/configuration dynamically.
- requirements.txt: Lists Python dependencies needed, ensuring a reproducible build environment.

C. Web and Deployment Component:
- The “web” directory (with index.html, index.js, index.css) is used to serve and possibly demonstrate the PDF. This might be for hosting a demo or interfacing with the user when custom WADs are inserted via the web interface.

D. PDF-Specific Components:
- The README explains how JavaScript is used inside the PDF. Although not directly present in separate source files, the “pre.js” file and aspects embedded with the C build output contribute to the PDF’s JS that drives the input and text-field based framebuffer.

##### Relationships and Data Flow
- The Build Process:
- The build.sh script orchestrates the compilation. It calls the appropriate Makefiles (from the doomgeneric directory) to compile the C source using Emscripten.
- The generated asm.js output is then embedded into the PDF. Python scripts (embed_file.py, generate.py) possibly handle the injection of compiled code and optional custom WAD files.
- The PDF Execution Environment:
- Once the PDF is created, its embedded JavaScript (loaded from pre.js and code injected during build) interprets user inputs via form fields (buttons for keys) and updates the display by rewriting text fields (representing row pixels of the game framebuffer).
- The Web Interface:
- The web folder is used to serve the final PDF via a simple HTTP server, providing a user interface particularly for loading custom WADs, as referenced in the README.

##### Final Checklist
- Major interactions, showing how the game is compiled, embedded within the PDF, and then run inside a PDF viewer with interactive inputs.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Build and Compilation Tools
    subgraph "Build and Compilation Tools"
        B1("build.sh"):::build
        B2("embed_file.py"):::build
        B3("generate.py"):::build
        B4("Makefile (emscripten)"):::build
    end

    %% Core Game Engine
    subgraph "Core Game Engine"
        G1("Doom Core (doomgeneric)"):::engine
    end

    %% PDF Integration Layer
    subgraph "PDF Integration Layer"
        P1("pre.js"):::pdf
        P2("Makefile (pdfjs)"):::pdf
    end

    %% Web Interface
    subgraph "Web Interface"
        W1("Web Interface (web)"):::web
    end

    %% Relationships and Data Flows
    B1 -->|"Emscripten Compilation"| G1
    B4 -->|"Embedding asm.js & WADs"| P1
    P1 -->|"User Inputs via PDF JS"| G1
    G1 -->|"Display Updates"| P1
    W1 -->|"ServePDF"| P1

    %% Click Events
    click B1 "https://github.com/ading2210/doompdf/blob/master/build.sh"
    click B2 "https://github.com/ading2210/doompdf/blob/master/embed_file.py"
    click B3 "https://github.com/ading2210/doompdf/blob/master/generate.py"
    click B4 "https://github.com/ading2210/doompdf/blob/master/doomgeneric/Makefile.emscripten"
    click G1 "https://github.com/ading2210/doompdf/tree/master/doomgeneric/"
    click P1 "https://github.com/ading2210/doompdf/blob/master/pre.js"
    click P2 "https://github.com/ading2210/doompdf/blob/master/doomgeneric/Makefile.pdfjs"
    click W1 "https://github.com/ading2210/doompdf/tree/master/web/"

    %% Styles
    classDef build fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef engine fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef pdf fill:#98fb98,stroke:#000,stroke-width:2px;
    classDef web fill:#ffcc99,stroke:#000,stroke-width:2px;
```
