diff --git a/src/FNAPlatform.cs b/src/FNAPlatform.cs index 9e2dbe7..179dfb3 100644 --- a/src/FNAPlatform.cs +++ b/src/FNAPlatform.cs @@ -36,8 +36,8 @@ namespace Microsoft.Xna.Framework // Environment.GetEnvironmentVariable("FNA_PLATFORM_BACKEND"); - Init = SDL2_FNAPlatform.Init; - Dispose = SDL2_FNAPlatform.Dispose; + CreateWindow = SDL2_FNAPlatform.CreateWindow; + DisposeWindow = SDL2_FNAPlatform.DisposeWindow; BeforeInitialize = SDL2_FNAPlatform.BeforeInitialize; RunLoop = SDL2_FNAPlatform.RunLoop; CreateGLDevice = SDL2_FNAPlatform.CreateGLDevice; @@ -59,17 +59,20 @@ namespace Microsoft.Xna.Framework ShowRuntimeError = SDL2_FNAPlatform.ShowRuntimeError; TextureDataFromStream = SDL2_FNAPlatform.TextureDataFromStream; SavePNG = SDL2_FNAPlatform.SavePNG; + + AppDomain.CurrentDomain.ProcessExit += SDL2_FNAPlatform.ProgramExit; + SDL2_FNAPlatform.ProgramInit(); } #endregion #region Public Static Methods - public delegate void InitFunc(Game game); - public static InitFunc Init; + public delegate GameWindow CreateWindowFunc(); + public static CreateWindowFunc CreateWindow; - public delegate void DisposeFunc(Game game); - public static DisposeFunc Dispose; + public delegate void DisposeWindowFunc(GameWindow window); + public static DisposeWindowFunc DisposeWindow; public delegate void BeforeInitializeFunc(); public static BeforeInitializeFunc BeforeInitialize; diff --git a/src/Game.cs b/src/Game.cs index 5f1fca3..40fa04f 100644 --- a/src/Game.cs +++ b/src/Game.cs @@ -287,7 +287,7 @@ namespace Microsoft.Xna.Framework _components = new GameComponentCollection(); _content = new ContentManager(_services); - FNAPlatform.Init(this); + Window = FNAPlatform.CreateWindow(); AudioDevice.Initialize(); @@ -349,7 +349,11 @@ namespace Microsoft.Xna.Framework AudioDevice.Dispose(); - FNAPlatform.Dispose(this); + if (Window != null) + { + FNAPlatform.DisposeWindow(Window); + Window = null; + } Mouse.WindowHandle = IntPtr.Zero; ContentTypeReaderManager.ClearTypeCreators(); diff --git a/src/SDL2/SDL2_FNAPlatform.cs b/src/SDL2/SDL2_FNAPlatform.cs index 24e30d3..b010a78 100644 --- a/src/SDL2/SDL2_FNAPlatform.cs +++ b/src/SDL2/SDL2_FNAPlatform.cs @@ -33,7 +33,7 @@ namespace Microsoft.Xna.Framework #region Public Static Methods - public static void Init(Game game) + public static void ProgramInit() { /* SDL2 might complain if an OS that uses SDL_main has not actually * used SDL_main by the time you initialize SDL2. @@ -72,15 +72,26 @@ namespace Microsoft.Xna.Framework mappingsDB ); } + } - // Set and initialize the SDL2 window + public static void ProgramExit(object sender, EventArgs e) + { + // This _should_ be the last SDL call we make... + SDL.SDL_Quit(); + } + + public static GameWindow CreateWindow() + { + // GLContext environment variables bool forceES2 = Environment.GetEnvironmentVariable( "FNA_OPENGL_FORCE_ES2" ) == "1"; bool forceCoreProfile = Environment.GetEnvironmentVariable( "FNA_OPENGL_FORCE_CORE_PROFILE" ) == "1"; - game.Window = new SDL2_GameWindow( + + // Set and initialize the SDL2 window + GameWindow result = new SDL2_GameWindow( forceES2 || OSVersion.Equals("Emscripten") || OSVersion.Equals("Android") || @@ -93,30 +104,24 @@ namespace Microsoft.Xna.Framework // We hide the mouse cursor by default. SDL.SDL_ShowCursor(0); + + return result; } - public static void Dispose(Game game) + public static void DisposeWindow(GameWindow window) { - if (game.Window != null) - { - /* Some window managers might try to minimize the window as we're - * destroying it. This looks pretty stupid and could cause problems, - * so set this hint right before we destroy everything. - * -flibit - */ - SDL.SDL_SetHintWithPriority( - SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, - "0", - SDL.SDL_HintPriority.SDL_HINT_OVERRIDE - ); - - SDL.SDL_DestroyWindow(game.Window.Handle); - - game.Window = null; - } + /* Some window managers might try to minimize the window as we're + * destroying it. This looks pretty stupid and could cause problems, + * so set this hint right before we destroy everything. + * -flibit + */ + SDL.SDL_SetHintWithPriority( + SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, + "0", + SDL.SDL_HintPriority.SDL_HINT_OVERRIDE + ); - // This _should_ be the last SDL call we make... - SDL.SDL_Quit(); + SDL.SDL_DestroyWindow(window.Handle); } public static void RunLoop(Game game)