class SFML::RenderWindow
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
Constants
- CSFML_PREFIX
- DEFAULT_STYLE
-
Default window style bitmask.
Attributes
What we asked for at creation time, if anything (otherwise nil).
Public Class Methods
Source
# File lib/sfml/graphics/render_window.rb, line 298 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 WindowError, "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
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).
Source
# File lib/sfml/graphics/render_window.rb, line 35 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 WindowError, "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
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
Public Instance Methods
Source
# File lib/sfml/graphics/render_window.rb, line 334 def _resolve_context_settings(opts) if opts[:context] unless opts[:context].is_a?(ContextSettings) raise ArgumentError, "context: must be a SFML::ContextSettings" end opts[:context] elsif opts[:antialiasing] ContextSettings.new(antialiasing: opts[:antialiasing]) end end
Source
# File lib/sfml/graphics/render_window.rb, line 345 def _vec2u_or_nil(value) return nil if value.nil? vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2u.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) v end
Source
# File lib/sfml/graphics/render_window.rb, line 255 def active=(value) C::Graphics.sfRenderWindow_setActive(@handle, value ? true : false) end
Activate / deactivate the window’s GL context on the current thread. Only one context can be active per thread at a time.
Source
# File lib/sfml/graphics/render_window.rb, line 162 def capture_image w, h = size.x, size.y tex = Texture.create(w, h) tex.update_from_render_window(self) tex.to_image end
Read the current back-buffer into a fresh CPU-side SFML::Image. Useful for processing/encoding the frame yourself (sending it over a socket, encoding as JPEG memory bytes, comparing against a reference, etc.).
Source
# File lib/sfml/graphics/render_window.rb, line 81 def close C::Graphics.sfRenderWindow_close(@handle) self end
Close the window — the next iteration of the main loop will see #open? == false.
Source
# File lib/sfml/graphics/render_window.rb, line 66 def context_settings ContextSettings.from_native(C::Graphics.sfRenderWindow_getSettings(@handle)) end
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.
Source
# File lib/sfml/graphics/render_window.rb, line 110 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
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.
Source
# File lib/sfml/graphics/render_window.rb, line 124 def cursor_grabbed=(grabbed) C::Graphics.sfRenderWindow_setMouseCursorGrabbed(@handle, grabbed ? true : false) end
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.
Source
# File lib/sfml/graphics/render_window.rb, line 117 def cursor_visible=(visible) C::Graphics.sfRenderWindow_setMouseCursorVisible(@handle, visible ? true : false) end
Toggle the OS mouse pointer’s visibility while it’s over the window.
Source
# File lib/sfml/graphics/render_window.rb, line 94 def each_event return enum_for(:each_event) unless block_given? while (event = poll_event) yield event end self end
Yields every pending event for this frame, then returns. Without a block returns an Enumerator.
Source
# File lib/sfml/graphics/render_window.rb, line 203 def focused? = C::Graphics.sfRenderWindow_hasFocus(@handle) # Ask the OS to give this window focus. Cooperative — most # window managers won't steal focus unconditionally. def request_focus = C::Graphics.sfRenderWindow_requestFocus(@handle) # ---- OS-window state ---- def visible=(value) C::Graphics.sfRenderWindow_setVisible(@handle, value ? true : false) end # Toggle key repeat. With `true`, holding a key fires repeated # `:key_pressed` events; with `false` only one fires per press. def key_repeat_enabled=(value) C::Graphics.sfRenderWindow_setKeyRepeatEnabled(@handle, value ? true : false) end # Dead-zone for joystick axis events in [0, 100]. Axes whose # absolute value is below this are reported as 0. def joystick_threshold=(value) C::Graphics.sfRenderWindow_setJoystickThreshold(@handle, Float(value)) end # Top-left corner in desktop coordinates. def position Vector2.from_native(C::Graphics.sfRenderWindow_getPosition(@handle)) end # Move the window's top-left corner in desktop coordinates. def position=(value) vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2i.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) C::Graphics.sfRenderWindow_setPosition(@handle, v) end # `true` if the framebuffer is sRGB-capable (i.e. the GL # context was created with the sRGB attribute and the driver # honoured it). def srgb? = C::Graphics.sfRenderWindow_isSrgb(@handle) # ---- GL interop ---- # # When mixing raw OpenGL calls with SFML rendering, surround # the OpenGL block with `push_gl_states` / `pop_gl_states` so # SFML's internal state survives. `reset_gl_states` is a # heavier "throw away whatever's been changed" reset. # `active=` toggles the GL context's activation on the # current thread — the only way to use SFML rendering from a # non-main thread. # Activate / deactivate the window's GL context on the current # thread. Only one context can be active per thread at a time. def active=(value) C::Graphics.sfRenderWindow_setActive(@handle, value ? true : false) end # Save SFML's GL state before raw GL calls. def push_gl_states = C::Graphics.sfRenderWindow_pushGLStates(@handle) # Restore SFML's GL state after raw GL calls (paired with # `#push_gl_states`). def pop_gl_states = C::Graphics.sfRenderWindow_popGLStates(@handle) # Re-initialise SFML's GL state from scratch — heavier hammer # than push/pop. def reset_gl_states = C::Graphics.sfRenderWindow_resetGLStates(@handle) # Block until an event arrives or `timeout` (a SFML::Time) # elapses. Returns the next pending Event or nil on timeout. # Useful for low-power apps that don't need to redraw at # 60fps — wake on input. def wait_event(timeout: nil) t = timeout || Time.zero ok = C::Graphics.sfRenderWindow_waitEvent(@handle, t.to_native, @event_buffer) return nil unless ok Event.from_native(@event_buffer) end # The pixel-space rect a `view` projects onto inside this # window. Combines the view's normalised viewport with the # window's pixel size. def viewport(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getViewport(@handle, view.handle)) end # The pixel-space scissor rect — same idea as `viewport` but # for the view's `scissor` property. Pixels outside this rect # are clipped before rendering. def scissor(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getScissor(@handle, view.handle)) end # 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). 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 WindowError, "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 # 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 def run clock = Clock.new while open? dt = clock.restart events = each_event.to_a yield dt, events display end self end attr_reader :handle # :nodoc: private def _resolve_context_settings(opts) if opts[:context] unless opts[:context].is_a?(ContextSettings) raise ArgumentError, "context: must be a SFML::ContextSettings" end opts[:context] elsif opts[:antialiasing] ContextSettings.new(antialiasing: opts[:antialiasing]) end end def _vec2u_or_nil(value) return nil if value.nil? vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2u.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) v end # Returns the parse args. def parse_args(args) case args.length when 2 # (video_mode, title) [args[0], args[1]] when 3 # (width, height, title) [VideoMode.new(args[0], args[1]), args[2]] else raise ArgumentError, "RenderWindow.new takes either (video_mode, title) or " \ "(width, height, title), got #{args.length} positional arg(s)" end
true if this window currently has OS-level keyboard focus.
Source
# File lib/sfml/graphics/render_window.rb, line 130 def framerate_limit=(value) C::Graphics.sfRenderWindow_setFramerateLimit(@handle, Integer(value)) end
Cap the render loop to value frames per second. Set 0 to disable the cap.
Source
# File lib/sfml/graphics/render_window.rb, line 172 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
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.
Source
# File lib/sfml/graphics/render_window.rb, line 221 def joystick_threshold=(value) C::Graphics.sfRenderWindow_setJoystickThreshold(@handle, Float(value)) end
Dead-zone for joystick axis events in [0, 100]. Axes whose absolute value is below this are reported as 0.
Source
# File lib/sfml/graphics/render_window.rb, line 215 def key_repeat_enabled=(value) C::Graphics.sfRenderWindow_setKeyRepeatEnabled(@handle, value ? true : false) end
Toggle key repeat. With true, holding a key fires repeated :key_pressed events; with false only one fires per press.
Source
# File lib/sfml/graphics/render_window.rb, line 190 def maximum_size=(value) C::Graphics.sfRenderWindow_setMaximumSize(@handle, _vec2u_or_nil(value)) end
Upper bound on user-driven resizes — see minimum_size=.
Source
# File lib/sfml/graphics/render_window.rb, line 185 def minimum_size=(value) C::Graphics.sfRenderWindow_setMinimumSize(@handle, _vec2u_or_nil(value)) end
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.
Source
# File lib/sfml/graphics/render_window.rb, line 196 def native_handle C::Graphics.sfRenderWindow_getNativeHandle(@handle) end
OS-specific native handle for the underlying window — HWND on Windows, NSView* on macOS, X11 Window xid on Linux.
Source
# File lib/sfml/graphics/render_window.rb, line 75 def open? C::Graphics.sfRenderWindow_isOpen(@handle) end
true while the window is still alive (hasn’t been closed by close or the user).
Source
# File lib/sfml/graphics/render_window.rb, line 355 def parse_args(args) case args.length when 2 # (video_mode, title) [args[0], args[1]] when 3 # (width, height, title) [VideoMode.new(args[0], args[1]), args[2]] else raise ArgumentError, "RenderWindow.new takes either (video_mode, title) or " \ "(width, height, title), got #{args.length} positional arg(s)" end end
Returns the parse args.
Source
# File lib/sfml/graphics/render_window.rb, line 87 def poll_event return nil unless C::Graphics.sfRenderWindow_pollEvent(@handle, @event_buffer) Event.from_native(@event_buffer) end
Returns the next pending Event or nil if the queue is empty.
Source
# File lib/sfml/graphics/render_window.rb, line 262 def pop_gl_states = C::Graphics.sfRenderWindow_popGLStates(@handle) # Re-initialise SFML's GL state from scratch — heavier hammer # than push/pop. def reset_gl_states = C::Graphics.sfRenderWindow_resetGLStates(@handle) # Block until an event arrives or `timeout` (a SFML::Time) # elapses. Returns the next pending Event or nil on timeout. # Useful for low-power apps that don't need to redraw at # 60fps — wake on input. def wait_event(timeout: nil) t = timeout || Time.zero ok = C::Graphics.sfRenderWindow_waitEvent(@handle, t.to_native, @event_buffer) return nil unless ok Event.from_native(@event_buffer) end # The pixel-space rect a `view` projects onto inside this # window. Combines the view's normalised viewport with the # window's pixel size. def viewport(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getViewport(@handle, view.handle)) end # The pixel-space scissor rect — same idea as `viewport` but # for the view's `scissor` property. Pixels outside this rect # are clipped before rendering. def scissor(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getScissor(@handle, view.handle)) end # 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). 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 WindowError, "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 # 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 def run clock = Clock.new while open? dt = clock.restart events = each_event.to_a yield dt, events display end self end attr_reader :handle # :nodoc: private def _resolve_context_settings(opts) if opts[:context] unless opts[:context].is_a?(ContextSettings) raise ArgumentError, "context: must be a SFML::ContextSettings" end opts[:context] elsif opts[:antialiasing] ContextSettings.new(antialiasing: opts[:antialiasing]) end end def _vec2u_or_nil(value) return nil if value.nil? vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2u.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) v end # Returns the parse args. def parse_args(args) case args.length when 2 # (video_mode, title) [args[0], args[1]] when 3 # (width, height, title) [VideoMode.new(args[0], args[1]), args[2]] else raise ArgumentError, "RenderWindow.new takes either (video_mode, title) or " \ "(width, height, title), got #{args.length} positional arg(s)" end end end end
Source
# File lib/sfml/graphics/render_window.rb, line 226 def position Vector2.from_native(C::Graphics.sfRenderWindow_getPosition(@handle)) end
Top-left corner in desktop coordinates.
Source
# File lib/sfml/graphics/render_window.rb, line 231 def position=(value) vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2i.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) C::Graphics.sfRenderWindow_setPosition(@handle, v) end
Move the window’s top-left corner in desktop coordinates.
Source
# File lib/sfml/graphics/render_window.rb, line 259 def push_gl_states = C::Graphics.sfRenderWindow_pushGLStates(@handle) # Restore SFML's GL state after raw GL calls (paired with # `#push_gl_states`). def pop_gl_states = C::Graphics.sfRenderWindow_popGLStates(@handle) # Re-initialise SFML's GL state from scratch — heavier hammer # than push/pop. def reset_gl_states = C::Graphics.sfRenderWindow_resetGLStates(@handle) # Block until an event arrives or `timeout` (a SFML::Time) # elapses. Returns the next pending Event or nil on timeout. # Useful for low-power apps that don't need to redraw at # 60fps — wake on input. def wait_event(timeout: nil) t = timeout || Time.zero ok = C::Graphics.sfRenderWindow_waitEvent(@handle, t.to_native, @event_buffer) return nil unless ok Event.from_native(@event_buffer) end # The pixel-space rect a `view` projects onto inside this # window. Combines the view's normalised viewport with the # window's pixel size. def viewport(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getViewport(@handle, view.handle)) end # The pixel-space scissor rect — same idea as `viewport` but # for the view's `scissor` property. Pixels outside this rect # are clipped before rendering. def scissor(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getScissor(@handle, view.handle)) end # 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). 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 WindowError, "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 # 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 def run clock = Clock.new while open? dt = clock.restart events = each_event.to_a yield dt, events display end self end attr_reader :handle # :nodoc: private def _resolve_context_settings(opts) if opts[:context] unless opts[:context].is_a?(ContextSettings) raise ArgumentError, "context: must be a SFML::ContextSettings" end opts[:context] elsif opts[:antialiasing] ContextSettings.new(antialiasing: opts[:antialiasing]) end end def _vec2u_or_nil(value) return nil if value.nil? vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2u.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) v end # Returns the parse args. def parse_args(args) case args.length when 2 # (video_mode, title) [args[0], args[1]] when 3 # (width, height, title) [VideoMode.new(args[0], args[1]), args[2]] else raise ArgumentError, "RenderWindow.new takes either (video_mode, title) or " \ "(width, height, title), got #{args.length} positional arg(s)" end end end
Save SFML’s GL state before raw GL calls.
Source
# File lib/sfml/graphics/render_window.rb, line 206 def request_focus = C::Graphics.sfRenderWindow_requestFocus(@handle) # ---- OS-window state ---- def visible=(value) C::Graphics.sfRenderWindow_setVisible(@handle, value ? true : false) end # Toggle key repeat. With `true`, holding a key fires repeated # `:key_pressed` events; with `false` only one fires per press. def key_repeat_enabled=(value) C::Graphics.sfRenderWindow_setKeyRepeatEnabled(@handle, value ? true : false) end # Dead-zone for joystick axis events in [0, 100]. Axes whose # absolute value is below this are reported as 0. def joystick_threshold=(value) C::Graphics.sfRenderWindow_setJoystickThreshold(@handle, Float(value)) end # Top-left corner in desktop coordinates. def position Vector2.from_native(C::Graphics.sfRenderWindow_getPosition(@handle)) end # Move the window's top-left corner in desktop coordinates. def position=(value) vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2i.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) C::Graphics.sfRenderWindow_setPosition(@handle, v) end # `true` if the framebuffer is sRGB-capable (i.e. the GL # context was created with the sRGB attribute and the driver # honoured it). def srgb? = C::Graphics.sfRenderWindow_isSrgb(@handle) # ---- GL interop ---- # # When mixing raw OpenGL calls with SFML rendering, surround # the OpenGL block with `push_gl_states` / `pop_gl_states` so # SFML's internal state survives. `reset_gl_states` is a # heavier "throw away whatever's been changed" reset. # `active=` toggles the GL context's activation on the # current thread — the only way to use SFML rendering from a # non-main thread. # Activate / deactivate the window's GL context on the current # thread. Only one context can be active per thread at a time. def active=(value) C::Graphics.sfRenderWindow_setActive(@handle, value ? true : false) end # Save SFML's GL state before raw GL calls. def push_gl_states = C::Graphics.sfRenderWindow_pushGLStates(@handle) # Restore SFML's GL state after raw GL calls (paired with # `#push_gl_states`). def pop_gl_states = C::Graphics.sfRenderWindow_popGLStates(@handle) # Re-initialise SFML's GL state from scratch — heavier hammer # than push/pop. def reset_gl_states = C::Graphics.sfRenderWindow_resetGLStates(@handle) # Block until an event arrives or `timeout` (a SFML::Time) # elapses. Returns the next pending Event or nil on timeout. # Useful for low-power apps that don't need to redraw at # 60fps — wake on input. def wait_event(timeout: nil) t = timeout || Time.zero ok = C::Graphics.sfRenderWindow_waitEvent(@handle, t.to_native, @event_buffer) return nil unless ok Event.from_native(@event_buffer) end # The pixel-space rect a `view` projects onto inside this # window. Combines the view's normalised viewport with the # window's pixel size. def viewport(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getViewport(@handle, view.handle)) end # The pixel-space scissor rect — same idea as `viewport` but # for the view's `scissor` property. Pixels outside this rect # are clipped before rendering. def scissor(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getScissor(@handle, view.handle)) end # 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). 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 WindowError, "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 # 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 def run clock = Clock.new while open? dt = clock.restart events = each_event.to_a yield dt, events display end self end attr_reader :handle # :nodoc: private def _resolve_context_settings(opts) if opts[:context] unless opts[:context].is_a?(ContextSettings) raise ArgumentError, "context: must be a SFML::ContextSettings" end opts[:context] elsif opts[:antialiasing] ContextSettings.new(antialiasing: opts[:antialiasing]) end end def _vec2u_or_nil(value) return nil if value.nil? vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2u.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) v end # Returns the parse args. def parse_args(args) case args.length when 2 # (video_mode, title) [args[0], args[1]] when 3 # (width, height, title) [VideoMode.new(args[0], args[1]), args[2]] else raise ArgumentError, "RenderWindow.new takes either (video_mode, title) or " \ "(width, height, title), got #{args.length} positional arg(s)" end
window managers won’t steal focus unconditionally.
Source
# File lib/sfml/graphics/render_window.rb, line 265 def reset_gl_states = C::Graphics.sfRenderWindow_resetGLStates(@handle) # Block until an event arrives or `timeout` (a SFML::Time) # elapses. Returns the next pending Event or nil on timeout. # Useful for low-power apps that don't need to redraw at # 60fps — wake on input. def wait_event(timeout: nil) t = timeout || Time.zero ok = C::Graphics.sfRenderWindow_waitEvent(@handle, t.to_native, @event_buffer) return nil unless ok Event.from_native(@event_buffer) end # The pixel-space rect a `view` projects onto inside this # window. Combines the view's normalised viewport with the # window's pixel size. def viewport(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getViewport(@handle, view.handle)) end # The pixel-space scissor rect — same idea as `viewport` but # for the view's `scissor` property. Pixels outside this rect # are clipped before rendering. def scissor(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getScissor(@handle, view.handle)) end # 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). 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 WindowError, "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 # 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 def run clock = Clock.new while open? dt = clock.restart events = each_event.to_a yield dt, events display end self end attr_reader :handle # :nodoc: private def _resolve_context_settings(opts) if opts[:context] unless opts[:context].is_a?(ContextSettings) raise ArgumentError, "context: must be a SFML::ContextSettings" end opts[:context] elsif opts[:antialiasing] ContextSettings.new(antialiasing: opts[:antialiasing]) end end def _vec2u_or_nil(value) return nil if value.nil? vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2u.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) v end # Returns the parse args. def parse_args(args) case args.length when 2 # (video_mode, title) [args[0], args[1]] when 3 # (width, height, title) [VideoMode.new(args[0], args[1]), args[2]] else raise ArgumentError, "RenderWindow.new takes either (video_mode, title) or " \ "(width, height, title), got #{args.length} positional arg(s)" end end end
than push/pop.
Source
# File lib/sfml/graphics/render_window.rb, line 319 def run clock = Clock.new while open? dt = clock.restart events = each_event.to_a yield dt, events display end self end
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
Source
# File lib/sfml/graphics/render_window.rb, line 289 def scissor(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getScissor(@handle, view.handle)) end
The pixel-space scissor rect — same idea as viewport but for the view’s scissor property. Pixels outside this rect are clipped before rendering.
Source
# File lib/sfml/graphics/render_window.rb, line 153 def screenshot(path, format: nil) capture_image.save(path.to_s) path end
Capture the current back-buffer to disk. The format is inferred from the file extension (png / jpg / bmp / tga supported by CSFML’s stb_image-based saver). Returns the path.
window.screenshot(“screenshot.png”)
format: lets you save under a different name than the extension. For an in-memory copy, use capture_image instead.
Source
# File lib/sfml/graphics/render_window.rb, line 140 def size v = C::Graphics.sfRenderWindow_getSize(@handle) Vector2.new(v[:x], v[:y]) end
Current window size in pixels as a Vector2.
Source
# File lib/sfml/graphics/render_window.rb, line 241 def srgb? = C::Graphics.sfRenderWindow_isSrgb(@handle) # ---- GL interop ---- # # When mixing raw OpenGL calls with SFML rendering, surround # the OpenGL block with `push_gl_states` / `pop_gl_states` so # SFML's internal state survives. `reset_gl_states` is a # heavier "throw away whatever's been changed" reset. # `active=` toggles the GL context's activation on the # current thread — the only way to use SFML rendering from a # non-main thread. # Activate / deactivate the window's GL context on the current # thread. Only one context can be active per thread at a time. def active=(value) C::Graphics.sfRenderWindow_setActive(@handle, value ? true : false) end # Save SFML's GL state before raw GL calls. def push_gl_states = C::Graphics.sfRenderWindow_pushGLStates(@handle) # Restore SFML's GL state after raw GL calls (paired with # `#push_gl_states`). def pop_gl_states = C::Graphics.sfRenderWindow_popGLStates(@handle) # Re-initialise SFML's GL state from scratch — heavier hammer # than push/pop. def reset_gl_states = C::Graphics.sfRenderWindow_resetGLStates(@handle) # Block until an event arrives or `timeout` (a SFML::Time) # elapses. Returns the next pending Event or nil on timeout. # Useful for low-power apps that don't need to redraw at # 60fps — wake on input. def wait_event(timeout: nil) t = timeout || Time.zero ok = C::Graphics.sfRenderWindow_waitEvent(@handle, t.to_native, @event_buffer) return nil unless ok Event.from_native(@event_buffer) end # The pixel-space rect a `view` projects onto inside this # window. Combines the view's normalised viewport with the # window's pixel size. def viewport(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getViewport(@handle, view.handle)) end # The pixel-space scissor rect — same idea as `viewport` but # for the view's `scissor` property. Pixels outside this rect # are clipped before rendering. def scissor(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getScissor(@handle, view.handle)) end # 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). 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 WindowError, "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 # 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 def run clock = Clock.new while open? dt = clock.restart events = each_event.to_a yield dt, events display end self end attr_reader :handle # :nodoc: private def _resolve_context_settings(opts) if opts[:context] unless opts[:context].is_a?(ContextSettings) raise ArgumentError, "context: must be a SFML::ContextSettings" end opts[:context] elsif opts[:antialiasing] ContextSettings.new(antialiasing: opts[:antialiasing]) end end def _vec2u_or_nil(value) return nil if value.nil? vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2u.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) v end # Returns the parse args. def parse_args(args) case args.length when 2 # (video_mode, title) [args[0], args[1]] when 3 # (width, height, title) [VideoMode.new(args[0], args[1]), args[2]] else raise ArgumentError, "RenderWindow.new takes either (video_mode, title) or " \ "(width, height, title), got #{args.length} positional arg(s)" end end
true if the framebuffer is sRGB-capable (i.e. the GL context was created with the sRGB attribute and the driver honoured it).
Source
# File lib/sfml/graphics/render_window.rb, line 103 def title=(value) C::Graphics.sfRenderWindow_setTitle(@handle, value.to_s) end
Change the window’s title bar text.
Source
# File lib/sfml/graphics/render_window.rb, line 281 def viewport(view = self.view) raise ArgumentError, "expected a SFML::View" unless view.is_a?(View) Rect.from_native(C::Graphics.sfRenderWindow_getViewport(@handle, view.handle)) end
The pixel-space rect a view projects onto inside this window. Combines the view’s normalised viewport with the window’s pixel size.
Source
# File lib/sfml/graphics/render_window.rb, line 209 def visible=(value) C::Graphics.sfRenderWindow_setVisible(@handle, value ? true : false) end
—- OS-window state —-
Source
# File lib/sfml/graphics/render_window.rb, line 135 def vsync=(enabled) C::Graphics.sfRenderWindow_setVerticalSyncEnabled(@handle, enabled ? true : false) end
Enable / disable vertical sync.
Source
# File lib/sfml/graphics/render_window.rb, line 271 def wait_event(timeout: nil) t = timeout || Time.zero ok = C::Graphics.sfRenderWindow_waitEvent(@handle, t.to_native, @event_buffer) return nil unless ok Event.from_native(@event_buffer) end
Block until an event arrives or timeout (a SFML::Time) elapses. Returns the next pending Event or nil on timeout. Useful for low-power apps that don’t need to redraw at 60fps — wake on input.