SFML community forums

Help => Graphics => Topic started by: hartwall on January 18, 2016, 10:54:31 pm

Title: [Solved] Drawing to a sf::RenderTexture on separate thread
Post by: hartwall on January 18, 2016, 10:54:31 pm
Hey folks! I'm trying to create a game using sfml and Qt on Windows! I want to separate all the game logic and rendering to a separate QThread and draw the result on a canvas on the main gui-thread. I was hoping I could render the picture in a sf::RenderTexture and signal the texture using Qt:s signal/slot system to the gui-thread. However, the program always crashes with a segmentation fault when calling the sf::RenderTexture::getTexture() in the game-thread. Any ideas? Here's some debug-info:
0       ntdll!RtlReleaseSRWLockShared                   0x77e64d78     
1       ntdll!RtlAllocateHeap                   0x77e62eab     
2       ntdll!RtlAllocateHeap                   0x77e62c0e     
3       ntdll!RtlpNtSetValueKey                 0x77f0d199     
4       ntdll!RtlReleaseSRWLockShared                   0x77e64b10     
5       ntdll!RtlAllocateHeap                   0x77e62eab     
6       ntdll!RtlAllocateHeap                   0x77e62c0e     
7       msvcrt!malloc                   0x77ba7990     
8       libstdc++-6!_Znwj                       0xab372da      
9       (anonymous namespace)::getInternalContext       GlContext.cpp   155     0xa81b35       
10      sf::priv::GlContext::ensureContext      GlContext.cpp   213     0xa81dd6                               
 
Title: Re: Drawing to a sf::RenderTexture on separate thread
Post by: Mr_Blame on January 19, 2016, 09:41:07 am
do not try to make multithreaded app in SFML. opengl does not support multithreading in current version. You will not increase perfomence. If you want multithreding then wait for new vulcan API. ;)
Title: Re: Drawing to a sf::RenderTexture on separate thread
Post by: eXpl0it3r on January 19, 2016, 10:57:20 am
I want to separate all the game logic and rendering to a separate QThread
Don't.

Multi-threading is an advanced topic, that requires very good knowledge of the memory model and programming in general. There are many, many problems that can arise from multi-threading and there's nearly no gain in doing so, especially since as Mr_Blame said, OpenGL is not multi-threaded and additionally to draw something you need a complete state, so you can't really parallelize it.
Title: Re: Drawing to a sf::RenderTexture on separate thread
Post by: hartwall on January 20, 2016, 05:40:32 pm
I understand that it is difficult but I really want to use Qt for the menus since they are an important part of the game.

My solution is to start a new QThread with it's own sf::Window to show the game world and minimize the menu(-thread) while the game itself is active.