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

Author Topic: Can't update and clear the window  (Read 2812 times)

0 Members and 1 Guest are viewing this topic.

Bisheshwor

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Can't update and clear the window
« on: July 31, 2017, 05:53:26 pm »
I have got some problems regarding my project and I'm creating a quiz game where the window needs to be refreshed every time a new question appears. The initial menu works fine. But after the 1st question appears, the window doesn't update and doesn't clear when using the window.clear() function. Please help me in this regard.

while(window.isOpen())
{
    Event event1,event;

    while (window.pollEvent(event1))
    {
        if (event1.type == Event::Closed)
                window.close();
    }
    if (event1.type == Event::TextEntered && done == false)
    {
    if (event1.text.unicode < 128 && event1.text.unicode !=0)
        {
        str1 = static_cast<char>(event1.text.unicode);
        text2.setString(str1);
        //done = true;
        }
    }

    //window.clear();;
    window.draw(rect1);
    window.draw(rect2);
    window.draw(text2);
    window.draw(text1);

    window.draw(text3);
    window.draw(text4);
    window.draw(text5);
    window.display();


   if (Keyboard::isKeyPressed(Keyboard::Return))
        {
            if(text2.getString()=="1")
                /*{j=1;}
        }
    if(j==1)*/

        {
            text2.setString("");
            text1.setString(line1[i]);
            text3.setString(line1[i+1]);
            text4.setString(line1[i+2]);
            window.draw(rect1);
            window.draw(rect2);
            window.draw(text1);
            window.draw(text2);
            window.draw(text3);
            window.draw(text4);
            window.draw(text5);
            window.display();
   if (event.type == Event::TextEntered && done == false)
    {
    if (event.text.unicode < 128 && event.text.unicode !=0)
        {
        str1 = static_cast<char>(event.text.unicode);
        text5.setString(str1);
        }
    }

    window.draw(rect1);
    window.draw(rect2);
    window.draw(text1);
    window.draw(text2);
    window.draw(text3);
    window.draw(text4);
    window.draw(text5);
    window.draw(rect3);
    window.display();
    window.clear();


   if (Keyboard::isKeyPressed(Keyboard::Return))
        {
            if(text5.getString()=="a")



               /* {j=2;}
        }
    if(j==2)*/

       {
            text5.setString("");
            //window.clear();;
            text1.setString(line1[i+2]);
            text3.setString(line1[i+3]);
            text4.setString(line1[i+3]);
            window.draw(rect1);
            window.draw(rect2);
            window.draw(text1);
            window.draw(text2);
            window.draw(text3);
            window.draw(text4);
            window.draw(text5);
            window.display();
   // window.close();
   if (event.type == Event::TextEntered && done == false)
    {
    if (event.text.unicode < 128 && event.text.unicode !=0)
        {
        str1 = static_cast<char>(event.text.unicode);
        text2.setString(str1);
        //done = true;
        }
    }
//done=false;



    //window.clear();;
    window.draw(rect1);
    window.draw(rect2);
    window.draw(rect3);
    window.draw(text1);
    window.draw(text2);
    window.draw(text3);
    window.draw(text4);
    window.draw(text5);
    window.display();



   if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
        {
            if(text5.getString()=="1")



               /* {j=2;}
        }
    if(j==2)*/

       {
            text5.setString("");
            //window.clear();;
            text1.setString(line1[i]);
            text3.setString(line1[i+1]);
            text4.setString(line1[i+2]);
            window.draw(rect1);
            window.draw(rect2);
            window.draw(text1);
            window.draw(text2);
            window.draw(text3);
            window.draw(text4);
            window.draw(text5);
            window.display();

        }
            }
}
}
}
}
}
 

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Can't update and clear the window
« Reply #1 on: August 01, 2017, 03:27:26 am »
I have got some problems regarding my project and I'm creating a quiz game where the window needs to be refreshed every time a new question appears. The initial menu works fine. But after the 1st question appears, the window doesn't update and doesn't clear when using the window.clear() function. Please help me in this regard.

