Skip to content

Commit

Permalink
Simple function signature updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Susko3 committed Apr 8, 2024
1 parent 6302046 commit 66e8ea1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
6 changes: 4 additions & 2 deletions osu.Framework/Platform/SDL2/SDL2Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,11 @@ public static unsafe bool TryGetStringFromBytePointer(byte* bytePointer, out str
return true;
}

public static DisplayMode ToDisplayMode(this SDL_DisplayMode mode, int displayIndex)
public static unsafe DisplayMode ToDisplayMode(this SDL_DisplayMode mode, int displayIndex)
{
SDL3.SDL_GetMasksForPixelFormatEnum(mode.format, out int bpp, out _, out _, out _, out _);
int bpp;
uint unused;
SDL3.SDL_GetMasksForPixelFormatEnum(mode.format, &bpp, &unused, &unused, &unused, &unused);
return new DisplayMode(SDL3.SDL_GetPixelFormatName(mode.format), new Size(mode.w, mode.h), bpp, mode.refresh_rate, displayIndex);
}

Expand Down
7 changes: 5 additions & 2 deletions osu.Framework/Platform/SDL2/SDL2GraphicsSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public void Initialise()

public Size GetDrawableSize()
{
SDL3.SDL_GetWindowSizeInPixels(window.SDLWindowHandle, out int width, out int height);
int width, height;
SDL3.SDL_GetWindowSizeInPixels(window.SDLWindowHandle, &width, &height);
return new Size(width, height);
}

Expand Down Expand Up @@ -178,7 +179,9 @@ bool IOpenGLGraphicsSurface.VerticalSync
if (verticalSync != null)
return verticalSync.Value;

return (verticalSync = SDL3.SDL_GL_GetSwapInterval() != 0).Value;
int interval;
SDL3.SDL_GL_GetSwapInterval(&interval);
return (verticalSync = interval != 0).Value;
}
set
{
Expand Down
8 changes: 6 additions & 2 deletions osu.Framework/Platform/SDL2Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public virtual void Create()
// so we deactivate it on startup.
SDL3.SDL_StopTextInput();

SDLWindowHandle = SDL3.SDL_CreateWindow(Encoding.UTF8.GetBytes(title), Position.X, Position.Y, Size.Width, Size.Height, flags);
SDLWindowHandle = SDL3.SDL_CreateWindow(Encoding.UTF8.GetBytes(title), Size.Width, Size.Height, flags);

if (SDLWindowHandle == null)
throw new InvalidOperationException($"Failed to create SDL window. SDL Error: {SDL3.SDL_GetError()}");
Expand Down Expand Up @@ -440,8 +440,12 @@ private unsafe void setSDLIcon(Image<Rgba32> image)
var pixelSpan = pixelMemory.Span;
SDL_Surface* surface;
fixed (Rgba32* ptr = pixelSpan)
surface = SDL_CreateRGBSurfaceFrom(new IntPtr(ptr), imageSize.Width, imageSize.Height, 32, imageSize.Width * 4, 0xff, 0xff00, 0xff0000, 0xff000000);
{
var pixelFormat = SDL3.SDL_GetPixelFormatEnumForMasks(32, 0xff, 0xff00, 0xff0000, 0xff000000);
surface = SDL3.SDL_CreateSurfaceFrom(new IntPtr(ptr), imageSize.Width, imageSize.Height, imageSize.Width * 4, pixelFormat);
}
SDL3.SDL_SetWindowIcon(SDLWindowHandle, surface);
SDL3.SDL_DestroySurface(surface);
Expand Down
23 changes: 16 additions & 7 deletions osu.Framework/Platform/SDL2Window_Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ public CursorState CursorState
private readonly Dictionary<int, SDL2ControllerBindings> controllers = new Dictionary<int, SDL2ControllerBindings>();

private void updateCursorVisibility(bool cursorVisible) =>
ScheduleCommand(() => SDL3.SDL_ShowCursor(cursorVisible ? SDL_ENABLE : SDL_DISABLE));
ScheduleCommand(() =>
{
if (cursorVisible)
SDL3.SDL_ShowCursor();
else
SDL3.SDL_HideCursor();
});

/// <summary>
/// Updates OS cursor confinement based on the current <see cref="CursorState"/>, <see cref="CursorConfineRect"/> and <see cref="RelativeMouseMode"/>.
Expand All @@ -105,18 +111,21 @@ private unsafe void updateCursorConfinement()
{
bool confined = CursorState.HasFlagFast(CursorState.Confined);

ScheduleCommand(() => SDL_SetWindowGrab(SDLWindowHandle, confined ? SDL_bool.SDL_TRUE : SDL_bool.SDL_FALSE));
ScheduleCommand(() => SDL3.SDL_SetWindowMouseGrab(SDLWindowHandle, confined ? SDL_bool.SDL_TRUE : SDL_bool.SDL_FALSE));

// Don't use SDL_SetWindowMouseRect when relative mode is enabled, as relative mode already confines the OS cursor to the window.
// This is fine for our use case, as UserInputManager will clamp the mouse position.
if (CursorConfineRect != null && confined && !RelativeMouseMode)
{
var rect = ((RectangleI)(CursorConfineRect / Scale)).ToSDLRect();
ScheduleCommand(() => SDL3.SDL_SetWindowMouseRect(SDLWindowHandle, ref rect));
ScheduleCommand(() =>
{
var rect = ((RectangleI)(CursorConfineRect / Scale)).ToSDLRect();
SDL3.SDL_SetWindowMouseRect(SDLWindowHandle, &rect);
});
}
else
{
ScheduleCommand(() => SDL3.SDL_SetWindowMouseRect(SDLWindowHandle, IntPtr.Zero));
ScheduleCommand(() => SDL3.SDL_SetWindowMouseRect(SDLWindowHandle, null));
}
}

Expand Down Expand Up @@ -188,10 +197,10 @@ private unsafe void pollMouse()
SDL3.SDL_StartTextInput();
});

