-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
105 lines (84 loc) · 3.06 KB
/
Program.cs
File metadata and controls
105 lines (84 loc) · 3.06 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
using System;
using System.Runtime.InteropServices;
namespace DotGLFW.Example;
public class Program
{
private const int GL_COLOR_BUFFER_BIT = 0x00004000;
private delegate void glClearColorHandler(float r, float g, float b, float a);
private delegate void glClearHandler(int mask);
private static glClearColorHandler glClearColor;
private static glClearHandler glClear;
static void Main(string[] args)
{
Glfw.Init();
var versionString = Glfw.GetVersionString();
Console.WriteLine($"GLFW Version: {versionString}");
Glfw.WindowHint(WindowHint.ClientAPI, ClientAPI.OpenGLAPI);
Glfw.WindowHint(WindowHint.ContextVersionMajor, 3);
Glfw.WindowHint(WindowHint.ContextVersionMinor, 3);
Glfw.WindowHint(WindowHint.OpenGLProfile, OpenGLProfile.CoreProfile);
Glfw.WindowHint(WindowHint.DoubleBuffer, true);
Glfw.WindowHint(WindowHint.Decorated, true);
Glfw.WindowHint(WindowHint.OpenGLForwardCompat, true);
Glfw.WindowHint(WindowHint.Resizable, true);
Glfw.WindowHint(WindowHint.Visible, false);
var WIDTH = 800;
var HEIGHT = 600;
var TITLE = "DotGLFW Example";
// Create window
var window = Glfw.CreateWindow(WIDTH, HEIGHT, TITLE, null, null);
var primary = Glfw.GetPrimaryMonitor();
Glfw.GetMonitorWorkarea(primary, out var wx, out var wy, out var ww, out var wh);
Glfw.SetWindowPos(window, wx + ww / 2 - WIDTH / 2, wy + wh / 2 - HEIGHT / 2);
Glfw.ShowWindow(window);
Glfw.MakeContextCurrent(window);
// Enable VSYNC
Glfw.SwapInterval(1);
var videoMode = Glfw.GetVideoMode(primary);
int refreshRate = videoMode.RefreshRate;
double delta = 1.0 / refreshRate;
glClearColor = Marshal.GetDelegateForFunctionPointer<glClearColorHandler>(
Glfw.GetProcAddress("glClearColor"));
glClear = Marshal.GetDelegateForFunctionPointer<glClearHandler>(
Glfw.GetProcAddress("glClear"));
Glfw.SetWindowIcon(window, [CreateIcon()]);
Glfw.SetKeyCallback(window, (window, key, scancode, action, mods) =>
{
Console.WriteLine($"Key: {key}, Scancode: {scancode}, Action: {action}, Mods: {mods}");
});
GC.Collect();
while (!Glfw.WindowShouldClose(window))
{
Glfw.PollEvents();
Glfw.SwapBuffers(window);
double currentTime = Glfw.GetTime();
SetHueShiftedColor(currentTime * delta * 200);
// Clear the buffer to the set color
glClear(GL_COLOR_BUFFER_BIT);
}
}
private static void SetHueShiftedColor(double time)
{
// Set the clear color to a shifted hue
float r = (float)(Math.Sin(time) / 2 + 0.5);
float g = (float)(Math.Sin(time + 2) / 2 + 0.5);
float b = (float)(Math.Sin(time + 4) / 2 + 0.5);
float a = 1.0f;
glClearColor(r, g, b, a);
}
private static Image CreateIcon()
{
var image = new Image
{
Width = 2,
Height = 2,
Pixels = [
0, 0, 0, 255,
255, 0, 0, 255,
0, 255, 0, 255,
0, 0, 255, 255
]
};
return image;
}
}