@@ -5,96 +5,101 @@ namespace DotGLFW.Example;
55
66public class Program
77{
8- private const int GL_COLOR_BUFFER_BIT = 0x00004000 ;
9- private delegate void glClearColorHandler ( float r , float g , float b , float a ) ;
10- private delegate void glClearHandler ( int mask ) ;
11- private static glClearColorHandler glClearColor ;
12- private static glClearHandler glClear ;
8+ private const int GL_COLOR_BUFFER_BIT = 0x00004000 ;
139
14- static void Main ( string [ ] args )
15- {
16- Glfw . Init ( ) ;
17-
18- // Set some common hints for the OpenGL profile creation
19- Glfw . WindowHint ( Hint . ClientAPI , ClientAPI . OpenGLAPI ) ;
20- Glfw . WindowHint ( Hint . ContextVersionMajor , 3 ) ;
21- Glfw . WindowHint ( Hint . ContextVersionMinor , 3 ) ;
22- Glfw . WindowHint ( Hint . OpenGLProfile , OpenGLProfile . CoreProfile ) ;
23- Glfw . WindowHint ( Hint . DoubleBuffer , true ) ;
24- Glfw . WindowHint ( Hint . Decorated , true ) ;
25- Glfw . WindowHint ( Hint . OpenGLForwardCompat , true ) ;
26- Glfw . WindowHint ( Hint . Resizable , false ) ;
10+ private delegate void glClearColorHandler ( float r , float g , float b , float a ) ;
11+ private delegate void glClearHandler ( int mask ) ;
12+ private static glClearColorHandler glClearColor ;
13+ private static glClearHandler glClear ;
2714
28- var WIDTH = 800 ;
29- var HEIGHT = 600 ;
30- var TITLE = "DotGLFW Example" ;
15+ static void Main ( string [ ] args )
16+ {
17+ Glfw . Init ( ) ;
3118
32- // Create window
33- var window = Glfw . CreateWindow ( WIDTH , HEIGHT , TITLE , Monitor . NULL , Window . NULL ) ;
34- Glfw . MakeContextCurrent ( window ) ;
19+ var versionString = Glfw . GetVersionString ( ) ;
20+ Console . WriteLine ( $ "GLFW Version: { versionString } ") ;
3521
36- // Enable VSYNC
37- Glfw . SwapInterval ( 1 ) ;
22+ Glfw . WindowHint ( WindowHint . ClientAPI , ClientAPI . OpenGLAPI ) ;
23+ Glfw . WindowHint ( WindowHint . ContextVersionMajor , 3 ) ;
24+ Glfw . WindowHint ( WindowHint . ContextVersionMinor , 3 ) ;
25+ Glfw . WindowHint ( WindowHint . OpenGLProfile , OpenGLProfile . CoreProfile ) ;
26+ Glfw . WindowHint ( WindowHint . DoubleBuffer , true ) ;
27+ Glfw . WindowHint ( WindowHint . Decorated , true ) ;
28+ Glfw . WindowHint ( WindowHint . OpenGLForwardCompat , true ) ;
29+ Glfw . WindowHint ( WindowHint . Resizable , true ) ;
30+ Glfw . WindowHint ( WindowHint . Visible , false ) ;
3831
39- var primaryMonitor = Glfw . GetPrimaryMonitor ( ) ;
40- Glfw . GetMonitorWorkarea ( primaryMonitor , out var x , out var y , out var width , out var height ) ;
41- VideoMode primaryVideoMode = Glfw . GetVideoMode ( primaryMonitor ) ;
32+ var WIDTH = 800 ;
33+ var HEIGHT = 600 ;
34+ var TITLE = "DotGLFW Example" ;
4235
43- int refreshRate = primaryVideoMode . RefreshRate ;
44- double delta = 1.0 / refreshRate ;
45- Console . WriteLine ( $ "Refresh rate: { refreshRate } Hz" ) ;
36+ // Create window
37+ var window = Glfw . CreateWindow ( WIDTH , HEIGHT , TITLE , null , null ) ;
38+ var primary = Glfw . GetPrimaryMonitor ( ) ;
4639
47- // Find center position based on window and monitor sizes
48- Glfw . SetWindowPos ( window , width / 2 - WIDTH / 2 , height / 2 - HEIGHT / 2 ) ;
40+ Glfw . GetMonitorWorkarea ( primary , out var wx , out var wy , out var ww , out var wh ) ;
41+ Glfw . SetWindowPos ( window , wx + ww / 2 - WIDTH / 2 , wy + wh / 2 - HEIGHT / 2 ) ;
42+ Glfw . ShowWindow ( window ) ;
4943
50- glClearColor = Marshal . GetDelegateForFunctionPointer < glClearColorHandler > ( Glfw . GetProcAddress ( "glClearColor" ) ) ;
51- glClear = Marshal . GetDelegateForFunctionPointer < glClearHandler > ( Glfw . GetProcAddress ( "glClear" ) ) ;
44+ Glfw . MakeContextCurrent ( window ) ;
5245
53- Glfw . SetKeyCallback ( window , ( window , key , scancode , action , mods ) =>
54- {
55- Console . WriteLine ( $ "Key: { key } , Scancode: { scancode } , Action: { action } , Mods: { mods } ") ;
56- } ) ;
46+ // Enable VSYNC
47+ Glfw . SwapInterval ( 1 ) ;
5748
58- Glfw . SetWindowIcon ( window , [ CreateIcon ( ) ] ) ;
49+ var videoMode = Glfw . GetVideoMode ( primary ) ;
50+ int refreshRate = videoMode . RefreshRate ;
51+ double delta = 1.0 / refreshRate ;
5952
60- GC . Collect ( ) ;
53+ glClearColor = Marshal . GetDelegateForFunctionPointer < glClearColorHandler > (
54+ Glfw . GetProcAddress ( "glClearColor" ) ) ;
55+ glClear = Marshal . GetDelegateForFunctionPointer < glClearHandler > (
56+ Glfw . GetProcAddress ( "glClear" ) ) ;
6157
62- while ( ! Glfw . WindowShouldClose ( window ) )
63- {
64- Glfw . PollEvents ( ) ;
65- Glfw . SwapBuffers ( window ) ;
58+ Glfw . SetWindowIcon ( window , [ CreateIcon ( ) ] ) ;
6659
67- double currentTime = Glfw . GetTime ( ) ;
68- SetHueShiftedColor ( currentTime * delta * 200 ) ;
60+ Glfw . SetKeyCallback ( window , ( window , key , scancode , action , mods ) =>
61+ {
62+ Console . WriteLine ( $ "Key: { key } , Scancode: { scancode } , Action: { action } , Mods: { mods } ") ;
63+ } ) ;
6964
70- // Clear the buffer to the set color
71- glClear ( GL_COLOR_BUFFER_BIT ) ;
72- }
73- }
65+ GC . Collect ( ) ;
7466
75- private static void SetHueShiftedColor ( double time )
67+ while ( ! Glfw . WindowShouldClose ( window ) )
7668 {
77- // Set the clear color to a shifted hue
78- float r = ( float ) ( Math . Sin ( time ) / 2 + 0.5 ) ;
79- float g = ( float ) ( Math . Sin ( time + 2 ) / 2 + 0.5 ) ;
80- float b = ( float ) ( Math . Sin ( time + 4 ) / 2 + 0.5 ) ;
81- float a = 1.0f ;
82- glClearColor ( r , g , b , a ) ;
83- }
69+ Glfw . PollEvents ( ) ;
70+ Glfw . SwapBuffers ( window ) ;
8471
85- private static Image CreateIcon ( )
86- {
87- var image = new Image
88- {
89- Width = 2 ,
90- Height = 2 ,
91- Pixels = [
92- 0 , 0 , 0 , 255 ,
93- 255 , 0 , 0 , 255 ,
94- 0 , 255 , 0 , 255 ,
95- 0 , 0 , 255 , 255
96- ]
97- } ;
98- return image ;
72+ double currentTime = Glfw . GetTime ( ) ;
73+ SetHueShiftedColor ( currentTime * delta * 200 ) ;
74+
75+ // Clear the buffer to the set color
76+ glClear ( GL_COLOR_BUFFER_BIT ) ;
9977 }
78+ }
79+
80+ private static void SetHueShiftedColor ( double time )
81+ {
82+ // Set the clear color to a shifted hue
83+ float r = ( float ) ( Math . Sin ( time ) / 2 + 0.5 ) ;
84+ float g = ( float ) ( Math . Sin ( time + 2 ) / 2 + 0.5 ) ;
85+ float b = ( float ) ( Math . Sin ( time + 4 ) / 2 + 0.5 ) ;
86+ float a = 1.0f ;
87+ glClearColor ( r , g , b , a ) ;
88+ }
89+
90+ private static Image CreateIcon ( )
91+ {
92+ var image = new Image
93+ {
94+ Width = 2 ,
95+ Height = 2 ,
96+ Pixels = [
97+ 0 , 0 , 0 , 255 ,
98+ 255 , 0 , 0 , 255 ,
99+ 0 , 255 , 0 , 255 ,
100+ 0 , 0 , 255 , 255
101+ ]
102+ } ;
103+ return image ;
104+ }
100105}
0 commit comments