diff --git a/sources/engine/Stride.Graphics/SDL/Window.cs b/sources/engine/Stride.Graphics/SDL/Window.cs index 484c1484d8..ee95bf5dd1 100644 --- a/sources/engine/Stride.Graphics/SDL/Window.cs +++ b/sources/engine/Stride.Graphics/SDL/Window.cs @@ -37,7 +37,14 @@ static Window() /// Initializes a new instance of the class with as the title of the Window. /// /// Title of the window, see Text property. - public unsafe Window(string title) + public Window(string title) : this(title, IntPtr.Zero) { } + + /// + /// Initializes a new instance of the class with as the title of the Window. + /// + /// Title of the window, see Text property. + /// Parent window handle + public Window(string title, IntPtr parent) { WindowFlags flags = WindowFlags.AllowHighdpi; #if STRIDE_GRAPHICS_API_OPENGL @@ -50,8 +57,34 @@ public unsafe Window(string title) #else flags |= WindowFlags.Hidden | WindowFlags.Resizable; #endif - // Create the SDL window and then extract the native handle. - sdlHandle = SDL.CreateWindow(title, Sdl.WindowposUndefined, Sdl.WindowposUndefined, 640, 480, (uint)flags); + + // If there is a parent hwnd + if (parent != IntPtr.Zero) + { + void* parentPtr = parent.ToPointer(); + + if (flags.HasFlag(WindowFlags.WindowOpengl)) + { + // SDL doesn't create OpenGL context when using SDL_CreateWindowFrom. + // See https://wiki.libsdl.org/SDL_CreateWindowFrom + // and https://gamedev.stackexchange.com/a/119903. + var dummy = SDL.CreateWindow($"{title} - OpenGL Dummy", 0, 0, 1, 1, (uint)flags); + var addrStr = new IntPtr(dummy).ToString("X"); + SDL.SetHint(Sdl.HintVideoWindowSharePixelFormat, addrStr); + sdlHandle = SDL.CreateWindowFrom(parentPtr); + SDL.SetHint(Sdl.HintVideoWindowSharePixelFormat, string.Empty); + SDL.DestroyWindow(dummy); + } + else + { + sdlHandle = SDL.CreateWindowFrom(parentPtr); + } + } + else // no parent window + { + // Create the SDL window and then extract the native handle. + sdlHandle = SDL.CreateWindow(title, Sdl.WindowposUndefined, Sdl.WindowposUndefined, 640, 480, (uint)flags); + } #if STRIDE_PLATFORM_ANDROID || STRIDE_PLATFORM_IOS GraphicsAdapter.DefaultWindow = sdlHandle;