Skip to content

Commit ee5f4a5

Browse files
committed
Changed delegates to use Window instead of IntPtr
Added explicit cast from IntPtr to Window Added public constructor for Window struct Added a very basic example for window creation and buffer clearing
1 parent 78ab995 commit ee5f4a5

File tree

8 files changed

+179
-20
lines changed

8 files changed

+179
-20
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\GLFW.NET\GLFW.NET.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

Examples/BasicExample/Program.cs

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using GLFW;
4+
5+
namespace BasicExample
6+
{
7+
class Program
8+
{
9+
private const string TITLE = "Simple Window";
10+
private const int WIDTH = 1024;
11+
private const int HEIGHT = 800;
12+
13+
private const int GL_COLOR_BUFFER_BIT = 0x00004000;
14+
15+
16+
private delegate void glClearColorHandler(float r, float g, float b, float a);
17+
private delegate void glClearHandler(int mask);
18+
19+
private static glClearColorHandler glClearColor;
20+
private static glClearHandler glClear;
21+
22+
private static Random rand;
23+
24+
static void Main(string[] args)
25+
{
26+
// Set some common hints for the OpenGL profile creation
27+
Glfw.WindowHint(Hint.ClientApi, ClientApi.OpenGL);
28+
Glfw.WindowHint(Hint.ContextVersionMajor, 3);
29+
Glfw.WindowHint(Hint.ContextVersionMinor, 3);
30+
Glfw.WindowHint(Hint.OpenglProfile, Profile.Core);
31+
Glfw.WindowHint(Hint.Doublebuffer, true);
32+
Glfw.WindowHint(Hint.Decorated, true);
33+
34+
rand = new Random();
35+
36+
#if OBJECTORIENTED
37+
// // The object oriented approach
38+
// using (var window = new NativeWindow(WIDTH, HEIGHT, title))
39+
// {
40+
// window.CenterOnScreen();
41+
// window.KeyPress += WindowOnKeyPress;
42+
// while (!window.IsClosing)
43+
// {
44+
// Glfw.PollEvents();
45+
// window.SwapBuffers();
46+
// }
47+
// }
48+
49+
#else
50+
51+
// Create window
52+
var window = Glfw.CreateWindow(WIDTH, HEIGHT, TITLE, Monitor.None, Window.None);
53+
Glfw.MakeContextCurrent(window);
54+
55+
// Effectively enables VSYNC by setting to 1.
56+
Glfw.SwapInterval(1);
57+
58+
// Find center position based on window and monitor sizes
59+
var screenSize = Glfw.PrimaryMonitor.WorkArea;
60+
var x = (screenSize.Width - WIDTH) / 2;
61+
var y = (screenSize.Height - HEIGHT) / 2;
62+
Glfw.SetWindowPosition(window, x, y);
63+
64+
// Set a key callback
65+
Glfw.SetKeyCallback(window, KeyCallback);
66+
67+
68+
glClearColor = Marshal.GetDelegateForFunctionPointer<glClearColorHandler>(Glfw.GetProcAddress("glClearColor"));
69+
glClear = Marshal.GetDelegateForFunctionPointer<glClearHandler>(Glfw.GetProcAddress("glClear"));
70+
71+
72+
var tick = 0L;
73+
ChangeRandomColor();
74+
75+
while (!Glfw.WindowShouldClose(window))
76+
{
77+
// Poll for OS events and swap front/back buffers
78+
Glfw.PollEvents();
79+
Glfw.SwapBuffers(window);
80+
81+
// Change background color to something random every 60 draws
82+
if (tick++ % 60 == 0)
83+
ChangeRandomColor();
84+
85+
// Clear the buffer to the set color
86+
glClear(GL_COLOR_BUFFER_BIT);
87+
}
88+
#endif
89+
}
90+
91+
private static void ChangeRandomColor()
92+
{
93+
var r = (float) rand.NextDouble();
94+
var g = (float) rand.NextDouble();
95+
var b = (float) rand.NextDouble();
96+
glClearColor(r, g, b, 1.0f);
97+
}
98+
99+
#if OBJECTORIENTED
100+
101+
private static void WindowOnKeyPress(object? sender, KeyEventArgs e)
102+
{
103+
var window = (NativeWindow) sender;
104+
switch (e.Key)
105+
{
106+
case Keys.Escape:
107+
window.Close();
108+
break;
109+
// ...and so on....
110+
}
111+
}
112+
#else
113+
114+
private static void KeyCallback(Window window, Keys key, int scancode, InputState state, ModifierKeys mods)
115+
{
116+
switch (key)
117+
{
118+
case Keys.Escape:
119+
Glfw.SetWindowShouldClose(window, true);
120+
break;
121+
}
122+
}
123+
#endif
124+
}
125+
}

