Skip to content

Commit

Permalink
[Window] Added option to add a parent reference for SDL window (#2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doprez authored Jan 26, 2024
1 parent d72aef5 commit 022296f
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions sources/engine/Stride.Graphics/SDL/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ static Window()
/// Initializes a new instance of the <see cref="Window"/> class with <paramref name="title"/> as the title of the Window.
/// </summary>
/// <param name="title">Title of the window, see Text property.</param>
public unsafe Window(string title)
public Window(string title) : this(title, IntPtr.Zero) { }

/// <summary>
/// Initializes a new instance of the <see cref="Window"/> class with <paramref name="title"/> as the title of the Window.
/// </summary>
/// <param name="title">Title of the window, see Text property.</param>
/// <param name="parent">Parent window handle</param>
public Window(string title, IntPtr parent)
{
WindowFlags flags = WindowFlags.AllowHighdpi;
#if STRIDE_GRAPHICS_API_OPENGL
Expand All @@ -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;
Expand Down

0 comments on commit 022296f

Please sign in to comment.