From 9f24e067906878899b2fe70cc756223f97cb2640 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Thu, 26 Sep 2024 16:12:15 -0400 Subject: [PATCH] Hook up SDL3_FNAPlatform to FNAPlatform --- FNA.NetFramework.csproj | 2 + FNA.NetStandard.csproj | 2 + Makefile | 2 + src/FNAPlatform/FNAPlatform.cs | 185 +++++++++++++++++++--------- src/FNAPlatform/README | 4 +- src/FNAPlatform/SDL3_FNAPlatform.cs | 18 +++ 6 files changed, 155 insertions(+), 58 deletions(-) diff --git a/FNA.NetFramework.csproj b/FNA.NetFramework.csproj index 9c6e7e2a9..7f0749a78 100644 --- a/FNA.NetFramework.csproj +++ b/FNA.NetFramework.csproj @@ -137,6 +137,7 @@ + @@ -338,6 +339,7 @@ + diff --git a/FNA.NetStandard.csproj b/FNA.NetStandard.csproj index 64157a3df..2d9900e73 100644 --- a/FNA.NetStandard.csproj +++ b/FNA.NetStandard.csproj @@ -136,6 +136,7 @@ + @@ -337,6 +338,7 @@ + diff --git a/Makefile b/Makefile index 8944fe35c..55c07b6f8 100644 --- a/Makefile +++ b/Makefile @@ -121,6 +121,7 @@ SRC = \ src/FNAPlatform/FNAPlatform.cs \ src/FNAPlatform/FNAWindow.cs \ src/FNAPlatform/SDL2_FNAPlatform.cs \ + src/FNAPlatform/SDL3_FNAPlatform.cs \ src/FrameworkDispatcher.cs \ src/Game.cs \ src/GameComponent.cs \ @@ -321,6 +322,7 @@ SRC = \ src/Vector3.cs \ src/Vector4.cs \ lib/SDL2-CS/src/SDL2.cs \ + lib/SDL3-CS/SDL3/SDL3.cs \ lib/FAudio/csharp/FAudio.cs \ lib/Theorafile/csharp/Theorafile.cs diff --git a/src/FNAPlatform/FNAPlatform.cs b/src/FNAPlatform/FNAPlatform.cs index 100175c28..3a8cca370 100644 --- a/src/FNAPlatform/FNAPlatform.cs +++ b/src/FNAPlatform/FNAPlatform.cs @@ -36,9 +36,16 @@ static FNAPlatform() * -flibit */ - // Environment.GetEnvironmentVariable("FNA_PLATFORM_BACKEND"); + bool useSDL3 = Environment.GetEnvironmentVariable("FNA_PLATFORM_BACKEND") == "SDL3"; - SetEnv = SDL2_FNAPlatform.SetEnv; + if (useSDL3) + { + SetEnv = SDL3_FNAPlatform.SetEnv; + } + else + { + SetEnv = SDL2_FNAPlatform.SetEnv; + } // Built-in command line arguments LaunchParameters args = new LaunchParameters(); @@ -93,63 +100,129 @@ static FNAPlatform() ); } - Malloc = SDL2_FNAPlatform.Malloc; - Free = SDL2.SDL.SDL_free; - CreateWindow = SDL2_FNAPlatform.CreateWindow; - DisposeWindow = SDL2_FNAPlatform.DisposeWindow; - ApplyWindowChanges = SDL2_FNAPlatform.ApplyWindowChanges; - ScaleForWindow = SDL2_FNAPlatform.ScaleForWindow; - GetWindowBounds = SDL2_FNAPlatform.GetWindowBounds; - GetWindowResizable = SDL2_FNAPlatform.GetWindowResizable; - SetWindowResizable = SDL2_FNAPlatform.SetWindowResizable; - GetWindowBorderless = SDL2_FNAPlatform.GetWindowBorderless; - SetWindowBorderless = SDL2_FNAPlatform.SetWindowBorderless; - SetWindowTitle = SDL2_FNAPlatform.SetWindowTitle; - IsScreenKeyboardShown = SDL2_FNAPlatform.IsScreenKeyboardShown; - RegisterGame = SDL2_FNAPlatform.RegisterGame; - UnregisterGame = SDL2_FNAPlatform.UnregisterGame; - PollEvents = SDL2_FNAPlatform.PollEvents; - GetGraphicsAdapters = SDL2_FNAPlatform.GetGraphicsAdapters; - GetCurrentDisplayMode = SDL2_FNAPlatform.GetCurrentDisplayMode; - GetKeyFromScancode = SDL2_FNAPlatform.GetKeyFromScancode; - IsTextInputActive = SDL2_FNAPlatform.IsTextInputActive; - StartTextInput = SDL2.SDL.SDL_StartTextInput; - StopTextInput = SDL2.SDL.SDL_StopTextInput; - SetTextInputRectangle = SDL2_FNAPlatform.SetTextInputRectangle; - GetMouseState = SDL2_FNAPlatform.GetMouseState; - SetMousePosition = SDL2.SDL.SDL_WarpMouseInWindow; - OnIsMouseVisibleChanged = SDL2_FNAPlatform.OnIsMouseVisibleChanged; - GetRelativeMouseMode = SDL2_FNAPlatform.GetRelativeMouseMode; - SetRelativeMouseMode = SDL2_FNAPlatform.SetRelativeMouseMode; - GetGamePadCapabilities = SDL2_FNAPlatform.GetGamePadCapabilities; - GetGamePadState = SDL2_FNAPlatform.GetGamePadState; - SetGamePadVibration = SDL2_FNAPlatform.SetGamePadVibration; - SetGamePadTriggerVibration = SDL2_FNAPlatform.SetGamePadTriggerVibration; - GetGamePadGUID = SDL2_FNAPlatform.GetGamePadGUID; - SetGamePadLightBar = SDL2_FNAPlatform.SetGamePadLightBar; - GetGamePadGyro = SDL2_FNAPlatform.GetGamePadGyro; - GetGamePadAccelerometer = SDL2_FNAPlatform.GetGamePadAccelerometer; - GetStorageRoot = SDL2_FNAPlatform.GetStorageRoot; - GetDriveInfo = SDL2_FNAPlatform.GetDriveInfo; - ReadFileToPointer = SDL2_FNAPlatform.ReadToPointer; - FreeFilePointer = SDL2_FNAPlatform.FreeFilePointer; - ShowRuntimeError = SDL2_FNAPlatform.ShowRuntimeError; - GetMicrophones = SDL2_FNAPlatform.GetMicrophones; - GetMicrophoneSamples = SDL2_FNAPlatform.GetMicrophoneSamples; - GetMicrophoneQueuedBytes = SDL2_FNAPlatform.GetMicrophoneQueuedBytes; - StartMicrophone = SDL2_FNAPlatform.StartMicrophone; - StopMicrophone = SDL2_FNAPlatform.StopMicrophone; - GetTouchCapabilities = SDL2_FNAPlatform.GetTouchCapabilities; - UpdateTouchPanelState = SDL2_FNAPlatform.UpdateTouchPanelState; - GetNumTouchFingers = SDL2_FNAPlatform.GetNumTouchFingers; - SupportsOrientationChanges = SDL2_FNAPlatform.SupportsOrientationChanges; - NeedsPlatformMainLoop = SDL2_FNAPlatform.NeedsPlatformMainLoop; - RunPlatformMainLoop = SDL2_FNAPlatform.RunPlatformMainLoop; + if (useSDL3) + { + Malloc = SDL3_FNAPlatform.Malloc; + Free = SDL3.SDL.SDL_free; + CreateWindow = SDL3_FNAPlatform.CreateWindow; + DisposeWindow = SDL3_FNAPlatform.DisposeWindow; + ApplyWindowChanges = SDL3_FNAPlatform.ApplyWindowChanges; + ScaleForWindow = SDL3_FNAPlatform.ScaleForWindow; + GetWindowBounds = SDL3_FNAPlatform.GetWindowBounds; + GetWindowResizable = SDL3_FNAPlatform.GetWindowResizable; + SetWindowResizable = SDL3_FNAPlatform.SetWindowResizable; + GetWindowBorderless = SDL3_FNAPlatform.GetWindowBorderless; + SetWindowBorderless = SDL3_FNAPlatform.SetWindowBorderless; + SetWindowTitle = SDL3_FNAPlatform.SetWindowTitle; + IsScreenKeyboardShown = SDL3_FNAPlatform.IsScreenKeyboardShown; + RegisterGame = SDL3_FNAPlatform.RegisterGame; + UnregisterGame = SDL3_FNAPlatform.UnregisterGame; + PollEvents = SDL3_FNAPlatform.PollEvents; + GetGraphicsAdapters = SDL3_FNAPlatform.GetGraphicsAdapters; + GetCurrentDisplayMode = SDL3_FNAPlatform.GetCurrentDisplayMode; + GetKeyFromScancode = SDL3_FNAPlatform.GetKeyFromScancode; + IsTextInputActive = SDL3_FNAPlatform.IsTextInputActive; + StartTextInput = SDL3_FNAPlatform.StartTextInput; + StopTextInput = SDL3_FNAPlatform.StopTextInput; + SetTextInputRectangle = SDL3_FNAPlatform.SetTextInputRectangle; + GetMouseState = SDL3_FNAPlatform.GetMouseState; + SetMousePosition = SDL3_FNAPlatform.WarpMouseInWindow; + OnIsMouseVisibleChanged = SDL3_FNAPlatform.OnIsMouseVisibleChanged; + GetRelativeMouseMode = SDL3_FNAPlatform.GetRelativeMouseMode; + SetRelativeMouseMode = SDL3_FNAPlatform.SetRelativeMouseMode; + GetGamePadCapabilities = SDL3_FNAPlatform.GetGamePadCapabilities; + GetGamePadState = SDL3_FNAPlatform.GetGamePadState; + SetGamePadVibration = SDL3_FNAPlatform.SetGamePadVibration; + SetGamePadTriggerVibration = SDL3_FNAPlatform.SetGamePadTriggerVibration; + GetGamePadGUID = SDL3_FNAPlatform.GetGamePadGUID; + SetGamePadLightBar = SDL3_FNAPlatform.SetGamePadLightBar; + GetGamePadGyro = SDL3_FNAPlatform.GetGamePadGyro; + GetGamePadAccelerometer = SDL3_FNAPlatform.GetGamePadAccelerometer; + GetStorageRoot = SDL3_FNAPlatform.GetStorageRoot; + GetDriveInfo = SDL3_FNAPlatform.GetDriveInfo; + ReadFileToPointer = SDL3_FNAPlatform.ReadToPointer; + FreeFilePointer = SDL3_FNAPlatform.FreeFilePointer; + ShowRuntimeError = SDL3_FNAPlatform.ShowRuntimeError; + GetMicrophones = SDL3_FNAPlatform.GetMicrophones; + GetMicrophoneSamples = SDL3_FNAPlatform.GetMicrophoneSamples; + GetMicrophoneQueuedBytes = SDL3_FNAPlatform.GetMicrophoneQueuedBytes; + StartMicrophone = SDL3_FNAPlatform.StartMicrophone; + StopMicrophone = SDL3_FNAPlatform.StopMicrophone; + GetTouchCapabilities = SDL3_FNAPlatform.GetTouchCapabilities; + UpdateTouchPanelState = SDL3_FNAPlatform.UpdateTouchPanelState; + GetNumTouchFingers = SDL3_FNAPlatform.GetNumTouchFingers; + SupportsOrientationChanges = SDL3_FNAPlatform.SupportsOrientationChanges; + NeedsPlatformMainLoop = SDL3_FNAPlatform.NeedsPlatformMainLoop; + RunPlatformMainLoop = SDL3_FNAPlatform.RunPlatformMainLoop; + } + else + { + Malloc = SDL2_FNAPlatform.Malloc; + Free = SDL2.SDL.SDL_free; + CreateWindow = SDL2_FNAPlatform.CreateWindow; + DisposeWindow = SDL2_FNAPlatform.DisposeWindow; + ApplyWindowChanges = SDL2_FNAPlatform.ApplyWindowChanges; + ScaleForWindow = SDL2_FNAPlatform.ScaleForWindow; + GetWindowBounds = SDL2_FNAPlatform.GetWindowBounds; + GetWindowResizable = SDL2_FNAPlatform.GetWindowResizable; + SetWindowResizable = SDL2_FNAPlatform.SetWindowResizable; + GetWindowBorderless = SDL2_FNAPlatform.GetWindowBorderless; + SetWindowBorderless = SDL2_FNAPlatform.SetWindowBorderless; + SetWindowTitle = SDL2_FNAPlatform.SetWindowTitle; + IsScreenKeyboardShown = SDL2_FNAPlatform.IsScreenKeyboardShown; + RegisterGame = SDL2_FNAPlatform.RegisterGame; + UnregisterGame = SDL2_FNAPlatform.UnregisterGame; + PollEvents = SDL2_FNAPlatform.PollEvents; + GetGraphicsAdapters = SDL2_FNAPlatform.GetGraphicsAdapters; + GetCurrentDisplayMode = SDL2_FNAPlatform.GetCurrentDisplayMode; + GetKeyFromScancode = SDL2_FNAPlatform.GetKeyFromScancode; + IsTextInputActive = SDL2_FNAPlatform.IsTextInputActive; + StartTextInput = SDL2.SDL.SDL_StartTextInput; + StopTextInput = SDL2.SDL.SDL_StopTextInput; + SetTextInputRectangle = SDL2_FNAPlatform.SetTextInputRectangle; + GetMouseState = SDL2_FNAPlatform.GetMouseState; + SetMousePosition = SDL2.SDL.SDL_WarpMouseInWindow; + OnIsMouseVisibleChanged = SDL2_FNAPlatform.OnIsMouseVisibleChanged; + GetRelativeMouseMode = SDL2_FNAPlatform.GetRelativeMouseMode; + SetRelativeMouseMode = SDL2_FNAPlatform.SetRelativeMouseMode; + GetGamePadCapabilities = SDL2_FNAPlatform.GetGamePadCapabilities; + GetGamePadState = SDL2_FNAPlatform.GetGamePadState; + SetGamePadVibration = SDL2_FNAPlatform.SetGamePadVibration; + SetGamePadTriggerVibration = SDL2_FNAPlatform.SetGamePadTriggerVibration; + GetGamePadGUID = SDL2_FNAPlatform.GetGamePadGUID; + SetGamePadLightBar = SDL2_FNAPlatform.SetGamePadLightBar; + GetGamePadGyro = SDL2_FNAPlatform.GetGamePadGyro; + GetGamePadAccelerometer = SDL2_FNAPlatform.GetGamePadAccelerometer; + GetStorageRoot = SDL2_FNAPlatform.GetStorageRoot; + GetDriveInfo = SDL2_FNAPlatform.GetDriveInfo; + ReadFileToPointer = SDL2_FNAPlatform.ReadToPointer; + FreeFilePointer = SDL2_FNAPlatform.FreeFilePointer; + ShowRuntimeError = SDL2_FNAPlatform.ShowRuntimeError; + GetMicrophones = SDL2_FNAPlatform.GetMicrophones; + GetMicrophoneSamples = SDL2_FNAPlatform.GetMicrophoneSamples; + GetMicrophoneQueuedBytes = SDL2_FNAPlatform.GetMicrophoneQueuedBytes; + StartMicrophone = SDL2_FNAPlatform.StartMicrophone; + StopMicrophone = SDL2_FNAPlatform.StopMicrophone; + GetTouchCapabilities = SDL2_FNAPlatform.GetTouchCapabilities; + UpdateTouchPanelState = SDL2_FNAPlatform.UpdateTouchPanelState; + GetNumTouchFingers = SDL2_FNAPlatform.GetNumTouchFingers; + SupportsOrientationChanges = SDL2_FNAPlatform.SupportsOrientationChanges; + NeedsPlatformMainLoop = SDL2_FNAPlatform.NeedsPlatformMainLoop; + RunPlatformMainLoop = SDL2_FNAPlatform.RunPlatformMainLoop; + } FNALoggerEXT.Initialize(); - AppDomain.CurrentDomain.ProcessExit += SDL2_FNAPlatform.ProgramExit; - TitleLocation = SDL2_FNAPlatform.ProgramInit(args); + if (useSDL3) + { + AppDomain.CurrentDomain.ProcessExit += SDL3_FNAPlatform.ProgramExit; + TitleLocation = SDL3_FNAPlatform.ProgramInit(args); + } + else + { + AppDomain.CurrentDomain.ProcessExit += SDL2_FNAPlatform.ProgramExit; + TitleLocation = SDL2_FNAPlatform.ProgramInit(args); + } /* Do this AFTER ProgramInit so the platform library * has a chance to load first! diff --git a/src/FNAPlatform/README b/src/FNAPlatform/README index 80d88bf46..fffc10b3c 100644 --- a/src/FNAPlatform/README +++ b/src/FNAPlatform/README @@ -11,8 +11,8 @@ platforms as well as multiple backends for each platform all simultaneously. That said, if you are adding a new platform, it is extremely likely that you will NOT be touching anything in FNA itself! The expectation is that 100% of -your work will end up in SDL2 and FNA3D. You _might_ add some OSVersion checks -to SDL2_FNAPlatform.cs, but that should be the only change in the managed code. +your work will end up in SDL3 and FNA3D. You _might_ add some OSVersion checks +to SDL3_FNAPlatform.cs, but that should be the only change in the managed code. If for some reason you REALLY need a new FNAPlatform, the new platforms will add code exclusively to this folder. Some interfaces may need to change for new diff --git a/src/FNAPlatform/SDL3_FNAPlatform.cs b/src/FNAPlatform/SDL3_FNAPlatform.cs index d58a9de9d..d4d39e7df 100644 --- a/src/FNAPlatform/SDL3_FNAPlatform.cs +++ b/src/FNAPlatform/SDL3_FNAPlatform.cs @@ -1341,6 +1341,12 @@ out ButtonState x2 x2 = (ButtonState) ((uint) (flags & SDL.SDL_MouseButtonFlags.SDL_BUTTON_X2MASK) >> 4); } + public static void WarpMouseInWindow(IntPtr window, int x, int y) + { + // Implicit conversion to float + SDL.SDL_WarpMouseInWindow(window, x, y); + } + public static void OnIsMouseVisibleChanged(bool visible) { if (visible) @@ -2245,6 +2251,18 @@ public static bool IsTextInputActive() return SDL.SDL_TextInputActive(IntPtr.Zero); } + public static void StartTextInput() + { + // FIXME SDL3: Need a window... + SDL.SDL_StartTextInput(IntPtr.Zero); + } + + public static void StopTextInput() + { + // FIXME SDL3: Need a window... + SDL.SDL_StopTextInput(IntPtr.Zero); + } + #endregion #region SDL2<->XNA Key Conversion Methods