SFML community forums

Help => Graphics => Topic started by: sudgy on November 07, 2018, 07:53:56 pm

Title: Drawing onto an externally-created context
Post by: sudgy on November 07, 2018, 07:53:56 pm
Hello,

I have some graphics code that draws by inheriting sf::Drawable, does a bunch of complicated stuff to draw, etc.  Now, I want to use this same drawing code in a different application.  The issue is that this different application is using a GUI library that creates the OpenGL context for you.  As far as I can tell, the only thing that I can get from this GUI library is the context itself.  Is there any way I can tell SFML to draw to this context?  It seems like SFML's classes must draw onto a RenderTarget, and I can't find a way to tell it to draw to my context.  Is there any way to do this?
Title: Re: Drawing onto an externally-created context
Post by: Hapax on November 07, 2018, 10:28:53 pm
I suppose it would depend on the GUI library and whether it exposes its context and its container. If it's exposed, however, you should be able to allow SFML to take it over using something like this:
window.create(gui.getWindowHandle());
Of course, the getWindowHandle function should be replaced by the GUI's method of doing it.

https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Window.php#a6d60912633bff9d33cf3ade4e0201de4

Note that "window handle" in this case doesn't necessarily only refer to actual user windows.
Title: Re: Drawing onto an externally-created context
Post by: sudgy on November 07, 2018, 11:00:15 pm
I don't see any way to get the native window handle in my GUI library (although I'm not too familiar with it at the moment and I could be missing something).  I see a way to make the window's context current, and then one thing that "returns an integer handle to the underlying OpenGL context" (it says on linux it's the result of glXCreateContext and on windows it's the result of wglCreateContext).  I'm sure I could get the native handle from those somehow (I only need to support linux and windows), but is there any way to tell SFML to just draw to the current context?  If not, I'll find some way to get the handle, but it would be nice if there was a simpler way.
Title: Re: Drawing onto an externally-created context
Post by: Hapax on November 07, 2018, 11:03:57 pm
I honestly don't know the details behind all of this. Perhaps SFML always creates its own context so needs the window (or control) in which to create one. Maybe it reuses one already there. I presume it always creates its own though.
Title: Re: Drawing onto an externally-created context
Post by: Laurent on November 08, 2018, 08:03:31 am
SFML always creates its own OpenGL context, even if you embed it into an existing window.

I don't think there's a way to make it use an external context.
Title: Re: Drawing onto an externally-created context
Post by: sudgy on June 10, 2019, 04:27:09 pm
In case anybody's wondering, I did find a way to work this out.  I drew onto a RenderTexture, copied those pixels to a texture in the external context, and did a bit of raw opengl just to draw a single texture.  It's probably not the most efficient due to the extra copying through memory, but it works for my case.
Title: Re: Drawing onto an externally-created context
Post by: FRex on June 12, 2019, 06:56:27 pm
Quote
SFML always creates its own OpenGL context, even if you embed it into an existing window.
This (yes, I see it's old 2018 post) is no longer true in code on GH btw if you don't want SFML's context: https://github.com/SFML/SFML/pull/1484

Quote
I don't think there's a way to make it use an external context.
Still true AFAIK.