Opening the window

Location
  1. Tutorials

    /

  2. Introduction to coding

    /

  3. Opening the window

Window filled with black

The object sf::RenderWindow

The following line of code defines an object of type sf::RenderWindow, which opens a window at its creation.

sf::RenderWindow win(sf::VideoMode(800, 600), "Introduction to coding", sf::Style::Close);

Above, we also provide arguments to the constructor of the object, configuring the window.

Note that the name of all the classes provided by the SFML library starts with 'sf::'.

Repeating instructions

A 'while' structure, like in the following code:

while() { }

Will execute the instructions inside its braces, as long as the value inside its parentheses equals true.

The method 'isOpen' of the class sf::RenderWindow returns the value 'true' if the window is open and 'false' otherwise.

The following code repeats the instructions inside the braces, as long as the window associated with the object 'win' is open:

while(win.isOpen()) { }

Events of the window

The class sf::Event is used to represent an event that happened on a window (Text has been entered, a button of the mouse has been pressed, it has been requested to be closed...).

The method 'pollEvent' of the class sf::RenderWindow returns 'true' if the window has events left to be extracted and fills the object of type sf::Event, that it received in argument, with information about the event.

The following code extracts the events captured by the window of the object 'win', as long as it has unretrieved events:

sf::Event event; while(win.pollEvent(event)) { }

Conditionally executing instructions

An 'if' structure executes the instructions in the braces following it, once and only if the value in its parentheses equals true.

if() { }

The following code closes the window associated with the object 'win', if the event is requesting for the window to be closed (Example: The user clicked on the close button of the window).

if(event.type == sf::Event::Closed) win.close();

Some points to note:

Clearing the content of the window

A window usually has two pixel buffers (2 dimensional pixel arrays). The one that is currently displayed on the screen and the one that is been drawn on.

The method 'clear' of the class 'sf::RenderWindow' fills the buffer of the window, used for the drawing operations, with the color black.

win.clear();

Alternating the buffers

The method 'display' of the class 'sf::RenderWindow' takes the buffer that was used for the drawing operations and displays it inside the window. The other one becomes the buffer used for the drawing operations.

win.display();

Comments

Texts starting with two backslashes or surrounded by /* and */ are comments. Those are not instructions and they are there simply to explain the code.

1
2
3
4
5
6
7
// A comment written on one line. /* A comment written on multiple lines. */

What we got so far

By assembling the portions of code from above, we can open a window (Filled with black) that will close itself when requested to.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <SFML/Graphics.hpp> int main() { // Opens a window. sf::RenderWindow win(sf::VideoMode(800, 600), "Introduction to coding", sf::Style::Close); // Holds data about an event. sf::Event event; // As long as the window is open. while(win.isOpen()) { // As long as the window has unretrieved events. while(win.pollEvent(event)) { // If the event is the window being asked to be closed. if(event.type == sf::Event::Closed) win.close(); // We close it. } // We fill the 'drawing' buffer of the window with black. win.clear(); // We display the 'drawing' buffer on the screen, inside the window. win.display(); } return 0; }