SDL_CreateWindowFrom with OpenGL

How? SDL_GL_CreateContext just returns a nullptr. And browsing through the SDL source, the whole OpenGL setup stuff is only done in SDL_CreateWindow with SDL_WINDOW_OPENGL flag.

Am I missing something? What is the right way to use SDL in a editor application?

Sik wrote:> Aren’t you supposed to get SDL to initialize OpenGL for you in that

case? (you should still be able to create an OpenGL context once you
have the window)

Did you look at the examples in the test directory? Check out
test/testgl2.c

Cheers!On Thu, Jul 11, 2013 at 2:49 PM, valoh wrote:

**
How? SDL_GL_CreateContext just returns a nullptr. And browsing through the
SDL source, the whole OpenGL setup stuff is only done in SDL_CreateWindow
with SDL_WINDOW_OPENGL flag.

Am I missing something? What is the right way to use SDL in a editor
application?

Sik wrote:

Aren’t you supposed to get SDL to initialize OpenGL for you in that
case? (you should still be able to create an OpenGL context once you
have the window)


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

No, thanks for the hint, but after taking a quick look, it uses SDL_CreateWindow internally in SDL_test_common and not SDL_CreateWindowFrom. The only use of SDL_CreateWindowFrom is in testnative.c but not with OpenGL.

So anyone actually used OpenGL with SDL_CreateWindow? Or SDL with OpenGL in an editor application with native GUI?

Any hints?

Sam Lantinga wrote:> Did you look at the examples in the test directory? ?Check out test/testgl2.c

Cheers!

Hi Valoh,

What is your actual use-case for using SDL_CreateWindowFrom to create an
OpenGL context?

-AlexOn Fri, Jul 12, 2013 at 5:30 PM, valoh wrote:

**
No, thanks for the hint, but after taking a quick look, it uses
SDL_CreateWindow internally in SDL_test_common and not
SDL_CreateWindowFrom. The only use of SDL_CreateWindowFrom is in
testnative.c but not with OpenGL.

So anyone actually used OpenGL with SDL_CreateWindow? Or SDL with OpenGL
in an editor application with native GUI?

Any hints?

Sam Lantinga wrote:

Did you look at the examples in the test directory? Check out
test/testgl2.c

Cheers!


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

Last time I fiddled with SDL_CreateWindowFrom was when integrating SDL2 into an Ogre based games (Torchlight on linux). and ended up punting on that approach and getting Ogre to play with the SDL created context using SDL_CreateWindow/SDL_GL_CreateContext…

The issues I had with SDL_CreateWindowFrom was that none of the event handling stuff was setup by Ogre so I received no events… But that may be different in your use-case…

another example…
Legend of Grimrock did a “native GUI menu” for it’s dungeon editor… for the linux port (using SDL2) I ended up doing something “Evil” as I couldn’t get CreateWindowFrom to hook into an FLTK created window, so I created a second window via FLTK to house the menu :slight_smile:

Now, in the first case I actually DID use CreateWindowFrom for a while with OpenGL and it did work… the “key” of course is making sure whatever creates the window creates a proper GL capable window (this is why the WINDOW_OPENGL flag and the GL Attributes must be specified at window creation).

So given that you setup a proper window with the right attributes (glX crap) then in theory createwindowfrom SHOULD pop onto it.

I would start by debugging through the CreateWindowFrom method and figure out WHAT is causing it to bail for you. As frankly I would love to figure this out and make needed fixes in SDL 2 so it’s usable…On Jul 12, 2013, at 17:30 , valoh wrote:

No, thanks for the hint, but after taking a quick look, it uses SDL_CreateWindow internally in SDL_test_common and not SDL_CreateWindowFrom. The only use of SDL_CreateWindowFrom is in testnative.c but not with OpenGL.

So anyone actually used OpenGL with SDL_CreateWindow? Or SDL with OpenGL in an editor application with native GUI?

Any hints?

Sam Lantinga wrote:
Did you look at the examples in the test directory? Check out test/testgl2.c

Cheers!


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

Edward Rudd
OutOfOrder.cc
Skype: outoforder_cc
317-674-3296

I’ve used it successfully, exactly as you say, for an OpenGL window in an editor application with a native GUI.? I built the code for it a few years back.? It’s a fairly simple Delphi component that creates a normal HWND control, and then passes it to SDL.

You can find the code at http://code.google.com/p/turbu/source/browse/trunk/components/TURBU/sdl_frame.pas.? Even if you’re not a Delphi developer, the basic principles in here should be easy enough to follow.

