Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Tutorials listed in this section have not been reviewed nor are affliated by Code Forum. It is the sole responsbility of the authour to properly maintain and ensure information is accurate.

Unmaintained Introduction to coding - 2. Opening the window

Opening the window​

View attachment 597

The object sf::RenderWindow​

The following line of code defines an object of type sf::RenderWindow, which opens a window at its creation.
C++:
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:
C++:
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:
C++:
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:
C++:
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.
C++:
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).
C++:
if(event.type == sf::Event::Closed)
    win.close();
Some points to note:
  • The member 'type' of the class 'sf::Event' represents the type of the event. The name of all the types of event are preceded by "sf::Event::".
  • In C++, to test if something equals an other thing, we use two equal signs.
  • The 'if' or 'while' structures do not require braces if they contain only one instruction.
  • The method 'close' of the class 'sf::RenderWindow' simply closes the window (After that the method 'isOpen' will return the value 'false').

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.
C++:
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.
C++:
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.
C++:
// 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.
C++:
#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;
}
 
Last edited by a moderator:
Back
Top Bottom