sf::Window Class Reference

Window that serves as a target for OpenGL rendering. More...

#include <Window.hpp>

Inheritance diagram for sf::Window:
sf::NonCopyable sf::RenderWindow

List of all members.

Public Member Functions

 Window ()
 Default constructor.
 Window (VideoMode mode, const std::string &title, unsigned long style=Style::Default, const ContextSettings &settings=ContextSettings())
 Construct a new window.
 Window (WindowHandle handle, const ContextSettings &settings=ContextSettings())
 Construct the window from an existing control.
virtual ~Window ()
 Destructor.
void Create (VideoMode mode, const std::string &title, unsigned long style=Style::Default, const ContextSettings &settings=ContextSettings())
 Create (or recreate) the window.
void Create (WindowHandle handle, const ContextSettings &settings=ContextSettings())
 Create (or recreate) the window from an existing control.
void Close ()
 Close the window and destroy all the attached resources.
bool IsOpened () const
 Tell whether or not the window is opened.
unsigned int GetWidth () const
 Get the width of the rendering region of the window.
unsigned int GetHeight () const
 Get the height of the rendering region of the window.
const ContextSettingsGetSettings () const
 Get the settings of the OpenGL context of the window.
bool GetEvent (Event &event)
 Pop the event on top of events stack, if any, and return it.
bool WaitEvent (Event &event)
 Wait for an event and return it.
void UseVerticalSync (bool enabled)
 Enable or disable vertical synchronization.
void ShowMouseCursor (bool show)
 Show or hide the mouse cursor.
void SetCursorPosition (unsigned int left, unsigned int top)
 Change the position of the mouse cursor.
void SetPosition (int left, int top)
 Change the position of the window on screen.
void SetSize (unsigned int width, unsigned int height)
 Change the size of the rendering region of the window.
void Show (bool show)
 Show or hide the window.
void EnableKeyRepeat (bool enabled)
 Enable or disable automatic key-repeat.
void SetIcon (unsigned int width, unsigned int height, const Uint8 *pixels)
 Change the window's icon.
bool SetActive (bool active=true) const
 Activate or deactivate the window as the current target for OpenGL rendering.
void Display ()
 Display on screen what has been rendered to the window so far.
const InputGetInput () const
 Get the input manager attached the window.
void SetFramerateLimit (unsigned int limit)
 Limit the framerate to a maximum fixed frequency.
float GetFrameTime () const
 Get the time elapsed since the last frame.
void SetJoystickThreshold (float threshold)
 Change the joystick threshold.

Detailed Description

Window that serves as a target for OpenGL rendering.

sf::Window is the main class of the Window module.

It defines an OS window that is able to receive an OpenGL rendering.

A sf::Window can create its own new window, or be embedded into an already existing control using the Create(handle) function. This can be useful for embedding an OpenGL rendering area into a view which is part of a bigger GUI with existing windows, controls, etc. It can also serve as embedding an OpenGL rendering area into a window created by another (probably richer) GUI library like Qt or wxWidgets.

The sf::Window class provides a simple interface for manipulating the window: move, resize, show/hide, control mouse cursor, etc. It also provides event handling through its GetEvent() function, and real-time state handling with its attached sf::Input object (see GetInput()).

Note that OpenGL experts can pass their own parameters (antialiasing level, bits for the depth and stencil buffers, etc.) to the OpenGL context attached to the window, with the sf::ContextSettings structure which is passed as an optional argument when creating the window.

Usage example:

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

 // Limit the framerate to 60 frames per second (this step is optional)
 window.SetFramerateLimit(60);

 // The main loop - ends as soon as the window is closed
 while (window.IsOpened())
 {
    // Event processing
    sf::Event event;
    while (window.GetEvent(event))
    {
        // Request for closing the window
        if (event.Type == sf::Event::Closed)
            window.Close();
    }

    // Activate the window for OpenGL rendering
    window.SetActive();

    // OpenGL drawing commands go here...

    // End the current frame and display its contents on screen
    window.Display();
 }

