Skip to content

Commit

Permalink
SDL3: Cleaned up GraphicsAdapter fetch routine
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Jan 30, 2025
1 parent b321fd4 commit 6a3ab36
Showing 1 changed file with 24 additions and 37 deletions.
61 changes: 24 additions & 37 deletions src/FNAPlatform/SDL3_FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -821,13 +821,7 @@ public static GraphicsAdapter RegisterGame(Game game)
// Store this for internal event filter work
activeGames.Add(game);

// Which display did we end up on?
uint displayId = SDL.SDL_GetDisplayForWindow(
game.Window.Handle
);
int displayIndex;
TryFetchDisplayIndex(displayId, out displayIndex);
return GraphicsAdapter.Adapters[displayIndex];
return FetchDisplayAdapter(game.Window.Handle);
}

public static void UnregisterGame(Game game)
Expand Down Expand Up @@ -1016,20 +1010,11 @@ ref bool textInputSuppress
* display, a GraphicsDevice Reset occurs.
* -flibit
*/
uint newId = SDL.SDL_GetDisplayForWindow(
game.Window.Handle
);
int newIndex;
if ( !TryFetchDisplayIndex(newId, out newIndex) ||
newIndex >= GraphicsAdapter.Adapters.Count )
{
GraphicsAdapter.AdaptersChanged(); // quickfix for this event coming in before the display reattach event. (must be fixed in sdl)
TryFetchDisplayIndex(newId, out newIndex);
}
GraphicsAdapter next = FetchDisplayAdapter(game.Window.Handle);

if (GraphicsAdapter.Adapters[newIndex] != currentAdapter)
if (next != currentAdapter)
{
currentAdapter = GraphicsAdapter.Adapters[newIndex];
currentAdapter = next;
game.GraphicsDevice.Reset(
game.GraphicsDevice.PresentationParameters,
currentAdapter
Expand All @@ -1053,18 +1038,7 @@ ref bool textInputSuppress
{
GraphicsAdapter.AdaptersChanged();

uint displayId = SDL.SDL_GetDisplayForWindow(
game.Window.Handle
);
int displayIndex;
if (TryFetchDisplayIndex(displayId, out displayIndex))
{
currentAdapter = GraphicsAdapter.Adapters[displayIndex];
}
else
{
currentAdapter = GraphicsAdapter.DefaultAdapter;
}
currentAdapter = FetchDisplayAdapter(game.Window.Handle);

// Orientation Change
if (evt.type == (uint) SDL.SDL_EventType.SDL_EVENT_DISPLAY_ORIENTATION)
Expand Down Expand Up @@ -1223,19 +1197,32 @@ private static void RunEmscriptenMainLoop()

// FIXME SDL3: This is really sloppy -flibit
private static uint[] displayIds;
private static bool TryFetchDisplayIndex(uint id, out int index)
private static GraphicsAdapter FetchDisplayAdapter(IntPtr window, bool retry = true)
{
uint displayId = SDL.SDL_GetDisplayForWindow(window);

int index = -1;
for (int i = 0; i < displayIds.Length; i += 1)
{
if (id == displayIds[i])
if (displayId == displayIds[i])
{
index = i;
return true;
break;
}
}

if (index < 0 || index > GraphicsAdapter.Adapters.Count)
{
FNALoggerEXT.LogWarn("SDL3 Window ID and Display ID desync'd");
if (retry)
{
GraphicsAdapter.AdaptersChanged();
return FetchDisplayAdapter(window, false);
}
FNALoggerEXT.LogWarn("SDL3 Window ID and Display ID desync'd really badly");
return GraphicsAdapter.DefaultAdapter;
}
FNALoggerEXT.LogWarn("SDL3 Window ID and display ID desync'd");
index = -1;
return false;
return GraphicsAdapter.Adapters[index];
}

public static GraphicsAdapter[] GetGraphicsAdapters()
Expand Down

0 comments on commit 6a3ab36

Please sign in to comment.