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
antialiasing: shorthand — MSAA level (2 / 4 / 8). Same as
              passing `context: ContextSettings.new(antialiasing: N)`
context:      a SFML::ContextSettings for full GL-context control

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sfml/graphics/render_window.rb', line 34

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

  settings = _resolve_context_settings(opts)
  # Hold a reference for the duration of the C call so the
  # struct's memory survives until CSFML has copied it.
  @ctx_struct = settings ? settings.to_native : nil
  ctx_ptr     = @ctx_struct ? @ctx_struct.to_ptr : nil

  ptr = C::Graphics.sfRenderWindow_create(
    mode.to_native,
    title.to_s,
    style,
    C::Window::State[state],
    ctx_ptr,
  )
  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
  @requested_context = settings

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

Instance Attribute Details

#handleObject (readonly)

:nodoc:



201
202
203
# File 'lib/sfml/graphics/render_window.rb', line 201

def handle
  @handle
end

#requested_contextObject (readonly)

What we asked for at creation time, if anything (otherwise nil).



70
71
72
# File 'lib/sfml/graphics/render_window.rb', line 70

def requested_context
  @requested_context
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:



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

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



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

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

#context_settingsObject

The actual ContextSettings the driver gave us. May differ from what we requested — the driver picks the closest level of MSAA / GL version it supports.



65
66
67
# File 'lib/sfml/graphics/render_window.rb', line 65

def context_settings
  ContextSettings.from_native(C::Graphics.sfRenderWindow_getSettings(@handle))
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)


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

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.



118
119
120
# File 'lib/sfml/graphics/render_window.rb', line 118

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.



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

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.



89
90
91
92
93
94
95
# File 'lib/sfml/graphics/render_window.rb', line 89

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

#framerate_limit=(value) ⇒ Object



122
123
124
# File 'lib/sfml/graphics/render_window.rb', line 122

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)


138
139
140
141
142
143
144
145
# File 'lib/sfml/graphics/render_window.rb', line 138

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



155
156
157
# File 'lib/sfml/graphics/render_window.rb', line 155

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.



151
152
153
# File 'lib/sfml/graphics/render_window.rb', line 151

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.



161
162
163
# File 'lib/sfml/graphics/render_window.rb', line 161

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

#open?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/sfml/graphics/render_window.rb', line 72

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

#poll_eventObject

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



82
83
84
85
# File 'lib/sfml/graphics/render_window.rb', line 82

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


190
191
192
193
194
195
196
197
198
199
# File 'lib/sfml/graphics/render_window.rb', line 190

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

#sizeObject



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

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

#title=(value) ⇒ Object



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

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

#vsync=(enabled) ⇒ Object



126
127
128
# File 'lib/sfml/graphics/render_window.rb', line 126

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