Mason________________________________
From: valoh
To: sdl at lists.libsdl.org
Sent: Friday, July 12, 2013 2:30 PM
Subject: Re: [SDL] SDL_CreateWindowFrom with OpenGL

No, thanks for the hint, but after taking a quick look, it uses SDL_CreateWindow internally in SDL_test_common and not SDL_CreateWindowFrom. The only use of SDL_CreateWindowFrom is in testnative.c but not with OpenGL.

So anyone actually used OpenGL with SDL_CreateWindow? Or SDL with OpenGL in an editor application with native GUI?

Any hints?

Sam Lantinga wrote:
Did you look at the examples in the test directory? ?Check out test/testgl2.c

Cheers!


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

btw: thanks for all the suggestions.

In the end I patched SDL_CreateWindowFrom a little bit:

changed:
SDLCALL SDL_CreateWindowFrom(const void *data);
to
SDLCALL SDL_CreateWindowFrom(const void *data, SDL_bool supportOpenGl);

and in the implementation

if (supportOpenGl)
window->flags |= SDL_WINDOW_OPENGL; // we want to support OpenGL

// create windows…

// copied from SDL_CreateWindow:
if (window->flags & SDL_WINDOW_OPENGL) {
if (!_this->GL_CreateContext) {
SDL_SetError(“No OpenGL support in video driver”);
return NULL;
}
if (SDL_GL_LoadLibrary(NULL) < 0) {
return NULL;
}
}

that did the job to start up all the needed opengl stuff.

I dislike the concrete patch but it was the easiest way.

How are the chances to get this kind of functionality into the official SDL 2.x code base? I find it very useful to support OpenGL for SDL_CreateWindowFrom. Be it for code sharing between OpenGL/directx path (shared window creation/handling) and Editor scenario (mixing native UI with SDL OpenGL). Currently this is not possible without patching the code :frowning:

btw: I’ve tested the patch only on windows. So no idea if it works on other platforms. Eg. for windows the whole set pixel format of windows part isn’t done by SDL in this case because I already handle this in my application. Not sure how the other platforms behave in this case. But adding this setup code was actually pretty straight forward (I removed it as I did the init stuff in my application).

I would also prefer if SDL 2.0 would support this natively. Currently you only can do it when patching SDL yourself :frowning:

tehcloud wrote:> Yeah, I could really use this functionality as well, but I don’t want to patch the SDL code to make it happen.

