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

Author Topic: Drawing onto an externally-created context  (Read 3239 times)

0 Members and 1 Guest are viewing this topic.

sudgy

  • Newbie
  • *
  • Posts: 3
    • View Profile
Drawing onto an externally-created context
« 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?

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing onto an externally-created context
« Reply #1 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

sudgy

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Drawing onto an externally-created context
« Reply #2 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing onto an externally-created context
« Reply #3 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing onto an externally-created context
« Reply #4 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.
Laurent Gomila - SFML developer

sudgy

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Drawing onto an externally-created context
« Reply #5 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.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Drawing onto an externally-created context
« Reply #6 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.
Back to C++ gamedev with SFML in May 2023