This repository has been archived by the owner on Nov 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTestReopen.cs
156 lines (128 loc) · 4.49 KB
/
TestReopen.cs
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
namespace Glfw3.Tests
{
using OpenGL;
using System;
/// <summary>
/// This test closes and re-opens the GLFW window every five seconds, alternating between
/// windowed and full screen mode. It also times and logs opening and closing actions and
/// attempts to separate user initiated window closing from its own.
/// </summary>
/// <remarks>
/// Ported from <c>reopen.c</c>.
/// </remarks>
class TestReopen : TestBase
{
static void FramebufferSizeCallback(Glfw.Window window, int width, int height)
{
Gl.Viewport(0, 0, width, height);
}
static void WindowCloseCallback(Glfw.Window window)
{
Log("Close callback triggered");
}
static void KeyCallback(Glfw.Window window, Glfw.KeyCode key, int scancode, Glfw.InputState state, Glfw.KeyMods mods)
{
if (state != Glfw.InputState.Press)
return;
switch (key)
{
case Glfw.KeyCode.Q:
case Glfw.KeyCode.Escape:
Glfw.SetWindowShouldClose(window, true);
break;
}
}
static Glfw.Window OpenWindow(int width, int height, Glfw.Monitor monitor)
{
double b;
Glfw.Window window;
b = Glfw.GetTime();
window = Glfw.CreateWindow(width, height, "Window Re-opener", monitor, Glfw.Window.None);
if (!window)
return Glfw.Window.None;
Glfw.MakeContextCurrent(window);
Glfw.SwapInterval(1);
Glfw.SetFramebufferSizeCallback(window, FramebufferSizeCallback);
Glfw.SetWindowCloseCallback(window, WindowCloseCallback);
Glfw.SetKeyCallback(window, KeyCallback);
if (monitor)
{
Log("Opening full screen window on monitor {0} took {1} seconds",
Glfw.GetMonitorName(monitor),
Glfw.GetTime() - b);
}
else
{
Log("Opening regular window took {0} seconds",
Glfw.GetTime() - b);
}
return window;
}
static void CloseWindow(Glfw.Window window)
{
double b = Glfw.GetTime();
Glfw.DestroyWindow(window);
Log("Closing window took {0} seconds", Glfw.GetTime() - b);
}
static void Main(string[] args)
{
Init();
int count = 0;
Glfw.Window window;
var rand = new Random();
if (!Glfw.Init())
Environment.Exit(1);
for (;;)
{
int width, height;
var monitor = Glfw.Monitor.None;
if (count % 2 == 0)
{
var monitors = Glfw.GetMonitors();
monitor = monitors[rand.Next() % monitors.Length];
}
if (monitor)
{
var mode = Glfw.GetVideoMode(monitor);
width = mode.Width;
height = mode.Height;
}
else
{
width = 640;
height = 480;
}
window = OpenWindow(width, height, monitor);
if (!window)
{
Glfw.Terminate();
Environment.Exit(1);
}
Gl.MatrixMode(MatrixMode.Projection);
Gl.Ortho(-1f, 1f, -1f, 1f, 1f, -1f);
Gl.MatrixMode(MatrixMode.Modelview);
Glfw.SetTime(0.0);
while (Glfw.GetTime() < 5.0)
{
Gl.Clear(ClearBufferMask.ColorBufferBit);
Gl.PushMatrix();
Gl.Rotate((float)Glfw.GetTime() * 100f, 0f, 0f, 1f);
Gl.Rect(-0.5f, -0.5f, 1f, 1f);
Gl.PopMatrix();
Glfw.SwapBuffers(window);
Glfw.PollEvents();
if (Glfw.WindowShouldClose(window))
{
CloseWindow(window);
Log("User closed window");
Glfw.Terminate();
return;
}
}
Log("Closing window");
CloseWindow(window);
count++;
}
}
}
}