Class: WGPU::Window::SDLWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/wgpu/window.rb

Constant Summary collapse

SDL_PROP_WINDOW_WIN32_HWND_POINTER =

Property names for native window handles

"SDL.window.win32.hwnd"
SDL_PROP_WINDOW_WIN32_INSTANCE_POINTER =
"SDL.window.win32.instance"
SDL_PROP_WINDOW_X11_DISPLAY_POINTER =
"SDL.window.x11.display"
SDL_PROP_WINDOW_X11_WINDOW_NUMBER =
"SDL.window.x11.window"
SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER =
"SDL.window.wayland.display"
SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER =
"SDL.window.wayland.surface"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, width: 800, height: 600, resizable: true) ⇒ SDLWindow

Creates an SDL window suitable for WebGPU presentation.

Parameters:

  • title (String)

    window title

  • width (Integer) (defaults to: 800)

    initial logical width

  • height (Integer) (defaults to: 600)

    initial logical height

  • resizable (Boolean) (defaults to: true)

    whether the user may resize the window



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wgpu/window.rb', line 30

def initialize(title:, width: 800, height: 600, resizable: true)
  @width = width
  @height = height
  @metal_view = nil

  flags = 0
  flags |= SDL3::Raw::SDL_WINDOW_RESIZABLE if resizable
  flags |= SDL3::Raw::SDL_WINDOW_HIGH_PIXEL_DENSITY if macos?
  flags |= SDL3::Raw::SDL_WINDOW_METAL if macos?

  @window = SDL3::Window.new(title, width, height, flags)
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



15
16
17
# File 'lib/wgpu/window.rb', line 15

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



15
16
17
# File 'lib/wgpu/window.rb', line 15

def width
  @width
end

#windowObject (readonly)

Returns the value of attribute window.



15
16
17
# File 'lib/wgpu/window.rb', line 15

def window
  @window
end

Instance Method Details

#closevoid

This method returns an undefined value.

Destroys platform-specific resources and the SDL window.



113
114
115
116
117
118
119
# File 'lib/wgpu/window.rb', line 113

def close
  if @metal_view && !@metal_view.null?
    SDL3::Raw.SDL_Metal_DestroyView(@metal_view)
    @metal_view = nil
  end
  @window.destroy if @window
end

#create_surface(instance) ⇒ Surface

Creates a presentation surface for this native window.

Parameters:

  • instance (Instance)

    WebGPU instance that owns the surface

Returns:

  • (Surface)

    platform-specific presentation surface

Raises:

  • (WindowError)

    if the platform or native window handle is unsupported



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wgpu/window.rb', line 48

def create_surface(instance)
  case platform
  when :macos
    create_metal_surface(instance)
  when :windows
    create_windows_surface(instance)
  when :linux_x11
    create_x11_surface(instance)
  when :linux_wayland
    create_wayland_surface(instance)
  else
    raise WindowError, "Unsupported platform: #{RbConfig::CONFIG["host_os"]}"
  end
end

#drawable_sizeArray(Integer, Integer)

Returns the drawable size in physical pixels.

Returns:

  • (Array(Integer, Integer))

    width and height



104
105
106
107
108
# File 'lib/wgpu/window.rb', line 104

def drawable_size
  @window.size_in_pixels
rescue
  [@width, @height]
end

#key_pressed?(events, key) ⇒ Boolean

Reports whether a key-down event matches the requested key.

Parameters:

  • events (Array)

    SDL events to inspect

  • key (Symbol, Integer)

    common key name or SDL scancode

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wgpu/window.rb', line 85

def key_pressed?(events, key)
  scancode = case key
             when :escape then SDL3::Raw::SDL_SCANCODE_ESCAPE
             when :space then SDL3::Raw::SDL_SCANCODE_SPACE
             when :return, :enter then SDL3::Raw::SDL_SCANCODE_RETURN
             else key
             end

  events.any? do |event|
    next unless event.key_down?

    event_scancode = event.raw[:key][:scancode] rescue nil
    event_scancode == scancode
  end
end

#poll_eventsArray

Collects currently pending SDL events.

Returns:

  • (Array)

    events in delivery order



66
67
68
69
70
71
72
# File 'lib/wgpu/window.rb', line 66

def poll_events
  events = []
  SDL3::Event.each do |event|
    events << event
  end
  events
end

#should_close?(events) ⇒ Boolean

Reports whether a quit event is present.

Parameters:

  • events (Array)

    SDL events to inspect

Returns:

  • (Boolean)


77
78
79
# File 'lib/wgpu/window.rb', line 77

def should_close?(events)
  events.any?(&:quit?)
end