Skip to content

Commit

Permalink
fix: Add possibility to add ScriptComponent before running game (#2230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jklawreszuk authored Apr 21, 2024
1 parent 82888c2 commit 3a9ed13
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 63 deletions.
19 changes: 9 additions & 10 deletions sources/engine/Stride.Engine/Engine/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ 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);
Expand Down Expand Up @@ -336,13 +345,6 @@ 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();

Expand All @@ -363,9 +365,6 @@ 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);

Expand Down
93 changes: 40 additions & 53 deletions sources/engine/Stride.Games/GameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ protected GameBase()

// Database file provider
Services.AddService<IDatabaseFileProviderService>(new DatabaseFileProviderService(null));
// Content manager
Content = new ContentManager(Services);
Services.AddService<IContentManager>(Content);
Services.AddService(Content);

LaunchParameters = new LaunchParameters();
GameSystems = new GameSystemCollection(Services);
Expand Down Expand Up @@ -344,51 +348,49 @@ 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<IGraphicsDeviceService>();
if (graphicsDeviceService == null)
{
throw new InvalidOperationException("No GraphicsDeviceService found");
}

// Checks the graphics device
if (graphicsDeviceService.GraphicsDevice == null)
{
throw new InvalidOperationException("No GraphicsDevice found");
}
// Gets the graphics device service
graphicsDeviceService = Services.GetService<IGraphicsDeviceService>();
if (graphicsDeviceService == null)
{
throw new InvalidOperationException("No GraphicsDeviceService found");
}

// Setup the graphics device if it was not already setup.
SetupGraphicsDeviceEvents();
// Checks the graphics device
if (graphicsDeviceService.GraphicsDevice == null)
{
throw new InvalidOperationException("No GraphicsDevice found");
}

// Bind Graphics Context enabling initialize to use GL API eg. SetData to texture ...etc
BeginDraw();
// Setup the graphics device if it was not already setup.
SetupGraphicsDeviceEvents();

LoadContentInternal();
// Bind Graphics Context enabling initialize to use GL API eg. SetData to texture ...etc
BeginDraw();

IsRunning = true;
LoadContentInternal();

BeginRun();
IsRunning = true;

autoTickTimer.Reset();
UpdateTime.Reset(UpdateTime.Total);
BeginRun();

// Run the first time an update
using (Profiler.Begin(GameProfilingKeys.GameUpdate))
{
Update(UpdateTime);
}
autoTickTimer.Reset();
UpdateTime.Reset(UpdateTime.Total);

// Unbind Graphics Context without presenting
EndDraw(false);
// Run the first time an update
using (Profiler.Begin(GameProfilingKeys.GameUpdate))
{
Update(UpdateTime);
}

// Unbind Graphics Context without presenting
EndDraw(false);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -471,13 +473,7 @@ public void Run(GameContext gameContext = null)
/// <summary>
/// Creates or updates <see cref="Context"/> before window and device are created.
/// </summary>
protected virtual void PrepareContext()
{
// Content manager
Content = new ContentManager(Services);
Services.AddService<IContentManager>(Content);
Services.AddService(Content);
}
protected virtual void PrepareContext() { }

/// <summary>
/// Prevents calls to Draw until the next Update.
Expand Down Expand Up @@ -717,27 +713,18 @@ protected override void Destroy()
for (int i = 0; i < array.Length; i++)
{
var disposable = array[i] as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
disposable?.Dispose();
}

// Reset graphics context
GraphicsContext = null;

var disposableGraphicsManager = graphicsDeviceManager as IDisposable;
if (disposableGraphicsManager != null)
{
disposableGraphicsManager.Dispose();
}
disposableGraphicsManager?.Dispose();

DisposeGraphicsDeviceEvents();

if (gamePlatform != null)
{
gamePlatform.Release();
}
gamePlatform?.Release();
}
}

Expand Down

0 comments on commit 3a9ed13

Please sign in to comment.