SFML 3.0.2
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Friends | List of all members
sf::WindowBase Class Reference
Window module

Window that serves as a base for other windows. More...

`#include <SFML/Window/WindowBase.hpp>`

Inheritance diagram for sf::WindowBase:

## Public Member Functions
| WindowBase ()
| Default constructor.
| WindowBase (VideoMode mode, const String &title, std::uint32_t style=Style::Default, State state=State::Windowed)
| Construct a new window.
| WindowBase (VideoMode mode, const String &title, State state)
| Construct a new window.
| WindowBase (WindowHandle handle)
| Construct the window from an existing control.
virtual | ~WindowBase ()
| Destructor.
| WindowBase (const WindowBase &)=delete
| Deleted copy constructor.
WindowBase & | operator= (const WindowBase &)=delete
| Deleted copy assignment.
| WindowBase (WindowBase &&) noexcept
| Move constructor.
WindowBase & | operator= (WindowBase &&) noexcept
| Move assignment.
virtual void | create (VideoMode mode, const String &title, std::uint32_t style=Style::Default, State state=State::Windowed)
| Create (or recreate) the window.
virtual void | create (VideoMode mode, const String &title, State state)
| Create (or recreate) the window.
virtual void | create (WindowHandle handle)
| Create (or recreate) the window from an existing control.
virtual void | close ()
| Close the window and destroy all the attached resources.
bool | isOpen () const
| Tell whether or not the window is open.
std::optional< Event > | pollEvent ()
| Pop the next event from the front of the FIFO event queue, if any, and return it.
std::optional< Event > | waitEvent (Time timeout=Time::Zero)
| Wait for an event and return it.
template<typename... Handlers>
void | handleEvents (Handlers &&... handlers)
| Handle all pending events.
Vector2i | getPosition () const
| Get the position of the window.
void | setPosition (Vector2i position)
| Change the position of the window on screen.
Vector2u | getSize () const
| Get the size of the rendering region of the window.
void | setSize (Vector2u size)
| Change the size of the rendering region of the window.
void | setMinimumSize (const std::optional< Vector2u > &minimumSize)
| Set the minimum window rendering region size.
void | setMaximumSize (const std::optional< Vector2u > &maximumSize)
| Set the maximum window rendering region size.
void | setTitle (const String &title)
| Change the title of the window.
void | setIcon (Vector2u size, const std::uint8_t *pixels)
| Change the window's icon.
void | setVisible (bool visible)
| Show or hide the window.
void | setMouseCursorVisible (bool visible)
| Show or hide the mouse cursor.
void | setMouseCursorGrabbed (bool grabbed)
| Grab or release the mouse cursor.
void | setMouseCursor (const Cursor &cursor)
| Set the displayed cursor to a native system cursor.
void | setKeyRepeatEnabled (bool enabled)
| Enable or disable automatic key-repeat.
void | setJoystickThreshold (float threshold)
| Change the joystick threshold.
void | requestFocus ()
| Request the current window to be made the active foreground window.
bool | hasFocus () const
| Check whether the window has the input focus.
WindowHandle | getNativeHandle () const
| Get the OS-specific handle of the window.
bool | createVulkanSurface (const VkInstance &instance, VkSurfaceKHR &surface, const VkAllocationCallbacks *allocator=nullptr)
| Create a Vulkan rendering surface.

## Protected Member Functions
virtual void | onCreate ()
| Function called after the window has been created.
virtual void | onResize ()
| Function called after the window has been resized.

## Friends
class | Window

## Detailed Description

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 pollEvent() and waitEvent() 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
while (const std::optional event = window.pollEvent())
{
// Request for closing the window
if (event->is<sf::Event::Closed>())
window.close();
}
// Do things with the window here...
}
sf::VideoMode
VideoMode defines a video mode (size, bpp)
Definition VideoMode.hpp:44
sf::WindowBase
Window that serves as a base for other windows.
Definition WindowBase.hpp:63
sf::WindowBase::pollEvent
std::optional< Event > pollEvent()
Pop the next event from the front of the FIFO event queue, if any, and return it.
sf::WindowBase::close
virtual void close()
Close the window and destroy all the attached resources.
sf::WindowBase::isOpen
bool isOpen() const
Tell whether or not the window is open.
sf::Event::Closed
Closed event subtype.
Definition Event.hpp:55

