# onitama/openhsp architecture

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

- Interactive diagram: https://gitdiagram.com/onitama/openhsp
- Repository: https://github.com/onitama/openhsp

## Overview

#### Purpose and outputs
OpenHSP is a C/C++ implementation and distribution of the HSP3 scripting language system for building desktop, graphical, game, and utility applications. Its primary product flow is HSP source (`.hsp`) compiled to an object (`.ax`) and executed by a selected runtime. Build entry points are `makefile`, `makefile.emscripten`, `makefile.raspbian`, and `setup.sh`.

#### User-facing entry points
- **Compiler:** `src/hspcmp/main.cpp` drives parsing, symbol management, and code generation into HSP object files.
- **Command-line runtime:** platform mains such as `src/hsp3/linux/main.cpp` and `src/hsp3/win32/main.cpp` host the core interpreter.
- **Graphical runtimes:** HSP3Dish/HGIMG4 implementations have platform executable entry points including `src/hsp3dish/linux/hsp3dish.cpp`, `src/hsp3dish/win32/main.cpp`, and `src/hsp3dish/win32gp/main.cpp`.
- **Editor:** Linux editor source is `src/tools/linux/hsed_gtk2.cpp`; Windows editor variants live under `src/tools/win32/hsed3_footy2/`.

#### Core interpreter and object boundary
The portable HSP virtual-machine core is concentrated in `src/hsp3/`. `src/hsp3/hsp3.cpp` and `src/hsp3/hsp3code.cpp` execute compiled instructions; `src/hsp3/hsp3int.cpp` implements internal operations; typed runtime values are handled by `src/hsp3/hspvar_core.cpp` plus integer/string/double/label/struct variants. The material boundary is compiled `.ax` bytecode/object data: compiler output is loaded and interpreted by each runtime rather than linking user scripts into native applications.

#### Compilation pipeline
`src/hspcmp/` is a separate compiler subsystem: lexical/token handling (`src/hspcmp/token.cpp`), labels and program state (`src/hspcmp/label.cpp`, `src/hspcmp/hsmanager.cpp`), command processing (`src/hspcmp/hspcmd.cpp`), and emission (`src/hspcmp/codegen.cpp`). It consumes HSP source plus include/module definitions from `common/`, notably `common/hspdef.as`, `common/hsp3dish.as`, and `common/hgimg4.as`. This shared script-level library layer defines the APIs exposed by runtimes and is a compatibility boundary between authored HSP programs and native implementation.

#### Runtime specialization and platform boundary
`src/hsp3/` supplies base execution, file/package support, system requests, and platform adapters. OS-specific extensions are isolated in `src/hsp3/linux/`, `src/hsp3/win32gui/`, `src/hsp3/ios/`, `src/hsp3/ndk/`, and `src/hsp3/emscripten/`. This enables the same bytecode and core semantics across Windows, Linux/Raspberry Pi, Android NDK, iOS, and browser/Emscripten targets, while windows, graphics, I/O, sockets, and native calls remain platform-specific.

#### HSP3Dish graphics/game stack
`src/hsp3dish/` is the graphical runtime layered on the HSP core. Its rendering/window abstraction is represented by `src/hsp3dish/hsp3gr_dish.cpp` and `src/hsp3dish/hspwnd_dish.cpp`; sprite support is in `src/hsp3dish/essprite.cpp`; OBAQ physics integration is in `src/hsp3dish/obaq/`. Windows has distinct Dish and HGIMG4-oriented builds (`src/hsp3dish/win32/`, `src/hsp3dish/win32gp/`). Scripts select these capabilities through includes such as `common/hsp3dish.as`, `common/hgimg4.as`, and `common/obaq.as`.

#### 3D assets and embedded engine dependencies
The repository includes a substantial GamePlay-derived 3D engine under `src/hsp3dish/gameplay/src/`, covering scenes, models, materials, rendering, animation, input, audio, and physics. Its asset encoder (`src/hsp3dish/gameplay/encoder/src/main.cpp`, `src/hsp3dish/gameplay/encoder/src/FBXSceneEncoder.cpp`) converts inputs such as FBX into runtime bundles/GPB assets used by samples. Bundled third-party dependencies include Bullet physics, Lua, OpenAL headers, ANGLE/OpenGL ES support, libpng, zlib, and FreeType under `src/hsp3dish/extlib/` and `src/hsp3dish/gameplay/encoder/lib/`.

#### External runtime services and distribution content
Linux builds depend on GCC/Make plus OpenGL ES/EGL, SDL2/SDL_mixer, GTK2, libcurl, libgpiod, and libffi. Native extensions/plugins are separate deliverables under `src/plugins/`, including Windows networking and socket plugins (`src/plugins/win32/hspinet/`, `src/plugins/win32/hspsock/`) and Linux extension test code (`src/plugins/linux/`). `sample/` provides executable HSP programs and assets that exercise the compiler-to-runtime path, while `doclib_en/` and `hsphelp_en/` ship end-user documentation and command help.

