Skip to content

Commit 69e6e4d

Browse files
devvoidPerksey
authored andcommitted
GLFW cleanup (#14)
* Start of GLFW libraries * Add libs * Add anti-gitignore for the libraries * Use included libs * Remove debug print from GlfwWindow * Organize variables, cleanup documentation * Add the ability to change update/render rate post-window creation * Revert change, it ended up being messier than expected * Replace int with KeyModifiers * Replace dispatcher null check with proper constructor * Document GlfwWindow constructor * Add documentation to the RaiseFrame functions * Change way libs are included * Fix errors in csproj * Better targets file * Fix indents
1 parent b73c26e commit 69e6e4d

File tree

10 files changed

+146
-68
lines changed

10 files changed

+146
-68
lines changed

build/native/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
!**/*.dll
2+
!**/*.so
3+
!**/*.dylib

build/native/glfw/glfw3-x64.dll

221 KB
Binary file not shown.

build/native/glfw/glfw3-x86.dll

202 KB
Binary file not shown.

build/native/glfw/libglfw.so.3.3

328 KB
Binary file not shown.

src/Windowing/Silk.NET.GLFW/GlfwCallbacks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public delegate void KeyCallback(WindowHandle* window, Keys key, int scanCode, I
105105
/// <seealso cref="IGlfw.SetMouseButtonCallback" />
106106
public delegate void
107107
MouseButtonCallback(WindowHandle* window, int button, InputAction action,
108-
int mods); // TODO: Make enums for int params in callback
108+
KeyModifiers mods);
109109

110110
/// <summary>
111111
/// The function signature for scroll callback functions.

src/Windowing/Silk.NET.GLFW/GlfwProvider.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ namespace Silk.NET.GLFW
1313
/// </summary>
1414
public static class GlfwProvider
1515
{
16+
/// <summary>
17+
/// Creates a new instance of the GlfwProvider class.
18+
/// </summary>
19+
static GlfwProvider()
20+
{
21+
ThreadDispatcher = new Dispatcher();
22+
GLFW = new Lazy<Glfw>(() =>
23+
{
24+
if (ThreadDispatcher == null) {
25+
ThreadDispatcher = new Dispatcher();
26+
}
27+
28+
var glfw = Glfw.GetAPI();
29+
30+
ThreadDispatcher.Invoke(() =>
31+
{
32+
glfw.Init();
33+
glfw.SetErrorCallback(Glfw.ErrorCallback);
34+
});
35+
36+
return glfw;
37+
});
38+
}
39+
1640
/// <summary>
1741
/// Gets a dispatcher to the main GLFW thread.
1842
/// </summary>
@@ -26,22 +50,7 @@ public static class GlfwProvider
2650
/// <summary>
2751
/// Gets a GLFW interface implementation lazily.
2852
/// </summary>
29-
public static Lazy<Glfw> GLFW { get; internal set; } = new Lazy<Glfw>(() =>
30-
{
31-
if (ThreadDispatcher == null) {
32-
ThreadDispatcher = new Dispatcher();
33-
}
34-
35-
var glfw = Glfw.GetAPI();
36-
37-
ThreadDispatcher.Invoke(() =>
38-
{
39-
glfw.Init();
40-
glfw.SetErrorCallback(Glfw.ErrorCallback);
41-
});
42-
43-
return glfw;
44-
});
53+
public static Lazy<Glfw> GLFW { get; internal set; }
4554

4655
/// <summary>
4756
/// Unloads the loaded <see cref="GLFW" /> interface implementation.

src/Windowing/Silk.NET.GLFW/Silk.NET.GLFW.csproj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,29 @@
88

99
<ItemGroup>
1010
<PackageReference Include="AdvancedDLSupport" Version="3.0.0" />
11-
<PackageReference Include="opentoolkit.redist.glfw" Version="3.3.0-pre20190424184726" />
1211
<PackageReference Include="Ultz.Dispatcher" Version="1.1.0" />
1312
</ItemGroup>
1413

1514
<ItemGroup>
1615
<ProjectReference Include="..\..\Silk.NET.Core\Silk.NET.Core.csproj" />
1716
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Content Include="..\..\..\build\native\glfw\glfw3-x64.dll"
20+
Link="runtimes/win-x64/native/glfw3-x64.dll"
21+
PackagePath="runtimes/win-x64/native"
22+
InProject="false" />
23+
24+
<Content Include="..\..\..\build\native\glfw\glfw3-x86.dll"
25+
Link="runtimes/win-x86/native/glfw3-x86.dll"
26+
PackagePath="runtimes/win-x86/native"
27+
InProject="false" />
28+
29+
<Content Include="..\..\..\build\native\glfw\libglfw.so.3.3"
30+
Link="runtimes/linux-x64/native/libglfw.so.3.3"
31+
PackagePath="runtimes/linux-x64/native"
32+
InProject="false" />
33+
34+
<Content Include="Silk.NET.GLFW.targets" PackagePath="build/" />
35+
</ItemGroup>
1836
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<Project>
2+
<PropertyGroup>
3+
<glfw_Platform Condition="'$(RuntimeIdentifier)' == 'win-x64'">win64</glfw_Platform>
4+
<glfw_Platform Condition="'$(RuntimeIdentifier)' == 'win-x86'">win86</glfw_Platform>
5+
<glfw_Platform Condition="'$(RuntimeIdentifier)' == 'linux-x64'">linux</glfw_Platform>
6+
<glfw_Platform Condition="'$(glfw_Platform)' == ''">any</glfw_Platform>
7+
</PropertyGroup>
8+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
9+
<None Include="$(MSBuildThisFileDirectory)\..\runtimes\win-x64\native\glfw3-x64.dll">
10+
<Link>glfw3-x64.dll</Link>
11+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
12+
<Visible>false</Visible>
13+
</None>
14+
<None Include="$(MSBuildThisFileDirectory)\..\runtimes\win-x86\native\glfw3-x86.dll">
15+
<Link>glfw3-x86.dll</Link>
16+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17+
<Visible>false</Visible>
18+
</None>
19+
</ItemGroup>
20+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
21+
<None Include="$(MSBuildThisFileDirectory)\..\runtimes\win-x64\native\glfw3-x64.dll" Condition="'$(glfw_Platform)' == 'win64' Or '$(glfw_Platform)' == 'any'">
22+
<Link>glfw3-x64.dll</Link>
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
<Visible>false</Visible>
25+
</None>
26+
<None Include="$(MSBuildThisFileDirectory)\..\runtimes\win-x86\native\glfw3-x86.dll" Condition="'$(glfw_Platform)' == 'win86' Or '$(glfw_Platform)' == 'any'">
27+
<Link>glfw3-x86.dll</Link>
28+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29+
<Visible>false</Visible>
30+
</None>
31+
<None Include="$(MSBuildThisFileDirectory)\..\runtimes\linux-x64\native\libglfw.so.3.3" Condition="'$(glfw_Platform)' == 'linux' Or '$(glfw_Platform)' == 'any'">
32+
<Link>libglfw.so.3.3</Link>
33+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
34+
<Visible>false</Visible>
35+
</None>
36+
<!-- TODO: Add Mac support -->
37+
</ItemGroup>
38+
</Project>

src/Windowing/Silk.NET.Windowing.Common/Interfaces/IWindowProperties.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public interface IWindowProperties
4747
/// <summary>
4848
/// The number of rendering operations to run every second.
4949
/// </summary>
50-
double FramesPerSecond { get; }
50+
double FramesPerSecond { get; set; }
5151

5252
/// <summary>
5353
/// The number of update operations to run every second.
5454
/// </summary>
55-
double UpdatesPerSecond { get; }
55+
double UpdatesPerSecond { get; set; }
5656

5757
/// <summary>
5858
/// Gets the graphics API the window will use.

src/Windowing/Silk.NET.Windowing.Desktop/GlfwWindow.cs

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,49 @@ public class GlfwWindow : IWindow
2222
// The number of frames that the window has been running slowly for.
2323
private int _isRunningSlowlyTries;
2424

25+
// Cache variables
2526
private Point _position;
26-
2727
private Size _size;
28-
2928
private string _title;
30-
3129
private VSyncMode _vSync;
32-
3330
private WindowBorder _windowBorder;
34-
3531
private WindowState _windowState;
3632

3733
// Glfw stuff
3834
private Glfw glfw = GlfwProvider.GLFW.Value;
3935
private Dispatcher glfwThread = GlfwProvider.ThreadDispatcher;
36+
private unsafe WindowHandle* WindowPtr;
37+
38+
// Callbacks
4039
private GlfwCallbacks.WindowCloseCallback onClosing;
4140
private GlfwCallbacks.DropCallback onFileDrop;
4241
private GlfwCallbacks.WindowFocusCallback onFocusChanged;
4342
private GlfwCallbacks.WindowMaximizeCallback onMaximized;
4443
private GlfwCallbacks.WindowIconifyCallback onMinimized;
45-
46-
// Callbacks
47-
4844
private GlfwCallbacks.WindowPosCallback onMove;
4945
private GlfwCallbacks.WindowSizeCallback onResize;
50-
private Dispatcher RenderDispatcher;
51-
52-
// The period for rendering and updating. In essence, the delay between events being fired off.
53-
private double renderPeriod;
5446

47+
// Main loop-related things
48+
5549
// The stopwatches. Used to calculate delta.
5650
private Stopwatch renderStopwatch;
57-
58-
// Main loop-related things
51+
private Stopwatch updateStopwatch;
5952

6053
// Dispatcher for the update and render threads
6154
private Dispatcher UpdateDispatcher;
55+
private Dispatcher RenderDispatcher;
56+
57+
// Update and render period. Represents the time in seconds that each frame should take.
6258
private double updatePeriod;
63-
private Stopwatch updateStopwatch;
64-
private unsafe WindowHandle* WindowPtr;
59+
private double renderPeriod;
60+
61+
private double updatesPerSecond;
62+
private double framesPerSecond;
6563

64+
/// <summary>
65+
/// Create and open a new GlfwWindow.
66+
/// </summary>
67+
/// <param name="options">The options to use for this window.</param>
6668
public GlfwWindow(WindowOptions options)
6769
{
6870
unsafe {
@@ -217,10 +219,36 @@ public Size Size
217219
}
218220

219221
/// <inheritdoc />
220-
public double FramesPerSecond { get; }
222+
public double FramesPerSecond {
223+
get => framesPerSecond;
224+
set
225+
{
226+
framesPerSecond = value;
227+
228+
if (value <= double.Epsilon) {
229+
renderPeriod = 0.0;
230+
return;
231+
}
232+
233+
renderPeriod = 1.0 / value;
234+
}
235+
}
221236

222237
/// <inheritdoc />
223-
public double UpdatesPerSecond { get; }
238+
public double UpdatesPerSecond
239+
{
240+
get => updatesPerSecond;
241+
set
242+
{
243+
updatesPerSecond = value;
244+
245+
if (value <= double.Epsilon) {
246+
updatePeriod = 0.0;
247+
return;
248+
}
249+
updatePeriod = 1.0 / value;
250+
}
251+
}
224252

225253
/// <inheritdoc />
226254
public GraphicsAPI API { get; }
@@ -280,7 +308,7 @@ public WindowBorder WindowBorder
280308
{
281309
get => _windowBorder;
282310
set
283-
{
311+
{
284312
glfwThread.Invoke(() =>
285313
{
286314
unsafe {
@@ -351,25 +379,10 @@ public void Run()
351379

352380
// Initialize some variables
353381
_isRunningSlowlyTries = 0;
382+
354383
renderStopwatch = new Stopwatch();
355384
updateStopwatch = new Stopwatch();
356-
357-
// Calculate the update speed.
358-
if (UpdatesPerSecond <= double.Epsilon) {
359-
updatePeriod = 0.0;
360-
}
361-
else {
362-
updatePeriod = 1.0 / UpdatesPerSecond;
363-
}
364-
365-
// Calculate the render speed.
366-
if (FramesPerSecond <= double.Epsilon) {
367-
renderPeriod = 0.0;
368-
}
369-
else {
370-
renderPeriod = 1.0 / FramesPerSecond;
371-
}
372-
385+
373386
UpdateDispatcher = new Dispatcher();
374387
RenderDispatcher = new Dispatcher();
375388

@@ -467,10 +480,13 @@ public Point PointToScreen(Point point)
467480
/// <inheritdoc />
468481
public event Action<double> OnRender;
469482

483+
/// <summary>
484+
/// Run an OnUpdate event.
485+
/// </summary>
470486
private void RaiseUpdateFrame()
471487
{
472-
// If using a capped framerate, we have to do some synchronization-related things before rendering.
473-
// This is only necessary if VSync is disabled.
488+
// If using a capped framerate without vsync, we have to do some synchronization-related things
489+
// before rendering.
474490
if (UpdatesPerSecond > double.Epsilon
475491
&& (VSync == VSyncMode.Off || VSync == VSyncMode.Adaptive && IsRunningSlowly)) {
476492
// Calculate the amount of time to sleep.
@@ -493,27 +509,21 @@ private void RaiseUpdateFrame()
493509
updateStopwatch.Restart();
494510
}
495511

512+
/// <summary>
513+
/// Run an OnRender event.
514+
/// </summary>
496515
private void RaiseRenderFrame()
497516
{
498-
// If using a capped framerate, we have to do some synchronization-related things before rendering.
517+
// Identical to RaiseUpdateFrame.
499518
if (FramesPerSecond > double.Epsilon
500519
&& (VSync == VSyncMode.Off || VSync == VSyncMode.Adaptive && IsRunningSlowly)) {
501-
// Calculate the amount of time to sleep.
502520
var sleepTime = renderPeriod - renderStopwatch.Elapsed.TotalSeconds;
503521

504-
// If the result is negative, that means the frame is running slowly, so don't sleep.
505522
if (sleepTime > 0.0) {
506-
// Else, sleep for that amount of time.
507-
var timeBefore = renderStopwatch.Elapsed.TotalSeconds;
508523
Thread.Sleep((int) (1000 * sleepTime));
509-
var timeAfter = renderStopwatch.Elapsed.TotalSeconds;
510-
511-
Console.WriteLine($"Tried sleeping for {sleepTime}, slept for {timeAfter - timeBefore}," +
512-
$"variance of {timeAfter - timeBefore - sleepTime}\n");
513524
}
514525
}
515526

516-
// Calculate delta and run frame.
517527
var delta = renderStopwatch.Elapsed.TotalSeconds;
518528
OnRender?.Invoke(delta);
519529
renderStopwatch.Restart();

0 commit comments

Comments
 (0)