Definition at line 62 of file WindowBase.hpp.

## Constructor & Destructor Documentation

## ◆ WindowBase() [1/6]

sf::WindowBase::WindowBase | ( |  | ) |

Default constructor.

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

## ◆ WindowBase() [2/6]

sf::WindowBase::WindowBase | ( | VideoMode | mode,
|  | const String & | title,
|  | std::uint32_t | style = Style::Default,
|  | State | state = State::Windowed )

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, ...). An optional state can be provided. If state is State::Fullscreen, then mode must be a valid video mode.

Parameters

  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
  state | Window state

## ◆ WindowBase() [3/6]

sf::WindowBase::WindowBase | ( | VideoMode | mode,
|  | const String & | title,
|  | State | state )

Construct a new window.

This constructor creates the window with the size and pixel depth defined in mode.

Parameters

  mode | Video mode to use (defines the width, height and depth of the rendering area of the window)
  title | Title of the window
  state | Window state

## ◆ WindowBase() [4/6]

sf::WindowBase::WindowBase | ( | WindowHandle | handle | ) |
| explicit

Construct the window from an existing control.

Parameters

  handle | Platform-specific handle of the control

## ◆ ~WindowBase()

virtual sf::WindowBase::~WindowBase | ( |  | ) |
| virtual

Destructor.

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

## ◆ WindowBase() [5/6]

sf::WindowBase::WindowBase | ( | const WindowBase & |  | ) |
| delete

Deleted copy constructor.

## ◆ WindowBase() [6/6]

sf::WindowBase::WindowBase | ( | WindowBase && |  | ) |
| noexcept

Move constructor.

## Member Function Documentation

## ◆ close()

