forked from Dynamitos/Seele
136 lines
7.7 KiB
Markdown
136 lines
7.7 KiB
Markdown
# Platform (OS abstraction, dynamic library loading, input enums)
|
|||
|
|
|
||
|
|
> **Scope:** `src/Engine/Platform/` — the OS-specific layer. In Seele this is *smaller than you'd
|
||
|
|
> expect*: the platform directory is mostly the **per-OS `GameInterface`** (which dynamically loads
|
||
|
|
> the game's own shared library) plus the shared input enums. Heavy OS work (windows, surfaces,
|
||
|
|
> Vulkan/Metal) lives in `Window/` and `Graphics/{Vulkan,Metal}/` respectively — this doc covers
|
||
|
|
> the platform abstraction and, critically, **how the demo's game code actually gets loaded**.
|
||
|
|
|
||
|
|
This is one of the most important subsystems to understand for cross-repo work, because the
|
||
|
|
`Platform` layer is where the **engine ↔ demo boundary is physically enforced** (dlopen /
|
||
|
|
LoadLibrary + symbol lookup).
|
||
|
|
|
||
|
|
## Key types & where they live
|
||
|
|
|
||
|
|
| Type | Defined in |
|
||
|
|
|------|------------|
|
||
|
|
| `GameInterface` (Windows) | `src/Engine/Platform/Windows/GameInterface.h` (impl `.cpp`) |
|
||
|
|
| `GameInterface` (Linux) | `src/Engine/Platform/Linux/GameInterface.h` (impl `.cpp`) |
|
||
|
|
| `GameInterface` (macOS) | `src/Engine/Platform/Mac/GameInterface.h` (impl `.cpp`) |
|
||
|
|
| `Game` | `src/Engine/Game.h` — the abstract class the loaded lib must instantiate |
|
||
|
|
| Input enums: `InputAction`, `KeyCode`, `MouseButton`, `KeyModifier` | `src/Engine/Platform/` (shared, included by `Window/View.h` input callbacks) |
|
||
|
|
| `Window` (per-OS native surface) | `src/Engine/Window/Window.h`/`Window.cpp` (the OS window lives here, not in `Platform/`) |
|
||
|
|
|
||
|
|
**There is no monolithic `Platform.h`** (an older description of the codebase implied there was
|
||
|
|
one — there isn't; `grep` for `dlopen`/`LoadLibrary` confirms all the dynamic-loading code is in the
|
||
|
|
three `Platform/<OS>/GameInterface.cpp` files). The `Platform/` directory is the OS *abstraction for
|
||
|
|
loading the game*, not a general OS facade.
|
||
|
|
|
||
|
|
## The per-OS `GameInterface` — how the demo is loaded
|
||
|
|
|
||
|
|
Each OS has a `GameInterface` with the same public shape:
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
class GameInterface {
|
||
|
|
public:
|
||
|
|
GameInterface(std::filesystem::path libPath); // path to the demo's .so/.dylib/.dll
|
||
|
|
~GameInterface();
|
||
|
|
Game* getGame(); // the live Game* (createInstance() result)
|
||
|
|
void reload(); // (re)load the library + re-instantiate
|
||
|
|
|
||
|
|
private:
|
||
|
|
// Windows: HMODULE lib; Linux: void* lib (dlopen); Mac: void* (dlopen)
|
||
|
|
Game* (*createInstance)() = nullptr; // resolved symbol
|
||
|
|
void (*destroyInstance)(Game*) = nullptr;
|
||
|
|
};
|
||
|
|
```
|
||
|
|
|
||
|
|
**`reload()` is the cross-repo contract in executable form.** It is *identical in logic* across all
|
||
|
|
three OSes, only the syscalls differ:
|
||
|
|
|
||
|
|
| OS | Load | Resolve symbols | Unload |
|
||
|
|
|----|------|-----------------|--------|
|
||
|
|
| **Windows** (`Platform/Windows/GameInterface.cpp`) | `LoadLibraryA(dllPath)` | `GetProcAddress(lib, "createInstance")` / `...("destroyInstance")` | `FreeLibrary(lib)` |
|
||
|
|
| **Linux** (`Platform/Linux/GameInterface.cpp`) | `dlopen(soPath, RTLD_NOW)` | `dlsym(lib, "createInstance")` / `...("destroyInstance")` | `dlclose(lib)` |
|
||
|
|
| **macOS** (`Platform/Mac/GameInterface.cpp`) | `dlopen` | `dlsym` | `dlclose` |
|
||
|
|
|
||
|
|
The contract:
|
||
|
|
|
||
|
|
1. If a previous library is loaded, call `destroyInstance(game)` then unload it.
|
||
|
|
2. Load the demo's shared library at `libPath`.
|
||
|
|
3. Look up **exactly two** C symbols: `createInstance` and `destroyInstance` (plain `extern "C"`,
|
||
|
|
no mangling).
|
||
|
|
4. Call `createInstance()` → `Game*`; store it; `getGame()` returns it.
|
||
|
|
|
||
|
|
**The demo must export exactly these two `extern "C"` symbols** — that is the *entire* ABI. See
|
||
|
|
[cross-repo.md](../cross-repo.md) for the demo-side half and [game-interface.md](game-interface.md)
|
||
|
|
for the engine-side `Game` class.
|
||
|
|
|
||
|
|
### Gotcha: `reload()` also copies game shaders
|
||
|
|
|
||
|
|
The **Windows** `GameInterface::reload()` does something the other two don't (a known
|
||
|
|
asymmetry): before loading, it **copies `<dllPath>/../res/shaders` → `./shaders/game`**
|
||
|
|
(`std::filesystem::copy(..., copy_options::overwrite_existing)`) if that path exists. This is the
|
||
|
|
**game-shader hot-reload** mechanism — the engine reads game shaders from `./shaders/game`, and
|
||
|
|
`reload()` refreshes that directory so a rebuilt demo's new shaders are picked up without a full
|
||
|
|
restart.
|
||
|
|
|
||
|
|
**Consequences:**
|
||
|
|
- On Windows, "reload the game" also refreshes its shaders. On Linux/mac, it does **not** — the
|
||
|
|
shader directory must be managed out-of-band (or you're on a platform where the copy step simply
|
||
|
|
doesn't exist yet). **If you add cross-platform shader hot-reload, this is the place to fix the
|
||
|
|
asymmetry.**
|
||
|
|
- The copy target is the **CWD** (`./shaders/game`), not next to the library. CWD matters.
|
||
|
|
- `rt`/`rt` and the engine's own shaders are separate — this only touches `shaders/game` (the
|
||
|
|
*game's* shaders), not the engine's.
|
||
|
|
|
||
|
|
## Input enums (the shared platform types)
|
||
|
|
|
||
|
|
`InputAction`, `KeyCode`, `MouseButton`, `KeyModifier` live in `src/Engine/Platform/` and are the
|
||
|
|
**vocabulary of every input callback** in `Window/View.h` (`keyCallback`, `mouseButtonCallback`,
|
||
|
|
etc.) and in `UI::System` (`key`, `mouseButton`, `scroll`). If you're adding a new input source or
|
||
|
|
keycode, these are the enums to extend. They are the one truly shared, cross-OS piece of the
|
||
|
|
`Platform/` layer (the `GameInterface` per-OS files are the rest).
|
||
|
|
|
||
|
|
## Where the OS "real" work actually lives (don't look here for it)
|
||
|
|
|
||
|
|
A common mistake is to look in `Platform/` for window/surface/GPU code. It's not here:
|
||
|
|
|
||
|
|
| Concern | Actual location |
|
||
|
|
|---|---|
|
||
|
|
| OS window + native surface | `src/Engine/Window/Window.{h,cpp}` (+ per-OS bits there) |
|
||
|
|
| Vulkan | `src/Engine/Graphics/Vulkan/` |
|
||
|
|
| Metal | `src/Engine/Graphics/Metal/` |
|
||
|
|
| Loading the *game* (this doc) | `src/Engine/Platform/<OS>/GameInterface.cpp` |
|
||
|
|
|
||
|
|
The reason `Platform/` is thin: Seele pushes OS-specific code *down into the subsystem that owns
|
||
|
|
the concern* (windowing → `Window/`, GPU → `Graphics/<Backend>/`), and keeps only the
|
||
|
|
**game-loading** logic in `Platform/` because that is the one cross-cutting OS difference that
|
||
|
|
belongs nowhere else.
|
||
|
|
|
||
|
|
## Gotchas
|
||
|
|
|
||
|
|
- **Exactly two exported symbols** (`createInstance`, `destroyInstance`). Any other symbol the
|
||
|
|
engine needs must be exposed *through* the `Game*` (virtual methods on `Game`), not as a new
|
||
|
|
C symbol. Adding a third `extern "C"` export breaks the contract.
|
||
|
|
- **Symbol names must be unmangled** (`extern "C"`). A mangled name = `dlsym`/`GetProcAddress`
|
||
|
|
returns null = `createInstance` is null → crash on call.
|
||
|
|
- **The shader-copy asymmetry** (Windows copies `res/shaders`→`./shaders/game` on reload; the
|
||
|
|
others don't). Know which platform you're on before assuming "reload refreshes my shaders."
|
||
|
|
- **`RTLD_NOW` on Linux** — all symbols are resolved at load; a missing symbol fails *at
|
||
|
|
`dlopen`*, not at first use. Good for debugging, means a bad demo build fails loudly at load.
|
||
|
|
- **`getGame()` lifetime** — the returned `Game*` is owned by the loaded library. When you `reload()`
|
||
|
|
or unload, the old `Game*` is destroyed. Never hold a `Game*` across a `reload()`.
|
||
|
|
- **CWD-sensitive** — the Windows shader copy writes to the *current working directory*, not the
|
||
|
|
library's directory. Run from the right CWD.
|
||
|
|
|
||
|
|
## Where to look
|
||
|
|
|
||
|
|
| What you're doing | Start here |
|
||
|
|
|---|---|
|
||
|
|
| Understand how the demo is loaded | `src/Engine/Platform/<OS>/GameInterface.cpp` (all three, compare) |
|
||
|
|
| Add a new exported symbol (you probably shouldn't) | `GameInterface.cpp` + the `Game` virtuals in `src/Engine/Game.h` |
|
||
|
|
| Fix shader hot-reload / the Windows-only copy | `Platform/Windows/GameInterface.cpp` `reload()` (the `std::filesystem::copy` block) |
|
||
|
|
| Add a key code / input action | the enums in `src/Engine/Platform/` + `Window/View.h` callbacks + `UI/System.h` |
|
||
|
|
| "my game lib won't load" | symbol names (unmangled?), the two exports present, the lib path, `dlopen`/`LoadLibrary` error |
|
||
|
|
| Per-OS window/surface (not here) | `src/Engine/Window/Window.{h,cpp}` |
|