SFML community forums

Help => Graphics => Topic started by: ercarlitosg on February 06, 2014, 05:43:58 pm

Title: RenderWindow doesn't clear
Post by: ercarlitosg on February 06, 2014, 05:43:58 pm
Hello to all.I am new to sfml and I'm making a small game.I have a problem.The game starts showing my logo and after 2 seconds I want to show the main menu.I thought that putting in my code window.clear(); will work,but in my code doesn't work.Here is the code:

main.cpp:
#include "main.h"
using namespace std;

void log(char * text){
        cout << text << endl;
}

void showLogo(){
    window.clear();
        sf::Texture texture;
    if (!texture.loadFromFile("img/logo.jpg")){
        cout << "Error: No se puede mostrar la aplicación" << endl;
        }
    sf::Sprite sprite(texture);
    window.draw(sprite);
        window.display();
        log("Mostrado");
        sf:sleep(sf::seconds(0.5f));
        log("Fin");
        window.clear(sf::Color(255,255,255,255));
}
int main(){
        while(window.isOpen()){
                showLogo();
                sf:sleep(sf::seconds(5.0f));
                sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        }
        return 0;

}

and here is main.h

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


sf::RenderWindow window(sf::VideoMode(854, 480), "Test");
void showLogo();
void log(char * text);
Title: AW: RenderWindow doesn't clear
Post by: eXpl0it3r on February 06, 2014, 05:55:42 pm
This is really not how SFML is intended to work. Instead you might want to read the tutorials (again) and adapt the rendering strctute from there.
You also might want to read up on game states/screens.
Title: Re: RenderWindow doesn't clear
Post by: ercarlitosg on February 06, 2014, 05:58:10 pm
I based my code following the example on this page:
http://www.sfml-dev.org/documentation/2.1/
Title: Re: RenderWindow doesn't clear
Post by: MadMartin on February 06, 2014, 06:32:35 pm
No, you clearly didn't. The tutorial says that there is this draw-display-cycle in your main loop. You draw once, display once and then your main loop just waits for the close event.

Put window.draw() and window.display() inside your mainloop, as it is intended to be!
Title: Re: RenderWindow doesn't clear
Post by: ercarlitosg on February 06, 2014, 06:36:21 pm
Thanks to the two,forgot window.display() when put window.clear().

I thought that window.clear() also update the screen,but this isn't true
Title: Re: RenderWindow doesn't clear
Post by: MadMartin on February 06, 2014, 09:23:45 pm
Yeah, that's right. To update the screen, i.e. swap the buffers, you need to call display().

Aside from that I wrote completely nonsense. I confused your usage of showLogo() somehow.
Your loop was nearly correct, apart from that last clear(). Sorry.
Title: AW: RenderWindow doesn't clear
Post by: eXpl0it3r on February 07, 2014, 08:09:14 am
You should still look at how to handle multiple game states/screens, because calling a function with sleep in it, is just a bad design. ;)
Title: Re: RenderWindow doesn't clear
Post by: Hapax on February 07, 2014, 01:54:50 pm
If you're not going to manage game states as eXpl0it3r suggested (you should) then you should at least take the showLogo() and sleep functions out of the main loop ;)