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
/
TestTearing.cs
177 lines (147 loc) · 5.44 KB
/
TestTearing.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
namespace Glfw3.Tests
{
using CommandLine;
using CommandLine.Text;
using OpenGL;
using System;
/// <summary>
/// This test renders a high contrast, horizontally moving bar, allowing for visual verification
/// of whether the set swap interval is indeed obeyed.
/// </summary>
/// <remarks>
/// Ported from <c>tearing.c</c>.
/// </remarks>
class TestTearing : TestBase
{
static bool swapTear;
static int swapInterval;
static double frameRate;
class Options
{
[Option('f', HelpText = "create full screen window")]
public bool Fullscreen { get; set; }
[HelpOption(HelpText = "Display this help screen.")]
public string GetUsage()
{
return HelpText.AutoBuild(this,
(HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
static void UpdateWindowTitle(Glfw.Window window)
{
string title = string.Format("Tearing detector (interval {0}{1}, {2} Hz)",
swapInterval,
(swapTear && swapInterval < 0) ? " (swap tear)" : "",
frameRate);
Glfw.SetWindowTitle(window, title);
}
static void SetSwapInterval(Glfw.Window window, int interval)
{
swapInterval = interval;
Glfw.SwapInterval(swapInterval);
UpdateWindowTitle(window);
}
static void FramebufferSizeCallback(Glfw.Window window, int width, int height)
{
Gl.Viewport(0, 0, width, height);
}
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.Up:
{
if (swapInterval + 1 > swapInterval)
SetSwapInterval(window, swapInterval + 1);
break;
}
case Glfw.KeyCode.Down:
{
if (swapTear)
{
if (swapInterval - 1 < swapInterval)
SetSwapInterval(window, swapInterval - 1);
}
else
{
if (swapInterval - 1 >= 0)
SetSwapInterval(window, swapInterval - 1);
}
break;
}
case Glfw.KeyCode.Escape:
Glfw.SetWindowShouldClose(window, true);
break;
}
}
static void Main(string[] args)
{
Init();
var options = new Options();
int width, height;
float position;
ulong frameCount = 0;
double lastTime, currentTime;
bool fullscreen = false;
var monitor = Glfw.Monitor.None;
Glfw.Window window;
if (Parser.Default.ParseArguments(args, options))
fullscreen = options.Fullscreen;
if (!Glfw.Init())
Environment.Exit(1);
if (fullscreen)
{
monitor = Glfw.GetPrimaryMonitor();
var mode = Glfw.GetVideoMode(monitor);
Glfw.WindowHint(Glfw.Hint.RedBits, mode.RedBits);
Glfw.WindowHint(Glfw.Hint.GreenBits, mode.GreenBits);
Glfw.WindowHint(Glfw.Hint.BlueBits, mode.BlueBits);
Glfw.WindowHint(Glfw.Hint.RefreshRate, mode.RefreshRate);
width = mode.Width;
height = mode.Height;
}
else
{
width = 640;
height = 480;
}
window = Glfw.CreateWindow(width, height, "", monitor, Glfw.Window.None);
if (!window)
{
Glfw.Terminate();
Environment.Exit(1);
}
Glfw.MakeContextCurrent(window);
SetSwapInterval(window, 0);
lastTime = Glfw.GetTime();
frameRate = 0.0;
swapTear = (Glfw.ExtensionSupported("WGL_EXT_swap_control_tear") ||
Glfw.ExtensionSupported("GLX_EXT_swap_control_tear"));
Glfw.SetFramebufferSizeCallback(window, FramebufferSizeCallback);
Glfw.SetKeyCallback(window, KeyCallback);
Gl.MatrixMode(MatrixMode.Projection);
Gl.Ortho(-1f, 1f, -1f, 1f, 1f, -1f);
Gl.MatrixMode(MatrixMode.Modelview);
while (!Glfw.WindowShouldClose(window))
{
Gl.Clear(ClearBufferMask.ColorBufferBit);
position = (float)System.Math.Cos(Glfw.GetTime() * 4f) * 0.75f;
Gl.Rect(position - 0.25f, -1f, position + 0.25f, 1f);
Glfw.SwapBuffers(window);
Glfw.PollEvents();
frameCount++;
currentTime = Glfw.GetTime();
if (currentTime - lastTime > 1.0)
{
frameRate = frameCount / (currentTime - lastTime);
frameCount = 0;
lastTime = currentTime;
UpdateWindowTitle(window);
}
}
Glfw.Terminate();
}
}
}