Skip to content

class SF::WindowBase
inherits Reference #

Window that serves as a base for other windows

SF::WindowBase serves as the base class for all Windows.

A SF::WindowBase can create its own new window, or be embedded into an already existing control using the create(handle) function.

The SF::WindowBase class provides a simple interface for manipulating the window: move, resize, show/hide, control mouse cursor, etc. It also provides event handling through its poll_event() and wait_event() functions.

Usage example:

// Declare and create a new window
sf::WindowBase window(sf::VideoMode(800, 600), "SFML window");

// The main loop - ends as soon as the window is closed
while (window.isOpen())
{
   // Event processing
   sf::Event event;
   while (window.pollEvent(event))
   {
       // Request for closing the window
       if (event.type == sf::Event::Closed)
           window.close();
   }

   // Do things with the window here...
}

Included modules

SF::NonCopyable

Direct known subclasses

SF::Window

Constructors#

.new(mode : VideoMode, title : String, style : Style = Style::Default)#

Construct a new window

This constructor creates the window with the size and pixel depth defined in mode. An optional style can be passed to customize the look and behavior of the window (borders, title bar, resizable, closable, ...). If style contains Style::Fullscreen, then mode must be a valid video mode.

  • mode - Video mode to use (defines the width, height and depth of the rendering area of the window)
  • title - Title of the window
  • style - Window style, a bitwise OR combination of SF::Style enumerators
View source

.new(handle : WindowHandle)#

Construct the window from an existing control

  • handle - Platform-specific handle of the control
View source

.new#

Default constructor

This constructor doesn't actually create the window, use the other constructors or call create() to do so.

View source

.new(*args, **kwargs) : self#

Shorthand for window_base = WindowBase.new; window_base.create(...); window_base

View source

Methods#

#close#

Close the window and destroy all the attached resources

After calling this function, the SF::Window instance remains valid and you can call create() to recreate the window. All other functions such as poll_event() or display() will still work (i.e. you don't have to test open?() every time), and will have no effect on closed windows.

View source

#create(mode : VideoMode, title : String, style : Style = Style::Default)#

Create (or recreate) the window

If the window was already created, it closes it first. If style contains Style::Fullscreen, then mode must be a valid video mode.

  • mode - Video mode to use (defines the width, height and depth of the rendering area of the window)
  • title - Title of the window
  • style - Window style, a bitwise OR combination of SF::Style enumerators
View source

#create(handle : WindowHandle)#

Create (or recreate) the window from an existing control

  • handle - Platform-specific handle of the control
View source

#create_vulkan_surface(instance : VkInstance, surface : VkSurfaceKHR, allocator : VkAllocationCallbacks | Nil = nil) : Bool#

Create a Vulkan rendering surface

  • instance - Vulkan instance
  • surface - Created surface
  • allocator - Allocator to use

Returns: True if surface creation was successful, false otherwise

View source

#finalize#

Destructor

Closes the window and frees all the resources attached to it.

View source

#focus? : Bool#

Check whether the window has the input focus

At any given time, only one window may have the input focus to receive input events such as keystrokes or most mouse events.

Returns: True if window has focus, false otherwise See also: request_focus

View source

#joystick_threshold=(threshold : Number)#

Change the joystick threshold

The joystick threshold is the value below which no JoystickMoved event will be generated.

The threshold value is 0.1 by default.

  • threshold - New threshold, in the range 0.0 .. 100.0
View source

#key_repeat_enabled=(enabled : Bool)#

Enable or disable automatic key-repeat

If key repeat is enabled, you will receive repeated KeyPressed events while keeping a key pressed. If it is disabled, you will only get a single event when the key is pressed.

Key repeat is enabled by default.

  • enabled - True to enable, false to disable
View source

#mouse_cursor=(cursor : Cursor)#

Set the displayed cursor to a native system cursor

Upon window creation, the arrow cursor is used by default.

Warning

The cursor must not be destroyed while in use by the window.

Warning

Features related to Cursor are not supported on iOS and Android.

  • cursor - Native system cursor type to display

See also: SF::Cursor.load_from_system See also: SF::Cursor.load_from_pixels

View source

#mouse_cursor_grabbed=(grabbed : Bool)#

Grab or release the mouse cursor

If set, grabs the mouse cursor inside this window's client area so it may no longer be moved outside its bounds. Note that grabbing is only active while the window has focus.

  • grabbed - True to enable, false to disable
View source

#mouse_cursor_visible=(visible : Bool)#

Show or hide the mouse cursor

The mouse cursor is visible by default.

  • visible - True to show the mouse cursor, false to hide it
View source

#open? : Bool#

Tell whether or not the window is open

This function returns whether or not the window exists. Note that a hidden window (visible=false) is open (therefore this function would return true).

Returns: True if the window is open, false if it has been closed

View source

#poll_event : Event | ::Nil#

Pop the event on top of the event queue, if any, and return it

This function is not blocking: if there's no pending event then it will return false and leave event unmodified. Note that more than one event may be present in the event queue, thus you should always call this function in a loop to make sure that you process every pending event.

while (event = window.poll_event)
  # process event...
end

  • event - Event to be returned

Returns: True if an event was returned, or false if the event queue was empty

See also: wait_event

View source

#position : Vector2i#

Get the position of the window

Returns: Position of the window, in pixels

See also: position=

View source

#position=(position : Vector2 | Tuple)#

Change the position of the window on screen

This function only works for top-level windows (i.e. it will be ignored for windows created from the handle of a child window/control).

  • position - New position, in pixels

See also: position

View source

#request_focus#

Request the current window to be made the active foreground window

At any given time, only one window may have the input focus to receive input events such as keystrokes or mouse events. If a window requests focus, it only hints to the operating system, that it would like to be focused. The operating system is free to deny the request. This is not to be confused with active=().

See also: focus?

View source

#set_icon(width : Int, height : Int, pixels : Pointer(UInt8))#

Change the window's icon

pixels must be an array of width x height pixels in 32-bits RGBA format.

The OS default icon is used by default.

  • width - Icon's width, in pixels
  • height - Icon's height, in pixels
  • pixels - Pointer to the array of pixels in memory. The pixels are copied, so you need not keep the source alive after calling this function.

See also: title=

View source

#size : Vector2u#

Get the size of the rendering region of the window

The size doesn't include the titlebar and borders of the window.

Returns: Size in pixels

See also: size=

View source

#size=(size : Vector2 | Tuple)#

Change the size of the rendering region of the window

  • size - New size, in pixels

See also: size

View source

#system_handle : WindowHandle#

Get the OS-specific handle of the window

The type of the returned handle is SF::WindowHandle, which is a typedef to the handle type defined by the OS. You shouldn't need to use this function, unless you have very specific stuff to implement that SFML doesn't support, or implement a temporary workaround until a bug is fixed.

Returns: System handle of the window

View source

#title=(title : String)#

Change the title of the window

  • title - New title

See also: icon=

View source

#visible=(visible : Bool)#

Show or hide the window

The window is shown by default.

  • visible - True to show the window, false to hide it
View source

#wait_event : Event | ::Nil#

Wait for an event and return it

This function is blocking: if there's no pending event then it will wait until an event is received. After this function returns (and no error occurred), the event object is always valid and filled properly. This function is typically used when you have a thread that is dedicated to events handling: you want to make this thread sleep as long as no new event is received.

if (event = window.wait_event)
  # process event...
end

  • event - Event to be returned

Returns: False if any error occurred

See also: poll_event

View source