virtual void sf::WindowBase::close | ( |  | ) |
| virtual

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 pollEvent() or display() will still work (i.e. you don't have to test isOpen() every time), and will have no effect on closed windows.

Reimplemented in sf::Window.

## ◆ create() [1/3]

virtual void sf::WindowBase::create | ( | VideoMode | mode,
|  | const String & | title,
|  | State | state )
| virtual

Create (or recreate) the window.

If the window was already created, it closes it first. If state is State::Fullscreen, then mode must be a valid video mode.

Parameters

  mode | Video mode to use (defines the width, height and depth of the rendering area of the window)
  title | Title of the window
  state | Window state

Reimplemented in sf::Window.

## ◆ create() [2/3]

virtual void sf::WindowBase::create | ( | VideoMode | mode,
|  | const String & | title,
|  | std::uint32_t | style = Style::Default,
|  | State | state = State::Windowed )
| virtual

Create (or recreate) the window.

If the window was already created, it closes it first. If state is State::Fullscreen, then mode must be a valid video mode.

Parameters

  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
  state | Window state

Reimplemented in sf::Window.

## ◆ create() [3/3]

virtual void sf::WindowBase::create | ( | WindowHandle | handle | ) |
| virtual

Create (or recreate) the window from an existing control.

Parameters

  handle | Platform-specific handle of the control

Reimplemented in sf::Window.

## ◆ createVulkanSurface()

bool sf::WindowBase::createVulkanSurface | ( | const VkInstance & | instance,
|  | VkSurfaceKHR & | surface,
|  | const VkAllocationCallbacks * | allocator = nullptr )
| nodiscard

Create a Vulkan rendering surface.

Parameters

  instance | Vulkan instance
  surface | Created surface
  allocator | Allocator to use

Returns
  true if surface creation was successful, false otherwise

## ◆ getNativeHandle()

WindowHandle sf::WindowBase::getNativeHandle | ( |  | ) | const
| nodiscard

Get the OS-specific handle of the window.

The type of the returned handle is sf::WindowHandle, which is a type alias 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

## ◆ getPosition()

Vector2i sf::WindowBase::getPosition | ( |  | ) | const
| nodiscard

Get the position of the window.

Returns
  Position of the window, in pixels

See also
  setPosition

## ◆ getSize()

Vector2u sf::WindowBase::getSize | ( |  | ) | const
| nodiscard

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
  setSize

## ◆ handleEvents()

template<typename... Handlers>

void sf::WindowBase::handleEvents | ( | Handlers &&... | handlers | ) |

Handle all pending events.

This function is not blocking: if there's no pending event then it will return without calling any of the handlers.

This function can take a variadic list of event handlers that each take a concrete event type as a single parameter. The event handlers can be any kind of callable object that has an operator() defined for a specific event type. Additionally a generic callable can also be provided that will be invoked for every event type. If both types of callables are provided, the callables taking concrete event types will be preferred over the generic callable by overload resolution. Generic callables can be used to customize handler dispatching based on the deduced type of the event and other information available at compile time.

Examples of callables:

- Lambda expressions: [&](const sf::Event::KeyPressed) { ... }
- Free functions: void handler(const sf::Event::KeyPressed&) { ... }

// Only provide handlers for concrete event types
window.handleEvents(
[&](const sf::Event::Closed&) { /* handle event */ },
[&](const sf::Event::KeyPressed& keyPress) { /* handle event */ }
);
sf::Event::KeyPressed
Key pressed event subtype.
Definition Event.hpp:97
// Provide a generic event handler
window.handleEvents(
[&](const auto& event)
{
if constexpr (std::is_same_v<std::decay_t<decltype(event)>, sf::Event::Closed>)
{
// Handle Closed
handleClosed();
}
else if constexpr (std::is_same_v<std::decay_t<decltype(event)>, sf::Event::KeyPressed>)
{
// Handle KeyPressed
handleKeyPressed(event);
}
else
{
// Handle non-KeyPressed
handleOtherEvents(event);
}
}
);
// Provide handlers for concrete types and fall back to generic handler
window.handleEvents(
[&](const sf::Event::Closed&) { /* handle event */ },
[&](const sf::Event::KeyPressed& keyPress) { /* handle event */ },
[&](const auto& event) { /* handle all other events */ }
);

Calling member functions is supported through lambda expressions.

// Provide a generic event handler
window.handleEvents(
[this](const auto& event) { handle(event); }
);

Parameters

  handlers | A variadic list of callables that take a specific event as their only parameter

See also
  waitEvent, pollEvent

## ◆ hasFocus()

bool sf::WindowBase::hasFocus | ( |  | ) | const
| nodiscard

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
  requestFocus

## ◆ isOpen()

bool sf::WindowBase::isOpen | ( |  | ) | const
| nodiscard

Tell whether or not the window is open.

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

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

## ◆ onCreate()

virtual void sf::WindowBase::onCreate | ( |  | ) |
| protected virtual

Function called after the window has been created.

This function is called so that derived classes can perform their own specific initialization as soon as the window is created.

Reimplemented in sf::RenderWindow.

## ◆ onResize()

virtual void sf::WindowBase::onResize | ( |  | ) |
| protected virtual

Function called after the window has been resized.

This function is called so that derived classes can perform custom actions when the size of the window changes.

Reimplemented in sf::RenderWindow.

## ◆ operator=() [1/2]

WindowBase & sf::WindowBase::operator= | ( | const WindowBase & |  | ) |
| delete

Deleted copy assignment.

## ◆ operator=() [2/2]

WindowBase & sf::WindowBase::operator= | ( | WindowBase && |  | ) |
| noexcept

Move assignment.

## ◆ pollEvent()

std::optional< Event > sf::WindowBase::pollEvent | ( |  | ) |
| nodiscard

Pop the next event from the front of the FIFO event queue, if any, and return it.

This function is not blocking: if there's no pending event then it will return a std::nullopt. 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 (const std::optional event = window.pollEvent())
{
// process event...
}

Returns
  The event, otherwise std::nullopt if no events are pending

See also
  waitEvent, handleEvents

## ◆ requestFocus()

void sf::WindowBase::requestFocus | ( |  | ) |

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 setActive().

See also
  hasFocus

## ◆ setIcon()

void sf::WindowBase::setIcon | ( | Vector2u | size,
|  | const std::uint8_t * | pixels )

Change the window's icon.

pixels must be an array of size pixels in 32-bits RGBA format.

The OS default icon is used by default.

Parameters

  size | Icon's width and 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
  setTitle

## ◆ setJoystickThreshold()

void sf::WindowBase::setJoystickThreshold | ( | float | threshold | ) |

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.

Parameters

  threshold | New threshold, in the range [0, 100]

## ◆ setKeyRepeatEnabled()

void sf::WindowBase::setKeyRepeatEnabled | ( | bool | enabled | ) |

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.

Parameters

  enabled | true to enable, false to disable

## ◆ setMaximumSize()

void sf::WindowBase::setMaximumSize | ( | const std::optional< Vector2u > & | maximumSize | ) |

Set the maximum window rendering region size.

Pass std::nullopt to unset the maximum size

Parameters

  maximumSize | New maximum size, in pixels

## ◆ setMinimumSize()

void sf::WindowBase::setMinimumSize | ( | const std::optional< Vector2u > & | minimumSize | ) |

Set the minimum window rendering region size.

Pass std::nullopt to unset the minimum size

Parameters

  minimumSize | New minimum size, in pixels

## ◆ setMouseCursor()

void sf::WindowBase::setMouseCursor | ( | const 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.
  Features related to Cursor are not supported on iOS and Android.

Parameters

  cursor | Native system cursor type to display

See also
  sf::Cursor::createFromSystem, sf::Cursor::createFromPixels

## ◆ setMouseCursorGrabbed()

void sf::WindowBase::setMouseCursorGrabbed | ( | bool | grabbed | ) |

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.

Parameters

  grabbed | true to enable, false to disable

## ◆ setMouseCursorVisible()

void sf::WindowBase::setMouseCursorVisible | ( | bool | visible | ) |

Show or hide the mouse cursor.

The mouse cursor is visible by default.

Warning
  On Windows, this function needs to be called from the thread that created the window.

Parameters

  visible | true to show the mouse cursor, false to hide it

## ◆ setPosition()

void sf::WindowBase::setPosition | ( | Vector2i | position | ) |

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).

Parameters

  position | New position, in pixels

See also
  getPosition

## ◆ setSize()

void sf::WindowBase::setSize | ( | Vector2u | size | ) |

Change the size of the rendering region of the window.

Parameters

  size | New size, in pixels

See also
  getSize

## ◆ setTitle()

void sf::WindowBase::setTitle | ( | const String & | title | ) |

Change the title of the window.

Parameters

  title | New title

See also
  setIcon

## ◆ setVisible()

void sf::WindowBase::setVisible | ( | bool | visible | ) |

Show or hide the window.

The window is shown by default.

Parameters

  visible | true to show the window, false to hide it

## ◆ waitEvent()

std::optional< Event > sf::WindowBase::waitEvent | ( | Time | timeout = Time::Zero | ) |
| nodiscard

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 or until the provided timeout elapses. Only if an error or a timeout occurs the returned event will be std::nullopt. 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.

while (const std::optional event = window.waitEvent())
{
// process event...
}

Parameters

  timeout | Maximum time to wait (Time::Zero for infinite)

Returns
  The event, otherwise std::nullopt on timeout or if window was closed

See also
  pollEvent, handleEvents

## Friends And Related Symbol Documentation

## ◆ Window

friend class Window
| friend

Definition at line 586 of file WindowBase.hpp.

The documentation for this class was generated from the following file:

- WindowBase.hpp