SDL_CreateWindowFrom and SDL_GL_CreateContext

Hi,

I am trying to use SDL to create an OpenGL context on a window that was
constructed by a 3rd-party library (in this case WinForms). Apparently,
SDL_GL_CreateContext requires the SDL_WINDOW_OPENGL flag to be set,
something that SDL_CreateWindowFrom does not / cannot do.

The use case here is to direct SDL rendering to a foreign window, in order
to create an editor UI without modifying the underlying game code.

Is there any way to use SDL_GL_CreateContext on a foreign window? I do not
use SDL_GL_LoadLibrary or any related API, as I handle this myself.

Thanks,
Stefanos

We can either always set the OPENGL flag for foreign windows, allowing you
to try to create a context and let it fail if it’s not compatible, or we
can add a new API, something like SDL_CreateOpenGLWindowFrom()

Thoughts?On Thu, Oct 3, 2013 at 11:32 PM, Stefanos A. wrote:

Hi,

I am trying to use SDL to create an OpenGL context on a window that was
constructed by a 3rd-party library (in this case WinForms). Apparently,
SDL_GL_CreateContext requires the SDL_WINDOW_OPENGL flag to be set,
something that SDL_CreateWindowFrom does not / cannot do.

The use case here is to direct SDL rendering to a foreign window, in order
to create an editor UI without modifying the underlying game code.

Is there any way to use SDL_GL_CreateContext on a foreign window? I do not
use SDL_GL_LoadLibrary or any related API, as I handle this myself.

Thanks,
Stefanos


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hm, this is trickier than it looks. If we always set the OPENGL flag, then
someone is going to complain in the future that he cannot initialize
DirectX instead. While that would solve my use-case it might not be the
optimal approach.

What about a new API such as SDL_SetForeignWindowFlags(), which works only
on FOREIGN windows (fails otherwise) and acts as a promise that these flags
have been correctly set by the user (if they haven’t, then context creation
will simply fail.)

I’m not 100% happy with this either, let me pore over the source code and
see if I can come up with anything better. Smoothing over the different
init sequence in various platforms is always tricky…

2013/10/4 Sam Lantinga > We can either always set the OPENGL flag for foreign windows, allowing you

to try to create a context and let it fail if it’s not compatible, or we
can add a new API, something like SDL_CreateOpenGLWindowFrom()

Thoughts?

On Thu, Oct 3, 2013 at 11:32 PM, Stefanos A. <@Stefanos_A> wrote:

Hi,

I am trying to use SDL to create an OpenGL context on a window that was
constructed by a 3rd-party library (in this case WinForms). Apparently,
SDL_GL_CreateContext requires the SDL_WINDOW_OPENGL flag to be set,
something that SDL_CreateWindowFrom does not / cannot do.

The use case here is to direct SDL rendering to a foreign window, in
order to create an editor UI without modifying the underlying game code.

Is there any way to use SDL_GL_CreateContext on a foreign window? I do
not use SDL_GL_LoadLibrary or any related API, as I handle this myself.

Thanks,
Stefanos


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I’ve managed to patch SDL2 to enable this behavior on Windows. I’ve also
tried on Mac OS X, but I only have a Carbon application to test with, and
SDL2 is using Cocoa (obvious crash).

I tried a few different approaches, but what worked in the end was the
following:

  1. hardcode the OPENGL flag in SDL_CreateWindowFrom,
  2. call SDL_GL_LoadLibrary on the foreign application (otherwise SDL
    crashes internally in wglCreateContext etc),
  3. add a call to WIN_GL_SetupWindow if both FOREIGN & OPENGL is set inside
    WIN_CreateWindowFrom. (This will fail if a PFD is already set on this HDC,
    but this is not a problem - if a PFD is set we can freely call
    wglCreateContext/wglCreateContextAttribsARB.)

Does anyone know if any other plaftorms require anything similar to
WIN_GL_SetupWindow? IIRC, neither X11 nor Carbon require anything like
SetPixelFormat, but I have no experience on Cocoa / iOS / Android.

I can rework this into a proper patch if there is interest.

2013/10/4 Stefanos A. <@Stefanos_A>> Hm, this is trickier than it looks. If we always set the OPENGL flag, then

someone is going to complain in the future that he cannot initialize
DirectX instead. While that would solve my use-case it might not be the
optimal approach.

What about a new API such as SDL_SetForeignWindowFlags(), which works only
on FOREIGN windows (fails otherwise) and acts as a promise that these flags
have been correctly set by the user (if they haven’t, then context creation
will simply fail.)

I’m not 100% happy with this either, let me pore over the source code and
see if I can come up with anything better. Smoothing over the different
init sequence in various platforms is always tricky…

2013/10/4 Sam Lantinga

We can either always set the OPENGL flag for foreign windows, allowing
you to try to create a context and let it fail if it’s not compatible, or
we can add a new API, something like SDL_CreateOpenGLWindowFrom()

Thoughts?

On Thu, Oct 3, 2013 at 11:32 PM, Stefanos A. <@Stefanos_A> wrote:

Hi,

I am trying to use SDL to create an OpenGL context on a window that was
constructed by a 3rd-party library (in this case WinForms). Apparently,
SDL_GL_CreateContext requires the SDL_WINDOW_OPENGL flag to be set,
something that SDL_CreateWindowFrom does not / cannot do.

The use case here is to direct SDL rendering to a foreign window, in
order to create an editor UI without modifying the underlying game code.

Is there any way to use SDL_GL_CreateContext on a foreign window? I do
not use SDL_GL_LoadLibrary or any related API, as I handle this myself.

Thanks,
Stefanos


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Yes please!

You can probably adapt the testnative example in the test directory to do
OpenGL after it creates a window.

