class SFML::Window
A bare window with input + an OpenGL context, without SFML’s 2D batcher. Use this when you want raw OpenGL (or another rendering library) and just need SFML to manage the platform-level window and event loop. For 2D drawing with SFML’s API, you want SFML::RenderWindow instead.
win = SFML::Window.new(800, 600, “GL”) while win.open? win.each_event do |event| case event in {type: :closed} then win.close in {type: :key_pressed, code: :escape} then win.close else end end
# ... draw with raw OpenGL calls here ... win.display
end
Constants
- DEFAULT_STYLE
-
Default window style bitmask.
Public Class Methods
Source
# File lib/sfml/window/window.rb, line 235 def self.from_handle(handle) ptr = handle.is_a?(FFI::Pointer) ? handle : FFI::Pointer.new(:void, Integer(handle)) raw = C::Window.sfWindow_createFromHandle(ptr, nil) raise WindowError, "sfWindow_createFromHandle returned NULL" if raw.null? win = allocate win.instance_variable_set(:@handle, FFI::AutoPointer.new(raw, C::Window.method(:sfWindow_destroy))) win.instance_variable_set(:@event_buffer, C::Window::Event.new) win end
Source
# File lib/sfml/window/window.rb, line 28 def initialize(*args, **opts) mode, title = parse_args(args) style = opts.fetch(:style, DEFAULT_STYLE) state = opts[:fullscreen] ? :fullscreen : :windowed ptr = C::Window.sfWindow_create( mode.to_native, title.to_s, style, C::Window::State[state], nil, ) raise WindowError, "sfWindow_create returned NULL" if ptr.null? @handle = FFI::AutoPointer.new(ptr, C::Window.method(:sfWindow_destroy)) @event_buffer = C::Window::Event.new self.framerate_limit = opts[:framerate] if opts[:framerate] self.vsync = opts[:vsync] unless opts[:vsync].nil? end
Construct a Window. Same constructor forms as RenderWindow.new ((w, h, title, **opts) or (video_mode, title, **opts)).
Public Instance Methods
Source
# File lib/sfml/window/window.rb, line 150 def active=(value) C::Window.sfWindow_setActive(@handle, value ? true : false) end
Make this window’s GL context current on the calling thread. Useful when juggling multiple windows / off-screen contexts.
Source
# File lib/sfml/window/window.rb, line 56 def close C::Window.sfWindow_close(@handle) self end
Close the window.
Source
# File lib/sfml/window/window.rb, line 184 def context_settings ContextSettings.from_native(C::Window.sfWindow_getSettings(@handle)) end
The actual GL context settings the driver gave us — may differ from what was requested via ContextSettings.
Source
# File lib/sfml/window/window.rb, line 158 def cursor=(cursor) raise ArgumentError, "Window#cursor= requires a SFML::Cursor" unless cursor.is_a?(Cursor) C::Window.sfWindow_setMouseCursor(@handle, cursor.handle) @cursor = cursor # keep alive end
Apply a SFML::Cursor as the visible mouse pointer over this window. Keeps a Ruby reference so the Cursor outlives the call.
Source
# File lib/sfml/window/window.rb, line 170 def cursor_grabbed=(grabbed) C::Window.sfWindow_setMouseCursorGrabbed(@handle, grabbed ? true : false) end
Set the cursor grabbed.
Source
# File lib/sfml/window/window.rb, line 165 def cursor_visible=(visible) C::Window.sfWindow_setMouseCursorVisible(@handle, visible ? true : false) end
Set the cursor visible.
Source
# File lib/sfml/window/window.rb, line 63 def display C::Window.sfWindow_display(@handle) self end
Swap front/back buffers, presenting whatever was rendered since the last call.
Source
# File lib/sfml/window/window.rb, line 76 def each_event return enum_for(:each_event) unless block_given? while (event = poll_event) yield event end self end
Yields each pending event then returns. Without a block, returns an Enumerator.
Source
# File lib/sfml/window/window.rb, line 144 def focused? C::Window.sfWindow_hasFocus(@handle) end
true if focused.
Source
# File lib/sfml/window/window.rb, line 123 def framerate_limit=(value) C::Window.sfWindow_setFramerateLimit(@handle, Integer(value)) end
Set the framerate limit.
Source
# File lib/sfml/window/window.rb, line 201 def icon=(image) raise ArgumentError, "Window#icon= requires a SFML::Image" unless image.is_a?(SFML::Image) size = C::System::Vector2u.new size[:x] = image.width size[:y] = image.height C::Window.sfWindow_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/window/window.rb, line 178 def joystick_threshold=(value) C::Window.sfWindow_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/window/window.rb, line 133 def key_repeat_enabled=(value) C::Window.sfWindow_setKeyRepeatEnabled(@handle, value ? true : false) end
Set the key repeat enabled.
Source
# File lib/sfml/window/window.rb, line 219 def maximum_size=(value) C::Window.sfWindow_setMaximumSize(@handle, _vec2u_or_nil(value)) end
Set the maximum size.
Source
# File lib/sfml/window/window.rb, line 214 def minimum_size=(value) C::Window.sfWindow_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/window/window.rb, line 226 def native_handle C::Window.sfWindow_getNativeHandle(@handle) end
OS-specific native handle for the underlying window — HWND on Windows, NSView* on macOS, X11 Window xid on Linux. Returns an FFI::Pointer; cast or read as the platform expects.
Source
# File lib/sfml/window/window.rb, line 51 def open? C::Window.sfWindow_isOpen(@handle) end
true while the window is alive (not closed by close or the user).
Source
# File lib/sfml/window/window.rb, line 69 def poll_event return nil unless C::Window.sfWindow_pollEvent(@handle, @event_buffer) Event.from_native(@event_buffer) end
Returns the next pending Event or nil. Same shape as RenderWindow#poll_event.
Source
# File lib/sfml/window/window.rb, line 104 def position v = C::Window.sfWindow_getPosition(@handle) Vector2.new(v[:x], v[:y]) end
Window top-left in desktop coordinates.
Source
# File lib/sfml/window/window.rb, line 110 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::Window.sfWindow_setPosition(@handle, v) end
Set the position.
Source
# File lib/sfml/window/window.rb, line 139 def request_focus C::Window.sfWindow_requestFocus(@handle) end
Ask the OS to give this window focus. Cooperative — most window managers will defer until the user clicks.
Source
# File lib/sfml/window/window.rb, line 90 def size v = C::Window.sfWindow_getSize(@handle) Vector2.new(v[:x], v[:y]) end
Current window size as a Vector2.
Source
# File lib/sfml/window/window.rb, line 96 def size=(value) vec = value.is_a?(Vector2) ? value : Vector2.new(*value) v = C::System::Vector2u.new v[:x] = Integer(vec.x); v[:y] = Integer(vec.y) C::Window.sfWindow_setSize(@handle, v) end
Set the size.
Source
# File lib/sfml/window/window.rb, line 85 def title=(value) C::Window.sfWindow_setTitle(@handle, value.to_s) end
Set the title.
Source
# File lib/sfml/window/window.rb, line 118 def visible=(value) C::Window.sfWindow_setVisible(@handle, value ? true : false) end
Set the visible.
Source
# File lib/sfml/window/window.rb, line 128 def vsync=(enabled) C::Window.sfWindow_setVerticalSyncEnabled(@handle, enabled ? true : false) end
Set the vsync.
Source
# File lib/sfml/window/window.rb, line 191 def wait_event(timeout: nil) t = timeout || Time.zero ok = C::Window.sfWindow_waitEvent(@handle, t.to_native, @event_buffer) return nil unless ok Event.from_native(@event_buffer) end
Block until the next event arrives or timeout (a SFML::Time) elapses. nil timeout = wait forever (matches CSFML’s sfTime_Zero / no-timeout convention).