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

Author Topic: RenderWindow doesn't clear  (Read 5289 times)

0 Members and 2 Guests are viewing this topic.

ercarlitosg

  • Newbie
  • *
  • Posts: 5
    • View Profile
RenderWindow doesn't clear
« 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);
« Last Edit: February 06, 2014, 05:48:14 pm by ercarlitosg »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: RenderWindow doesn't clear
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ercarlitosg

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: RenderWindow doesn't clear
« Reply #2 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/

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: RenderWindow doesn't clear
« Reply #3 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!

ercarlitosg

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: RenderWindow doesn't clear
« Reply #4 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

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: RenderWindow doesn't clear
« Reply #5 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: RenderWindow doesn't clear
« Reply #6 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: RenderWindow doesn't clear
« Reply #7 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 ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything