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

Author Topic: [SOLVED] Mix RenderWindow with OpenGL  (Read 1971 times)

0 Members and 1 Guest are viewing this topic.

Lauthai

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED] Mix RenderWindow with OpenGL
« 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.
« Last Edit: January 30, 2021, 07:27:57 pm by Lauthai »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Mix RenderWindow with OpenGL
« Reply #1 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). See also this tutorial on how to mix SFML with OpenGL.

Finally, I'd suggest to use Glad or similar to load OpenGL functions instead of GLEW.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lauthai

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Mix RenderWindow with OpenGL
« Reply #2 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 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.