|
| 1 | +# bgfx + GLFW TypeScript sample |
| 2 | + |
| 3 | +Cross-platform graphics sample for the TypeScript native compiler (`tslang`). Application logic is written in TypeScript; GLFW windowing and bgfx rendering live in a thin C++ bridge exposed through `.d.ts` FFI declarations. |
| 4 | + |
| 5 | +Control flow matches [`cmake_vulkan`](../cmake_vulkan/) and [`cmake_winapp`](../cmake_winapp/): **C++ owns the event loop**, TypeScript creates the window and reacts via an `onMessage` callback. |
| 6 | + |
| 7 | +## What it does |
| 8 | + |
| 9 | +- Opens an 800x600 GLFW window (no client API — bgfx owns rendering) |
| 10 | +- Initializes bgfx with the native window handle (Win32, X11, or Cocoa) |
| 11 | +- C++ `run_loop()` polls GLFW and dispatches `Messages.Frame` each iteration |
| 12 | +- TypeScript `onMessage` calls `run_bgfx_frame()` on Frame (like `run_vulkan()` on Paint) |
| 13 | +- Renders an animated clear color and on-screen debug text: "Hello from tslang" |
| 14 | +- Quits on Escape or window close |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +1. **Built `tslang`** — follow the main [README](../../../README.md) to build the compiler and install the default library (`tslang --install-default-lib`). |
| 19 | +2. **CMake 3.20+** and **Ninja** (required for the custom `TSLANG` CMake language). |
| 20 | +3. **C++20** toolchain (GCC, Clang, or MSVC). |
| 21 | +4. **Graphics drivers** — bgfx auto-selects an available backend (OpenGL, Vulkan, DirectX on Windows). |
| 22 | + |
| 23 | +### Linux packages (Fedora example) |
| 24 | + |
| 25 | +```bash |
| 26 | +sudo dnf install cmake ninja-build gcc-c++ ncurses-devel \ |
| 27 | + libX11-devel libXcursor-devel libXi-devel libXrandr-devel \ |
| 28 | + libXinerama-devel libXxf86vm-devel mesa-libGL-devel |
| 29 | +``` |
| 30 | + |
| 31 | +### Windows |
| 32 | + |
| 33 | +- Visual Studio 2022+ with C++ workload, CMake, and Ninja. |
| 34 | +- Ensure `tslang.exe` is on `PATH` or pass `-DTSLANG_ROOT=...` pointing at the folder that contains `bin/tslang.exe`. |
| 35 | + |
| 36 | +## Layout |
| 37 | + |
| 38 | +``` |
| 39 | +cmake_bgfx/ |
| 40 | +├── CMakeLists.txt # FetchContent GLFW + bgfx, mixed TS/C++ target |
| 41 | +├── CMakePresets.json |
| 42 | +├── cmake/ # TSLANG custom language modules |
| 43 | +├── src/ |
| 44 | +│ ├── main.ts # entry point (Main) |
| 45 | +│ ├── application.ts # constructs AppWindow only |
| 46 | +│ ├── appwindow.ts # onMessage handler (Frame / KeyDown / Close / Destroy) |
| 47 | +│ └── bgfx_glfw.d.ts # FFI declarations |
| 48 | +└── native/ |
| 49 | + ├── bgfx_bridge.cpp # extern "C" GLFW + bgfx glue + run_loop() |
| 50 | + └── main_entry.cpp # main() -> Main() -> run_loop() |
| 51 | +``` |
| 52 | + |
| 53 | +## Build and run |
| 54 | + |
| 55 | +### Linux |
| 56 | + |
| 57 | +```bash |
| 58 | +cd docs/how/cmake_bgfx |
| 59 | + |
| 60 | +cmake --preset default |
| 61 | +# or explicitly: |
| 62 | +# cmake --preset default -DTSLANG_ROOT=/path/to/TypeScriptCompiler/__build |
| 63 | + |
| 64 | +cmake --build --preset default |
| 65 | +cmake --build --target run |
| 66 | +``` |
| 67 | + |
| 68 | +Binary: `build/cmake_bgfx` |
| 69 | + |
| 70 | +If CMake reports `tslang compiler not found`, build the compiler from the repo root first: |
| 71 | + |
| 72 | +```bash |
| 73 | +./prepare_3rdParty_release.sh |
| 74 | +cd tslang && ./config_tslang_release.sh && ./build_tslang_release.sh |
| 75 | +./bin/tslang --install-default-lib |
| 76 | +``` |
| 77 | + |
| 78 | +Then re-run `cmake --preset default` from `docs/how/cmake_bgfx`. |
| 79 | + |
| 80 | +### Windows |
| 81 | + |
| 82 | +```bat |
| 83 | +cd docs\how\cmake_bgfx |
| 84 | +
|
| 85 | +cmake --preset default -DTSLANG_ROOT=C:\path\to\TypeScriptCompiler\__build |
| 86 | +cmake --build --preset default |
| 87 | +cmake --build --target run |
| 88 | +``` |
| 89 | + |
| 90 | +Binary: `build\cmake_bgfx.exe` |
| 91 | + |
| 92 | +### macOS |
| 93 | + |
| 94 | +Same CMake flow as Linux. Requires Xcode command-line tools and a working OpenGL/Metal backend for bgfx. |
| 95 | + |
| 96 | +### Debug build |
| 97 | + |
| 98 | +```bash |
| 99 | +cmake --preset debug |
| 100 | +cmake --build --preset debug |
| 101 | +``` |
| 102 | + |
| 103 | +## How it works |
| 104 | + |
| 105 | +1. **`main_entry.cpp`** calls TypeScript `Main()`, then C++ `run_loop()`. |
| 106 | +2. **`Main()`** constructs `AppWindow`, which registers `onMessage` and calls `create_bgfx`. |
| 107 | +3. **`run_loop()`** polls GLFW, dispatches `Messages.Frame` each tick, then `Messages.Destroy` on exit. |
| 108 | +4. **CMake `TSLANG` language** compiles `.ts` sources to object files with `tslang --emit=obj`. |
| 109 | +5. **`bgfx_bridge.cpp`** implements the flat `extern "C"` API declared in `bgfx_glfw.d.ts`. |
| 110 | +6. **FetchContent** downloads GLFW 3.4 and [bgfx.cmake](https://github.com/bkaradzic/bgfx.cmake) on first configure. |
| 111 | + |
| 112 | +## Platform notes |
| 113 | + |
| 114 | +| Platform | Window backend | Notes | |
| 115 | +|----------|----------------|-------| |
| 116 | +| Windows | Win32 (`glfwGetWin32Window`) | bgfx D3D11/D3D12/OpenGL/Vulkan | |
| 117 | +| Linux | X11 (`glfwGetX11Display` / `glfwGetX11Window`) | Wayland disabled (`GLFW_BUILD_WAYLAND=OFF`) | |
| 118 | +| macOS | Cocoa (`glfwGetCocoaWindow`) | OpenGL/Metal via bgfx | |
| 119 | + |
| 120 | +## Related samples |
| 121 | + |
| 122 | +- [`cmake_winapp`](../cmake_winapp/) — Win32 window only (C++ message loop) |
| 123 | +- [`cmake_vulkan`](../cmake_vulkan/) — Win32 + Vulkan cube (same onMessage pattern) |
| 124 | +- [`cmake_tslang`](../cmake_tslang/) — mixed C++/TypeScript with custom CMake language |
| 125 | +- [`c-cpp-header-import.md`](../../c-cpp-header-import.md) — FFI / native binding design |
0 commit comments