Examples/Glfw.Skia/Glfw.Skia.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

GLFW.NET.sln

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{7D
1515
EndProject
1616
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Glfw.Skia", "Examples\Glfw.Skia\Glfw.Skia.csproj", "{5C97B9A1-2FD4-4B8D-A024-96C052A5DC4C}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicExample", "Examples\BasicExample\BasicExample.csproj", "{53E7BEED-08D7-43F9-97C9-13368F4C47CA}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -29,6 +31,10 @@ Global
2931
{5C97B9A1-2FD4-4B8D-A024-96C052A5DC4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
3032
{5C97B9A1-2FD4-4B8D-A024-96C052A5DC4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
3133
{5C97B9A1-2FD4-4B8D-A024-96C052A5DC4C}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{53E7BEED-08D7-43F9-97C9-13368F4C47CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{53E7BEED-08D7-43F9-97C9-13368F4C47CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{53E7BEED-08D7-43F9-97C9-13368F4C47CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{53E7BEED-08D7-43F9-97C9-13368F4C47CA}.Release|Any CPU.Build.0 = Release|Any CPU
3238
EndGlobalSection
3339
GlobalSection(SolutionProperties) = preSolution
3440
HideSolutionNode = FALSE
@@ -38,5 +44,6 @@ Global
3844
EndGlobalSection
3945
GlobalSection(NestedProjects) = preSolution
4046
{5C97B9A1-2FD4-4B8D-A024-96C052A5DC4C} = {7DC92A2E-41E8-408E-A022-9A9170D520A8}
47+
{53E7BEED-08D7-43F9-97C9-13368F4C47CA} = {7DC92A2E-41E8-408E-A022-9A9170D520A8}
4148
EndGlobalSection
4249
EndGlobal

GLFW.NET/Delegates.cs

+13-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace GLFW
1818
/// <param name="width">The new width, in screen coordinates, of the window.</param>
1919
/// <param name="height">The new height, in screen coordinates, of the window.</param>
2020
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
21-
public delegate void SizeCallback(IntPtr window, int width, int height);
21+
public delegate void SizeCallback(Window window, int width, int height);
2222

2323
/// <summary>
2424
/// This is the function signature for cursor position callback functions.
@@ -27,22 +27,22 @@ namespace GLFW
2727
/// <param name="x">The new cursor x-coordinate, relative to the left edge of the client area.</param>
2828
/// <param name="y">The new cursor y-coordinate, relative to the left edge of the client area.</param>
2929
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
30-
public delegate void PositionCallback(IntPtr window, double x, double y);
30+
public delegate void PositionCallback(Window window, double x, double y);
3131

3232
/// <summary>
3333
/// This is the function signature for window focus callback functions.
3434
/// </summary>
3535
/// <param name="window">The window handle.</param>
3636
/// <param name="focusing"><c>true</c> if window is gaining focus; otherise <c>false</c>.</param>
3737
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
38-
public delegate void FocusCallback(IntPtr window, bool focusing);
38+
public delegate void FocusCallback(Window window, bool focusing);
3939

4040
/// <summary>
4141
/// Generic signature for window callbacks.
4242
/// </summary>
4343
/// <param name="window">The window handle.</param>
4444
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
45-
public delegate void WindowCallback(IntPtr window);
45+
public delegate void WindowCallback(Window window);
4646

4747
/// <summary>
4848
/// This is the function signature for file drop callbacks.
@@ -51,7 +51,7 @@ namespace GLFW
5151
/// <param name="count">The number of dropped files.</param>
5252
/// <param name="arrayPtr">Pointer to an array UTF-8 encoded file and/or directory path name pointers.</param>
5353
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
54-
public delegate void FileDropCallback(IntPtr window, int count, IntPtr arrayPtr);
54+
public delegate void FileDropCallback(Window window, int count, IntPtr arrayPtr);
5555

5656
/// <summary>
5757
/// This is the function signature for cursor position callback functions.
@@ -60,15 +60,15 @@ namespace GLFW
6060
/// <param name="x">The new cursor x-coordinate, relative to the left edge of the client area.</param>
6161
/// <param name="y">The new cursor y-coordinate, relative to the left edge of the client area.</param>
6262
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
63-
public delegate void MouseCallback(IntPtr window, double x, double y);
63+
public delegate void MouseCallback(Window window, double x, double y);
6464

6565
/// <summary>
6666
/// This is the function signature for cursor enter/leave callback functions.
6767
/// </summary>
6868
/// <param name="window">The window handle.</param>
6969
/// <param name="entering"><c>true</c> if cursor is entering the window client area; otherwise <c>false</c>.</param>
7070
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
71-
public delegate void MouseEnterCallback(IntPtr window, bool entering);
71+
public delegate void MouseEnterCallback(Window window, bool entering);
7272

7373
/// <summary>
7474
/// This is the function signature for mouse button callback functions.
@@ -78,7 +78,7 @@ namespace GLFW
7878
/// <param name="state">The state.</param>
7979
/// <param name="modifiers">Flags describing which modifier keys were held down.</param>
8080
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
81-
public delegate void MouseButtonCallback(IntPtr window, MouseButton button, InputState state,
81+
public delegate void MouseButtonCallback(Window window, MouseButton button, InputState state,
8282
ModifierKeys modifiers);
8383

8484
/// <summary>
@@ -87,7 +87,7 @@ public delegate void MouseButtonCallback(IntPtr window, MouseButton button, Inpu
8787
/// <param name="window">The window handle.</param>
8888
/// <param name="codePoint">The Unicode code point of the character.</param>
8989
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
90-
public delegate void CharCallback(IntPtr window, uint codePoint);
90+
public delegate void CharCallback(Window window, uint codePoint);
9191

9292
/// <summary>
9393
/// This is the function signature for Unicode character callback functions.
@@ -96,7 +96,7 @@ public delegate void MouseButtonCallback(IntPtr window, MouseButton button, Inpu
9696
/// <param name="codePoint">The Unicode code point of the character.</param>
9797
/// <param name="mods">Bit field describing which modifier keys were held down.</param>
9898
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
99-
public delegate void CharModsCallback(IntPtr window, uint codePoint, ModifierKeys mods);
99+
public delegate void CharModsCallback(Window window, uint codePoint, ModifierKeys mods);
100100

101101
/// <summary>
102102
/// This is the function signature for keyboard key callback functions.
@@ -107,7 +107,7 @@ public delegate void MouseButtonCallback(IntPtr window, MouseButton button, Inpu
107107
/// <param name="state">The state of the key.</param>
108108
/// <param name="mods"> Bit field describing which modifier keys were held down.</param>
109109
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
110-
public delegate void KeyCallback(IntPtr window, Keys key, int scanCode, InputState state, ModifierKeys mods);
110+
public delegate void KeyCallback(Window window, Keys key, int scanCode, InputState state, ModifierKeys mods);
111111

112112
/// <summary>
113113
/// This is the function signature for joystick configuration callback functions.
@@ -140,13 +140,13 @@ public delegate void MouseButtonCallback(IntPtr window, MouseButton button, Inpu
140140
/// <param name="xScale">The new x-axis content scale of the window.</param>
141141
/// <param name="yScale">The new y-axis content scale of the window.</param>
142142
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
143-
public delegate void WindowContentsScaleCallback(IntPtr window, float xScale, float yScale);
143+
public delegate void WindowContentsScaleCallback(Window window, float xScale, float yScale);
144144

145145
/// <summary>
146146
/// This is the function signature for window maximize/restore callback functions.
147147
/// </summary>
148148
/// <param name="window">The window that was maximized or restored.</param>
149149
/// <param name="maximized"><c>true</c> if the window was maximized, or <c>false</c> if it was restored.</param>
150150
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
151-
public delegate void WindowMaximizedCallback(IntPtr window, bool maximized);
151+
public delegate void WindowMaximizedCallback(Window window, bool maximized);
152152
}

GLFW.NET/GLFW.NET.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>netstandard2.1</TargetFramework>
55
<RootNamespace>GLFW</RootNamespace>
66
<Authors>Eric Freed</Authors>
77
<AssemblyVersion>1.0.1.0</AssemblyVersion>

GLFW.NET/NativeWindow.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.ComponentModel;
3-
using System.Diagnostics.CodeAnalysis;
43
using System.Drawing;
54
using System.Runtime.InteropServices;
65
using JetBrains.Annotations;
@@ -230,7 +229,7 @@ public void RequestAttention()
230229
/// <value>
231230
/// The clipboard string.
232231
/// </value>
233-
[NotNull]
232+
[JetBrains.Annotations.NotNull]
234233
public string Clipboard
235234
{
236235
get => Glfw.GetClipboardString(Window);
@@ -698,7 +697,7 @@ public void SetAspectRatio(int numerator, int denominator)
698697
/// <para>Standard sizes are 16x16, 32x32, and 48x48.</para>
699698
/// </summary>
700699
/// <param name="images">One or more images to set as an icon.</param>
701-
public void SetIcons([NotNull] params Image[] images)
700+
public void SetIcons([JetBrains.Annotations.NotNull] params Image[] images)
702701
{
703702
Glfw.SetWindowIcon(Window, images.Length, images);
704703
}
@@ -977,7 +976,7 @@ protected virtual void OnClosing()
977976
/// Raises the <see cref="FileDrop" /> event.
978977
/// </summary>
979978
/// <param name="paths">The filenames of the dropped files.</param>
980-
protected virtual void OnFileDrop([NotNull] string[] paths)
979+
protected virtual void OnFileDrop([JetBrains.Annotations.NotNull] string[] paths)
981980
{
982981
FileDrop?.Invoke(this, new FileDropEventArgs(paths));
983982
}
@@ -1073,7 +1072,7 @@ protected virtual void OnMouseScroll(double x, double y)
10731072
/// </summary>
10741073
/// <param name="x">The new position on the x-axis.</param>
10751074
/// <param name="y">The new position on the y-axis.</param>
1076-
[SuppressMessage("ReSharper", "UnusedParameter.Global")]
1075+
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "UnusedParameter.Global")]
10771076
protected virtual void OnPositionChanged(double x, double y)
10781077
{
10791078
PositionChanged?.Invoke(this, EventArgs.Empty);

GLFW.NET/Structs/Window.cs

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ public struct Window : IEquatable<Window>
2727
/// The result of the conversion.
2828
/// </returns>
2929
public static implicit operator IntPtr(Window window) { return window.handle; }
30+
31+
/// <summary>
32+
/// Performs an explicit conversion from <see cref="IntPtr"/> to <see cref="Window"/>.
33+
/// </summary>
34+
/// <param name="handle">A pointer representing the window handle.</param>
35+
/// <returns>The result of the conversion.</returns>
36+
public static explicit operator Window(IntPtr handle) => new Window(handle);
37+
38+
/// <summary>
39+
/// Creates a new instance of the <see cref="Window"/> struct.
40+
/// </summary>
41+
/// <param name="handle">A pointer representing the window handle.</param>
42+
public Window(IntPtr handle)
43+
{
44+
this.handle = handle;
45+
}
3046

3147
/// <summary>
3248
/// Returns a <see cref="System.String" /> that represents this instance.

0 commit comments

Comments
 (0)