Create Window in SFML

 SFML Window

First, set up the working environment of your favorite code editor or IDE. Now we will start to create the window. I think you have created the main.cpp file.

So, let's start creating our first window with SFML, a window is pretty straightforward.

without wasting more time here is the code for creating a window. I will explain below how things work.

/*
blog.diwakar-phuyal.com.np
*/

#include <SFML/graphics.hpp>
#include <iostream>

using namespace std;
using namespace sf;

// window properties
int _width = 640;
int _height = 480;
string _title = "Hello sfml ";

// main function
int main()
{
    RenderWindow window(VideoMode(_width, _height), _title);

    // main loop
    while (window.isOpen())
    {
        Event event; // listen click events
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed) // Check if close button pressed or not
                window.close();
        }

        // Clearing the window
        window.clear();
       
        // display everything
        window.display();
    }
    return 0;
}

So let's talk about the header file:

#include <SFML/graphics.hpp>
#include <iostream>

First, we will include SFML/graphics.hpp
then   <iostream>   for basic cin and cout on our program

using namespace std;
using namespace sf;

we are using a namespace object to get rid of unwanted code and make it more readable

// window properties
int _width = 640;
int _height = 480;
string _title = "Hello sfml ";

Now, we will define our window properties like its Width, height, and Title. we define here which makes code much clearer and readable

// main function
int main()
{
    RenderWindow window(VideoMode(_width, _height), _title);

   
    return 0;
}

I don't need to explain how the main function and talk about rendering window
 RenderWindow window(VideoMode(_width, _height), _title);
 
We will call the RenderWindow class and make a window object to do our stuff, we can name it something like an app or what you like. 
(VideoMode(_width, _height), _title);

we call the VideoMode class from the SFML library and we pass window width, height, and title. window has lots of properties we talk about that later. Remember always write width before height 

 // main loop
    while (window.isOpen())
    {
       
    }

create a while and check the condition  if the window is open or not 
Event event; // listen click events

it listens click event is user press a close button or not is yes 

 while (window.pollEvent(event))
        {
            if (event.type == Event::Closed) // Check if close button pressed or not
                window.close();
        }

we will close the window with the window.close(); it listens for the closed event while running the program if the user presses close the program will be terminated 
 
Now, let's clear the window and display everything in the window
// Clearing the window
        window.clear();
       
        // display everything
        window.display();

Happy coding ๐Ÿ™‚
Keep learning ๐ŸŽ—
369
Previous Post