1 #include "glfw_helpers.h"
2 #include <GLFW/glfw3.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 using namespace GlfwHelpers;
8
9 static GLFWwindow *window;
10
die(const char * msg)11 static void die(const char *msg) {
12 fprintf(stderr, "%s\n", msg);
13 exit(EXIT_FAILURE);
14 }
15
error_callback(int error,const char * description)16 static void error_callback(int error, const char *description) {
17 die(description);
18 }
19
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)20 static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
21 if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
22 glfwSetWindowShouldClose(window, GL_TRUE);
23 }
24
25 static bool first_focus = false;
focus_callback(GLFWwindow * window,int)26 static void focus_callback(GLFWwindow *window, int) {
27 first_focus = true;
28 }
29
setup(int width,int height)30 struct info GlfwHelpers::setup(int width, int height) {
31 struct info info;
32
33 glfwSetErrorCallback(error_callback);
34 if (!glfwInit()) die("couldn't init glfw!");
35 glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE); // Single buffer mode, to avoid any doublebuffering timing issues
36 window = glfwCreateWindow(width, height, "opengl_halide_test", NULL, NULL);
37 if (!window) die("couldn't create window!");
38 glfwSetKeyCallback(window, key_callback);
39 glfwSetWindowFocusCallback(window, focus_callback);
40 glfwMakeContextCurrent(window);
41
42 while (!first_focus) {
43 glfwWaitEvents();
44 }
45
46 int framebuffer_width, framebuffer_height;
47 glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
48 info.dpi_scale = float(framebuffer_width) / float(width);
49
50 return info;
51 }
52
terminate()53 void GlfwHelpers::terminate() {
54 while (!glfwWindowShouldClose(window)) {
55 glfwPollEvents();
56 }
57 glfwDestroyWindow(window);
58 glfwTerminate();
59 }
60
set_opengl_context()61 void GlfwHelpers::set_opengl_context() {
62 glfwMakeContextCurrent(window);
63 }
64