static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (action != GLFW_PRESS) return; switch (key) { case GLFW_KEY_UP: { if (swap_interval + 1 > swap_interval) set_swap_interval(window, swap_interval + 1); break; } case GLFW_KEY_DOWN: { if (swap_tear) { if (swap_interval - 1 < swap_interval) set_swap_interval(window, swap_interval - 1); } else { if (swap_interval - 1 >= 0) set_swap_interval(window, swap_interval - 1); } break; } case GLFW_KEY_ESCAPE: glfwSetWindowShouldClose(window, 1); break; } }
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (action != GLFW_PRESS) return; switch (key) { case GLFW_KEY_UP: { if (swap_interval + 1 > swap_interval) set_swap_interval(window, swap_interval + 1); break; } case GLFW_KEY_DOWN: { if (swap_tear) { if (swap_interval - 1 < swap_interval) set_swap_interval(window, swap_interval - 1); } else { if (swap_interval - 1 >= 0) set_swap_interval(window, swap_interval - 1); } break; } case GLFW_KEY_ESCAPE: glfwSetWindowShouldClose(window, 1); break; case GLFW_KEY_F11: case GLFW_KEY_ENTER: { static int x, y, width, height; if (mods != GLFW_MOD_ALT) return; if (glfwGetWindowMonitor(window)) glfwSetWindowMonitor(window, NULL, x, y, width, height, 0); else { GLFWmonitor* monitor = glfwGetPrimaryMonitor(); const GLFWvidmode* mode = glfwGetVideoMode(monitor); glfwGetWindowPos(window, &x, &y); glfwGetWindowSize(window, &width, &height); glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); } break; } } }
int main(void) { float position; unsigned long frame_count = 0; double last_time, current_time; GLFWwindow* window; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(640, 480, "", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); set_swap_interval(window, 0); last_time = glfwGetTime(); frame_rate = 0.0; glfwSetWindowSizeCallback(window, window_size_callback); glfwSetKeyCallback(window, key_callback); glMatrixMode(GL_PROJECTION); glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f); glMatrixMode(GL_MODELVIEW); while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT); position = cosf((float) glfwGetTime() * 4.f) * 0.75f; glRectf(position - 0.25f, -1.f, position + 0.25f, 1.f); glfwSwapBuffers(window); glfwPollEvents(); frame_count++; current_time = glfwGetTime(); if (current_time - last_time > 1.0) { frame_rate = frame_count / (current_time - last_time); frame_count = 0; last_time = current_time; update_window_title(window); } } glfwTerminate(); exit(EXIT_SUCCESS); }
int main(void) { GLFWwindow window; int width, height; if (!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError())); exit(EXIT_FAILURE); } window = glfwCreateWindow(window_width, window_height, GLFW_WINDOWED, "", NULL); if (!window) { glfwTerminate(); fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError())); exit(EXIT_FAILURE); } glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetWindowSizeCallback(window, window_size_callback); glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); glfwGetWindowSize(window, &width, &height); window_size_callback(window, width, height); set_swap_interval(window, swap_interval); while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINES); glVertex2f(0.f, (GLfloat) window_height - cursor_y); glVertex2f((GLfloat) window_width, (GLfloat) window_height - cursor_y); glVertex2f((GLfloat) cursor_x, 0.f); glVertex2f((GLfloat) cursor_x, (GLfloat) window_height); glEnd(); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); exit(EXIT_SUCCESS); }
int main(void) { GLFWwindow* window; int width, height; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(window_width, window_height, "", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); glfwGetFramebufferSize(window, &width, &height); framebuffer_size_callback(window, width, height); set_swap_interval(window, swap_interval); while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINES); glVertex2f(0.f, (GLfloat) window_height - cursor_y); glVertex2f((GLfloat) window_width, (GLfloat) window_height - cursor_y); glVertex2f((GLfloat) cursor_x, 0.f); glVertex2f((GLfloat) cursor_x, (GLfloat) window_height); glEnd(); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); exit(EXIT_SUCCESS); }
int main(void) { float position; GLFWwindow window; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(640, 480, GLFW_WINDOWED, "", NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); set_swap_interval(window, swap_interval); glfwSetWindowSizeCallback(window, window_size_callback); glfwSetKeyCallback(window, key_callback); glMatrixMode(GL_PROJECTION); glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f); glMatrixMode(GL_MODELVIEW); while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { glClear(GL_COLOR_BUFFER_BIT); position = cosf(glfwGetTime() * 4.f) * 0.75f; glRectf(position - 0.25f, -1.f, position + 0.25f, 1.f); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); exit(EXIT_SUCCESS); }
int main(int argc, char** argv) { unsigned long frame_count = 0; double last_time, current_time; GLFWwindow* window; GLuint vertex_buffer, vertex_shader, fragment_shader, program; GLint mvp_location, vpos_location; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); window = glfwCreateWindow(640, 480, "Tearing detector", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); gladLoadGL(glfwGetProcAddress); set_swap_interval(window, 0); last_time = glfwGetTime(); frame_rate = 0.0; swap_tear = (glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear")); glfwSetKeyCallback(window, key_callback); glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); glCompileShader(vertex_shader); fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); glCompileShader(fragment_shader); program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); mvp_location = glGetUniformLocation(program, "MVP"); vpos_location = glGetAttribLocation(program, "vPos"); glEnableVertexAttribArray(vpos_location); glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (void*) 0); while (!glfwWindowShouldClose(window)) { int width, height; mat4x4 m, p, mvp; float position = cosf((float) glfwGetTime() * 4.f) * 0.75f; glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); mat4x4_ortho(p, -1.f, 1.f, -1.f, 1.f, 0.f, 1.f); mat4x4_translate(m, position, 0.f, 0.f); mat4x4_mul(mvp, p, m); glUseProgram(program); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glfwSwapBuffers(window); glfwPollEvents(); frame_count++; current_time = glfwGetTime(); if (current_time - last_time > 1.0) { frame_rate = frame_count / (current_time - last_time); frame_count = 0; last_time = current_time; update_window_title(window); } } glfwTerminate(); exit(EXIT_SUCCESS); }
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) set_swap_interval(window, 1 - swap_interval); }
int main(int argc, char** argv) { int ch, width, height; float position; unsigned long frame_count = 0; double last_time, current_time; GLboolean fullscreen = GL_FALSE; GLFWmonitor* monitor = NULL; GLFWwindow* window; while ((ch = getopt(argc, argv, "fh")) != -1) { switch (ch) { case 'h': usage(); exit(EXIT_SUCCESS); case 'f': fullscreen = GL_TRUE; break; } } glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); if (fullscreen) { const GLFWvidmode* mode; monitor = glfwGetPrimaryMonitor(); mode = glfwGetVideoMode(monitor); glfwWindowHint(GLFW_RED_BITS, mode->redBits); glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits); glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits); glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate); width = mode->width; height = mode->height; } else { width = 640; height = 480; } window = glfwCreateWindow(width, height, "", monitor, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); set_swap_interval(window, 0); last_time = glfwGetTime(); frame_rate = 0.0; swap_tear = (glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear")); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetKeyCallback(window, key_callback); glMatrixMode(GL_PROJECTION); glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f); glMatrixMode(GL_MODELVIEW); while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT); position = cosf((float) glfwGetTime() * 4.f) * 0.75f; glRectf(position - 0.25f, -1.f, position + 0.25f, 1.f); glfwSwapBuffers(window); glfwPollEvents(); frame_count++; current_time = glfwGetTime(); if (current_time - last_time > 1.0) { frame_rate = frame_count / (current_time - last_time); frame_count = 0; last_time = current_time; update_window_title(window); } } glfwTerminate(); exit(EXIT_SUCCESS); }