Main entry points: [`src/hspcmp/main.cpp`](https://github.com/onitama/openhsp/blob/master/src/hspcmp/main.cpp) (HSP compiler), [`makefile`](https://github.com/onitama/openhsp/blob/master/makefile) (Build entry points), [`src/hsp3/hsp3.cpp`](https://github.com/onitama/openhsp/blob/master/src/hsp3/hsp3.cpp) (Instruction execution), [`src/hsp3/linux/main.cpp`](https://github.com/onitama/openhsp/blob/master/src/hsp3/linux/main.cpp) (Command-line runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_authoring["Authoring &amp; compilation"]
  node_scripts["HSP source programs<br/>script source"]
  node_script_api["Script API includes<br/>compatibility API"]
  node_compiler_main["HSP compiler<br/>compiler entry point<br/>[main.cpp]"]
  node_compiler_pipeline["Compiler pipeline<br/>lexer, symbols &amp; emitter"]
  node_ax_object["Compiled .ax object<br/>bytecode object"]
  node_editor["HSP editor<br/>authoring executable<br/>[hsed_gtk2.cpp]"]
end

subgraph group_runtime["Portable runtime"]
  node_vm_core["HSP VM core<br/>interpreter"]
  node_vm_execution["Instruction execution<br/>VM execution layer<br/>[hsp3.cpp]"]
  node_runtime_values["Typed values<br/>runtime value system<br/>[hspvar_core.cpp]"]
  node_platform_adapters["Platform adapters<br/>OS integration"]
  node_cli_runtime["Command-line runtime<br/>native executable<br/>[main.cpp]"]
end

subgraph group_graphics["Graphics &amp; 3D"]
  node_dish_runtime["HSP3Dish runtime<br/>graphical executable"]
  node_dish_rendering["Dish rendering &amp; windows<br/>graphics abstraction<br/>[hsp3gr_dish.cpp]"]
  node_sprites_physics["Sprites &amp; OBAQ physics<br/>game features<br/>[essprite.cpp]"]
  node_gameplay_engine["GamePlay 3D engine<br/>embedded 3D engine"]
  node_asset_encoder["3D asset encoder<br/>asset build tool<br/>[main.cpp]"]
end

subgraph group_delivery["Delivery &amp; extensions"]
  node_plugins["Native plugins<br/>extension deliverables"]
  node_documentation["Documentation &amp; help<br/>distribution content"]
  node_build_entrypoints["Build entry points<br/>build system"]
end

node_editor -->|"authors"| node_scripts
node_scripts -->|"compiles"| node_compiler_main
node_script_api -->|"included by"| node_compiler_main
node_compiler_main -->|"drives"| node_compiler_pipeline
node_compiler_pipeline -->|"emits"| node_ax_object
node_ax_object -->|"loaded by"| node_vm_core
node_vm_core -->|"executes through"| node_vm_execution
node_vm_execution -->|"uses"| node_runtime_values
node_platform_adapters -->|"extends"| node_vm_core
node_cli_runtime -->|"hosts"| node_vm_core
node_dish_runtime -->|"layers on"| node_vm_core
node_dish_runtime -->|"uses"| node_dish_rendering
node_dish_runtime -->|"includes"| node_sprites_physics
node_dish_runtime -->|"embeds"| node_gameplay_engine
node_asset_encoder -->|"produces assets for"| node_gameplay_engine
node_script_api -.->|"selects capabilities"| node_dish_runtime
node_plugins -.->|"extend"| node_platform_adapters
node_build_entrypoints -->|"builds"| node_compiler_main
node_build_entrypoints -->|"builds"| node_cli_runtime
node_build_entrypoints -->|"builds"| node_dish_runtime
node_documentation -.->|"documents"| node_scripts

click node_compiler_main "https://github.com/onitama/openhsp/blob/master/src/hspcmp/main.cpp"
click node_vm_execution "https://github.com/onitama/openhsp/blob/master/src/hsp3/hsp3.cpp"
click node_runtime_values "https://github.com/onitama/openhsp/blob/master/src/hsp3/hspvar_core.cpp"
click node_cli_runtime "https://github.com/onitama/openhsp/blob/master/src/hsp3/linux/main.cpp"
click node_dish_rendering "https://github.com/onitama/openhsp/blob/master/src/hsp3dish/hsp3gr_dish.cpp"
click node_sprites_physics "https://github.com/onitama/openhsp/blob/master/src/hsp3dish/essprite.cpp"
click node_asset_encoder "https://github.com/onitama/openhsp/blob/master/src/hsp3dish/gameplay/encoder/src/main.cpp"
click node_editor "https://github.com/onitama/openhsp/blob/master/src/tools/linux/hsed_gtk2.cpp"
click node_build_entrypoints "https://github.com/onitama/openhsp/blob/master/makefile"

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_scripts,node_script_api,node_compiler_main,node_compiler_pipeline,node_ax_object,node_editor toneBlue
class node_vm_core,node_vm_execution,node_runtime_values,node_platform_adapters,node_cli_runtime toneAmber
class node_dish_runtime,node_dish_rendering,node_sprites_physics,node_gameplay_engine,node_asset_encoder toneMint
class node_plugins,node_documentation,node_build_entrypoints toneRose
```

## Components

### Authoring & compilation

- **HSP source programs**: script source · Authored .hsp programs and sample applications.
- **Script API includes**: compatibility API · Shared definitions expose runtime capabilities to scripts.
- [HSP compiler](https://github.com/onitama/openhsp/blob/master/src/hspcmp/main.cpp): compiler entry point · Drives parsing, symbols, and object generation. · `src/hspcmp/main.cpp`
- **Compiler pipeline**: lexer, symbols & emitter · Tokenizes, manages program state, processes commands, and emits code.
- **Compiled .ax object**: bytecode object · Portable compiled program loaded by runtimes.
- [HSP editor](https://github.com/onitama/openhsp/blob/master/src/tools/linux/hsed_gtk2.cpp): authoring executable · `src/tools/linux/hsed_gtk2.cpp`

### Portable runtime

- **HSP VM core**: interpreter · Executes bytecode and provides core runtime semantics.
- [Instruction execution](https://github.com/onitama/openhsp/blob/master/src/hsp3/hsp3.cpp): VM execution layer · `src/hsp3/hsp3.cpp`
- [Typed values](https://github.com/onitama/openhsp/blob/master/src/hsp3/hspvar_core.cpp): runtime value system · Core and variant implementations for script values. · `src/hsp3/hspvar_core.cpp`
- **Platform adapters**: OS integration · Platform-specific windows, I/O, sockets, and native integration.
- [Command-line runtime](https://github.com/onitama/openhsp/blob/master/src/hsp3/linux/main.cpp): native executable · Hosts the core interpreter for command-line execution. · `src/hsp3/linux/main.cpp`

### Graphics & 3D

- **HSP3Dish runtime**: graphical executable · Graphical and game runtime layered on the HSP core.
- [Dish rendering & windows](https://github.com/onitama/openhsp/blob/master/src/hsp3dish/hsp3gr_dish.cpp): graphics abstraction · `src/hsp3dish/hsp3gr_dish.cpp`
- [Sprites & OBAQ physics](https://github.com/onitama/openhsp/blob/master/src/hsp3dish/essprite.cpp): game features · `src/hsp3dish/essprite.cpp`
- **GamePlay 3D engine**: embedded 3D engine · Provides scenes, rendering, animation, input, audio, and physics.
- [3D asset encoder](https://github.com/onitama/openhsp/blob/master/src/hsp3dish/gameplay/encoder/src/main.cpp): asset build tool · Converts FBX and other inputs into GPB runtime assets. · `src/hsp3dish/gameplay/encoder/src/main.cpp`

### Delivery & extensions

- **Native plugins**: extension deliverables · Separate platform-specific networking and extension modules.
- **Documentation & help**: distribution content
- [Build entry points](https://github.com/onitama/openhsp/blob/master/makefile): build system · Makefiles and setup script select native and cross-platform builds. · `makefile`

## Connections

- HSP editor → HSP source programs: authors
- HSP source programs → HSP compiler: compiles
- Script API includes → HSP compiler: included by
- HSP compiler → Compiler pipeline: drives
- Compiler pipeline → Compiled .ax object: emits
- Compiled .ax object → HSP VM core: loaded by
- HSP VM core → Instruction execution: executes through
- Instruction execution → Typed values: uses
- Platform adapters → HSP VM core: extends
- Command-line runtime → HSP VM core: hosts
- HSP3Dish runtime → HSP VM core: layers on
- HSP3Dish runtime → Dish rendering & windows: uses
- HSP3Dish runtime → Sprites & OBAQ physics: includes
- HSP3Dish runtime → GamePlay 3D engine: embeds
- 3D asset encoder → GamePlay 3D engine: produces assets for
- Script API includes → HSP3Dish runtime: selects capabilities
- Native plugins → Platform adapters: extend
- Build entry points → HSP compiler: builds
- Build entry points → Command-line runtime: builds
- Build entry points → HSP3Dish runtime: builds
- Documentation & help → HSP source programs: documents