I’ve been struggling to get this working for weeks, but keep hitting a brick wall. I’m trying to use a GtkDrawingArea. In SDL 1.2 you could easily do this with the SDL_WINDOWID hack, but that’s gone now :(.

Yeah, I could really use this functionality as well, but I don’t want to patch the SDL code to make it happen.

I’ve been struggling to get this working for weeks, but keep hitting a brick wall. I’m trying to use a GtkDrawingArea. In SDL 1.2 you could easily do this with the SDL_WINDOWID hack, but that’s gone now :(.

Well, lets hope this ability is added. Or maybe there’s some other way to do it that we’re overlooking?

I don’t see it making it into the SDL 2.0 release at this stage in the game. Maybe 2.2 or something. But either way, the ability to embed a hardware-accelerated SDL window in a GUI toolkit is important for a lot of use cases, especially videogame emulators.

What are you trying to do that’s not working?? I’ve been doing exactly that–creating an SDL OpenGL window inside a VCL (Delphi’s GUI toolkit) frame–ever since the early SDL 1.3 days, and it works fine.? Is there some specific point you’re having trouble with?

Mason________________________________
From: tehcloud
To: sdl at lists.libsdl.org
Sent: Friday, August 9, 2013 5:22 AM
Subject: Re: [SDL] SDL_CreateWindowFrom with OpenGL

Well, lets hope this ability is added. Or maybe there’s some other way to do it that we’re overlooking?

I don’t see it making it into the SDL 2.0 release at this stage in the game. Maybe 2.2 or something. But either way, the ability to embed a hardware-accelerated SDL window in a GUI toolkit is important for a lot of use cases, especially videogame emulators.


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

I’ve been playing with gears as a proof of concept.

https://github.com/rdanbrook/gears/blob/master/GTKgears.c

This line in particular:
window = SDL_CreateWindowFrom( (void *)GDK_WINDOW_XID(gtk_widget_get_window(sdlsocket)) );

I can tell by the window flags that OpenGL will not work:
printf(“Window Flags: %x\n”, SDL_GetWindowFlags(window));

Mason Wheeler wrote:> What are you trying to do that’s not working? I’ve been doing exactly that–creating an SDL OpenGL window inside a VCL (Delphi’s GUI toolkit) frame–ever since the early SDL 1.3 days, and it works fine. Is there some specific point you’re having trouble with?

Mason

From: tehcloud <@tehcloud>
To: sdl at lists.libsdl.org
Sent: Friday, August 9, 2013 5:22 AM
Subject: Re: [SDL] SDL_CreateWindowFrom with OpenGL

  Well, lets hope this ability is added. Or maybe there's some other way to do it that we're overlooking?

I don’t see it making it into the SDL 2.0 release at this stage in the game. Maybe 2.2 or something. But either way, the ability to embed a hardware-accelerated SDL window in a GUI toolkit is important for a lot of use cases, especially videogame emulators.


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

@Mason Wheeler: With SDL 2.0? I’m pretty sure it isn’t possible with SDL 2.0. I’ve browsed through the code and the OpenGL initialization is only done with SDL_CreateWindow not in SDL_CreateWindowFrom. You can easily patch SDL_CreateWindowFrom to support it (just copy some lines of code from SDL_CreateWindow) but natively SDL_CreateWindowFrom unfortunately doesn’t support OpenGL which is a pitty as it is really easy to add.

Oh, is that what you’re having trouble with?

Well yes, of course SDL doesn’t do the OpenGL initialization on the window; the window doesn’t belong to SDL.? You do that part in your code, but once you’ve done it, you can use SDL_CreateWindowFrom and get a working GL context.________________________________
From: valoh
To: sdl at lists.libsdl.org
Sent: Tuesday, August 13, 2013 1:09 PM
Subject: Re: [SDL] SDL_CreateWindowFrom with OpenGL

@Mason Wheeler: With SDL 2.0? I’m pretty sure it isn’t possible with SDL 2.0. I’ve browsed through the code and the OpenGL initialization is only done with SDL_CreateWindow not in SDL_CreateWindowFrom. You can easily patch SDL_CreateWindowFrom to support it (just copy some lines of code from SDL_CreateWindow) but natively SDL_CreateWindowFrom unfortunately doesn’t support OpenGL which is a pitty as it is really easy to add.


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

Well, then why using SDL at all? :wink:

Why should you do all the setting up OpenGL extensions stuff yourself, when SDL already can do it for you? Imo it should be possible to say here SDL is may existing window, please do all the tedious OpenGL setup for me, thanks. And actually it’s not hard to patch in.

Mason Wheeler wrote:> Oh, is that what you’re having trouble with?

Well yes, of course SDL doesn’t do the OpenGL initialization on the window; the window doesn’t belong to SDL. You do that part in your code, but once you’ve done it, you can use SDL_CreateWindowFrom and get a working GL context.

I guess you need a widget that can be a GL context (like GtkGlExt, which is discontinued :frowning: :().

It would be useful if SDL could just do all the OpenGL stuff, or at least bring back the SDL_WINDOWID environment variable so we can draw on top of a widget without worrying about what the widget is.

Hi valoh

    Did you have solved this question? I meet the same problem!

juny

valoh wrote:> SDL_CreateWindowFrom doesn’t seem compatible with OpenGL. Is there a way to create a window from an existing handle with OpenGL support?

Are there any other alternatives to use SDL in an editor environment (native GUI with SDL OpenGL window)?


sunshine

Hi volah

Do you have solved this question? I have met the same problem!

juny

valoh wrote:> SDL_CreateWindowFrom doesn’t seem compatible with OpenGL. Is there a way to create a window from an existing handle with OpenGL support?

Are there any other alternatives to use SDL in an editor environment (native GUI with SDL OpenGL window)?


sunshine

I haven’t solved it yet. I’m thinking of either reparenting the window using Xlib + using Wayland subsurfaces in the future, or just importing and changing small pieces of SDL.

I have the same problem. I am running SDL in Win7. It was much more convenient to fix the solution using the SDL_WINDOWID environment variable trick then SDL_CreateWindowFrom.

I implemented a solution similar to valoh’s SDLCALL SDL_CreateWindowFrom(const void *data, SDL_bool supportOpenGl), but on Windows I also had to modify the code in SDL_windowswindow.c to setup the pixel format after setting the windowdata, otherwise the context was still invalid like this:

Code:
int
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
{
HWND hwnd = (HWND) data;
[…]
if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
return -1;
}

// Added code to setup the pixel format
if (window->flags & SDL_WINDOW_OPENGL) {
    if (WIN_GL_SetupWindow(_this, window) < 0) {
        WIN_DestroyWindow(_this, window);
        return -1;
    }
}
return 0;

}