/
gl_window.cpp
108 lines (82 loc) · 2.45 KB
/
gl_window.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
97
98
99
100
101
102
103
104
105
106
107
#include "gl_window.h"
GL_Window::GL_Window()
{
}
GL_Window::GL_Window(int w, int h)
{
int width = w;
int height = h;
XSetWindowAttributes swa;
x_display = XOpenDisplay(NULL);
DefaultScreen(x_display);
root = DefaultRootWindow(x_display);
swa.event_mask = ExposureMask | PointerMotionMask | KeyPressMask | ButtonPressMask |
MotionNotify | ButtonReleaseMask | StructureNotifyMask;
win = XCreateWindow(x_display, root, 0, 0, width, height, 0,
CopyFromParent, InputOutput, CopyFromParent, CWEventMask, &swa);
// handle window close correctly
XMapWindow(x_display, win);
Atom delmsg = XInternAtom(x_display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(x_display, win, &delmsg, 1);
// limit resize to 16:9 ratio
XSizeHints *hints = XAllocSizeHints();
hints->flags |= PAspect;
hints->min_aspect.x = 16;
hints->min_aspect.y = 9;
hints->max_aspect.x = 16;
hints->max_aspect.y = 9;
XSetWMNormalHints(x_display, win, hints);
XFree(hints);
// EGL stuff
EGLint major, minor;
EGLint numConfigs;
EGLConfig config;
const EGLint configAttribs[] = {
EGL_RENDERABLE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_NONE
};
const EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
display = eglGetDisplay((EGLNativeDisplayType)x_display);
eglInitialize(display, &major, &minor);
eglChooseConfig(display, configAttribs, &config, 1, &numConfigs);
surface = eglCreateWindowSurface(display, config,
(EGLNativeWindowType)win, NULL);
context = eglCreateContext(display, config,
EGL_NO_CONTEXT, contextAttribs);
eglMakeCurrent(display, surface, surface, context);
}
GL_Window::~GL_Window()
{
eglMakeCurrent(display, NULL, NULL, NULL);
eglDestroyContext(display, context);
eglDestroySurface(display, surface);
eglTerminate(display);
}
void GL_Window::update()
{
eglSwapBuffers(display, surface);
}
void GL_Window::destroy()
{
XDestroyWindow(x_display, win);
XCloseDisplay(x_display);
}
void GL_Window::resize(unsigned int w, unsigned int h)
{
XResizeWindow(x_display, win, w, h);
}
Display *GL_Window::xdisplay()
{
return x_display;
}
Window *GL_Window::window()
{
return &win;
}