Class: SFML::RenderWindow

Inherits:
Object
  • Object
show all
Includes:
Graphics::RenderTarget
Defined in:
lib/sfml/graphics/render_window.rb

Overview

The main drawing surface. Wraps sfRenderWindow.

window = SFML::RenderWindow.new(800, 600, "Hello")

while window.open?
  window.each_event do |event|
    case event
    in {type: :closed}                     then window.close
    in {type: :key_pressed, code: :escape} then window.close
    end
  end

  window.clear(SFML::Color.cornflower_blue)
  window.display
end

Constant Summary collapse

CSFML_PREFIX =
:sfRenderWindow
DEFAULT_STYLE =
C::Window::Style::DEFAULT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Graphics::RenderTarget

#clear, #default_view, #display, #draw, #draw_primitives, #map_coords_to_pixel, #map_pixel_to_coords, #view, #view=

Constructor Details

#initialize(*args, **opts) ⇒ RenderWindow

The first form takes (width, height, title, **opts). The second form takes (video_mode, title, **opts) for full control.

Options:

style:      bitmask of SFML::C::Window::Style constants
fullscreen: true to use sfFullscreen state instead of sfWindowed
framerate:  cap to N FPS via sfRenderWindow_setFramerateLimit
vsync:      enable vertical sync

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sfml/graphics/render_window.rb', line 31

def initialize(*args, **opts)
  mode, title = parse_args(args)
  style = opts.fetch(:style, DEFAULT_STYLE)
  state = opts[:fullscreen] ? :fullscreen : :windowed

  ptr = C::Graphics.sfRenderWindow_create(
    mode.to_native,
    title.to_s,
    style,
    C::Window::State[state],
    nil,
  )
  raise Error, "sfRenderWindow_create returned NULL" if ptr.null?

  @handle = FFI::AutoPointer.new(ptr, C::Graphics.method(:sfRenderWindow_destroy))
  @event_buffer = C::Window::Event.new

  self.framerate_limit = opts[:framerate] if opts[:framerate]
  self.vsync = opts[:vsync]               unless opts[:vsync].nil?
end

Instance Attribute Details

#handleObject (readonly)

:nodoc:



181
182
183
# File 'lib/sfml/graphics/render_window.rb', line 181

def handle
  @handle
end

Class Method Details

.from_handle(handle) ⇒ Object

