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

Author Topic: Multiple windows each in own thread  (Read 6159 times)

0 Members and 1 Guest are viewing this topic.

pummelFlummel

  • Newbie
  • *
  • Posts: 4
    • View Profile
Multiple windows each in own thread
« on: September 06, 2016, 09:05:13 pm »
I want a console application that is able to create some small sfml-windows that appear next to the console.
My idea was to leave logical computations together with console io in the main thread and to create a new thread for each external window to handle events etc.

I tried things like:

void spawn()
{
        sf::RenderWindow w(sf::VideoMode(400, 400), "external");
        w.setActive(true);

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

                w.clear();
                w.display();
        }
}

void main()
{
        std::thread(&spawn).detach();
        std::thread(&spawn).detach();

        std::cin.get();
}

I also experimented with creating the window in the main thread and passing it to the functions (with deactivating it before).
However, as soon as I try to spawn more than one external window, I always get an stack overflow error.

Is it possible to handle multiple windows like that? What am I missing here?
Thanks.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Multiple windows each in own thread
« Reply #1 on: September 07, 2016, 02:06:21 am »
Is strange, i test your sample, even with event handling and sprite drawing and worth as expected.

MS Visal Studio 2013 - Config. Release. SFML 2.3.2.

Code:


#include <sfml/graphics.hpp>
#include <thread>
#include <iostream>

struct ABC
{
   const char *name ;
   sf::Vector2f pos ;
};

void spawn( const char *name )
{
    sf::RenderWindow w(sf::VideoMode(400, 400), name );
    w.setActive(true);

        sf::Sprite sp ;

        sf::Texture tx ; tx.loadFromFile( "icon.png" ) ;

        sp.setTexture( tx ) ;

    while (w.isOpen())
    {
        sf::Event event;
        while (w.pollEvent(event))
            if (event.type == sf::Event::Closed)
                w.close();
                        else if( event.type == sf::Event::MouseMoved )
                                sp.setPosition( sf::Vector2f( sf::Mouse::getPosition( w ) ) ) ;

        w.clear();
        w.draw( sp ) ;
        w.display();
    }
       
}

void main()
{
        std::thread(&spawn , "WINDOW #1" ).detach();
        std::thread(&spawn , "WINDOW #2" ).detach();

    std::cin.get();
}
 

Proof -> See attachment.
I would like a spanish/latin community...
Problems building for Android? Look here

pummelFlummel

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Multiple windows each in own thread
« Reply #2 on: September 07, 2016, 02:50:28 pm »
Thank you for the quick reply and for your effort to try this.

I used SFML 2.4.0 x64 with Visual Studio 2015.

Now I set up the 2.3.2 and it works! (x86 and x64)

Is this a new feature or a new bug? Can't find anything in the changelog.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Multiple windows each in own thread
« Reply #3 on: September 07, 2016, 03:14:29 pm »
Quote
I used SFML 2.4.0 x64 with Visual Studio 2015.
Are you able to compile and run other SFML examples with this environment?
Laurent Gomila - SFML developer

pummelFlummel

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Multiple windows each in own thread
« Reply #4 on: September 07, 2016, 04:02:40 pm »
Yes, I am. For instance DarkRokus code above runs fine for one thread.
Didn't try the other modules.

I downloaded the VS 14 version of SFML 2.4 and I'm using the VS 14 Community Edition Update 3.

But the latest improvements since 2.3.2 dont seem to be dramatical, so I will simply use the older version now.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Multiple windows each in own thread
« Reply #5 on: September 07, 2016, 04:41:37 pm »
This is just another flavour of #989. If you can afford to, stick to 2.3.2 for now until the context management is cleaned up a bit.

If you still want to use 2.4.0, you can create all your windows in your main thread and offload the actual "stuff" (event processing doesn't take any significant amount of time) that takes up processing time to secondary threads. There is no performance advantage when creating each window on its own thread.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

pummelFlummel

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Multiple windows each in own thread
« Reply #6 on: September 08, 2016, 06:00:00 pm »
Ok, thank you :)

 

anything