Definition at line 55 of file Window/Window.hpp.


Constructor & Destructor Documentation

sf::Window::Window (  ) 

Default constructor.

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

Definition at line 47 of file Window.cpp.

sf::Window::Window ( VideoMode  mode,
const std::string &  title,
unsigned long  style = Style::Default,
const ContextSettings settings = ContextSettings() 
)

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 behaviour of the window (borders, title bar, resizable, closable, ...). If style contains Style::Fullscreen, then mode must be a valid video mode.

The fourth parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc. You shouldn't care about these parameters for a regular usage of the graphics module.

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
settings Additional settings for the underlying OpenGL context

Definition at line 61 of file Window.cpp.

sf::Window::Window ( WindowHandle  handle,
const ContextSettings settings = ContextSettings() 
) [explicit]

Construct the window from an existing control.

Use this constructor if you want to create an OpenGL rendering area into an already existing control.

The fourth parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing, depth-buffer bits, etc. You shouldn't care about these parameters for a regular usage of the graphics module.

Parameters:
handle Platform-specific handle of the control
settings Additional settings for the underlying OpenGL context

Definition at line 75 of file Window.cpp.

sf::Window::~Window (  )  [virtual]

Destructor.

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

Definition at line 89 of file Window.cpp.


Member Function Documentation

