-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.cs
More file actions
147 lines (122 loc) · 4.77 KB
/
Copy pathGameWindow.cs
File metadata and controls
147 lines (122 loc) · 4.77 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
namespace Engine
{
using System;
using System.Diagnostics;
using Veldrid;
using Veldrid.Sdl2;
using Veldrid.StartupUtilities;
using Veldrid.Utilities;
/// <summary>
/// The game window.
/// </summary>
/// <seealso cref="Engine.IGameWindow" />
public class GameWindow : IGameWindow
{
/// <summary>
/// The SDL window.
/// </summary>
private readonly Sdl2Window window;
/// <summary>
/// The graphics device.
/// </summary>
private GraphicsDevice graphicsDevice;
/// <summary>
/// The resource factory.
/// </summary>
private DisposeCollectorResourceFactory resourceFactory;
/// <summary>
/// Temporarily stores a flag indicating whether the window has just been resized.
/// </summary>
private bool windowResized = true;
/// <summary>
/// Initializes a new instance of the <see cref="GameWindow"/> class.
/// </summary>
/// <param name="title">The title.</param>
public GameWindow(string title)
{
var windowCreateInfo = new WindowCreateInfo
{
X = Sdl2Native.SDL_WINDOWPOS_CENTERED,
Y = Sdl2Native.SDL_WINDOWPOS_CENTERED,
WindowWidth = Configuration.ChunkWidth * Configuration.TileSize,
WindowHeight = Configuration.ChunkHeight * Configuration.TileSize,
WindowTitle = title,
};
this.window = VeldridStartup.CreateWindow(ref windowCreateInfo);
this.window.Resizable = false;
this.window.Resized += () => { this.windowResized = true; };
this.window.KeyDown += this.OnKeyDown;
}
/// <inheritdoc />
public event Action<float> Rendering;
/// <inheritdoc />
public event Action<GraphicsDevice, ResourceFactory, Swapchain> GraphicsDeviceCreated;
/// <inheritdoc />
public event Action GraphicsDeviceDestroyed;
/// <inheritdoc />
public event Action Resized;
/// <inheritdoc />
public event Action<KeyEvent> KeyPressed;
/// <inheritdoc />
public uint Width => (uint)this.window.Width;
/// <inheritdoc />
public uint Height => (uint)this.window.Height;
/// <inheritdoc />
public string Title
{
get => this.window.Title;
set => this.window.Title = value;
}
/// <inheritdoc />
public void Close() => this.window.Close();
/// <inheritdoc />
public void Run(GraphicsBackend graphicsAPI = GraphicsBackend.Direct3D11)
{
var options = new GraphicsDeviceOptions(
debug: false,
swapchainDepthFormat: null,
syncToVerticalBlank: true,
resourceBindingModel: ResourceBindingModel.Improved,
preferDepthRangeZeroToOne: false,
preferStandardClipSpaceYDirection: false);
#if DEBUG
options.Debug = true;
#endif
this.graphicsDevice = VeldridStartup.CreateGraphicsDevice(this.window, options, graphicsAPI);
this.resourceFactory = new DisposeCollectorResourceFactory(this.graphicsDevice.ResourceFactory);
this.GraphicsDeviceCreated?.Invoke(this.graphicsDevice, this.resourceFactory, this.graphicsDevice.MainSwapchain);
var timer = Stopwatch.StartNew();
var previousElapsed = timer.Elapsed.TotalSeconds;
while (this.window.Exists)
{
var newElapsed = timer.Elapsed.TotalSeconds;
var deltaSeconds = (float)(newElapsed - previousElapsed);
var inputSnapshot = this.window.PumpEvents();
InputTracker.UpdateFrameInput(inputSnapshot);
if (this.window.Exists)
{
previousElapsed = newElapsed;
if (this.windowResized)
{
this.windowResized = false;
this.graphicsDevice.ResizeMainWindow((uint)this.window.Width, (uint)this.window.Height);
this.Resized?.Invoke();
}
this.Rendering?.Invoke(deltaSeconds);
}
}
this.graphicsDevice.WaitForIdle();
this.resourceFactory.DisposeCollector.DisposeAll();
this.graphicsDevice.Dispose();
this.GraphicsDeviceDestroyed?.Invoke();
}
/// <summary>
/// Called when a key has been pressed.
/// </summary>
/// <param name="keyEvent">The key event.</param>
protected void OnKeyDown(KeyEvent keyEvent)
{
this.KeyPressed?.Invoke(keyEvent);
}
}
}