-
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathWindow.cs
More file actions
229 lines (208 loc) · 7.66 KB
/
Copy pathWindow.cs
File metadata and controls
229 lines (208 loc) · 7.66 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using BepuUtilities;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Platform;
using System;
using System.Diagnostics;
using System.Threading;
using Vector2 = System.Numerics.Vector2;
namespace DemoUtilities
{
public enum WindowMode
{
FullScreen,
Windowed
}
/// <summary>
/// Simple and not-very-general-purpose window management.
/// </summary>
public class Window : IDisposable
{
internal NativeWindow window;
private bool resized;
private WindowMode windowMode;
WindowMode WindowMode
{
get
{
return windowMode;
}
set
{
switch (value)
{
case WindowMode.FullScreen:
if (windowMode != WindowMode.FullScreen)
{
windowMode = value;
window.WindowState = WindowState.Fullscreen;
window.WindowBorder = WindowBorder.Hidden;
window.Location = new Point(0, 0);
var primaryBounds = DisplayDevice.Default.Bounds;
window.Size = new Size(primaryBounds.Width, primaryBounds.Height);
resized = true;
}
break;
case WindowMode.Windowed:
if (windowMode != WindowMode.Windowed)
{
windowMode = value;
window.WindowState = WindowState.Normal;
window.WindowBorder = WindowBorder.Resizable;
resized = true;
}
break;
}
}
}
/// <summary>
/// Gets or sets the resolution of the window's body.
/// </summary>
public Int2 Resolution
{
get
{
return new Int2(window.ClientSize.Width, window.ClientSize.Height);
}
set
{
window.ClientSize = new Size(value.X, value.Y);
resized = true;
}
}
public IntPtr Handle { get { return window.WindowInfo.Handle; } }
public IWindowInfo WindowInfo => window.WindowInfo;
/// <summary>
/// Gets whether the window is currently focused.
/// </summary>
public bool Focused { get { return window.Focused; } }
/// <summary>
/// Constructs a new rendering-capable window.
/// </summary>
/// <param name="title">Title of the window.</param>
/// <param name="resolution">Initial size in pixels of the window's drawable surface.</param>
/// <param name="location">Initial location of the window's drawable surface.</param>
/// <param name="windowMode">Initial window mode.</param>
public Window(string title, Int2 resolution, Int2 location, WindowMode windowMode)
{
window = new NativeWindow(location.X, location.Y, resolution.X, resolution.Y, title, GameWindowFlags.FixedWindow, GraphicsMode.Default, DisplayDevice.Default);
window.Visible = true;
Resolution = resolution;
window.Resize += (form, args) => resized = true;
window.Closing += OnClosing;
WindowMode = windowMode;
}
/// <summary>
/// Constructs a new rendering-capable window.
/// </summary>
/// <param name="title">Title of the window.</param>
/// <param name="resolution">Initial size in pixels of the window's drawable surface.</param>
/// <param name="windowMode">Initial window mode.</param>
public Window(string title, Int2 resolution, WindowMode windowMode)
: this(title, resolution, new Int2((DisplayDevice.Default.Width - resolution.X) / 2, (DisplayDevice.Default.Height - resolution.Y) / 2), windowMode)
{
}
public Vector2 GetNormalizedMousePosition(Int2 mousePosition)
{
return new Vector2((float)mousePosition.X / Resolution.X, (float)mousePosition.Y / Resolution.Y);
}
private void OnClosing(object sender, EventArgs e)
{
//This will redundantly call window.Close, but that's fine.
tryToClose = true;
}
private bool windowUpdateLoopRunning;
private bool tryToClose;
/// <summary>
/// Closes the window at the next available opportunity.
/// </summary>
public void Close()
{
if (windowUpdateLoopRunning)
tryToClose = true;
else
window.Close();
}
/// <summary>
/// Launches the update loop for the window. Processes events before every invocation of the update handler.
/// </summary>
/// <param name="updateHandler">Delegate to be invoked within the loop repeatedly.</param>
public void Run(Action<float> updateHandler, Action<Int2> onResize)
{
long previousTime = Stopwatch.GetTimestamp();
windowUpdateLoopRunning = true;
while (true)
{
if (disposed)
break;
if (resized)
{
//Note that minimizing or resizing the window to invalid sizes don't result in actual resize attempts. Zero width rendering surfaces aren't allowed.
if (window.Width > 0 && window.Height > 0)
{
onResize(new Int2(window.Width, window.Height));
}
resized = false;
}
window.ProcessEvents();
if (tryToClose)
{
window.Close();
break;
}
long time = Stopwatch.GetTimestamp();
var dt = (float)((time - previousTime) / (double)Stopwatch.Frequency);
previousTime = time;
if (window.WindowState != WindowState.Minimized)
{
updateHandler(dt);
}
else
{
//If the window is minimized, take a breather.
Thread.Sleep(1);
}
}
windowUpdateLoopRunning = false;
}
public void SingleFrame(Action<float> updateHandler, Action<Int2> onResize, float dt)
{
if (disposed)
return;
if (resized)
{
//Note that minimizing or resizing the window to invalid sizes don't result in actual resize attempts. Zero width rendering surfaces aren't allowed.
if (window.Width > 0 && window.Height > 0)
{
onResize(new Int2(window.Width, window.Height));
}
resized = false;
}
window.ProcessEvents();
if (tryToClose)
{
window.Close();
return;
}
long time = Stopwatch.GetTimestamp();
if (window.WindowState != WindowState.Minimized)
{
updateHandler(dt);
}
else
{
//If the window is minimized, take a breather.
Thread.Sleep(1);
}
}
private bool disposed;
public void Dispose()
{
if (!disposed)
{
window.Dispose();
disposed = true;
}
}
}
}