Thanks!On Fri, Oct 4, 2013 at 6:07 AM, Stefanos A. wrote:

I’ve managed to patch SDL2 to enable this behavior on Windows. I’ve also
tried on Mac OS X, but I only have a Carbon application to test with, and
SDL2 is using Cocoa (obvious crash).

I tried a few different approaches, but what worked in the end was the
following:

  1. hardcode the OPENGL flag in SDL_CreateWindowFrom,
  2. call SDL_GL_LoadLibrary on the foreign application (otherwise SDL
    crashes internally in wglCreateContext etc),
  3. add a call to WIN_GL_SetupWindow if both FOREIGN & OPENGL is set inside
    WIN_CreateWindowFrom. (This will fail if a PFD is already set on this HDC,
    but this is not a problem - if a PFD is set we can freely call
    wglCreateContext/wglCreateContextAttribsARB.)

Does anyone know if any other plaftorms require anything similar to
WIN_GL_SetupWindow? IIRC, neither X11 nor Carbon require anything like
SetPixelFormat, but I have no experience on Cocoa / iOS / Android.

I can rework this into a proper patch if there is interest.

2013/10/4 Stefanos A.

Hm, this is trickier than it looks. If we always set the OPENGL flag, then

someone is going to complain in the future that he cannot initialize
DirectX instead. While that would solve my use-case it might not be the
optimal approach.

What about a new API such as SDL_SetForeignWindowFlags(), which works
only on FOREIGN windows (fails otherwise) and acts as a promise that these
flags have been correctly set by the user (if they haven’t, then context
creation will simply fail.)

I’m not 100% happy with this either, let me pore over the source code and
see if I can come up with anything better. Smoothing over the different
init sequence in various platforms is always tricky…

2013/10/4 Sam Lantinga <@slouken>

We can either always set the OPENGL flag for foreign windows, allowing
you to try to create a context and let it fail if it’s not compatible, or
we can add a new API, something like SDL_CreateOpenGLWindowFrom()

Thoughts?

On Thu, Oct 3, 2013 at 11:32 PM, Stefanos A. wrote:

Hi,

I am trying to use SDL to create an OpenGL context on a window that was
constructed by a 3rd-party library (in this case WinForms). Apparently,
SDL_GL_CreateContext requires the SDL_WINDOW_OPENGL flag to be set,
something that SDL_CreateWindowFrom does not / cannot do.

The use case here is to direct SDL rendering to a foreign window, in
order to create an editor UI without modifying the underlying game code.

Is there any way to use SDL_GL_CreateContext on a foreign window? I do
not use SDL_GL_LoadLibrary or any related API, as I handle this myself.

Thanks,
Stefanos


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I really need this functionality as well. I’d be happy to be able to CreateOpenGLWindowFrom() or SetForeignWindowFlags(), doesn’t matter as long as I can easily embed OpenGL output in a GUI.

Yes, I would really like to include this functionality after the 2.0.1
release. Can you submit a bug report and attach a patch?
http://bugzilla.libsdl.org

Thanks!On Fri, Oct 4, 2013 at 6:07 AM, Stefanos A. wrote:

I’ve managed to patch SDL2 to enable this behavior on Windows. I’ve also
tried on Mac OS X, but I only have a Carbon application to test with, and
SDL2 is using Cocoa (obvious crash).

I tried a few different approaches, but what worked in the end was the
following:

  1. hardcode the OPENGL flag in SDL_CreateWindowFrom,
  2. call SDL_GL_LoadLibrary on the foreign application (otherwise SDL
    crashes internally in wglCreateContext etc),
  3. add a call to WIN_GL_SetupWindow if both FOREIGN & OPENGL is set inside
    WIN_CreateWindowFrom. (This will fail if a PFD is already set on this HDC,
    but this is not a problem - if a PFD is set we can freely call
    wglCreateContext/wglCreateContextAttribsARB.)

Does anyone know if any other plaftorms require anything similar to
WIN_GL_SetupWindow? IIRC, neither X11 nor Carbon require anything like
SetPixelFormat, but I have no experience on Cocoa / iOS / Android.

I can rework this into a proper patch if there is interest.

2013/10/4 Stefanos A.

Hm, this is trickier than it looks. If we always set the OPENGL flag, then

someone is going to complain in the future that he cannot initialize
DirectX instead. While that would solve my use-case it might not be the
optimal approach.

What about a new API such as SDL_SetForeignWindowFlags(), which works
only on FOREIGN windows (fails otherwise) and acts as a promise that these
flags have been correctly set by the user (if they haven’t, then context
creation will simply fail.)

I’m not 100% happy with this either, let me pore over the source code and
see if I can come up with anything better. Smoothing over the different
init sequence in various platforms is always tricky…

2013/10/4 Sam Lantinga <@slouken>

We can either always set the OPENGL flag for foreign windows, allowing
you to try to create a context and let it fail if it’s not compatible, or
we can add a new API, something like SDL_CreateOpenGLWindowFrom()

Thoughts?

On Thu, Oct 3, 2013 at 11:32 PM, Stefanos A. wrote:

Hi,

I am trying to use SDL to create an OpenGL context on a window that was
constructed by a 3rd-party library (in this case WinForms). Apparently,
SDL_GL_CreateContext requires the SDL_WINDOW_OPENGL flag to be set,
something that SDL_CreateWindowFrom does not / cannot do.

The use case here is to direct SDL rendering to a foreign window, in
order to create an editor UI without modifying the underlying game code.

Is there any way to use SDL_GL_CreateContext on a foreign window? I do
not use SDL_GL_LoadLibrary or any related API, as I handle this myself.

Thanks,
Stefanos


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org