-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvsync.c
More file actions
180 lines (149 loc) · 3.54 KB
/
Copy pathvsync.c
File metadata and controls
180 lines (149 loc) · 3.54 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
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
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include "../miniglut.h"
#if defined(unix) || defined(__unix__)
#define BUILD_UNIX
#include <unistd.h>
#elif defined(_WIN32)
#define BUILD_WIN32
#include <windows.h>
#endif
void display(void);
void reshape(int x, int y);
void keypress(unsigned char key, int x, int y);
int interv = 1;
int win_width, win_height;
long delay;
const char *exttext[8];
int exttext_width;
const char *check_ext[] = {
#ifdef BUILD_UNIX
"GLX_EXT_swap_control", "GLX_EXT_swap_control_tear",
"GLX_MESA_swap_control", "GLX_SGI_swap_control",
#endif
#ifdef BUILD_WIN32
"WGL_EXT_swap_control", "WGL_EXT_swap_control_tear",
#endif
0
};
int main(int argc, char **argv)
{
int i, len, n = 0;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow("vsync test");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(glutPostRedisplay);
glutKeyboardFunc(keypress);
exttext[n++] = "extensions:";
exttext_width = glutBitmapLength(GLUT_BITMAP_HELVETICA_12, exttext[0]);
for(i=0; check_ext[i]; i++) {
if(glutExtensionSupported(check_ext[i])) {
exttext[n++] = check_ext[i];
len = glutBitmapLength(GLUT_BITMAP_HELVETICA_12, check_ext[i]);
if(len > exttext_width) {
exttext_width = len;
}
}
}
glutSwapInterval(interv);
glutMainLoop();
return 0;
}
void mglut_sincos(float angle, float *sptr, float *cptr);
void display(void)
{
static long nframes, prev_fps_upd;
static char fpsbuf[128], text[128];
int i;
long upd_time, msec = glutGet(GLUT_ELAPSED_TIME);
float t = (float)msec / 1000.0f;
float sa, ca;
mglut_sincos(t * 8.0f, &sa, &ca);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(sa, 0, 0);
glScalef(0.5, 0.5, 0.5);
glBegin(GL_QUADS);
glColor3f(1.0, 0.3, 0.2);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glVertex2f(1, 1);
glVertex2f(-1, 1);
glEnd();
if((upd_time = msec - prev_fps_upd) >= 1000) {
float fps = nframes * 1000.0f / (float)upd_time;
nframes = 0;
prev_fps_upd = msec;
sprintf(fpsbuf, "%.1f fps", fps);
}
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, win_width, 0, win_height, -1, 1);
glColor3f(0.2, 1, 0.2);
glRasterPos2i(5, win_height - 20);
sprintf(text, "swap interval: %d", interv);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, text);
glRasterPos2i(5, win_height - 40);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, fpsbuf);
if(delay) {
glRasterPos2i(5, win_height - 60);
sprintf(text, "delay: %ld ms", delay);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, text);
}
if(exttext[1]) {
for(i=0; exttext[i]; i++) {
glRasterPos2i(win_width - exttext_width - 5, win_height - i * 16 - 16);
glutBitmapString(GLUT_BITMAP_HELVETICA_12, exttext[i]);
}
}
glPopMatrix();
if(delay) {
#if defined(unix) || defined(__unix__)
usleep(delay * 1000);
#elif defined(_WIN32)
Sleep(delay);
#endif
}
glutSwapBuffers();
nframes++;
}
void reshape(int x, int y)
{
float aspect = (float)x / (float)y;
win_width = x;
win_height= y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-aspect, aspect, -1, 1, -1, 1);
}
void keypress(unsigned char key, int x, int y)
{
switch(key) {
case 27:
exit(0);
case '=':
delay += 5;
break;
case '-':
delay -= 5;
if(delay < 0) delay = 0;
break;
default:
if(key >= '0' && key <= '9') {
interv = key - '0';
if(glutGetModifiers() & GLUT_ACTIVE_CTRL) {
interv = -interv;
}
glutSwapInterval(interv);
}
}
}