glfw.cr

GLFW 3.4 C bindings and type-oriented Crystal interfaces

glfw.cr

GLFW 3.4's portable C API and type-oriented Crystal wrappers in one independent shard. require "glfw" exports GLFW and LibGLFW; require "lib_glfw" loads only the C bindings. Requires Crystal 1.19.1+ and a system GLFW 3.4 library. There is no Garnet, OpenGL, Vulkan, or Metal dependency and no initialization on require.

require "glfw"

GLFW.run do
  GLFW.window_hint(GLFW::WindowHint::ClientAPI, GLFW::ClientAPI::None)
  window = GLFW::Window.new(800, 600, "My application")
  window.on_key do |key, scancode, action, modifiers|
    window.should_close = true if key.escape? && action.press?
  end
  until window.should_close?
    GLFW.wait_events
  end
end

Types and ownership

Window holds window operations, context operations, input state, clipboard access, icons and event handlers. Monitor holds display modes, work area, scaling and gamma. Cursor owns a standard or custom cursor and can be shared between windows. Joystick holds an input device ID and exposes copied axes, buttons, hats and gamepad state. Image owns copied RGBA8 bytes. Monitor and gamepad records contain values; pointer-backed native arrays and strings are copied before returning.

GLFW.run opens one process-wide session and guarantees cleanup with ensure. The session owns its open windows and cursors, including their callback procs, until explicitly closed or the session exits. Window#close and Cursor#close are idempotent. Windows retain assigned cursors. Retained window/cursor wrappers reject use after cleanup; monitor wrappers reject use after a disconnect or session exit. These lifetimes follow GLFW's requirement that destruction and termination happen on the main thread; there is no finalizer that calls GLFW from a GC thread.

Window, monitor, cursor and event operations in the typed layer are confined to the session thread. Start the session on the process main thread (checked additionally with pthread_main_np on macOS). Rendering-context transfer to another thread currently requires the low-level API and application synchronization. Timer queries, post_empty_event, and context-global GL helpers retain GLFW's threading semantics. Do not terminate GLFW or destroy a typed object's native handle through LibGLFW while its session owns it.

to_unsafe borrows an object's native representation for FFI integration; it transfers no ownership. Native arrays passed during icon/cursor/gamma setup are kept alive through the call. Native errors raise GLFW::Error with a code and description after returning from C. The safe layer does not throw exceptions through C callbacks.

Events

All 17 window callbacks have on_* block registration and clear_*_callback removal. GLFW.on_monitor and GLFW.on_joystick report typed connection events. C trampolines copy callback data and queue delivery; user handlers run after poll_events or wait_events returns from C. GLFW.session.dispatch explicitly delivers events queued by other GLFW operations. Handlers can close windows and raise exceptions. Events targeting a closed window are discarded.

Coverage and low-level access

The checked-in generated binding covers all 124 functions, callback signatures, structures and constants in the portable GLFW 3.4 header. Ordinary windowing, display, input, gamepad, clipboard, timing and GL context operations have typed methods. Vulkan support/required-extension discovery is available without a Vulkan dependency; surface creation and Vulkan function loading remain low-level integration points for adapters that know the Vulkan binding's types. C allocator hooks, custom Vulkan loader installation, raw user pointers and raw error callbacks are intentionally exposed through LibGLFW: these operations inherently control unmanaged memory or FFI behavior. Platform-specific native-access extensions from glfw3native.h are not included.

Typed examples use enums for keys, buttons, actions, modifiers, cursor shapes/modes, hints and platforms. Hint values also accept integers so new platform-specific values do not require changes to the wrapper. The low-level layer remains available for explicit native interop.

Build and validation

Install GLFW 3.4 using the platform's package manager, then run from this directory:

crystal spec
crystal build spec/api_compile.cr --no-codegen
crystal run examples/window.cr

The specs create hidden native windows and require a graphical desktop. The compile fixture instantiates typed APIs without modifying display gamma or fullscreen settings. Do not set GLFW_EXERCISE_DESTRUCTIVE_API for ordinary verification.

The upstream header is pinned at vendor/glfw3.h, including its original license. Regenerate with crystal run scripts/generate.cr, then crystal tool format src scripts spec examples. The generator is Crystal and needs no network or C++ bridge. This replaces the former separate LibGLFW and CrystGLFW shards; downstream dependencies should use glfw.

References: GLFW introduction and threading rules, window guide, input guide.

Repository

glfw.cr

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

MIT License

Links
Synced at

Wed, 02 Dec 2026 02:09:08 GMT

Languages