diff --git a/sources/engine/Stride.Engine/Engine/Game.cs b/sources/engine/Stride.Engine/Engine/Game.cs index e02101440a..b485aa43aa 100644 --- a/sources/engine/Stride.Engine/Engine/Game.cs +++ b/sources/engine/Stride.Engine/Engine/Game.cs @@ -225,15 +225,6 @@ public Game() VRDeviceSystem = new VRDeviceSystem(Services); Services.AddService(VRDeviceSystem); - // Add the input manager - // Add it first so that it can obtained by the UI system - var inputSystem = new InputSystem(Services); - Input = inputSystem.Manager; - Services.AddService(Input); - GameSystems.Add(inputSystem); - - EffectSystem = new EffectSystem(Services); - Services.AddService(EffectSystem); // Creates the graphics device manager GraphicsDeviceManager = new GraphicsDeviceManager(this); @@ -345,6 +336,13 @@ protected override void Initialize() // (Unless overriden by gameSystem.UpdateOrder) // --------------------------------------------------------- + // Add the input manager + // Add it first so that it can obtained by the UI system + var inputSystem = new InputSystem(Services); + Input = inputSystem.Manager; + Services.AddService(Input); + GameSystems.Add(inputSystem); + // Initialize the systems base.Initialize(); @@ -365,6 +363,9 @@ protected override void Initialize() GameSystems.Add(DebugTextSystem); GameSystems.Add(ProfilingSystem); + EffectSystem = new EffectSystem(Services); + Services.AddService(EffectSystem); + // If requested in game settings, compile effects remotely and/or notify new shader requests EffectSystem.Compiler = EffectCompilerFactory.CreateEffectCompiler(Content.FileProvider, EffectSystem, Settings?.PackageName, Settings?.EffectCompilation ?? EffectCompilationMode.Local, Settings?.RecordUsedEffects ?? false); diff --git a/sources/engine/Stride.Games/GameBase.cs b/sources/engine/Stride.Games/GameBase.cs index 4a7d943124..c865867b3a 100644 --- a/sources/engine/Stride.Games/GameBase.cs +++ b/sources/engine/Stride.Games/GameBase.cs @@ -90,10 +90,6 @@ protected GameBase() // Database file provider Services.AddService(new DatabaseFileProviderService(null)); - // Content manager - Content = new ContentManager(Services); - Services.AddService(Content); - Services.AddService(Content); LaunchParameters = new LaunchParameters(); GameSystems = new GameSystemCollection(Services); @@ -348,49 +344,51 @@ internal void InitializeBeforeRun() { try { - using var profile = Profiler.Begin(GameProfilingKeys.GameInitialize); - // Initialize this instance and all game systems before trying to create the device. - Initialize(); + using (var profile = Profiler.Begin(GameProfilingKeys.GameInitialize)) + { + // Initialize this instance and all game systems before trying to create the device. + Initialize(); - // Make sure that the device is already created - graphicsDeviceManager.CreateDevice(); + // Make sure that the device is already created + graphicsDeviceManager.CreateDevice(); - // Gets the graphics device service - graphicsDeviceService = Services.GetService(); - if (graphicsDeviceService == null) - { - throw new InvalidOperationException("No GraphicsDeviceService found"); - } + // Gets the graphics device service + graphicsDeviceService = Services.GetService(); + if (graphicsDeviceService == null) + { + throw new InvalidOperationException("No GraphicsDeviceService found"); + } - // Checks the graphics device - if (graphicsDeviceService.GraphicsDevice == null) - { - throw new InvalidOperationException("No GraphicsDevice found"); - } + // Checks the graphics device + if (graphicsDeviceService.GraphicsDevice == null) + { + throw new InvalidOperationException("No GraphicsDevice found"); + } - // Setup the graphics device if it was not already setup. - SetupGraphicsDeviceEvents(); + // Setup the graphics device if it was not already setup. + SetupGraphicsDeviceEvents(); - // Bind Graphics Context enabling initialize to use GL API eg. SetData to texture ...etc - BeginDraw(); + // Bind Graphics Context enabling initialize to use GL API eg. SetData to texture ...etc + BeginDraw(); - LoadContentInternal(); + LoadContentInternal(); - IsRunning = true; + IsRunning = true; - BeginRun(); + BeginRun(); - autoTickTimer.Reset(); - UpdateTime.Reset(UpdateTime.Total); + autoTickTimer.Reset(); + UpdateTime.Reset(UpdateTime.Total); - // Run the first time an update - using (Profiler.Begin(GameProfilingKeys.GameUpdate)) - { - Update(UpdateTime); - } + // Run the first time an update + using (Profiler.Begin(GameProfilingKeys.GameUpdate)) + { + Update(UpdateTime); + } - // Unbind Graphics Context without presenting - EndDraw(false); + // Unbind Graphics Context without presenting + EndDraw(false); + } } catch (Exception ex) { @@ -473,7 +471,13 @@ public void Run(GameContext gameContext = null) /// /// Creates or updates before window and device are created. /// - protected virtual void PrepareContext() { } + protected virtual void PrepareContext() + { + // Content manager + Content = new ContentManager(Services); + Services.AddService(Content); + Services.AddService(Content); + } /// /// Prevents calls to Draw until the next Update. @@ -713,18 +717,27 @@ protected override void Destroy() for (int i = 0; i < array.Length; i++) { var disposable = array[i] as IDisposable; - disposable?.Dispose(); + if (disposable != null) + { + disposable.Dispose(); + } } // Reset graphics context GraphicsContext = null; var disposableGraphicsManager = graphicsDeviceManager as IDisposable; - disposableGraphicsManager?.Dispose(); + if (disposableGraphicsManager != null) + { + disposableGraphicsManager.Dispose(); + } DisposeGraphicsDeviceEvents(); - gamePlatform?.Release(); + if (gamePlatform != null) + { + gamePlatform.Release(); + } } }