1 //========================================================================
2 // Vsync enabling test
3 // Copyright (c) Camilla Berglund <elmindreda@elmindreda.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 //    claim that you wrote the original software. If you use this software
15 //    in a product, an acknowledgment in the product documentation would
16 //    be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 //    be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 //    distribution.
23 //
24 //========================================================================
25 //
26 // This test renders a high contrast, horizontally moving bar, allowing for
27 // visual verification of whether the set swap interval is indeed obeyed
28 //
29 //========================================================================
30 
31 #include <GL/glfw.h>
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <math.h>
36 
37 static int swap_interval;
38 
set_swap_interval(int interval)39 static void set_swap_interval(int interval)
40 {
41     char title[256];
42 
43     swap_interval = interval;
44     glfwSwapInterval(swap_interval);
45 
46     sprintf(title, "Tearing detector (interval %i)", swap_interval);
47 
48     glfwSetWindowTitle(title);
49 }
50 
key_callback(int key,int action)51 static void key_callback(int key, int action)
52 {
53     if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
54         set_swap_interval(1 - swap_interval);
55 }
56 
window_size_callback(int width,int height)57 static void GLFWCALL window_size_callback(int width, int height)
58 {
59     glViewport(0, 0, width, height);
60 }
61 
main(void)62 int main(void)
63 {
64     float position;
65 
66     if (!glfwInit())
67     {
68         fprintf(stderr, "Failed to initialize GLFW\n");
69         exit(1);
70     }
71 
72     if (!glfwOpenWindow(0, 0, 0, 0, 0, 0, 0, 0, GLFW_WINDOW))
73     {
74         glfwTerminate();
75 
76         fprintf(stderr, "Failed to open GLFW window\n");
77         exit(1);
78     }
79 
80     glfwSetKeyCallback(key_callback);
81     glfwSetWindowSizeCallback(window_size_callback);
82     set_swap_interval(0);
83 
84     glClearColor(0.f, 0.f, 0.f, 0.f);
85     glColor3f(1.f, 1.f, 1.f);
86 
87     glMatrixMode(GL_PROJECTION);
88     glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f);
89     glMatrixMode(GL_MODELVIEW);
90 
91     while (glfwGetWindowParam(GLFW_OPENED) == GL_TRUE)
92     {
93         glClear(GL_COLOR_BUFFER_BIT);
94 
95         position = cosf(glfwGetTime() * 4.f) * 0.75f;
96         glRectf(position - 0.25f, -1.f, position + 0.25f, 1.f);
97 
98         glfwSwapBuffers();
99     }
100 
101     glfwTerminate();
102     exit(0);
103 }
104 
105