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

Author Topic: Clearing the window  (Read 1434 times)

0 Members and 1 Guest are viewing this topic.

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Clearing the window
« on: July 14, 2018, 03:06:19 am »
I know that it's not a smart question, but I couldnt find the answers and english isn't my first language.
I made a window, couple of buttons, and put a few strings around. Now I'm trying to make a button that would "take me to another screen", so basically clear and then replace everything. Cool thing would be to be able to return to the previous one as well.
I would be super grateful for a short example. Thank you very much!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Clearing the window
« Reply #1 on: July 14, 2018, 03:25:46 am »
Since the window should be cleared, drawn and displayed completely each time, to stop showing something, you simply stop drawing it. If you want to draw something different, draw that instead.

The concept of switching between "screens" or "states" can be found by googling something like "state machine" or "screen state".

I believe eXpl0it3r has a basic version somewhere from that you can learn.



If a state manager is a little daunting at first, consider starting of using an alternative using flags with ifs or enums with switches. The code can get a bit messy (or at least rather indented) but it's a good starting point to see what's going on.

Here's an example (it's not complete; you'll need to create the window, include stuff and put it in main(), for example):
sf::Rectangle rectangle({ 200.f, 100.f });
sf::CircleShape circle(100.f);
sf::CircleShape triangle(100.f, 3u);

enum class Screen
{
    One,
    Two,
    Three
} screen{ Screen::One };

while (window.isOpen())
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
        else if (event.type) == sf::Event::Keypressed)
        {
            if (event.key.code == sf::Keyboard::Num1)
                screen = Screen::One;
            else if (event.key.code == sf::Keyboard::Num2)
                screen = Screen::Two;
            else if (event.key.code == sf::Keyboard::Num3)
                screen = Screen::Three;
        }
    }

    window.clear();

    // this is the important part of this example
    switch (screen)
    {
    case Screen::One:
        window.draw(rectangle);
        break;
    case Screen::Two:
        window.draw(circle);
        break;
    case Screen::Three:
        window.draw(triangle);
        break;
    }

    window.display();
}
In this example, you can change "screens" by simply pressing the 1, 2 or 3 keys. Each screen has on it a different shape.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Clearing the window
« Reply #2 on: July 14, 2018, 01:26:19 pm »
Oooh, that's really cool! Thanks for the example!
But you're right, if I wanted to make lets say a menu that you can enter, then go deeper into settings and then again into lets say controls, the code would be super messy. Each part would have to have enums or ifs inside ifs inside ifs.
I never heard of screen state/state machine, but hopefully with some studying it would provide a bit cleaner solution.
Thanks once again!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Clearing the window
« Reply #3 on: July 14, 2018, 02:26:42 pm »
I found eXpl0it3r's example; it's here:
https://github.com/eXpl0it3r/SmallGameEngine

One thing to note is that it's basically replacing sections of code by things that change in each state (screen). This is probably what is drawn to the window but also usually includes the actual processing of events and other updates since controls and logic change from one state to the next.
You should also be comfortable working with inheritance.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*