Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Fill the Window by color in Fullscreen mode  (Read 2261 times)

0 Members and 1 Guest are viewing this topic.

Incatrix

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Fill the Window by color in Fullscreen mode
« on: February 20, 2019, 12:20:35 am »
Hi SFML lovers :),

I have a simple program, where I want create the window in Fullscreen mode, where parameters are same as an user actually has and fill It by choosen color (Red). For this I used getDesktopMode() to detect user's parameters of the window.

Code here:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdlib>

int main() {   
        sf::VideoMode mode;
        sf::RenderWindow window;

        mode = sf::VideoMode::getDesktopMode();
        if (!mode.isValid()) {
                std::cout << "Window could not be created." << std::endl;
                std::exit(1);
        }

        window.create(mode, "", sf::Style::Fullscreen); // None or Default is OK
        window.setFramerateLimit(60);
       
        window.clear(sf::Color::Red); // It doesn't work with Fullscreen
        window.display();
       
        while (window.isOpen()) {
                sf::Event event;
               
                while (window.pollEvent(event)) {
                        switch (event.type) {
                                case sf::Event::KeyPressed:
                                        window.close();
                        }
                }
        }
       
        return 0;
}
 

When I try create the window and set Fullscreen style, window will be create. For sure I use isValid() fuction to confirm to this mode is valid in Fullscreen. Up to here all is allright.

Next I want to set backround color of the window to red. But It doesn't work, just It flash from red to white and stay white until keypressed (to the end of the program). Where is problem? When I use Default style or None style, It works. Window will be filled by red. This is what I want, but I want to do with Fullscreen style.

And my question is:

How to fill the Window correctly by red color in Fullscreen style? Just sf::Style::Fullscreen of the window I want to use.

Thank You for your hints and help.

Incatrix

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Re: Fill the Window by color in Fullscreen mode
« Reply #1 on: February 20, 2019, 01:09:46 am »
Put clear and display inside your main loop.

Incatrix

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Fill the Window by color in Fullscreen mode
« Reply #2 on: February 20, 2019, 08:36:50 pm »
Yes, for first look It will be right, but issue is just masked behind endless loop (a lot of time was spend to avoid issue). Putting clear and display in main loop is good for dynamic image, not for static. Where calling display one time is enough. There is not place to play just with graphics. But this is not point of my post.

It looks unspecified number of draw iterations are skipped.

//...
window.create(mode, "", sf::Style::Fullscreen);
window.setFramerateLimit(60);

while (window.isOpen()) {
        sf::Event event;
       
        while (window.pollEvent(event)) {
                switch (event.type) {
                        case sf::Event::KeyPressed:
                                window.close();
                }
        }
        window.clear(sf::Color::Red); // Skipped X times before do allright
        window.display();
}
//...
 

Perfectly It is seen in this code:

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>

void wait(clock_t frequency, sf::RenderWindow *win);

int main() {   
        sf::VideoMode mode;
        sf::RenderWindow window;

        mode = sf::VideoMode::getDesktopMode();
        if (!mode.isValid()) {
                std::cout << "Window could not be created." << std::endl;
                std::exit(1);
        }

        window.create(mode, "", sf::Style::Fullscreen);
        window.setFramerateLimit(60);

        window.clear(sf::Color::Red); // Fail
        window.display();
        wait(5000, &window);
        window.clear(sf::Color::Green); // Ok
        window.display();
        wait(5000, &window);
        window.clear(sf::Color::Blue); // Ok
        window.display();
        wait(5000, &window);

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

                window.clear(sf::Color::Black); // Ok
                window.display();
        }
       
        return 0;
}

void wait(clock_t frequency, sf::RenderWindow *win) {
        sf::Event event;

        clock_t start = clock();
        while ((clock() - start) < frequency) {
                while (win->pollEvent(event));
        }
}
 

It could be pass between Red, Green, Blue and Black color after five secs. But red part doesn't work. Default and Non style of window work well, Fullscreen style not.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: Fill the Window by color in Fullscreen mode
« Reply #3 on: February 20, 2019, 10:07:27 pm »
SFML is designed for real-time applications, so calling clear-display is required.

Yes, there can be some load up time, before the window first appears and before the first draw call is rendered. And then there's double buffering, which delays the rendering to the screen by one frame.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Incatrix

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Fill the Window by color in Fullscreen mode
« Reply #4 on: February 21, 2019, 12:00:25 am »
Using clear-display is without debate, it works well. Support real-time apps is important, all is about how whatever use correctly. It would be wrong just to rely on powerful components which they are today.

Quote
Yes, there can be some load up time, before the window first appears and before the first draw call is rendered. And then there's double buffering, which delays the rendering to the screen by one frame.

Thank You, I thought so.

Creating window with using getDesktopMode(), None style and setting top-left corner position do same work as well. Or...

It could be good create something to tell that window is ready for drawing.

Something like this:

//...
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "", sf::Style::Fullscreen);
window.waitForDraw(&window);
//...
 

where waitForDraw() is optional member function of sf::RenderWindow class to do not let go next until window will not to be completly ready for drawing.

But at the moment it's such a fresh idea..  :)

Incatrix

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Fill the Window by color in Fullscreen mode
« Reply #5 on: February 21, 2019, 12:34:25 pm »
The solution of this is creating of delayed loop with clear-display member functions of RenderWindow class and calling It after create or Ctor of the window. 10ms interval is enought. It is solid short time for reduce flashing and long time to completly prepare window for drawing.

 

anything