|
| 1 | +export module imgui.backend.headless; |
| 2 | + |
| 3 | +import imgui.core; |
| 4 | +export import imgui.backend; // shared types (GlConfig/Error/FbSize) + BackendApi |
| 5 | + |
| 6 | +// Headless backend: a second, display-free BackendApi implementation. |
| 7 | +// |
| 8 | +// It satisfies the exact same compile-time contract as GlfwOpenGL3, so the |
| 9 | +// SAME application loop runs against either backend by swapping one import |
| 10 | +// and one alias (I3). Useful for CI smoke runs, logic tests, and servers: |
| 11 | +// frames are produced (ImGui draw data is generated) but nothing is |
| 12 | +// presented; RenderDrawData is a deliberate no-op. |
| 13 | +// |
| 14 | +// This module imports imgui.core for signatures but does NOT re-export it. |
| 15 | +export namespace ImGui::Backend { |
| 16 | + struct Headless { |
| 17 | + struct WindowImpl { |
| 18 | + int width = 0; |
| 19 | + int height = 0; |
| 20 | + bool shouldClose = false; |
| 21 | + }; |
| 22 | + using Window = WindowImpl; |
| 23 | + |
| 24 | + static bool InitPlatform() { return true; } |
| 25 | + static void TerminatePlatform() {} |
| 26 | + |
| 27 | + static Error LastError() { return Error{}; } |
| 28 | + |
| 29 | + static Window* CreateWindow( |
| 30 | + int width, |
| 31 | + int height, |
| 32 | + const char* /*title*/, |
| 33 | + GlConfig /*config*/ = RecommendedGlConfig() |
| 34 | + ) { |
| 35 | + return new WindowImpl{width, height, false}; |
| 36 | + } |
| 37 | + |
| 38 | + static void DestroyWindow(Window* window) { delete window; } |
| 39 | + |
| 40 | + static void MakeContextCurrent(Window* /*window*/) {} |
| 41 | + static void SwapInterval(int /*interval*/) {} |
| 42 | + |
| 43 | + static FbSize FramebufferSize(Window* window) { |
| 44 | + return window ? FbSize{window->width, window->height} : FbSize{}; |
| 45 | + } |
| 46 | + |
| 47 | + static bool WindowShouldClose(Window* window) { |
| 48 | + return window == nullptr || window->shouldClose; |
| 49 | + } |
| 50 | + |
| 51 | + static void SetWindowShouldClose(Window* window, bool value) { |
| 52 | + if (window) window->shouldClose = value; |
| 53 | + } |
| 54 | + |
| 55 | + static void PollEvents() {} |
| 56 | + static void SwapBuffers(Window* /*window*/) {} |
| 57 | + |
| 58 | + // ImGui bindings: a headless frame needs a display size and a built |
| 59 | + // font atlas; there is no platform/renderer library to initialize. |
| 60 | + static bool Init( |
| 61 | + Window* window, |
| 62 | + GlConfig /*config*/ = RecommendedGlConfig(), |
| 63 | + bool /*installCallbacks*/ = true |
| 64 | + ) { |
| 65 | + if (window == nullptr || ImGui::GetCurrentContext() == nullptr) return false; |
| 66 | + ImGuiIO& io = ImGui::GetIO(); |
| 67 | + io.DisplaySize = ImVec2{static_cast<float>(window->width), |
| 68 | + static_cast<float>(window->height)}; |
| 69 | + unsigned char* pixels = nullptr; |
| 70 | + int w = 0, h = 0; |
| 71 | + io.Fonts->GetTexDataAsRGBA32(&pixels, &w, &h); |
| 72 | + return pixels != nullptr; |
| 73 | + } |
| 74 | + |
| 75 | + static void NewFrame() {} |
| 76 | + static void Viewport(int, int, int, int) {} |
| 77 | + static void ClearColor(float, float, float, float) {} |
| 78 | + static void ClearColorBuffer() {} |
| 79 | + static void RenderDrawData(ImDrawData* /*drawData*/) {} // headless: no-op |
| 80 | + static void Shutdown() {} |
| 81 | + }; |
| 82 | + |
| 83 | + static_assert(BackendApi<Headless>, |
| 84 | + "Headless must satisfy the backend contract"); |
| 85 | +} |
0 commit comments