SFML community forums

Help => Window => Topic started by: mickes27 on May 29, 2019, 11:33:14 am

Title: Combining multiple windows
Post by: mickes27 on May 29, 2019, 11:33:14 am
Hello, I am making a program which is a physical simulation. I would like to have big lattice with atoms, next to it, on right - plots and charts and on left - sliders and buttons with options. I am wondering if I can make 3 different classes, which contains separate windows and than just combine them with single, one, big window? Or maybe there is a better solution for that?

EDIT: Just found views tutorial - is it a correct way to do that?
Title: Re: Combining multiple windows
Post by: Hapax on September 21, 2019, 05:21:33 pm
You can simply just draw the parts where you want them. You can, of course, also group together those things that go together.

Using views to separate the window into partitions would likely be better done using the views' viewports although that can be tricky to understand at first. Viewports allow you to define rectangular regions of the window of which cannot be drawn outside; things are clipped to them. Definitely useful if you need things clipping to a hard border. They can (sort of) be considered separate windows (in the way you describe) so if that's what you need, views with custom viewports are the way to go.

View tutorial:
https://www.sfml-dev.org/tutorials/2.5/graphics-view.php
Viewport part of the above tutorial:
https://www.sfml-dev.org/tutorials/2.5/graphics-view.php#defining-how-the-view-is-viewed

More in-depth tutorial about views (that help with understanding how they work and how to use them properly) on the SFML Wiki:
https://github.com/SFML/SFML/wiki/Tutorial%3A-Using-View
Title: Re: Combining multiple windows
Post by: CrashManga on October 05, 2022, 06:17:31 am
Well personally I have never been a fan of Viewport or split screen but sometimes it has its merits.

How ever Multiple windows I do not find that an issue in the least bit its a bit troublesome but very doable
to make two windows here is how I did it in a test.

sf::RenderWindow window1(sf::VideoMode(640, 480), "Top down view", sf::Style::Titlebar | sf::Style::Close);
   sf::RenderWindow window2(sf::VideoMode(320, 240), "First person view", sf::Style::Titlebar | sf::Style::Close);
    std::cout << "Hello World!\n";

   window1.clear(sf::Color::White);
   window2.clear(sf::Color::Black);
   window1.display();
   window2.display();
   sf::Event ev;
   while (window1.isOpen())
   {
      while (window2.pollEvent(ev))
      {
         switch (ev.type)
         {
         case sf::Event::Closed:
            window1.close();
            break;
         case sf::Event::KeyPressed:
            if (ev.key.code == sf::Keyboard::Escape)
               window1.close();
            break;
         default:
            break;

         }
      }
   }

Now I would have loved to show a picture but if you copy this code into your main window and have the #includes incorporated in the code you will see a Hello world in the back, and two windows named as described in the code.

Happy trails my friends.
Title: Re: Combining multiple windows
Post by: Hapax on October 16, 2022, 08:04:53 pm
It's worth noting that you must be processing the events from all windows at all times, not just the one that has focus.