Created
July 19, 2022 09:41
-
-
Save EricEzaM/5797be1f4b28f15e9be53287a02d3d67 to your computer and use it in GitHub Desktop.
Stride3D embed in WPF xaml control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<UserControl x:Class="Namespace.StrideView" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:local="clr-namespace:Namespace" | |
mc:Ignorable="d" | |
d:DesignHeight="300" d:DesignWidth="300"> | |
<Grid> | |
<ContentPresenter x:Name="SceneView"> | |
</ContentPresenter> | |
</Grid> | |
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class StrideView : UserControl | |
{ | |
private Thread gameThread; | |
private readonly TaskCompletionSource<bool> gameStartedTaskSource = new TaskCompletionSource<bool>(); | |
private IntPtr windowHandle; | |
public StrideView() | |
{ | |
InitializeComponent(); | |
gameThread = new Thread(SafeAction.Wrap(GameRunThread)) | |
{ | |
IsBackground = true, | |
Name = "Game Thread" | |
}; | |
gameThread.SetApartmentState(ApartmentState.STA); | |
Loaded += (sender, args) => | |
{ | |
StartGame(); | |
}; | |
} | |
private async Task StartGame() | |
{ | |
gameThread.Start(); | |
await gameStartedTaskSource.Task; | |
SceneView.Content = new GameEngineHost(windowHandle); | |
} | |
private void GameRunThread() | |
{ | |
// Create the form from this thread | |
// EmbeddedGameForm is in Stride.Editor. You may need to copy this class to your own project. | |
var form = new EmbeddedGameForm() | |
{ | |
TopLevel = false, | |
Visible = false | |
}; | |
windowHandle = form.Handle; | |
var context = new GameContextWinforms(form); | |
gameStartedTaskSource.SetResult(true); | |
var game = new Game(); | |
game.Run(context, scene => | |
{ | |
game.Window.IsBorderLess = true; | |
game.SetupBase3DScene(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code has an implicit dependency on extension methods in this repo.