# janiorca/tinywin architecture

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

- Interactive diagram: https://gitdiagram.com/janiorca/tinywin
- Repository: https://github.com/janiorca/tinywin

## Overview

##### Project type and purpose
- This is a pair of minimal Win32 applications written in Rust using no_std, designed to demonstrate:
- miniwin: raw Win32 window creation and message loop
- miniwinGL: same plus OpenGL context setup, shader pipeline, and quad rendering
- Primary goal: produce tiny executables (4K intro style) without Rust’s standard library, leveraging Windows API and optionally OpenGL.

##### File structure
- Top–level
- LICENSE, README.md
- miniwin/
– Cargo.toml, xargo.toml → custom build toolchain for no_std
– src/main.rs → entry point, Win32 API bindings via extern “system”
- miniwinGL/
– Cargo.toml, xargo.toml
– src/main.rs → window + GL context init + render loop
– src/gl.rs → raw OpenGL FFI bindings (wgl*, gl*)
– src/gl_util.rs → helper functions for shader compilation, program linking
– readme.md → build instructions
- tools/
– crinkler.exe → binary packer/compressor for final binary size reduction

##### README for additional insights
- miniwin: “very small but functional Win32 apps in Rust using no_std”
- miniwinGL: “expanded version… modern OpenGL rendering context and draws a quad with vertex + fragment shader”
- Emphasis on manual API calls, minimal runtime, extreme binary size optimization.

a. Main components
1) Rust no_std runtime (miniwin and miniwinGL crates)
2) Win32 API subsystem (user32.dll, kernel32.dll calls)
3) Window management (RegisterClassEx, CreateWindowEx, message loop)
4) In miniwinGL only: OpenGL subsystem (wgl interface for context creation + core gl functions)
5) Shader loader/manager (vertex & fragment shaders)
6) Tools (Crinkler compressor for post-build optimization)
b. Relationships and interactions
- Application entry (main.rs) calls into no_std runtime stubs → invokes Win32 API to register window class → creates window → enters message loop
- In miniwinGL, after window creation, hand off to wglCreateContext → make current → call gl_util to compile/link shaders → GPU pipeline draws geometry → swap buffers via wglSwapLayerBuffers or SwapBuffers
- Crinkler acts externally after build, compresses final .exe
c. Architectural patterns and design principles
- Minimal layering: direct FFI boundary between Rust code and native OS/GPU libraries
- Separation of concerns: window setup vs. OpenGL context vs. shader utilities
- No dynamic allocation from standard library—static buffers and stack usage only
- Build-time toolchain (xargo) for custom target and no_std environment
d. Key technologies/frameworks
- Rust (no_std, custom panic handler)
- Xargo for building Rust core library only
- Windows API (user32.dll, gdi32.dll, kernel32.dll)
- WGL / OpenGL (core profile)
- Crinkler for binary packing

##### Detail without overcomplicating
- Module (main.rs, gl.rs, gl_util.rs) as its own component.
- Do not omit the build-time chain (xargo → Cargo → Crinkler).
- runtime vs. build‐time
- OS boundary vs. Rust code boundary
- window management vs. rendering pipeline

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Build-Time Pipeline
    subgraph "Build-Time Pipeline"
        README["README.md"]:::build
        CargoMiniwin["miniwin/Cargo.toml"]:::build
        XargoMiniwin["miniwin/xargo.toml"]:::build
        CargoMiniwinGL["miniwinGL/Cargo.toml"]:::build
        XargoMiniwinGL["miniwinGL/xargo.toml"]:::build
        BuildExeMiniwin["miniwin.exe"]:::build
        BuildExeMiniwinGL["miniwinGL.exe"]:::build
        Crinkler["Crinkler"]:::tool
        FinalBinary["Compressed .exe"]:::tool

        README --> CargoMiniwin
        README --> CargoMiniwinGL
        CargoMiniwin --> BuildExeMiniwin
        XargoMiniwin --> BuildExeMiniwin
        CargoMiniwinGL --> BuildExeMiniwinGL
        XargoMiniwinGL --> BuildExeMiniwinGL
        BuildExeMiniwin --> Crinkler
        BuildExeMiniwinGL --> Crinkler
        Crinkler --> FinalBinary
    end

    %% Runtime for miniwin
    subgraph "Runtime: miniwin"
        RustMiniwin["Rust no_std runtime"]:::rustcode
        Win32API["Win32 API (user32, kernel32)"]:::oslib
        MessageLoop["Window creation & message loop"]:::oslib

        RustMiniwin -->|"extern calls"| Win32API
        Win32API -->|"RegisterClassEx/CreateWindowEx"| MessageLoop
        MessageLoop -->|"Dispatch messages"| RustMiniwin
    end

    %% Runtime for miniwinGL
    subgraph "Runtime: miniwinGL"
        RustMiniwinGL["Rust no_std runtime w/ OpenGL"]:::rustcode
        Win32API2["Win32 API (user32, kernel32)"]:::oslib
        GLSubsystem["OpenGL subsystem (wgl, gl)"]:::gl
        ShaderUtil["Shader utilities"]:::util
        GPU["GPU pipeline"]:::oslib

        RustMiniwinGL -->|"Window setup"| Win32API2
        Win32API2 -->|"wglCreateContext"| GLSubsystem
        GLSubsystem -->|"Compile/link shaders"| ShaderUtil
        ShaderUtil -->|"Shaders bound"| GLSubsystem
        GLSubsystem -->|"Render quad"| GPU
        GPU -->|"SwapBuffers"| RustMiniwinGL
    end

    %% Build to Runtime Links
    BuildExeMiniwin --> RustMiniwin
    BuildExeMiniwinGL --> RustMiniwinGL

    %% Click Events
    click CargoMiniwin "https://github.com/janiorca/tinywin/blob/master/miniwin/Cargo.toml"
    click XargoMiniwin "https://github.com/janiorca/tinywin/blob/master/miniwin/xargo.toml"
    click RustMiniwin "https://github.com/janiorca/tinywin/blob/master/miniwin/src/main.rs"
    click CargoMiniwinGL "https://github.com/janiorca/tinywin/blob/master/miniwinGL/Cargo.toml"
    click XargoMiniwinGL "https://github.com/janiorca/tinywin/blob/master/miniwinGL/xargo.toml"
    click RustMiniwinGL "https://github.com/janiorca/tinywin/blob/master/miniwinGL/src/main.rs"
    click Win32API "https://github.com/janiorca/tinywin/blob/master/miniwin/src/main.rs"
    click Win32API2 "https://github.com/janiorca/tinywin/blob/master/miniwinGL/src/main.rs"
    click GLSubsystem "https://github.com/janiorca/tinywin/blob/master/miniwinGL/src/gl.rs"
    click ShaderUtil "https://github.com/janiorca/tinywin/blob/master/miniwinGL/src/gl_util.rs"
    click README "https://github.com/janiorca/tinywin/blob/master/README.md"
    click MiniwinGLReadme "https://github.com/janiorca/tinywin/blob/master/miniwinGL/readme.md"
    click Crinkler "https://github.com/janiorca/tinywin/blob/master/tools/crinkler.exe"

    %% Styles
    classDef build fill:#f9f,stroke:#333,stroke-width:1px
    classDef rustcode fill:#ccf,stroke:#333,stroke-width:1px
    classDef oslib fill:#cfc,stroke:#333,stroke-width:1px
    classDef gl fill:#fcf,stroke:#333,stroke-width:1px
    classDef util fill:#cff,stroke:#333,stroke-width:1px
    classDef tool fill:#ffc,stroke:#333,stroke-width:1px
```
