menu Samuli Natri - Software Developer

SFML - Window Creation (C++ Tutorial)

How to create and manage windows with SFML.

Source Code

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

int main()
{
    std::cout << "Window creation" << '\n';
    // sf::RenderWindow window(sf::VideoMode(1000, 1000), "Title");

    sf::VideoMode currentMode = sf::VideoMode::getDesktopMode();
    std::cout << currentMode.width << ',' << currentMode.height << '\n';

    std::vector<sf::VideoMode> modes = sf::VideoMode::getFullscreenModes();

    for (sf::VideoMode mode : modes)
    {
       std::cout << mode.width << ',' << mode.height << '\n';
    }

    sf::RenderWindow window;
    window.create(currentMode, "Title"
          // sf::Style::None
          // sf::Style::Fullscreen,
          // sf::Style::Titlebar |
          // sf::Style::Resize |
          // sf::Style::Close
          // sf::Style::Default 
          );

    window.setTitle("NEW TITLE");
    window.setFramerateLimit(60);
    window.setMouseCursorVisible(false);

    sf::RectangleShape rect;
    rect.setSize(sf::Vector2f(200.f, 300.f));
    rect.setPosition(sf::Vector2f(200.f, 200.f));
    // rect.setColor(sf::Color::Blue);
    rect.setFillColor(sf::Color(107, 160, 222, 255));

    sf::Event event;

    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                window.close();
                break;

            case sf::Event::KeyPressed:
                if (event.key.code == sf::Keyboard::Q)
                {
                    window.close(); 
                }           
                break;
            default:
                break;
            }
        }
        window.clear(sf::Color::White);
        window.draw(rect);
        window.display();
    }   
    return 0;
}

Latest post

Variables (C++ Tutorial)
Let's take a closer look at C++ variables.
Mar 2, 2019