SFML community forums

Help => Graphics => Topic started by: Lauthai on January 28, 2021, 07:48:19 am

Title: [SOLVED] Mix RenderWindow with OpenGL
Post by: Lauthai on January 28, 2021, 07:48:19 am
Hello everyone,

I am working on programming a game in which there are two threads, Thread A (which renders using SFML based calls to draw) and Thread B (which uses direct OpenGL based calls).  What happens is that I draw some text on screen in Thread A, then pause and switch to Thread B where I draw a Purple Triangle with a blue background.  When it switches back from Thread B to Thread A and tries to draw using SFML again, the purple triangle persists and the text does not appear.  This triangle then stays on screen forever and nothing overrides it.  I am using a RenderWindow when creating my window for the game.  I believe that I am missing some call that is needed to switch from OpenGL back to SFML but I am not sure what it is.  Maybe it is something else I am missing.

I went and created a fresh .cpp file that contains only the base code needed to run this example.  Each Thread runs for 3 seconds then switches (mostly documented in code) and if you run the code, the ESC key is a kill key as it runs in full screen.

(click to show/hide)

I am using the latest version of SFML and GLEW to run this .cpp example file.  I have attached the .cpp file (which is the code in the spoiler above)

Please let me know if there is something that I missed in my code that causes it to not be able to switch back from OpenGL drawing to SFML drawing.
Title: Re: Mix RenderWindow with OpenGL
Post by: eXpl0it3r on January 30, 2021, 12:07:21 pm
I don't see how threads help with anything you're doing here, instead they raise complexity by a lot and require special handling for OpenGL contexts.
You should instead really just be using one thread (see also this explanation (https://en.sfml-dev.org/forums/index.php?topic=27854.msg175231#msg175231)). See also this tutorial (https://www.sfml-dev.org/tutorials/2.5/window-opengl.php) on how to mix SFML with OpenGL.

Finally, I'd suggest to use Glad (https://glad.dav1d.de/) or similar to load OpenGL functions instead of GLEW.
Title: Re: Mix RenderWindow with OpenGL
Post by: Lauthai on January 30, 2021, 07:27:39 pm
I know the threads seem very convoluted and even complicate things, but that's the way the program I am making runs everything.  I was about to find a solution over on this thread: https://en.sfml-dev.org/forums/index.php?topic=24081.0 (https://en.sfml-dev.org/forums/index.php?topic=24081.0) Where after my while loop that ends around line 217 in the second thread, I needed to add

glBindVertexArray(0);
window->pushGLStates();
 

I needed to do additional clean up before returning out of Thread B.  But those lines did fix the problem.