Wrap an existing OS-level window. ‘handle` is a platform native handle (Integer address or FFI::Pointer). Useful for embedding the renderer inside another framework’s window (Qt, Gtk, raw Win32, NSView).

Raises:



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/sfml/graphics/render_window.rb', line 149

def self.from_handle(handle)
  ptr = handle.is_a?(FFI::Pointer) ? handle : FFI::Pointer.new(:void, Integer(handle))
  raw = C::Graphics.sfRenderWindow_createFromHandle(ptr, nil)
  raise Error, "sfRenderWindow_createFromHandle returned NULL" if raw.null?

  win = allocate
  win.instance_variable_set(:@handle,
    FFI::AutoPointer.new(raw, C::Graphics.method(:sfRenderWindow_destroy)))
  win.instance_variable_set(:@event_buffer, C::Window::Event.new)
  win
end

Instance Method Details

#closeObject



56
57
58
59
# File 'lib/sfml/graphics/render_window.rb', line 56

def close
  C::Graphics.sfRenderWindow_close(@handle)
  self
end

#cursor=(cursor) ⇒ Object

Apply a SFML::Cursor as the visible mouse pointer over this window. Keeps a Ruby reference so the Cursor object’s lifetime spans at least until the next assignment.

Raises:

  • (ArgumentError)


84
85
86
87
88
# File 'lib/sfml/graphics/render_window.rb', line 84

def cursor=(cursor)
  raise ArgumentError, "RenderWindow#cursor= requires a SFML::Cursor" unless cursor.is_a?(Cursor)
  C::Graphics.sfRenderWindow_setMouseCursor(@handle, cursor.handle)
  @cursor = cursor
end

#cursor_grabbed=(grabbed) ⇒ Object

Lock the mouse pointer inside the window’s client area while focused — useful for FPS-style games or when dragging widgets that need pixel-precise input.



98
99
100
# File 'lib/sfml/graphics/render_window.rb', line 98

def cursor_grabbed=(grabbed)
  C::Graphics.sfRenderWindow_setMouseCursorGrabbed(@handle, grabbed ? true : false)
end

#cursor_visible=(visible) ⇒ Object

Toggle the OS mouse pointer’s visibility while it’s over the window.



91
92
93
# File 'lib/sfml/graphics/render_window.rb', line 91

def cursor_visible=(visible)
  C::Graphics.sfRenderWindow_setMouseCursorVisible(@handle, visible ? true : false)
end

#each_eventObject

Yields every pending event for this frame, then returns. Without a block returns an Enumerator.



69
70
71
72
73
74
75
# File 'lib/sfml/graphics/render_window.rb', line 69

def each_event
  return enum_for(:each_event) unless block_given?
  while (event = poll_event)
    yield event
  end
  self
end

#framerate_limit=(value) ⇒ Object



102
103
104
# File 'lib/sfml/graphics/render_window.rb', line 102

def framerate_limit=(value)
  C::Graphics.sfRenderWindow_setFramerateLimit(@handle, Integer(value))
end

#icon=(image) ⇒ Object

Replace the window’s title-bar / taskbar icon with the pixels from the given SFML::Image. The OS scales it as needed; 32×32 RGBA is the typical sweet spot.

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
125
# File 'lib/sfml/graphics/render_window.rb', line 118

def icon=(image)
  raise ArgumentError, "RenderWindow#icon= requires a SFML::Image" unless image.is_a?(Image)

  size = C::System::Vector2u.new
  size[:x] = image.width
  size[:y] = image.height
  C::Graphics.sfRenderWindow_setIcon(@handle, size, C::Graphics.sfImage_getPixelsPtr(image.handle))
end

#maximum_size=(value) ⇒ Object



135
136
137
# File 'lib/sfml/graphics/render_window.rb', line 135

def maximum_size=(value)
  C::Graphics.sfRenderWindow_setMaximumSize(@handle, _vec2u_or_nil(value))
end

#minimum_size=(value) ⇒ Object

Constrain user-driven resizes. Accepts a [w, h] Array, a Vector2, or nil to clear the limit. When set, the OS won’t let the user drag the window smaller (or larger) than this — programmatic ‘size=` is not affected.



131
132
133
# File 'lib/sfml/graphics/render_window.rb', line 131

def minimum_size=(value)
  C::Graphics.sfRenderWindow_setMinimumSize(@handle, _vec2u_or_nil(value))
end

#native_handleObject

OS-specific native handle for the underlying window — ‘HWND` on Windows, `NSView*` on macOS, X11 `Window` xid on Linux.



141
142
143
# File 'lib/sfml/graphics/render_window.rb', line 141

def native_handle
  C::Graphics.sfRenderWindow_getNativeHandle(@handle)
end

#open?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/sfml/graphics/render_window.rb', line 52

def open?
  C::Graphics.sfRenderWindow_isOpen(@handle)
end

#poll_eventObject

Returns the next pending Event or nil if the queue is empty.



62
63
64
65
# File 'lib/sfml/graphics/render_window.rb', line 62

def poll_event
  return nil unless C::Graphics.sfRenderWindow_pollEvent(@handle, @event_buffer)
  Event.from_native(@event_buffer)
end

#runObject

Convenience driver loop. Yields the per-frame delta (SFML::Time) and auto-pumps events + display. The block is responsible for #clear and any drawing.

window.run do |dt, events|
  events.each { |e| ... }
  window.clear(...)
  window.draw(...)
end


170
171
172
173
174
175
176
177
178
179
# File 'lib/sfml/graphics/render_window.rb', line 170

def run
  clock = Clock.new
  while open?
    dt = clock.restart
    events = each_event.to_a
    yield dt, events
    display
  end
  self
end

#sizeObject



110
111
112
113
# File 'lib/sfml/graphics/render_window.rb', line 110

def size
  v = C::Graphics.sfRenderWindow_getSize(@handle)
  Vector2.new(v[:x], v[:y])
end

#title=(value) ⇒ Object



77
78
79
# File 'lib/sfml/graphics/render_window.rb', line 77

def title=(value)
  C::Graphics.sfRenderWindow_setTitle(@handle, value.to_s)
end

#vsync=(enabled) ⇒ Object



106
107
108
# File 'lib/sfml/graphics/render_window.rb', line 106

def vsync=(enabled)
  C::Graphics.sfRenderWindow_setVerticalSyncEnabled(@handle, enabled ? true : false)
end