Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to add a parent reference for SDL window #2111

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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