while(window.isOpen())
{
    Event event1,event;

    while (window.pollEvent(event1))
    {
        if (event1.type == Event::Closed)
                window.close();
    }
    if (event1.type == Event::TextEntered && done == false)
    {
    if (event1.text.unicode < 128 && event1.text.unicode !=0)
        {
        str1 = static_cast<char>(event1.text.unicode);
        text2.setString(str1);
        //done = true;
        }
    }

    //window.clear();;
    window.draw(rect1);
    window.draw(rect2);
    window.draw(text2);
    window.draw(text1);

    window.draw(text3);
    window.draw(text4);
    window.draw(text5);
    window.display();


   if (Keyboard::isKeyPressed(Keyboard::Return))
        {
            if(text2.getString()=="1")
                /*{j=1;}
        }
    if(j==1)*/

        {
            text2.setString("");
            text1.setString(line1[i]);
            text3.setString(line1[i+1]);
            text4.setString(line1[i+2]);
            window.draw(rect1);
            window.draw(rect2);
            window.draw(text1);
            window.draw(text2);
            window.draw(text3);
            window.draw(text4);
            window.draw(text5);
            window.display();
   if (event.type == Event::TextEntered && done == false)
    {
    if (event.text.unicode < 128 && event.text.unicode !=0)
        {
        str1 = static_cast<char>(event.text.unicode);
        text5.setString(str1);
        }
    }

    window.draw(rect1);
    window.draw(rect2);
    window.draw(text1);
    window.draw(text2);
    window.draw(text3);
    window.draw(text4);
    window.draw(text5);
    window.draw(rect3);
    window.display();
    window.clear();


   if (Keyboard::isKeyPressed(Keyboard::Return))
        {
            if(text5.getString()=="a")



               /* {j=2;}
        }
    if(j==2)*/

       {
            text5.setString("");
            //window.clear();;
            text1.setString(line1[i+2]);
            text3.setString(line1[i+3]);
            text4.setString(line1[i+3]);
            window.draw(rect1);
            window.draw(rect2);
            window.draw(text1);
            window.draw(text2);
            window.draw(text3);
            window.draw(text4);
            window.draw(text5);
            window.display();
   // window.close();
   if (event.type == Event::TextEntered && done == false)
    {
    if (event.text.unicode < 128 && event.text.unicode !=0)
        {
        str1 = static_cast<char>(event.text.unicode);
        text2.setString(str1);
        //done = true;
        }
    }
//done=false;



    //window.clear();;
    window.draw(rect1);
    window.draw(rect2);
    window.draw(rect3);
    window.draw(text1);
    window.draw(text2);
    window.draw(text3);
    window.draw(text4);
    window.draw(text5);
    window.display();



   if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
        {
            if(text5.getString()=="1")



               /* {j=2;}
        }
    if(j==2)*/

       {
            text5.setString("");
            //window.clear();;
            text1.setString(line1[i]);
            text3.setString(line1[i+1]);
            text4.setString(line1[i+2]);
            window.draw(rect1);
            window.draw(rect2);
            window.draw(text1);
            window.draw(text2);
            window.draw(text3);
            window.draw(text4);
            window.draw(text5);
            window.display();

        }
            }
}
}
}
}
}
 

I think you are lacking some basic understanding of SFML, perhaps you need to invest in some time in some tutorials? or purchase one of the many SFML books to get you started. A few issues in your code:

Code: [Select]
    window.draw(rect1);
    window.draw(rect2);
    window.draw(text1);
    window.draw(text2);
    window.draw(text3);
    window.draw(text4);
    window.draw(text5);
    window.draw(rect3);
    window.display();
    window.clear();

Clears the window straight after you display the back buffer that you've drawn to. Therefore those draw calls are redundant.

  • You're mixing a lot of events with draw calls. Which could work, but makes it hard to know what is going on.
  • event1 and event?
  • In your case, your event checking should happen in the while(window.pollEvent(event) loop


Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Can't update and clear the window
« Reply #2 on: August 01, 2017, 02:21:37 pm »
As mentioned by Gleade, the event should be checked within the pollEvent loop. This is because otherwise you would be checking the final event if there are more than one, skipping and ignoring potentially important events. Also, if there are no events, the event is invalid and checking it could result in unexpected results.

The linearity of this code is also quite difficult to read. Since each block of code represents the current state of the program, I would recommend looking into a form of state management.
The simplest is to use an enum to track which state you are in and then "switch" the code based on that enum.
Moving on from there, you can implement a simple Fixed State Machine so that code states are separated and clearer.
From there, you may then want to consider using a stack of states and "peel them off" to go back and/or have the ability to have multiple states working at once.

Clears the window straight after you display the back buffer that you've drawn to. Therefore those draw calls are redundant.
Although often the clearest way to use clear, draw and display is to place them in that exact order, it doesn't actually matter where they go in your code, just as long as they go in that order.
In this case, once everything has been drawn to the back buffer (draw), the back buffer is swapped with the front (display). Then, the back buffer (new one - was the old front) is cleared - not affecting the stuff just drawn as it's now on the front.
This way can be less clear but it isn't as bad as you might this. However, the fact that sometimes the stuff is drawn without it being first cleared (even from the previous code) means that the first frame could be garbage.
One thing to note is that there should be a clear for each display (usually).
« Last Edit: August 01, 2017, 02:24:33 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Can't update and clear the window
« Reply #3 on: August 02, 2017, 12:44:43 am »
Clears the window straight after you display the back buffer that you've drawn to. Therefore those draw calls are redundant.
Although often the clearest way to use clear, draw and display is to place them in that exact order, it doesn't actually matter where they go in your code, just as long as they go in that order.
In this case, once everything has been drawn to the back buffer (draw), the back buffer is swapped with the front (display). Then, the back buffer (new one - was the old front) is cleared - not affecting the stuff just drawn as it's now on the front.

Thanks for correcting me! I indeed made a mistake by stating that  ;D