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

Author Topic: SFML2 blinking screen  (Read 7427 times)

0 Members and 1 Guest are viewing this topic.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
SFML2 blinking screen
« on: August 27, 2012, 03:54:27 pm »
hello everybody!
first, i'd like to thank the creator and contributors of this library. it's really easy to understand, and is helping me a lot in learning on how to make a game.

then, i have a question that i believe it's simple.
i have loaded a texture and then created a sprite. this sprite is a tile, so i draw it a lot of times in the screen. then i update the screen, and draw all over again.
the problem is that the screen blinks every time it draws the tiles. (it blinks all tiles at once, and not one at time)
i don't know what could be wrong.

this is how i made it:

#include "head.h"
int main(){
    sf::RenderWindow window(sf::VideoMode (480, 480), "Jogo2");
    window.setFramerateLimit(60);
    Mapa mapa;
    mapa.criarMapa();
    for (int i = 0; i<100; i++){
        window.clear();
        mapa.desMapa(&window); //[b]see note below for this[/b]
        sf::sleep(sf::milliseconds(10));
        window.display();
    }
    system("pause");
    return 0;
}

note: the line mapa.desMapa(&window) draws the tiles on the screen. it does the following:
    window->draw(sChaoPedr); //draw the sprite
    sChaoPedr.setPosition(mapX*48, mapY*48); //set the sprite position
for each tile.

for some reason, it seems that window.display() clears the screen! after this comand, the screen turns black. i don't know why.

do i have to create one sprite for each tile?
if anyone thinks the error is somewhere else, please let me know, so i'll put the whole code.

i'm working on windows 7 SP1, SFML 2.0rc, Code::Blocks 10.05 (and the default mingw and gcc shipped with it).

thanks in advance :)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: SFML2 blinking screen
« Reply #1 on: August 27, 2012, 04:00:46 pm »
Hello,
Why don't you have any render loop ?

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: SFML2 blinking screen
« Reply #2 on: August 27, 2012, 04:10:37 pm »
You're sleeping between clear and display. So you clear the contents of your screen, then wait for 10 ms and then display again (followed by the next clear without any waiting period). That would be my honest guess.

Also you should really rethink your code, I don't even understand why you would use a for-loop for rendering.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: SFML2 blinking screen
« Reply #3 on: August 27, 2012, 04:24:31 pm »
thanks for the answers

but if i put the line
sf::sleep(sf::milliseconds(10));
after the window.display(), or before the window.clear(), the screen blinks even worse.
the only way it doesn't blink is if i take out the window.clear().

also, there's no real render loop. i'm just testing if the tilemap works...

Visit my game site (and hopefully help funding it? )
Website | IndieDB

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: SFML2 blinking screen
« Reply #4 on: August 27, 2012, 04:42:36 pm »
Let me refer to my signature for anything I say, but why would you add a sleep anyway when you already limit the framerate?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: SFML2 blinking screen
« Reply #5 on: August 27, 2012, 04:58:45 pm »
no worries. i'm still learning too :) i'm newbie to both SFML and C++ (and anything beyond RPG Maker).

well, that's a good thinking. but i what i understand is that the "sleep" function halts the whole program, to reduce RAM usage. and the  "limit framerate" only limits the screen refresh.
anyway, it still doesn't work if i take ou the "limit framerate" line, nor if i take out the "sleep" line  :-\

maybe i should make a double buffer? doesn't SFML do that by default?
also, i think its very strange that window.display() makes the screen black...
« Last Edit: August 27, 2012, 05:02:19 pm by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: SFML2 blinking screen
« Reply #6 on: August 27, 2012, 06:00:29 pm »
There are a few things that have a wrong approache.
You should take a look at the official tutorials, the example given in the documentation and the examples that come with SFML. They contain will show you how SFML is intended to use.

Now a few hints:
  • You should not have one header file that includes all the header files you'll need, but you should only include the header files you're actually using.
  • You should have a normal 'game-loop', i.e. while(window.isOpen()) ...
  • The 'game-loop' should have the following order: Handle events, update positions/do calculations, draw entites/sprites.
  • The draw should follow the way: clear the window, draw the entites, display them.
  • Do not use the system("") function!

Here again an example of a clean loop:
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Hello World");
    window.setFramerateLimit(60);

    sf::Texture texture;
    texture.loadFromFile("image.png");
    sf::Sprite sprite(texture);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.poll(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(sprite);
        window.display();
    }
}

If this code above also produces blinking/flashing or if any of the examples that ship with SFML are flashing, then it's not SFML fault but your PC's, i.e. is your graphics driver uptodate? Have you some software interfering with graphics? Have you setup some strange video card settings?

for some reason, it seems that window.display() clears the screen! after this comand, the screen turns black. i don't know why.
Since you've set the framerate to 60fps, and you're pausing the application for 10ms every iteration and there are 100 iterations, you're application will stop running after 1-3 seconds and then just show a black screen or similar.

do i have to create one sprite for each tile?
There are diffrent ways to handle tiles, the best solution would be to use a sf::VertexArray.

If you think you did everything right, then you need to post a complete and minimal example that reproduces the problem (see the rules of the forum). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: SFML2 blinking screen
« Reply #7 on: August 28, 2012, 01:10:41 am »
GEE! i figured out what i was doing wrong!
in the "mapa.desMapa" function, everytime i draw a tile, i also use window->display(). without clearing the screen nor anything. and then, in the main loop, everything is drawn again!

like this, there's no way anyone could ever help me :P sorry for that, my fault. i did not wanted to post too much code at once...

just took that line off, and now everything is working like a charm.


the 'for' loop, as the 'system("pause") and the header are there just for tests. i'm not making any real game yet, just learning.
but thanks for the hints, anyway :)


EDIT: i forgot, thanks to everyone who tried to help me too   :D
« Last Edit: August 28, 2012, 01:19:06 am by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB