-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontext_glfw.cpp
96 lines (73 loc) · 3.01 KB
/
context_glfw.cpp
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
#include "context_glfw.h"
static void errorCallback(int error, const char* description) {
if (error == GLFW_VERSION_UNAVAILABLE) {
// Silent this error, which can happen as we loop over versions
// to find the highest one available.
return;
}
crash("GLFW Error %d: %s", error, description);
}
/*---------------------------------------------------------------------------*/
void contextInitAndGetAPI(Params& params, Context& ctx) {
glfwSetErrorCallback(errorCallback);
if (!glfwInit()) {
crash("%s", "glfwInit()");
}
// TODO: Must request GLFW_OPENGL_ANY_PROFILE for profiles below
// 3.2, but we could request core / compat depending on the version.
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
// TODO: here, try OpenGL ES API depending on the shader version?
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
// Create window using the highest possible version
int versions[] = {
460, 450, 440, 430, 420, 410, 400,
330, 320, 310, 300,
210, 200,
};
GLFWwindow* window = NULL;
for (unsigned i = 0; i < (sizeof(versions) / sizeof(versions[0])); i++) {
int majorHint = versions[i] / 100;
int minorHint = (versions[i] % 100) / 10;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, majorHint);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minorHint);
window = glfwCreateWindow(params.width, params.height, "get_image_glfw", NULL, NULL);
if (window != NULL) {
break;
}
}
if (window == NULL) {
crash("%s", "glfwCreateWindow()");
}
params.API = API_OPENGL;
int major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
int minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
params.APIVersion = ((int)major * 100) + ((int) minor * 10);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glfwSwapInterval(1);
ctx.window = window;
}
/*---------------------------------------------------------------------------*/
bool contextKeepLooping(Context &ctx) {
return !glfwWindowShouldClose(ctx.window);
}
/*---------------------------------------------------------------------------*/
void contextSwap(Context& ctx) {
glfwSwapBuffers(ctx.window);
glfwPollEvents();
}
/*---------------------------------------------------------------------------*/
static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
exit(EXIT_SUCCESS);
}
/*---------------------------------------------------------------------------*/
void contextSetKeyCallback(Context& ctx) {
glfwSetKeyCallback(ctx.window, keyCallback);
}
/*---------------------------------------------------------------------------*/
void contextTerminate(Context& ctx) {
glfwDestroyWindow(ctx.window);
glfwTerminate();
}
/*---------------------------------------------------------------------------*/