Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

Commit

Permalink
Allow game hosts to provide custom base directories and rendering dim…
Browse files Browse the repository at this point in the history
…ensions (#184)
  • Loading branch information
hach-que committed May 31, 2017
1 parent 203c33c commit e75e29d
Show file tree
Hide file tree
Showing 25 changed files with 506 additions and 67 deletions.
6 changes: 6 additions & 0 deletions Build/Projects/Protogame.definition
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@
<Compile Include="Core\Default3DRenderUtilities.cs" />
<Compile Include="Core\DefaultAudioHandle.cs" />
<Compile Include="Core\DefaultAudioUtilities.cs" />
<Compile Include="Core\DefaultBackBufferDimensions.cs" />
<Compile Include="Core\DefaultBaseDirectory.cs" />
<Compile Include="Core\DefaultBoundingBoxUtilities.cs" />
<Compile Include="Core\DefaultFinalTransform.cs" />
<Compile Include="Core\DefaultGameContext.cs" />
Expand All @@ -305,6 +307,8 @@
<Compile Include="Core\I3DRenderUtilities.cs" />
<Compile Include="Core\IAudioHandle.cs" />
<Compile Include="Core\IAudioUtilities.cs" />
<Compile Include="Core\IBackBufferDimensions.cs" />
<Compile Include="Core\IBaseDirectory.cs" />
<Compile Include="Core\IBoundingBox.cs" />
<Compile Include="Core\IBoundingBoxUtilities.cs" />
<Compile Include="Core\ICommand.cs" />
Expand All @@ -319,6 +323,7 @@
<Compile Include="Core\IHasSize.cs" />
<Compile Include="Core\IHasTransform.cs" />
<Compile Include="Core\IHasVelocity.cs" />
<Compile Include="Core\IHostGame.cs" />
<Compile Include="Core\IKeyboardStringReader.cs" />
<Compile Include="Core\ILoadingScreen.cs" />
<Compile Include="Core\IPrerenderableEntity.cs" />
Expand Down Expand Up @@ -821,6 +826,7 @@
<Compile Include="UserInterface\Control\Container\HorizontalContainer.cs" />
<Compile Include="UserInterface\Control\Container\IContainer.cs" />
<Compile Include="UserInterface\Control\Container\IContainerConstant.cs" />
<Compile Include="UserInterface\Control\Container\RawTextureContainer.cs" />
<Compile Include="UserInterface\Control\Container\RelativeContainer.cs" />
<Compile Include="UserInterface\Control\Container\ScrollableContainer.cs" />
<Compile Include="UserInterface\Control\Container\SingleContainer.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace Protogame
{
public class CurrentDirLocalFilesystemAssetFsLayer : LocalFilesystemAssetFsLayer
{
public CurrentDirLocalFilesystemAssetFsLayer()
: base(GetPath())
public CurrentDirLocalFilesystemAssetFsLayer(IBaseDirectory baseDirectory)
: base(GetPath(baseDirectory))
{ }

private static string GetPath()
private static string GetPath(IBaseDirectory baseDirectory)
{
var path = Path.Combine(Environment.CurrentDirectory, "Content");
var path = Path.Combine(baseDirectory.FullPath, "Content");
Directory.CreateDirectory(path);
return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace Protogame
{
public class CurrentDirPlatformLocalFilesystemAssetFsLayer : LocalFilesystemAssetFsLayer
{
public CurrentDirPlatformLocalFilesystemAssetFsLayer()
: base(GetPath())
public CurrentDirPlatformLocalFilesystemAssetFsLayer(IBaseDirectory baseDirectory)
: base(GetPath(baseDirectory))
{ }

private static string GetPath()
private static string GetPath(IBaseDirectory baseDirectory)
{
var path = Path.Combine(Environment.CurrentDirectory, "Content", TargetPlatformUtility.GetExecutingPlatform().ToString());
var path = Path.Combine(baseDirectory.FullPath, "Content", TargetPlatformUtility.GetExecutingPlatform().ToString());
Directory.CreateDirectory(path);
return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public class SourceDirLocalFilesystemAssetFsLayer : IAssetFsLayer
{
private readonly LocalFilesystemAssetFsLayer _localLayer;

public SourceDirLocalFilesystemAssetFsLayer()
public SourceDirLocalFilesystemAssetFsLayer(IBaseDirectory baseDirectory)
{
_localLayer = null;

var sourcePath = Path.Combine(Environment.CurrentDirectory, "Content", ".source");
var sourcePath = Path.Combine(baseDirectory.FullPath, "Content", ".source");
if (!File.Exists(sourcePath))
{
return;
Expand All @@ -22,7 +22,7 @@ public SourceDirLocalFilesystemAssetFsLayer()
{
sourcePath = reader.ReadLine();

if (string.Equals(sourcePath, Path.Combine(Environment.CurrentDirectory, "Content"), StringComparison.OrdinalIgnoreCase))
if (string.Equals(sourcePath, Path.Combine(baseDirectory.FullPath, "Content"), StringComparison.OrdinalIgnoreCase))
{
return;
}
Expand Down
11 changes: 9 additions & 2 deletions Protogame/Camera/FirstPersonCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ namespace Protogame
/// <internal>True</internal>
public class FirstPersonCamera : IFirstPersonCamera
{
private readonly IBackBufferDimensions _backBufferDimensions;

public FirstPersonCamera(IBackBufferDimensions backBufferDimensions)
{
_backBufferDimensions = backBufferDimensions;
}

public void Apply(
IRenderContext renderContext,
Vector3 position,
Expand All @@ -21,8 +28,8 @@ public class FirstPersonCamera : IFirstPersonCamera
float nearPlaneDistance,
float farPlaneDistance)
{
var viewport = renderContext.GraphicsDevice.PresentationParameters;
var aspectRatio = viewport.BackBufferWidth / (float)viewport.BackBufferHeight;
var size = _backBufferDimensions.GetSize(renderContext.GraphicsDevice);
var aspectRatio = size.X / (float)size.Y;

up = up ?? Vector3.Up;
renderContext.CameraPosition = position;
Expand Down
12 changes: 10 additions & 2 deletions Protogame/Camera/PanningCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ namespace Protogame
/// <internal>True</internal>
public class PanningCamera : IPanningCamera
{
private readonly IBackBufferDimensions _backBufferDimensions;

public PanningCamera(IBackBufferDimensions backBufferDimensions)
{
_backBufferDimensions = backBufferDimensions;
}

public void Apply(IRenderContext renderContext, Vector2 centerOfScreenPosition)
{
if (renderContext.IsCurrentRenderPass<I2DBatchedRenderPass>())
{
var size = _backBufferDimensions.GetSize(renderContext.GraphicsDevice);
var batchedRenderPass = renderContext.GetCurrentRenderPass<I2DBatchedRenderPass>();
batchedRenderPass.RestartWithTransformMatrix(
renderContext,
Matrix.CreateTranslation(
-new Vector3(centerOfScreenPosition, 0)
+ new Vector3(
(batchedRenderPass.Viewport?.Width ?? renderContext.GraphicsDevice.PresentationParameters.BackBufferWidth) / 2,
(batchedRenderPass.Viewport?.Height ?? renderContext.GraphicsDevice.PresentationParameters.BackBufferHeight) / 2,
(batchedRenderPass.Viewport?.Width ?? size.X) / 2,
(batchedRenderPass.Viewport?.Height ?? size.Y) / 2,
0)));
}
}
Expand Down
17 changes: 13 additions & 4 deletions Protogame/Core/CoreGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public abstract class CoreGame<TInitialWorld, TWorldManager> : ICoreGame
/// <summary>
/// The MonoGame game instance that is hosting this game.
/// </summary>
private HostGame _hostGame;
private IHostGame _hostGame;

/// <summary>
/// Whether resize events can be handled.
Expand Down Expand Up @@ -312,12 +312,16 @@ protected CoreGame(IKernel kernel)
/// </summary>
public int SkipFrames { get; set; }

public IHostGame HostGame => _hostGame;

public bool HasLoadedContent => _isReadyForMainRenderTakeover;

/// <summary>
/// Called by <see cref="HostGame"/> to assign itself to this game
/// instance, allowing us to access MonoGame game members.
/// </summary>
/// <param name="hostGame">The MonoGame game instance.</param>
public void AssignHost(HostGame hostGame)
public void AssignHost(IHostGame hostGame)
{
_hostGame = hostGame;
_hostGame.Exiting += _hostGame_Exiting;
Expand All @@ -334,6 +338,11 @@ public void AssignHost(HostGame hostGame)
_loadingScreen = _kernel.Get<ILoadingScreen>();
}

public void EnableImmediateStartFromHost()
{
_hasDoneEarlyRender = true;
}

private void _hostGame_Exiting(object sender, EventArgs e)
{
Exiting?.Invoke(sender, e);
Expand Down Expand Up @@ -534,7 +543,7 @@ public void Dispose(bool disposing)
}
}

public void Update(GameTime gameTime)
public virtual void Update(GameTime gameTime)
{
if (!_isReadyForMainRenderTakeover)
{
Expand Down Expand Up @@ -601,7 +610,7 @@ public void Update(GameTime gameTime)
}
}

public void Draw(GameTime gameTime)
public virtual void Draw(GameTime gameTime)
{
if (!_isReadyForMainRenderTakeover)
{
Expand Down
15 changes: 15 additions & 0 deletions Protogame/Core/DefaultBackBufferDimensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Protogame
{
public class DefaultBackBufferDimensions : IBackBufferDimensions
{
public Point GetSize(GraphicsDevice graphicsDevice)
{
return new Point(
graphicsDevice.PresentationParameters.BackBufferWidth,
graphicsDevice.PresentationParameters.BackBufferHeight);
}
}
}
14 changes: 14 additions & 0 deletions Protogame/Core/DefaultBaseDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Protogame
{
public class DefaultBaseDirectory : IBaseDirectory
{
public DefaultBaseDirectory()
{
FullPath = Environment.CurrentDirectory;
}

public string FullPath { get; }
}
}
2 changes: 1 addition & 1 deletion Protogame/Core/HostGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Protogame
{
public class HostGame : Game
public class HostGame : Game, IHostGame
{
private GraphicsDeviceManager _graphicsDeviceManager;
private SpriteBatch _splashSpriteBatch;
Expand Down
10 changes: 10 additions & 0 deletions Protogame/Core/IBackBufferDimensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Protogame
{
public interface IBackBufferDimensions
{
Point GetSize(GraphicsDevice graphicsDevice);
}
}
16 changes: 16 additions & 0 deletions Protogame/Core/IBaseDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Protogame
{
/// <summary>
/// Provides the base directory of the game. You should always use this interface instead of
/// <see cref="System.Environment.CurrentDirectory"/>, as this value is correct when the game is
/// running inside an editor.
/// </summary>
public interface IBaseDirectory
{
/// <summary>
/// The full directory path to the game; this directory is the base where the content directory
/// will then be searched from.
/// </summary>
string FullPath { get; }
}
}
8 changes: 7 additions & 1 deletion Protogame/Core/ICoreGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface ICoreGame
{
IGameWindow Window { get; }

IHostGame HostGame { get; }

GraphicsDevice GraphicsDevice { get; }

GraphicsDeviceManager GraphicsDeviceManager { get; }
Expand All @@ -20,7 +22,11 @@ public interface ICoreGame

bool IsMouseVisible { get; set; }

void AssignHost(HostGame hostGame);
bool HasLoadedContent { get; }

void AssignHost(IHostGame hostGame);

void EnableImmediateStartFromHost();

void LoadContent();

Expand Down
32 changes: 32 additions & 0 deletions Protogame/Core/IHostGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace Protogame
{
public interface IHostGame
{
bool IsMouseVisible { get; set; }

GraphicsDevice GraphicsDevice { get; }

GraphicsDeviceManager GraphicsDeviceManager { get; }

IGameWindow ProtogameWindow { get; }

event EventHandler<EventArgs> Exiting;

GameWindow Window { get; }

GameServiceContainer Services { get; }

ContentManager Content { get; set; }

void Exit();

SpriteBatch SplashScreenSpriteBatch { get; }

Texture2D SplashScreenTexture { get; }
}
}
2 changes: 2 additions & 0 deletions Protogame/Core/ProtogameBaseModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public virtual void Load(IKernel kernel)
kernel.Bind<ICoroutineScheduler>().To<DefaultCoroutineScheduler>().InSingletonScope();
kernel.Bind<IEngineHook>().To<CoroutineEngineHook>();
kernel.Bind<ICoroutine>().To<DefaultCoroutine>();
kernel.Bind<IBaseDirectory>().To<DefaultBaseDirectory>().InSingletonScope();
kernel.Bind<IBackBufferDimensions>().To<DefaultBackBufferDimensions>().InSingletonScope();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Protogame/Coroutine/DefaultCoroutine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public Task Run(Func<Task> coroutine)
{
var syncContext = (SynchronizationContext) _coroutineScheduler;
SynchronizationContext.SetSynchronizationContext(syncContext);
// The await inside WrapCoroutine will cause it to be placed on the coroutine scheduler.
var task = WrapCoroutine(coroutine);
syncContext.Post(async _=> { await task; }, null);
return task;
}
finally
Expand All @@ -49,8 +49,8 @@ public Task<T> Run<T>(Func<Task<T>> coroutine)
{
var syncContext = (SynchronizationContext)_coroutineScheduler;
SynchronizationContext.SetSynchronizationContext(syncContext);
// The await inside WrapCoroutine will cause it to be placed on the coroutine scheduler.
var task = WrapCoroutine(coroutine);
syncContext.Post(async _ => { await task; }, null);
return task;
}
finally
Expand Down
10 changes: 7 additions & 3 deletions Protogame/Graphics/Default3DDeferredRenderPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Default3DDeferredRenderPass : I3DDeferredRenderPass
private readonly IRenderTargetBackBufferUtilities _renderTargetBackBufferUtilities;
private readonly IGraphicsBlit _graphicsBlit;
private readonly IRenderBatcher _renderBatcher;
private readonly IBackBufferDimensions _backBufferDimensions;
private readonly IAssetReference<EffectAsset> _gbufferClearEffect;
private readonly IAssetReference<EffectAsset> _gbufferCombineEffect;

Expand Down Expand Up @@ -45,12 +46,14 @@ public class Default3DDeferredRenderPass : I3DDeferredRenderPass
IRenderTargetBackBufferUtilities renderTargetBackBufferUtilities,
IGraphicsBlit graphicsBlit,
IAssetManager assetManager,
IRenderBatcher renderBatcher)
IRenderBatcher renderBatcher,
IBackBufferDimensions backBufferDimensions)
{
_hierarchy = hierarchy;
_renderTargetBackBufferUtilities = renderTargetBackBufferUtilities;
_graphicsBlit = graphicsBlit;
_renderBatcher = renderBatcher;
_backBufferDimensions = backBufferDimensions;
_gbufferClearEffect =
assetManager.Get<EffectAsset>("effect.GBufferClear");
_gbufferCombineEffect =
Expand Down Expand Up @@ -229,6 +232,7 @@ public class Default3DDeferredRenderPass : I3DDeferredRenderPass
renderContext.GraphicsDevice.Clear(Color.Transparent);
renderContext.PopRenderTarget();

var size = _backBufferDimensions.GetSize(renderContext.GraphicsDevice);
var lightContext = new DefaultLightContext(
_colorRenderTarget,
_normalRenderTarget,
Expand All @@ -241,8 +245,8 @@ public class Default3DDeferredRenderPass : I3DDeferredRenderPass
_rasterizerStateCullClockwiseFace,
_rasterizerStateCullCounterClockwiseFace,
new Vector2(
0.5f/renderContext.GraphicsDevice.PresentationParameters.BackBufferWidth,
0.5f/renderContext.GraphicsDevice.PresentationParameters.BackBufferHeight));
0.5f/size.X,
0.5f/size.Y));

var lights = new List<ILight>();

Expand Down

0 comments on commit e75e29d

Please sign in to comment.