void sf::Window::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 GetEvent() or Display() will still work (i.e. you don't have to test IsOpened() every time), and will have no effect on closed windows.

Definition at line 157 of file Window.cpp.

void sf::Window::Create ( WindowHandle  handle,
const ContextSettings settings = ContextSettings() 
)

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

Use this function if you want to create an OpenGL rendering area into an already existing control. If the window was already created, it closes it first.

Parameters:
handle Platform-specific handle of the control
settings Additional settings for the underlying OpenGL context

Definition at line 140 of file Window.cpp.

void sf::Window::Create ( VideoMode  mode,
const std::string &  title,
unsigned long  style = Style::Default,
const ContextSettings settings = ContextSettings() 
)

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.

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
settings Additional settings for the underlying OpenGL context

Definition at line 96 of file Window.cpp.

void sf::Window::Display (  ) 

Display on screen what has been rendered to the window so far.

This function is typically called after all OpenGL rendering has been done for the current frame, in order to show it on screen.

Definition at line 330 of file Window.cpp.

void sf::Window::EnableKeyRepeat ( bool  enabled  ) 

Enable or disable automatic key-repeat.

If key repeat is enabled, you will receive repeated KeyPress 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

Definition at line 292 of file Window.cpp.

bool sf::Window::GetEvent ( Event event  ) 

Pop the event on top of events stack, 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 event may be present in the events stack, thus you should always call this function in a loop to make sure that you process every pending event.

 sf::Event event;
 while (window.GetEvent(event))
 {
    // process event...
 }
Parameters:
event Event to be returned
Returns:
True if an event was returned, or false if the events stack was empty
See also:
WaitEvent

Definition at line 210 of file Window.cpp.

float sf::Window::GetFrameTime (  )  const

Get the time elapsed since the last frame.

This function returns the time elapsed during the last frame. This can be useful for calculating the framerate, or for updating the application's objects.

Returns:
Time elapsed, in seconds

Definition at line 365 of file Window.cpp.

unsigned int sf::Window::GetHeight (  )  const

Get the height of the rendering region of the window.

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

Returns:
Height in pixels
See also:
GetWidth

Reimplemented in sf::RenderWindow.

Definition at line 194 of file Window.cpp.

const Input & sf::Window::GetInput (  )  const

Get the input manager attached the window.

This input gives access to the real-time state of keyboard, mouse and joysticks for this window.

Returns:
Read-only reference to the input manager

Definition at line 351 of file Window.cpp.

const ContextSettings & sf::Window::GetSettings (  )  const

Get the settings of the OpenGL context of the window.

Note that these settings may be different from what was passed to the constructor or the Create() function, if one or more settings were not supported. In this case, SFML chose the closest match.

Returns:
Structure containing the OpenGL context settings

Definition at line 201 of file Window.cpp.

unsigned int sf::Window::GetWidth (  )  const

Get the width of the rendering region of the window.

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

Returns:
Width in pixels
See also:
GetHeight

Reimplemented in sf::RenderWindow.

Definition at line 187 of file Window.cpp.

bool sf::Window::IsOpened (  )  const

Tell whether or not the window is opened.

This function returns whether or not the window exists. Note that a hidden window (Show(false)) will return true.

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

Definition at line 180 of file Window.cpp.

bool sf::Window::SetActive ( bool  active = true  )  const

Activate or deactivate the window as the current target for OpenGL rendering.

A window is active only on the current thread, if you want to make it active on another thread you have to deactivate it on the previous thread first if it was active. Only one window can be active on a thread at a time, thus the window previously active (if any) automatically gets deactivated.

Parameters:
active True to activate, false to deactivate
Returns:
True if operation was successful, false otherwise

Definition at line 308 of file Window.cpp.

void sf::Window::SetCursorPosition ( unsigned int  left,
unsigned int  top 
)

Change the position of the mouse cursor.

Parameters:
left Left coordinate of the cursor, relative to the window
top Top coordinate of the cursor, relative to the window

Definition at line 254 of file Window.cpp.

void sf::Window::SetFramerateLimit ( unsigned int  limit  ) 

Limit the framerate to a maximum fixed frequency.

If a limit is set, the window will use a small delay after each call to Display() to ensure that the current frame lasted long enough to match the framerate limit.

Parameters:
limit Framerate limit, in frames per seconds (use 0 to disable limit)

Definition at line 358 of file Window.cpp.

void sf::Window::SetIcon ( unsigned int  width,
unsigned int  height,
const Uint8 *  pixels 
)

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.

Parameters:
width Icon's width, in pixels
height Icon's height, in pixels
pixels Pointer to the array of pixels in memory

Definition at line 300 of file Window.cpp.

void sf::Window::SetJoystickThreshold ( float  threshold  ) 

Change the joystick threshold.

Ths joystick threshold is the value below which no JoyMoved event will be generated.

The threshold value is 0.1 by default.

Parameters:
threshold New threshold, in the range [0, 100]

Definition at line 372 of file Window.cpp.

void sf::Window::SetPosition ( int  left,
int  top 
)

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:
left Left position
top Top position

Definition at line 268 of file Window.cpp.

void sf::Window::SetSize ( unsigned int  width,
unsigned int  height 
)

Change the size of the rendering region of the window.

Parameters:
width New width, in pixels
height New height, in pixels

Definition at line 276 of file Window.cpp.

void sf::Window::Show ( bool  show  ) 

Show or hide the window.

The window is shown by default.

Parameters:
show True to show, false to hide

Definition at line 284 of file Window.cpp.

void sf::Window::ShowMouseCursor ( bool  show  ) 

Show or hide the mouse cursor.

The mouse cursor is shown by default.

Parameters:
show True to show, false to hide

Definition at line 246 of file Window.cpp.

void sf::Window::UseVerticalSync ( bool  enabled  ) 

Enable or disable vertical synchronization.

Activating vertical synchronization will limit the number of frames displayed to the refresh rate of the monitor. This can avoid some visual artifacts, and limit the framerate to a good value (but not constant across different computers).

Vertical synchronization is disabled by default.

Parameters:
enabled True to enable v-sync, false to deactivate

Definition at line 238 of file Window.cpp.

bool sf::Window::WaitEvent ( Event event  ) 

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

 sf::Event event;
 if (window.WaitEvent(event))
 {
    // process event...
 }
Parameters:
event Event to be returned
Returns:
False if any error occured
See also:
GetEvent

Definition at line 224 of file Window.cpp.


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