Skip to content

Commit

Permalink
Small game refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Aminator committed Jul 18, 2019
1 parent 0bcea20 commit 24f929e
Show file tree
Hide file tree
Showing 51 changed files with 607 additions and 565 deletions.
4 changes: 2 additions & 2 deletions DirectX12CoreWindowApp/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void Run()
{
if (gameContext is null) throw new InvalidOperationException();

game = new MyGame(gameContext);
game.Run();
game = new MyGame();
game.Run(gameContext);
}

public void Uninitialize()
Expand Down
16 changes: 10 additions & 6 deletions DirectX12Game/MyGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@
using System.Threading.Tasks;
using DirectX12GameEngine.Assets;
using DirectX12GameEngine.Engine;
using DirectX12GameEngine.Games;
using DirectX12GameEngine.Graphics;
using Windows.Storage;

namespace DirectX12Game
{
public sealed class MyGame : Game
{
public MyGame(GameContext gameContext) : base(gameContext)
public MyGame()
{
if (GraphicsDevice.Presenter != null)
SceneSystem.InitialScenePath = @"Assets\Scenes\Scene1";
}

protected override void Initialize()
{
base.Initialize();

if (GraphicsDevice?.Presenter != null)
{
GraphicsDevice.Presenter.PresentationParameters.SyncInterval = 1;
}

SceneSystem.InitialScenePath = @"Assets\Scenes\Scene1";
}

protected override void BeginDraw()
Expand Down Expand Up @@ -51,7 +55,7 @@ protected override async Task LoadContentAsync()
ShaderContent.RootFolder = await temporaryFolder.CreateFolderAsync("ShaderCache", CreationCollisionOption.OpenIfExists);

// TODO: DirectX12GameEngine.Assets.dll does not get copied to the output directory if it is never used.
MaterialAsset materialAsset = new MaterialAsset(Content, ShaderContent, GraphicsDevice);
MaterialAsset materialAsset = new MaterialAsset();
materialAsset.ToString();

await base.LoadContentAsync();
Expand Down
20 changes: 9 additions & 11 deletions DirectX12GameEngine.Assets/MaterialAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DirectX12GameEngine.Core.Assets;
using DirectX12GameEngine.Engine;
using DirectX12GameEngine.Graphics;
using DirectX12GameEngine.Rendering;
using DirectX12GameEngine.Rendering.Materials;
using Microsoft.Extensions.DependencyInjection;

namespace DirectX12GameEngine.Assets
{
[AssetContentType(typeof(Material))]
public class MaterialAsset : AssetWithSource<Material>
{
private readonly ContentManager contentManager;
private readonly ShaderContentManager shaderContentManager;
private readonly GraphicsDevice device;
public MaterialAttributes Attributes { get; set; } = new MaterialAttributes();

public MaterialAsset(ContentManager contentManager, ShaderContentManager shaderContentManager, GraphicsDevice device)
public async override Task CreateAssetAsync(Material material, IServiceProvider services)
{
this.contentManager = contentManager;
this.shaderContentManager = shaderContentManager;
this.device = device;
}
ContentManager contentManager = services.GetRequiredService<ContentManager>();
ShaderContentManager shaderContentManager = services.GetRequiredService<ShaderContentManager>();
IGraphicsDeviceManager graphicsDeviceManager = services.GetRequiredService<IGraphicsDeviceManager>();
GraphicsDevice? device = graphicsDeviceManager.GraphicsDevice;

public MaterialAttributes Attributes { get; set; } = new MaterialAttributes();
if (device is null) throw new InvalidOperationException();

public async override Task CreateAssetAsync(Material material)
{
MaterialDescriptor descriptor;

if (string.IsNullOrEmpty(Source))
Expand Down
17 changes: 8 additions & 9 deletions DirectX12GameEngine.Assets/ModelAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
using System.IO;
using System.Threading.Tasks;
using DirectX12GameEngine.Core.Assets;
using DirectX12GameEngine.Engine;
using DirectX12GameEngine.Graphics;
using DirectX12GameEngine.Rendering;
using Microsoft.Extensions.DependencyInjection;

namespace DirectX12GameEngine.Assets
{
[AssetContentType(typeof(Model))]
public class ModelAsset : AssetWithSource<Model>
{
private readonly ContentManager contentManager;
private readonly GraphicsDevice device;
public IList<Material> Materials { get; } = new List<Material>();

public ModelAsset(ContentManager contentManager, GraphicsDevice device)
public async override Task CreateAssetAsync(Model model, IServiceProvider services)
{
this.contentManager = contentManager;
this.device = device;
}
ContentManager contentManager = services.GetRequiredService<ContentManager>();
IGraphicsDeviceManager graphicsDeviceManager = services.GetRequiredService<IGraphicsDeviceManager>();
GraphicsDevice? device = graphicsDeviceManager.GraphicsDevice;

public IList<Material> Materials { get; } = new List<Material>();
if (device is null) throw new InvalidOperationException();

public async override Task CreateAssetAsync(Model model)
{
string extension = Path.GetExtension(Source);

if (extension == ".glb")
Expand Down
17 changes: 8 additions & 9 deletions DirectX12GameEngine.Assets/TextureAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
using System.IO;
using System.Threading.Tasks;
using DirectX12GameEngine.Core.Assets;
using DirectX12GameEngine.Engine;
using DirectX12GameEngine.Graphics;
using Microsoft.Extensions.DependencyInjection;

namespace DirectX12GameEngine.Assets
{
[AssetContentType(typeof(Texture))]
public class TextureAsset : AssetWithSource<Texture>
{
private readonly ContentManager contentManager;
private readonly GraphicsDevice device;

public TextureAsset(ContentManager contentManager, GraphicsDevice device)
public async override Task CreateAssetAsync(Texture texture, IServiceProvider services)
{
this.contentManager = contentManager;
this.device = device;
}
ContentManager contentManager = services.GetRequiredService<ContentManager>();
IGraphicsDeviceManager graphicsDeviceManager = services.GetRequiredService<IGraphicsDeviceManager>();
GraphicsDevice? device = graphicsDeviceManager.GraphicsDevice;

if (device is null) throw new InvalidOperationException();

public async override Task CreateAssetAsync(Texture texture)
{
string extension = Path.GetExtension(Source);

if (extension == ".png" || extension == ".jpg" || extension == ".jpeg")
Expand Down
6 changes: 3 additions & 3 deletions DirectX12GameEngine.Core.Assets/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public abstract class Asset

public virtual string? MainSource => null;

public abstract Task CreateAssetAsync(object obj);
public abstract Task CreateAssetAsync(object obj, IServiceProvider services);
}

public abstract class Asset<T> : Asset
{
public abstract Task CreateAssetAsync(T obj);
public abstract Task CreateAssetAsync(T obj, IServiceProvider services);

public override Task CreateAssetAsync(object obj) => CreateAssetAsync((T)obj);
public override Task CreateAssetAsync(object obj, IServiceProvider services) => CreateAssetAsync((T)obj, services);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ internal async Task<object> DeserializeAsync(string path, Type type, Reference?
if (asset != null)
{
await DeserializeAsync(root, operation, asset);
await asset.CreateAssetAsync(result);
await asset.CreateAssetAsync(result, Services);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion DirectX12GameEngine.Core/WindowHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace DirectX12GameEngine.Core
{
public sealed class WindowHandle
{
public WindowHandle(AppContextType contextType, object nativeWindow, IntPtr handle)
public WindowHandle(AppContextType contextType, object nativeWindow, IntPtr handle = default)
{
ContextType = contextType;
NativeWindow = nativeWindow;
Expand Down
25 changes: 2 additions & 23 deletions DirectX12GameEngine.Editor/DirectX12GameEngine.Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,9 @@
<Compile Include="Factories\SceneViewFactory.cs" />
<Compile Include="Messages\LaunchStorageItemMessage.cs" />
<Compile Include="Messaging\IMessenger.cs" />
<Compile Include="ViewModels\EditorViewModel.cs" />
<Compile Include="ViewModels\MainViewModel.cs" />
<Compile Include="ViewModels\EditorViewsViewModel.cs" />
<Compile Include="Views\MenuBarItems\EditMenuBarItem.xaml.cs">
<DependentUpon>EditMenuBarItem.xaml</DependentUpon>
</Compile>
<Compile Include="Views\MenuBarItems\FileMenuBarItem.xaml.cs">
<DependentUpon>FileMenuBarItem.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModels\SceneViewModel.cs" />
<Compile Include="Views\EditorMenuBar.xaml.cs">
<DependentUpon>EditorMenuBar.xaml</DependentUpon>
</Compile>
Expand All @@ -154,17 +149,13 @@
<Compile Include="Views\Shell.xaml.cs">
<DependentUpon>Shell.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModels\SolutionExplorerViewModel.cs" />
<Compile Include="Views\SolutionExplorerView.xaml.cs">
<DependentUpon>SolutionExplorerView.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModels\StorageItemViewModel.cs" />
<Compile Include="ViewModels\ViewModelBase.cs" />
<Compile Include="ViewModels\ObservableViewModelCollection.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Views\MenuBarItems\ViewMenuBarItem.xaml.cs">
<DependentUpon>ViewMenuBarItem.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
Expand All @@ -191,14 +182,6 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\MenuBarItems\EditMenuBarItem.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\MenuBarItems\FileMenuBarItem.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\EditorMenuBar.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand All @@ -215,10 +198,6 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\MenuBarItems\ViewMenuBarItem.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
Expand Down
14 changes: 10 additions & 4 deletions DirectX12GameEngine.Editor/EditorGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
using System.Numerics;
using System.Threading.Tasks;
using DirectX12GameEngine.Engine;
using DirectX12GameEngine.Games;
using DirectX12GameEngine.Graphics;
using Windows.Storage;

#nullable enable

namespace DirectX12GameEngine.Editor
{
public class EditorGame : Game
{
public EditorGame(GameContext gameContext, StorageFolder rootFolder) : base(gameContext)
public EditorGame(StorageFolder rootFolder)
{
Content.RootFolder = rootFolder;
}

protected override void Initialize()
{
base.Initialize();

if (GraphicsDevice.Presenter != null)
if (GraphicsDevice?.Presenter != null)
{
GraphicsDevice.Presenter.PresentationParameters.SyncInterval = 1;
}
Expand All @@ -24,7 +30,7 @@ protected override void BeginDraw()
{
base.BeginDraw();

if (GraphicsDevice.Presenter != null)
if (GraphicsDevice?.Presenter != null)
{
GraphicsDevice.CommandList.Clear(GraphicsDevice.Presenter.BackBuffer, new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
GraphicsDevice.CommandList.Clear(GraphicsDevice.Presenter.DepthStencilBuffer, ClearFlags.FlagsDepth);
Expand Down
2 changes: 1 addition & 1 deletion DirectX12GameEngine.Editor/Factories/SceneViewFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SceneViewFactory : IAssetViewFactory
if (rootItem.Model is StorageFolder rootFolder)
{
SceneView sceneView = new SceneView(rootFolder);
await sceneView.LoadAsync(path);
Task sceneTask = sceneView.ViewModel.LoadAsync(path);

return sceneView;
}
Expand Down
7 changes: 0 additions & 7 deletions DirectX12GameEngine.Editor/ViewModels/EditorViewModel.cs

This file was deleted.

37 changes: 37 additions & 0 deletions DirectX12GameEngine.Editor/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#nullable enable

using DirectX12GameEngine.Editor.Messages;
using DirectX12GameEngine.Editor.Messaging;

namespace DirectX12GameEngine.Editor.ViewModels
{
public class MainViewModel : ViewModelBase
{
private StorageItemViewModel? rootFolder;

public MainViewModel()
{
RegisterMessages();
}

public EditorViewsViewModel EditorViews { get; } = new EditorViewsViewModel();

public ProjectLoaderViewModel ProjectLoader { get; } = new ProjectLoaderViewModel();

public StorageItemViewModel? RootFolder
{
get => rootFolder;
set => Set(ref rootFolder, value);
}

private void RegisterMessages()
{
Messenger.Default.Register<ProjectLoadedMessage>(this, async m =>
{
RootFolder = m.RootFolder;
RootFolder.HasUnrealizedChildren = true;
await RootFolder.FillAsync();
});
}
}
}
31 changes: 31 additions & 0 deletions DirectX12GameEngine.Editor/ViewModels/SceneViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading.Tasks;
using DirectX12GameEngine.Engine;

#nullable enable

namespace DirectX12GameEngine.Editor.ViewModels
{
public class SceneViewModel : ViewModelBase
{
private readonly EditorGame game;

public SceneViewModel(EditorGame game)
{
this.game = game;

game.SceneSystem.SceneInstance.RootEntity = RootEntity.Model;
}

public EntityViewModel RootEntity { get; } = new EntityViewModel(new Entity("RootEntity"));

public async Task LoadAsync(string path)
{
RootEntity.Children.Clear();

Entity scene = await game.Content.LoadAsync<Entity>(path);
EntityViewModel sceneViewModel = new EntityViewModel(scene);

RootEntity.Children.Add(sceneViewModel);
}
}
}
Loading

0 comments on commit 24f929e

Please sign in to comment.