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
- #display ⇒ Object
- #each_event ⇒ Object
- #focused? ⇒ Boolean
- #framerate_limit=(value) ⇒ Object
-
#icon=(image) ⇒ Object
Replace the window’s title-bar / taskbar icon with the pixels from the given SFML::Image.
-
#initialize(*args, **opts) ⇒ Window
constructor
A new instance of Window.
- #key_repeat_enabled=(value) ⇒ Object
- #maximum_size=(value) ⇒ Object
-
#minimum_size=(value) ⇒ Object
Constrain user-driven resizes.
-
#native_handle ⇒ Object
OS-specific native handle for the underlying window — ‘HWND` on Windows, `NSView*` on macOS, X11 `Window` xid on Linux.
- #open? ⇒ Boolean
-
#poll_event ⇒ Object
Returns the next pending Event or nil.
- #position ⇒ Object
- #position=(value) ⇒ Object
- #request_focus ⇒ Object
- #size ⇒ Object
- #size=(value) ⇒ Object
- #title=(value) ⇒ Object
- #visible=(value) ⇒ Object
- #vsync=(enabled) ⇒ Object
Constructor Details
#initialize(*args, **opts) ⇒ Window
Returns a new instance of Window.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sfml/window/window.rb', line 25 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 Error, "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:
180 181 182 |
# File 'lib/sfml/window/window.rb', line 180 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.
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/sfml/window/window.rb', line 168 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 Error, "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.
128 129 130 |
# File 'lib/sfml/window/window.rb', line 128 def active=(value) C::Window.sfWindow_setActive(@handle, value ? true : false) end |
#close ⇒ Object
50 51 52 53 |
# File 'lib/sfml/window/window.rb', line 50 def close C::Window.sfWindow_close(@handle) self end |
#display ⇒ Object
55 56 57 58 |
# File 'lib/sfml/window/window.rb', line 55 def display C::Window.sfWindow_display(@handle) self end |
#each_event ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/sfml/window/window.rb', line 66 def each_event return enum_for(:each_event) unless block_given? while (event = poll_event) yield event end self end |
#focused? ⇒ Boolean
122 123 124 |
# File 'lib/sfml/window/window.rb', line 122 def focused? C::Window.sfWindow_hasFocus(@handle) end |
#framerate_limit=(value) ⇒ Object
106 107 108 |
# File 'lib/sfml/window/window.rb', line 106 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.
135 136 137 138 139 140 141 142 |
# File 'lib/sfml/window/window.rb', line 135 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 |
#key_repeat_enabled=(value) ⇒ Object
114 115 116 |
# File 'lib/sfml/window/window.rb', line 114 def key_repeat_enabled=(value) C::Window.sfWindow_setKeyRepeatEnabled(@handle, value ? true : false) end |
#maximum_size=(value) ⇒ Object
152 153 154 |
# File 'lib/sfml/window/window.rb', line 152 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.
148 149 150 |
# File 'lib/sfml/window/window.rb', line 148 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.
159 160 161 |
# File 'lib/sfml/window/window.rb', line 159 def native_handle C::Window.sfWindow_getNativeHandle(@handle) end |
#open? ⇒ Boolean
46 47 48 |
# File 'lib/sfml/window/window.rb', line 46 def open? C::Window.sfWindow_isOpen(@handle) end |
#poll_event ⇒ Object
Returns the next pending Event or nil. Same shape as RenderWindow#poll_event.
61 62 63 64 |
# File 'lib/sfml/window/window.rb', line 61 def poll_event return nil unless C::Window.sfWindow_pollEvent(@handle, @event_buffer) Event.from_native(@event_buffer) end |
#position ⇒ Object
90 91 92 93 |
# File 'lib/sfml/window/window.rb', line 90 def position v = C::Window.sfWindow_getPosition(@handle) Vector2.new(v[:x], v[:y]) end |
#position=(value) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/sfml/window/window.rb', line 95 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
118 119 120 |
# File 'lib/sfml/window/window.rb', line 118 def request_focus C::Window.sfWindow_requestFocus(@handle) end |
#size ⇒ Object
78 79 80 81 |
# File 'lib/sfml/window/window.rb', line 78 def size v = C::Window.sfWindow_getSize(@handle) Vector2.new(v[:x], v[:y]) end |
#size=(value) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/sfml/window/window.rb', line 83 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
74 75 76 |
# File 'lib/sfml/window/window.rb', line 74 def title=(value) C::Window.sfWindow_setTitle(@handle, value.to_s) end |