Class: SFML::Window
- Inherits:
-
Object
- Object
- SFML::Window
- Defined in:
- lib/sfml/window/window.rb
Overview
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
Constant Summary collapse
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
.from_handle(handle) ⇒ Object
Wrap an existing OS-level window.
Instance Method Summary collapse
-
#active=(value) ⇒ Object
Make this window's GL context current on the calling thread.
-
#close ⇒ Object
Close the window.
-
#context_settings ⇒ Object
The actual GL context settings the driver gave us — may differ from what was requested via
ContextSettings. -
#cursor=(cursor) ⇒ Object
Apply a
SFML::Cursoras the visible mouse pointer over this window. -
#cursor_grabbed=(grabbed) ⇒ Object
Set the cursor grabbed.
-
#cursor_visible=(visible) ⇒ Object
Set the cursor visible.
-
#display ⇒ Object
Swap front/back buffers, presenting whatever was rendered since the last call.
-
#each_event ⇒ Object
Yields each pending event then returns.
-
#focused? ⇒ Boolean
trueif focused. -
#framerate_limit=(value) ⇒ Object
Set the framerate limit.
-
#icon=(image) ⇒ Object
Replace the window's title-bar / taskbar icon with the pixels from the given SFML::Image.
-
#initialize(*args, **opts) ⇒ Window
constructor
Construct a Window.
-
#joystick_threshold=(value) ⇒ Object
Dead-zone for joystick axis events in [0, 100].
-
#key_repeat_enabled=(value) ⇒ Object
Set the key repeat enabled.
-
#maximum_size=(value) ⇒ Object
Set the maximum size.
-
#minimum_size=(value) ⇒ Object
Constrain user-driven resizes.
-
#native_handle ⇒ Object
OS-specific native handle for the underlying window —
HWNDon Windows,NSView*on macOS, X11Windowxid on Linux. -
#open? ⇒ Boolean
truewhile the window is alive (not closed by#closeor the user). -
#poll_event ⇒ Object
Returns the next pending Event or nil.
-
#position ⇒ Object
Window top-left in desktop coordinates.
-
#position=(value) ⇒ Object
Set the position.
-
#request_focus ⇒ Object
Ask the OS to give this window focus.
-
#size ⇒ Object
Current window size as a
Vector2. -
#size=(value) ⇒ Object
Set the size.
-
#title=(value) ⇒ Object
Set the title.
-
#visible=(value) ⇒ Object
Set the visible.
-
#vsync=(enabled) ⇒ Object
Set the vsync.
-
#wait_event(timeout: nil) ⇒ Object
Block until the next event arrives or
timeout(a SFML::Time) elapses.
Constructor Details
#initialize(*args, **opts) ⇒ Window
Construct a Window. Same constructor forms as RenderWindow.new
((w, h, title, **opts) or (video_mode, title, **opts)).
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 |
Instance Attribute Details
#handle ⇒ Object (readonly)
:nodoc:
247 248 249 |
# File 'lib/sfml/window/window.rb', line 247 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 when SFML is
being embedded inside another framework (Qt, Gtk, raw Win32,
Cocoa NSView). The framework owns the window's lifecycle; SFML
only renders into it.
235 236 237 238 239 240 241 242 243 244 245 |
# 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 |
Instance Method Details
#active=(value) ⇒ Object
Make this window's GL context current on the calling thread. Useful when juggling multiple windows / off-screen contexts.
150 151 152 |
# File 'lib/sfml/window/window.rb', line 150 def active=(value) C::Window.sfWindow_setActive(@handle, value ? true : false) end |
#close ⇒ Object
Close the window.
56 57 58 59 |
# File 'lib/sfml/window/window.rb', line 56 def close C::Window.sfWindow_close(@handle) self end |
#context_settings ⇒ Object
The actual GL context settings the driver gave us — may differ
from what was requested via ContextSettings.
184 185 186 |
# File 'lib/sfml/window/window.rb', line 184 def context_settings ContextSettings.from_native(C::Window.sfWindow_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 outlives the call.
158 159 160 161 162 |
# 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 |
#cursor_grabbed=(grabbed) ⇒ Object
Set the cursor grabbed.
170 171 172 |
# File 'lib/sfml/window/window.rb', line 170 def cursor_grabbed=(grabbed) C::Window.sfWindow_setMouseCursorGrabbed(@handle, grabbed ? true : false) end |
#cursor_visible=(visible) ⇒ Object
Set the cursor visible.
165 166 167 |
# File 'lib/sfml/window/window.rb', line 165 def cursor_visible=(visible) C::Window.sfWindow_setMouseCursorVisible(@handle, visible ? true : false) end |
#display ⇒ Object
Swap front/back buffers, presenting whatever was rendered since the last call.
63 64 65 66 |
# File 'lib/sfml/window/window.rb', line 63 def display C::Window.sfWindow_display(@handle) self end |
#each_event ⇒ Object
Yields each pending event then returns. Without a block, returns an Enumerator.
76 77 78 79 80 81 82 |
# 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 |
#focused? ⇒ Boolean
true if focused.
144 145 146 |
# File 'lib/sfml/window/window.rb', line 144 def focused? C::Window.sfWindow_hasFocus(@handle) end |
#framerate_limit=(value) ⇒ Object
Set the framerate limit.
123 124 125 |
# File 'lib/sfml/window/window.rb', line 123 def framerate_limit=(value) C::Window.sfWindow_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.
201 202 203 204 205 206 207 208 |
# 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 |
#joystick_threshold=(value) ⇒ Object
Dead-zone for joystick axis events in [0, 100]. Axes whose absolute value is below this are reported as 0.
178 179 180 |
# File 'lib/sfml/window/window.rb', line 178 def joystick_threshold=(value) C::Window.sfWindow_setJoystickThreshold(@handle, Float(value)) end |
#key_repeat_enabled=(value) ⇒ Object
Set the key repeat enabled.
133 134 135 |
# File 'lib/sfml/window/window.rb', line 133 def key_repeat_enabled=(value) C::Window.sfWindow_setKeyRepeatEnabled(@handle, value ? true : false) end |
#maximum_size=(value) ⇒ Object
Set the maximum size.
219 220 221 |
# File 'lib/sfml/window/window.rb', line 219 def maximum_size=(value) C::Window.sfWindow_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.
214 215 216 |
# File 'lib/sfml/window/window.rb', line 214 def minimum_size=(value) C::Window.sfWindow_setMinimumSize(@handle, _vec2u_or_nil(value)) end |
#native_handle ⇒ Object
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.
226 227 228 |
# File 'lib/sfml/window/window.rb', line 226 def native_handle C::Window.sfWindow_getNativeHandle(@handle) end |
#open? ⇒ Boolean
true while the window is alive (not closed by #close or
the user).
51 52 53 |
# File 'lib/sfml/window/window.rb', line 51 def open? C::Window.sfWindow_isOpen(@handle) end |
#poll_event ⇒ Object
Returns the next pending Event or nil. Same shape as RenderWindow#poll_event.
69 70 71 72 |
# 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 |
#position ⇒ Object
Window top-left in desktop coordinates.
104 105 106 107 |
# File 'lib/sfml/window/window.rb', line 104 def position v = C::Window.sfWindow_getPosition(@handle) Vector2.new(v[:x], v[:y]) end |
#position=(value) ⇒ Object
Set the position.
110 111 112 113 114 115 |
# 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 |
#request_focus ⇒ Object
Ask the OS to give this window focus. Cooperative — most window managers will defer until the user clicks.
139 140 141 |
# File 'lib/sfml/window/window.rb', line 139 def request_focus C::Window.sfWindow_requestFocus(@handle) end |
#size ⇒ Object
Current window size as a Vector2.
90 91 92 93 |
# File 'lib/sfml/window/window.rb', line 90 def size v = C::Window.sfWindow_getSize(@handle) Vector2.new(v[:x], v[:y]) end |
#size=(value) ⇒ Object
Set the size.
96 97 98 99 100 101 |
# 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 |
#title=(value) ⇒ Object
Set the title.
85 86 87 |
# File 'lib/sfml/window/window.rb', line 85 def title=(value) C::Window.sfWindow_setTitle(@handle, value.to_s) end |
#visible=(value) ⇒ Object
Set the visible.
118 119 120 |
# File 'lib/sfml/window/window.rb', line 118 def visible=(value) C::Window.sfWindow_setVisible(@handle, value ? true : false) end |
#vsync=(enabled) ⇒ Object
Set the vsync.
128 129 130 |
# File 'lib/sfml/window/window.rb', line 128 def vsync=(enabled) C::Window.sfWindow_setVerticalSyncEnabled(@handle, enabled ? true : false) end |
#wait_event(timeout: nil) ⇒ Object
Block until the next event arrives or timeout (a SFML::Time)
elapses. nil timeout = wait forever (matches CSFML's
sfTime_Zero / no-timeout convention).
191 192 193 194 195 196 |
# 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 |