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

Author Topic: Help me with this doubt.  (Read 1809 times)

0 Members and 1 Guest are viewing this topic.

deso

  • Newbie
  • *
  • Posts: 13
    • View Profile
Help me with this doubt.
« on: October 14, 2015, 12:07:45 am »
I wrote a class to draw my game screen. When it initializes, it draws some stuff to a window pointer. The funny thing is, if I call window.display() from the same member function that draws the stuff to the window, I get everything working correctly. But if I wait to call window.display() after the member function returns, the stuff is drawn, but I get flickering. Why does this happen?

Lemme post some code so you can understand, if I was not clear enough:

This works:

   
Quote
tgui::Gui *p_gui = &gui;
   sf::RenderWindow *p_window = &window;
   Game_State *game_screen = new Game_Screen();
   game_screen->start(p_gui, p_window);

void Game_Screen::start(tgui::Gui *gui, sf::RenderWindow *window)
{   
   sf::Texture map_texture;
   if (!map_texture.loadFromFile("test_map.png"))
   {
      std::cout << "Failed to load map file" << std::endl;
   }

   sf::Sprite map_sprite;
   map_sprite.setTexture(map_texture);

   sf::View view(sf::FloatRect(0, 0, 600, 600));
   view.setViewport(sf::FloatRect(0, 0, 0.75, 1));

   window->clear();
   window->setView(view);
   window->draw(map_sprite);
   window->display();   
}
//----------MEMBER FUNCTION ENDS HERE, WINDOW LOOP STARTS

   while (window.isOpen())
   {
      sf::Event event;
      while (window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            window.close();
      }
      window.display();
   }

This doesn't.

   
Quote
tgui::Gui *p_gui = &gui;
   sf::RenderWindow *p_window = &window;
   Game_State *game_screen = new Game_Screen();
   game_screen->start(p_gui, p_window);

void Game_Screen::start(tgui::Gui *gui, sf::RenderWindow *window)
{   
   sf::Texture map_texture;
   if (!map_texture.loadFromFile("test_map.png"))
   {
      std::cout << "Failed to load map file" << std::endl;
   }

   sf::Sprite map_sprite;
   map_sprite.setTexture(map_texture);

   sf::View view(sf::FloatRect(0, 0, 600, 600));
   view.setViewport(sf::FloatRect(0, 0, 0.75, 1));

   window->clear();
   window->setView(view);
   window->draw(map_sprite);
}
//----------MEMBER FUNCTION ENDS HERE, WINDOW LOOP STARTS

   while (window.isOpen())
   {
      sf::Event event;
      while (window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            window.close();
      }
      window.display();
   }



zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Help me with this doubt.
« Reply #1 on: October 14, 2015, 12:13:08 am »
SFML's windows are double buffered. This means you must clear/draw/display every frame as explained in the tutorials. The flickering is because you draw once to the back buffer then only call display every frame. The display call swaps the back buffer with the front buffer and since one buffer has never been drawn on you get flickering.

On a side note: It is clear that you aren't a native english speaker, but in english most people use the term 'I have a question' or 'Please help me with this problem' instead of 'doubt'.  ;)
« Last Edit: October 14, 2015, 12:21:55 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Help me with this doubt.
« Reply #2 on: October 14, 2015, 12:58:47 am »
You should provide code that we can compile, and test.

But like zsbzsb said you need to call clear/draws/display in this order and avoid to call display more than one per cicle if not you will get a flickering screen.
I would like a spanish/latin community...
Problems building for Android? Look here

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Help me with this doubt.
« Reply #3 on: October 14, 2015, 01:10:13 am »
You should provide code that we can compile, and test.

I already explained what the problem is, he doesn't need to provide anything else.

avoid to call display more than one per cicle if not you will get a flickering screen.

What is this supposed to mean? This has nothing to do with the problem.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Help me with this doubt.
« Reply #4 on: October 14, 2015, 05:01:48 pm »
Quote
What is this supposed to mean? This has nothing to do with the problem.

If for expample you make this:

window.display()
window.display() // make it twice
 

You'll get a flickering screen.

The code that he provide is not clear at all. A compilable example will give us the opportunity of see the error and correct it , then we can post the correct code and explain better the problem.

Yeah, we are not here to code for others, but we can test and correct a minimal example that show the error.

You give the answer, but he could need to know where is the problem and how avoid it in the future.
I would like a spanish/latin community...
Problems building for Android? Look here

 

anything