/
glx.c
284 lines (249 loc) · 6.54 KB
/
glx.c
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdbool.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <GL/glx.h>
#include <X11/Xlib.h>
#define USE_GLES2 0
#define _unused __attribute__((unused))
static Display *dpy;
static Atom wm_delete_window, wm_protocols;
static Window win;
static GLXContext ctx;
static void
pr_info(const char *fmt, ...)
{
va_list ap;
char msg[256];
va_start(ap, fmt);
strcpy(msg, "INFO:");
vsnprintf(msg + 5, sizeof(msg) - 5, fmt, ap);
va_end(ap);
strcat(msg, "\n");
fputs(msg, stderr);
}
static void
pr_err(const char *fmt, ...)
{
va_list ap;
char msg[256];
va_start(ap, fmt);
strcpy(msg, "ERROR:");
vsnprintf(msg + 6, sizeof(msg) - 6, fmt, ap);
va_end(ap);
strcat(msg, "\n");
fputs(msg, stderr);
}
#if NDEBUG
#define pr_dbg(...) do { /* nothing */ } while(0)
#else
static void
pr_dbg(const char *fmt, ...) _unused;
static void
pr_dbg(const char *fmt, ...)
{
char msg[256];
va_list ap;
va_start(ap, fmt);
strcpy(msg, "DEBUG:");
vsnprintf(msg + 6, sizeof(msg) - 6, fmt, ap);
va_end(ap);
puts(msg);
}
#endif
static bool
borisgl_initialize(int width, int height, const char *title)
{
int screen;
Window root;
XVisualInfo *xvi;
int fbcount;
GLXFBConfig *fbconfiglist, fbconfig;
int i;
const int glx_attribs[] = {
GLX_X_RENDERABLE, True,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 1,
GLX_DOUBLEBUFFER, True,
None
};
static int ctx_attribs[] = {
#if USE_GLES2
GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
#else
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
/* GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, */
#endif
None
};
dpy = XOpenDisplay(NULL);
if (!dpy)
return false;
wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", 0);
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
fbconfiglist = glXChooseFBConfig(dpy, screen, glx_attribs, &fbcount);
if (fbcount <= 0) {
pr_err("No valid FB configurations");
return 1;
}
for (i = 0; i < fbcount; i++) {
xvi = glXGetVisualFromFBConfig(dpy, fbconfiglist[i]);
if (!xvi)
continue;
fbconfig = fbconfiglist[i];
goto found_fbconfig;
}
pr_err("No valid FB configurations");
return 1;
found_fbconfig:
XFree(fbconfiglist);
XSetWindowAttributes wattr;
wattr.event_mask = StructureNotifyMask | KeyPressMask;
wattr.background_pixmap = None;
wattr.background_pixel = 0;
wattr.border_pixel = 0;
wattr.colormap = XCreateColormap(dpy, root, xvi->visual, AllocNone);
win = XCreateWindow(dpy, root,
(DisplayWidth(dpy, screen) - width) / 2, (DisplayHeight(dpy, screen) - height) / 2,
width, height, 0, xvi->depth, InputOutput, xvi->visual,
CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask, &wattr);
if (!win) {
pr_err("Unable to create window");
return false;
}
/* setup various window manager(WM) metadata */
if (title)
XStoreName(dpy, win, title);
XSizeHints sizehints;
sizehints.flags = PPosition | PBaseSize | PMinSize | PMaxSize | PResizeInc | PAspect;
sizehints.x = 200;
sizehints.y = 100;
sizehints.base_width = width;
sizehints.base_height = height;
sizehints.min_width = width / 8;
sizehints.min_height = height / 8;
sizehints.max_width = width * 8 ;
sizehints.max_height = height * 8;
sizehints.width_inc = 16;
sizehints.height_inc = 16;
sizehints.min_aspect.x = width;
sizehints.min_aspect.y = height;
sizehints.max_aspect.x = width;
sizehints.max_aspect.y = height;
XSetWMNormalHints(dpy, win, &sizehints );
XSetWMProtocols(dpy, win, &wm_delete_window, 1);
/* make a temporary context to find glXCreateContextAttribsARB() */
GLXContext ctx_tmp = glXCreateContext(dpy, xvi, 0, True);
if (!ctx_tmp) {
pr_err("Failed to allocate legacy GL context!");
goto failed;
}
glXMakeCurrent(dpy, win, ctx_tmp);
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB =
(PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
if (!glXCreateContextAttribsARB) {
pr_err("Failed to bind glXCreateContextAttribsARB!");
glXMakeCurrent(dpy, None, 0);
glXDestroyContext(dpy, ctx_tmp);
goto failed_destroy_context;
}
glXMakeCurrent(dpy, None, 0);
glXDestroyContext(dpy, ctx_tmp);
/* now that we have glXCreateContextAttribsARB, we can make the real context */
ctx = glXCreateContextAttribsARB(dpy, fbconfig, 0, True, ctx_attribs);
if (!ctx) {
pr_err("Failed to allocate GL context!");
goto failed;
}
XSync(dpy, False);
glXMakeCurrent(dpy, win, ctx);
pr_info("GL_VERSION=%s", glGetString(GL_VERSION));
pr_info("GL_RENDERER=%s", glGetString(GL_RENDERER));
#if 0 // not supported/broken on one of my systems
GLint gl_major = 0, gl_minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &gl_major);
glerr("GL_MAJOR_VERSION");
glGetIntegerv(GL_MINOR_VERSION, &gl_minor);
inf("GL_VERSION_MAJOR=%d GL_VERSION_MINOR=%d", (int)gl_major, (int)gl_minor);
#endif
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
if (!glXSwapIntervalEXT) {
pr_err("Failed to bind glXCreateContextAttribsARB!");
goto failed_destroy_context;
}
glXSwapIntervalEXT(dpy, win, 1);
/* done - we can show the window */
XMapRaised(dpy, win);
XFlush(dpy);
/* report to the game that we are ready */
game_initialize();
return true;
failed_destroy_context:
glXMakeCurrent(dpy, None, 0);
glXDestroyContext(dpy, ctx);
failed:
XDestroyWindow(dpy, win);
return false;
}
static void
borisgl_loop(void)
{
XEvent ev;
while (1) {
game_paint();
// glFlush();
glXSwapBuffers(dpy, win);
// glXWaitGL();
while (XEventsQueued(dpy, QueuedAfterFlush)) {
XNextEvent(dpy, &ev);
switch (ev.type) {
case KeyPress:
if (XLookupKeysym(&ev.xkey, 0) == XK_Escape)
return;
break;
case ClientMessage:
if (ev.xclient.message_type == wm_protocols &&
(Atom)ev.xclient.data.l[0] == wm_delete_window)
return;
break;
}
}
}
}
static void
borisgl_cleanup(void)
{
if (ctx != None) {
glXMakeCurrent(dpy, win, 0);
glXDestroyContext(dpy, ctx);
ctx = None;
}
if (win != None) {
XDestroyWindow(dpy, win);
win = None;
}
if (dpy) {
XCloseDisplay(dpy);
dpy = NULL;
}
}
int
main()
{
if (!borisgl_initialize(640, 480, "game"))
return 1;
borisgl_loop();
borisgl_cleanup();
return 0;
}