Skip to content

Commit

Permalink
Fix window size not being stored to config during resize
Browse files Browse the repository at this point in the history
Uses SDL event watch to store only after the internal state is properly updated.
  • Loading branch information
Susko3 committed Jul 19, 2023
1 parent 25d97ca commit 5317750
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 20 additions & 0 deletions osu.Framework/Platform/SDL2Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ internal SDL.SDL_SysWMinfo GetWindowSystemInformation()
[UsedImplicitly]
private SDL.SDL_EventFilter? eventFilterDelegate;

[UsedImplicitly]
private SDL.SDL_EventFilter? eventWatchDelegate;

/// <summary>
/// Represents a handle to this <see cref="SDL2Window"/> instance, used for unmanaged callbacks.
/// </summary>
Expand Down Expand Up @@ -254,6 +257,7 @@ public virtual void Create()
public void Run()
{
SDL.SDL_SetEventFilter(eventFilterDelegate = eventFilter, ObjectHandle.Handle);
SDL.SDL_AddEventWatch(eventWatchDelegate = eventWatch, ObjectHandle.Handle);

RunMainLoop();
}
Expand Down Expand Up @@ -326,7 +330,13 @@ protected virtual void HandleEventFromFilter(SDL.SDL_Event evt)
case SDL.SDL_EventType.SDL_APP_LOWMEMORY:
LowOnMemory?.Invoke();
break;
}
}

protected void HandleEventFromWatch(SDL.SDL_Event evt)
{
switch (evt.type)
{
case SDL.SDL_EventType.SDL_WINDOWEVENT:
// polling via SDL_PollEvent blocks on resizes (https://stackoverflow.com/a/50858339)
if (evt.window.windowEvent == SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED && !updatingWindowStateAndSize)
Expand All @@ -346,6 +356,16 @@ private static int eventFilter(IntPtr userdata, IntPtr eventPtr)
return 1;
}

[MonoPInvokeCallback(typeof(SDL.SDL_EventFilter))]
private static int eventWatch(IntPtr userdata, IntPtr eventPtr)
{
var handle = new ObjectHandle<SDL2Window>(userdata);
if (handle.GetTarget(out SDL2Window window))
window.HandleEventFromWatch(Marshal.PtrToStructure<SDL.SDL_Event>(eventPtr));

return 1;
}

private bool firstDraw = true;

public void OnDraw()
Expand Down
4 changes: 1 addition & 3 deletions osu.Framework/Platform/SDL2Window_Windowing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,7 @@ private void fetchWindowSize()
Scale = (float)drawableW / w;
Size = new Size(w, h);

// This function may be invoked before the SDL internal states are all changed. (as documented here: https://wiki.libsdl.org/SDL_SetEventFilter)
// Scheduling the store to config until after the event poll has run will ensure the window is in the correct state.
EventScheduler.AddOnce(storeWindowSizeToConfig);
storeWindowSizeToConfig();
}

#region SDL Event Handling
Expand Down

0 comments on commit 5317750

Please sign in to comment.