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

Author Topic: (Solved) Splitting the screen with sf::View  (Read 2336 times)

0 Members and 1 Guest are viewing this topic.

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
(Solved) Splitting the screen with sf::View
« on: April 19, 2020, 03:51:03 pm »
Hello,
The non spiltted window shows as this :


The entity that is drawn is the grid, it is a rectangur shape called RS, you will see it mentioned at the end of post.

I try to split the screen in half by having 2 views in the same window like this  (view3b is the main view)
if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::D) // Double view
                  {
                                    sf::View view3bSave=view3b; // i save a copy of the main view)
                                   //   player 1 (left side of the screen)
                                    view3b.setViewport(sf::FloatRect(0.f, 0.f, 0.5f, 1.f));
                                    window.setView(view3b);
                                    // player 2 (right side of the screen)
                                    view3bSave.setViewport(sf::FloatRect(0.5f, 0.f, 0.5f, 1.f));
                                    window.setView(view3bSave);
                  }
 
And i get only the right part :


Then i read this post : https://en.sfml-dev.org/forums/index.php?topic=11526.0
Where it is expalained the "window.draw(world);" has to be done following each "setView".
I tried to draw the rectangular shape (the grid) RD, as following.

                  if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::D) // Double view
                  {
                                    sf::View view3bSave=view3b;
                                   //   player 1 (left side of the screen)
                                    view3b.setViewport(sf::FloatRect(0.f, 0.f, 0.5f, 1.f));
                                    window.setView(view3b);
                                    window.draw(RS);
                                    // player 2 (right side of the screen)
                                    view3bSave.setViewport(sf::FloatRect(0.5f, 0.f, 0.5f, 1.f));
                                    window.setView(view3bSave);
                                    window.draw(RS);

                  }

It did not work ! By the way here is the declaration of the main View :
sf::View view3b;
    view3b.reset(sf::FloatRect(0.f, 0.f, 800.f, 800.f));
// its the size of the window.
What am i missing?
« Last Edit: April 19, 2020, 03:57:59 pm by Power »

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Splitting the screen with sf::View
« Reply #1 on: April 19, 2020, 03:57:29 pm »
Found the solution !   :P
The draw function had to be written after the clear function, so i took them outside the "window.pollEvent" thing, and it worked

window.clear(sf::Color::White);


                                    window.setView(view3b);
                                    window.draw(RS);
                                    window.setView(view3bSave);
                                    window.draw(RS);



        window.display();
« Last Edit: April 19, 2020, 04:00:57 pm by Power »

 

anything