public void SetTextInputRect(RectangleF rect) => ScheduleCommand(() =>
public unsafe void SetTextInputRect(RectangleF rect) => ScheduleCommand(() =>
{
var sdlRect = ((RectangleI)(rect / Scale)).ToSDLRect();
SDL3.SDL_SetTextInputRect(ref sdlRect);
SDL3.SDL_SetTextInputRect(&sdlRect);
});

#region SDL Event Handling
Expand Down
8 changes: 5 additions & 3 deletions osu.Framework/Platform/SDL2Window_Windowing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ private Rectangle windowDisplayBounds
/// <returns>Whether the window size has been changed after updating.</returns>
private unsafe void fetchWindowSize()
{
SDL3.SDL_GetWindowSize(SDLWindowHandle, out int w, out int h);
int w, h;
SDL3.SDL_GetWindowSize(SDLWindowHandle, &w, &h);

int drawableW = graphicsSurface.GetDrawableSize().Width;

Expand All @@ -468,7 +469,8 @@ private unsafe void handleWindowEvent(SDL_WindowEvent evtWindow)
{
case SDL_EventType.SDL_EVENT_WINDOW_MOVED:
// explicitly requery as there are occasions where what SDL has provided us with is not up-to-date.
SDL3.SDL_GetWindowPosition(SDLWindowHandle, out int x, out int y);
int x, y;
SDL3.SDL_GetWindowPosition(SDLWindowHandle, &x, &y);
var newPosition = new Point(x, y);

if (!newPosition.Equals(Position))
Expand Down Expand Up @@ -620,7 +622,7 @@ protected virtual unsafe void UpdateWindowStateAndSize(WindowState state, Displa

ensureWindowOnDisplay(display);

SDL3.SDL_SetWindowFullscreenMode(SDLWindowHandle, ref closestMode);
SDL3.SDL_SetWindowFullscreenMode(SDLWindowHandle, &closestMode);
SDL3.SDL_SetWindowFullscreen(SDLWindowHandle, (uint)SDL_WindowFlags.SDL_WINDOW_FULLSCREEN);
break;

Expand Down

0 comments on commit 66e8ea1

Please sign in to comment.