platform-glfw

Explicit GLFW host lifecycle for standalone Garnet examples

Garnet GLFW host

A minimal explicit desktop host for the standalone graphics slice. Requiring the shard performs no initialization. GLFWHost.open(width, height, title, visible: true) { |host| ... } owns one GLFW session/window and creates a current OpenGL 3.3 core context. Do not nest sessions or combine it with another owner of GLFW's global initialization.

Call open on the actual process main thread (enforced on macOS; caller requirement elsewhere). Host operations stay on that owner. framebuffer_size, poll_events, closed? and swap_buffers support the example loop. Renderers must be closed inside the block before its ensure path destroys the window and terminates GLFW, including exceptional exits.

Use focused? for current window focus and on_focus to release or suspend input on focus changes. After poll_events, call host.poll_gamepads(pads) to update a Garnet::Input::Gamepads registry with mapped controller state and hotplug information. The standalone glfw shard remains available for applications that manage the window and input loop directly.

Engine input adapter

Garnet::Platform::GLFWInput.new(window, devices) (also available alone via require "garnet-platform-glfw/input", which needs no OpenGL) owns a GLFW window's key, character, mouse, scroll, cursor-enter and focus callbacks and feeds a backend-neutral Garnet::Input::Devices. Call poll_gamepads once per frame after polling events and sync_cursor after the frame to apply devices.cursor. Pass map_point to convert window points to logical coordinates (for example, a letterboxed virtual resolution). It installs a clipboard on devices.clipboard. Call close before closing the window.

GLFWHost also reports logical_size, content_scale and iconified?; poll_events returns whether the window remains open, and wait_events blocks for input while the app is idle.

SDL3 gamepads

Applications with SDL3 installed (brew install sdl3) can require "garnet-platform-glfw/sdl_gamepads". SDL's controller database maps far more pads than GLFW's (notably on macOS), and it reports button presses as events. Requiring the file makes SDLGamepads the GLFWInput.default_gamepad_source, so every GLFWInput — including the one App::Desktop creates — reads pads through SDL instead of GLFW's joysticks, falling back to them if SDL can't start. Input::Devices then emits each frame's GamepadButtonEvents in SDL press order (two pads pressing Start in one frame join in that order), and a press released before the frame's snapshot is still seen as a one-frame press. garnet bundle copies libSDL3 into macOS app bundles with the other linked libraries.

SDLGamepads also works standalone with the GLFWHost loop: pads_source.poll(pads, focused: host.focused?) updates a Garnet::Input::Gamepads registry and returns the Start presses in event order. Create, poll and close it on the main thread; it consumes SDL's event queue, so use one source per application.

Repository

platform-glfw

Owner
Statistic
  • 0
  • 0
  • 0
  • 7
  • 3
  • 1 day ago
  • November 30, 2026
License

MIT License

Links
Synced at

Wed, 02 Dec 2026 02:09:09 GMT

Languages