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

Author Topic: Weird flickering shapes.  (Read 6334 times)

0 Members and 4 Guests are viewing this topic.

Heinrich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Weird flickering shapes.
« on: August 15, 2015, 02:48:41 pm »
I'm getting some curious screen flickering. Below is the code. I'm just drawing a triangle at a random point on the screen every second. The idea is to keep what has been drawn previously and just draw another triangle. I'm don't clear the screen between redraws and reuse the shape object. I assume this is a valid approach.

Here's an album with the first few frames the code generates. Pictures are in order of appearance. It's curious that every odd frame has the first triangle in it. But every even frame don't have the first triangle. Why doesn't all frames have the first triangle in it?

http://imgur.com/a/nd5xW


#include "stdafx.h"
#include <Windows.h>
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1024, 576), "SFML works!");
    sf::Vector2u windowSize = window.getSize();

    sf::CircleShape triangle(50, 3);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {          
            if (event.type == sf::Event::Closed ||
                event.type == event.KeyPressed && event.key.code == sf::Keyboard::Escape)
            {
                window.close();            
            }
        }

        int posX = rand() % windowSize.x;
        int posY = rand() % windowSize.y;

        triangle.setPosition(posX,posY);

        int r = rand() % 255;
        int g = rand() % 255;
        int b = rand() % 255;

        triangle.setFillColor(sf::Color::Color(r, g, b));

        window.draw(triangle);
        window.display();

        Sleep(1000);
    }

    return 0;
}
 

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Weird flickering shapes.
« Reply #1 on: August 15, 2015, 03:02:22 pm »
I'm getting some curious screen flickering. Below is the code. I'm just drawing a triangle at a random point on the screen every second. The idea is to keep what has been drawn previously and just draw another triangle. I'm don't clear the screen between redraws and reuse the shape object. I assume this is a valid approach.
It is not.

Please see the big red box in the tutorial.

Quote
This clear/draw/display cycle is the only good way to draw things. Don't try other strategies, such as keeping pixels from the previous frame, "erasing" pixels, or drawing once and calling display multiple times. You'll get strange results due to double-buffering.
Modern graphics hardware and APIs are really made for repeated clear/draw/display cycles where everything is completely refreshed at each iteration of the main loop. Don't be scared to draw 1000 sprites 60 times per second, you're far below the millions of triangles that your computer can handle.


Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Weird flickering shapes.
« Reply #2 on: August 15, 2015, 03:03:29 pm »
I can not tell you for sure why you get the flickering problem, but the tutorial states (as far as I can remember) that the only true way to draw stuff in SFML is the clear - draw - display cycle.
//Edit: This as of now has already been pointed out by someone.

My personal approach to keeping multiple objects inbetwen rendering cycles would be to store them in a vector and draw each object again each frame.

And instead of using sleep, consider accumulating delta times and only create a new triangle when one second has passed (and then reset the total time). Check sf::Time, sf::Clock.

You don't need stdafx.h and Windows.h, use en empty project.

I have not worked that much with vertex arrays yet myself, but they might improve performance in comparison to using sf::CircleShape for triangles (but I really can't guarantee that).

I hope that my suggestions may help you. Perhaps someone else has better ones.

Heinrich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Weird flickering shapes.
« Reply #3 on: August 15, 2015, 03:08:26 pm »

Thanks both. It seems my attention slipped when reading the tutorial. :)

Out of curiosity, how would you go about just adding to a previous frame without the need to remember all the objects? I.e. draw a trillion random triangles. That's more than you can keep in ram but perfectly doable if you just draw on the same surface. Do you draw them to an Image or something?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Weird flickering shapes.
« Reply #4 on: August 15, 2015, 03:09:52 pm »
Draw to a rendertexture.
Or just draw fewer triangles ;)  you won't be able to see one trillion individual triangles on your screen anyway.
« Last Edit: August 15, 2015, 03:11:49 pm by Jesper Juhl »

Heinrich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Weird flickering shapes.
« Reply #5 on: August 15, 2015, 03:26:03 pm »
Got it to work now with rendertexture. Thanks!

Gotta have those trillion trinagles...

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Weird flickering shapes.
« Reply #6 on: August 15, 2015, 03:29:55 pm »
Just out of interest, why do you need a trillion triangles?

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Weird flickering shapes.
« Reply #7 on: August 15, 2015, 03:39:05 pm »
Using the Rendertexture is considerably faster than using std::vector (I just tested it). You will still have to wait quite some time until you got the trillion triangles. Like, years?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Weird flickering shapes.
« Reply #8 on: August 15, 2015, 03:40:58 pm »
Just out of interest, why do you need a trillion triangles?
Because who can't display 120k triangles per pixel?  :D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Heinrich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Weird flickering shapes.
« Reply #9 on: August 15, 2015, 03:44:48 pm »
You will still have to wait quite some time until you got the trillion triangles. Like, years?

I'm not in a hurry. :)

No seriously it was mostly academic curiosity. I don't need to draw a trillion triangles. But with rendertexture I could if I wanted and it wouldn't use more ram than 1 triangle (or thereabouts). At least not proportionally more.