Skip to content

Commit

Permalink
Fix (or warn on) potential window/display ID desyncs
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Jan 30, 2025
1 parent 85a8457 commit b321fd4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
30 changes: 21 additions & 9 deletions src/FNAPlatform/SDL3_FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,8 @@ public static GraphicsAdapter RegisterGame(Game game)
uint displayId = SDL.SDL_GetDisplayForWindow(
game.Window.Handle
);
int displayIndex = FetchDisplayIndex(displayId);
int displayIndex;
TryFetchDisplayIndex(displayId, out displayIndex);
return GraphicsAdapter.Adapters[displayIndex];
}

Expand Down Expand Up @@ -1018,11 +1019,12 @@ ref bool textInputSuppress
uint newId = SDL.SDL_GetDisplayForWindow(
game.Window.Handle
);
int newIndex = FetchDisplayIndex(newId);

if (newIndex >= GraphicsAdapter.Adapters.Count)
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);
}

if (GraphicsAdapter.Adapters[newIndex] != currentAdapter)
Expand Down Expand Up @@ -1054,8 +1056,15 @@ ref bool textInputSuppress
uint displayId = SDL.SDL_GetDisplayForWindow(
game.Window.Handle
);
int displayIndex = FetchDisplayIndex(displayId);
currentAdapter = GraphicsAdapter.Adapters[displayIndex];
int displayIndex;
if (TryFetchDisplayIndex(displayId, out displayIndex))
{
currentAdapter = GraphicsAdapter.Adapters[displayIndex];
}
else
{
currentAdapter = GraphicsAdapter.DefaultAdapter;
}

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

// FIXME SDL3: This is really sloppy -flibit
private static uint[] displayIds;
private static int FetchDisplayIndex(uint id)
private static bool TryFetchDisplayIndex(uint id, out int index)
{
for (int i = 0; i < displayIds.Length; i += 1)
{
if (id == displayIds[i])
{
return i;
index = i;
return true;
}
}
throw new InvalidOperationException();
FNALoggerEXT.LogWarn("SDL3 Window ID and display ID desync'd");
index = -1;
return false;
}

public static GraphicsAdapter[] GetGraphicsAdapters()
Expand Down
8 changes: 4 additions & 4 deletions src/Graphics/GraphicsAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ public static ReadOnlyCollection<GraphicsAdapter> Adapters
{
if (adapters == null)
{
adapters = new ReadOnlyCollection<GraphicsAdapter>(
FNAPlatform.GetGraphicsAdapters()
);
AdaptersChanged();
}
return adapters;
}
Expand Down Expand Up @@ -255,7 +253,9 @@ public bool QueryBackBufferFormat(

internal static void AdaptersChanged()
{
adapters = null;
adapters = new ReadOnlyCollection<GraphicsAdapter>(
FNAPlatform.GetGraphicsAdapters()
);
}

#endregion
Expand Down

0 comments on commit b321fd4

Please sign in to comment.