/
HelloWindow.cs
65 lines (54 loc) · 2.17 KB
/
HelloWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
using System;
using System.Linq;
using System.Reflection;
using TerraFX.ApplicationModel;
using TerraFX.Graphics;
using TerraFX.UI;
using TerraFX.Utilities;
namespace TerraFX.Samples.Graphics
{
public sealed class HelloWindow : Sample
{
private IGraphicsContext? _graphicsContext;
private IWindow? _window;
private TimeSpan _elapsedTime;
public HelloWindow(string name, params Assembly[] compositionAssemblies)
: base(name, compositionAssemblies)
{
}
public override void OnIdle(object? sender, ApplicationIdleEventArgs eventArgs)
{
ExceptionUtilities.ThrowIfNull(sender, nameof(sender));
var application = (Application)sender;
if (_window is null)
{
var windowProvider = application.GetService<IWindowProvider>();
_window = windowProvider.CreateWindow();
_window.Show();
}
else if (_window.IsVisible)
{
if (_graphicsContext is null)
{
var graphicsProvider = application.GetService<IGraphicsProvider>();
var graphicsAdapter = graphicsProvider.GraphicsAdapters.First();
var graphicsSurface = _window.CreateGraphicsSurface(bufferCount: 2);
_graphicsContext = graphicsAdapter.CreateGraphicsContext(graphicsSurface);
}
else
{
_elapsedTime += eventArgs.Delta;
if (_elapsedTime.TotalSeconds >= 2.5)
{
application.RequestExit();
}
var backgroundColor = new ColorRgba(red: 100.0f / 255.0f, green: 149.0f / 255.0f, blue: 237.0f / 255.0f, alpha: 1.0f);
_graphicsContext.BeginFrame(backgroundColor);
_graphicsContext.EndFrame();
_graphicsContext.PresentFrame();
}
}
}
}
}