Skip to content

Conversation

@rudrabeniwal
Copy link
Collaborator

Window Management with GLFW

Summary

This pr adds a new window management system to the Cyfra project, under the io.computenode.cyfra.window package. The main goal is to separate window handling from the Vulkan rendering logic and give us a consistent way to manage windows across platforms.

The initial version includes a general WindowSystem trait for abstraction, and a working GLFW-based implementation called GLFWWindowSystem.

Features

  • Window Lifecycle Management: You can now easily create, display, move, and close windows using a simpler API. The system also makes sure GLFW is cleaned up properly when done.
  • Event Handling: A unified way to handle events like resizing, key presses, mouse input, scrolling, and closing. All events are queued and processed consistently.
  • GLFW Backend Implementation: GLFWWindowSystem provides a good implementation using the GLFW library.
  • Vulkan Surface Integration: The WindowHandle (specifically GLFWWindowHandle) is designed for easy Vulkan surface creation, exposing the native window pointer (nativePtr) required to create a VkSurfaceKHR.
  • Demo Application: Includes WindowSystemTest to demonstrate the system's functionality.

Implementation Details

1. Architecture

  • The core abstraction is the WindowSystem trait defining methods like createWindow, pollEvents, and shouldWindowClose.
  • GLFWWindowSystem handles all GLFW-specific stuff like initializing the library, setting platform hints, and wiring up callbacks.
  • WindowHandle (implemented as GLFWWindowHandle) gives access to the raw window pointer, which is needed for Vulkan surface creation.
  • There's a utility object called GLFWSystem that handles GLFW init and error checks, and ensures Vulkan compatibility.

2. Event System

  • GLFW callbacks (e.g., GLFWKeyCallback, GLFWCursorPosCallback) are registered within GLFWWindowSystem.setupCallbacks.
  • These callbacks convert GLFW events into custom WindowEvent types (like Key, Resize, etc.).
  • Events are added to a thread-safe queue ConcurrentLinkedQueue.
  • The pollEvents method calls GLFW.glfwPollEvents() to trigger callbacks, then queue is drained and events are returned as a list to process (assumes single-threaded usage).

3. Vulkan Integration

  • Sets GLFW_CLIENT_API to GLFW_NO_API so Vulkan can handle rendering.
  • WindowHandle.nativePtr provides the underlying GLFWwindow pointer, essential for creating the Vulkan VkSurfaceKHR via glfwCreateWindowSurface, which connects the window to Vulkan’s rendering system (swapchain).

Testing

  • Includes WindowSystemTest, a runnable standalone test/demo application.
  • Run command: sbt "runMain io.computenode.cyfra.window.WindowSystemTest".
  • Manual Verification Performed On: Windows.
    • Verified window creation, visibility, positioning, closing correctly (via button and events).
    • Confirmed correct logging and handling of Resize, Keyboard, MouseButton, MouseMove, Scroll, and Close events.

Potential Issues & Limitations

  • Currently, only the GLFW backend (GLFWWindowSystem) is implemented and supported.
  • The current design does not explicitly support managing multiple windows simultaneously, though the architecture might allow it.
  • High-DPI handling is basic and mostly depends on OS settings.
  • Might run into OS-specific issues (not tested on macOS or Linux)

Future Work

  • Add support for managing multiple application windows, potentially with independent event loops.
  • Maybe support other backends like SDL2 or native APIs (Win32, etc.).
  • Maybe add drag-and-drop support.

This provides a base for window management in Cyfra.

@rudrabeniwal rudrabeniwal changed the title Implement GLFW window system with event handling and Vulkan support Implement GLFW window system Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant