Skip to content

Instantly share code, notes, and snippets.

@branan
Created June 4, 2011 20:07
Show Gist options
  • Save branan/1008299 to your computer and use it in GitHub Desktop.
Save branan/1008299 to your computer and use it in GitHub Desktop.
void Pipeline::platformInit() {
platform = new PipelinePlatform;
try {
Display *dpy = platform->dpy = XOpenDisplay(0);
if(!dpy)
throw std::runtime_error("Could not open X11 display!");
try { // display is open. If we get an error from here down we need to close the display
// Verify our GLX version is the minimum to be doing our fun stuff
int glx_major, glx_minor;
if(!glXQueryVersion(dpy, &glx_major, &glx_minor) || ((glx_major == 1 && glx_minor < 3) || glx_major < 1))
throw std::runtime_error("GLX 1.3 or greater is required!");
// Query for an FBConfig that matches our requirements
int fbcount;
GLXFBConfig *cfgs = glXChooseFBConfig(dpy, DefaultScreen(dpy), visual_attribs, &fbcount);
if(!cfgs)
throw std::runtime_error("No framebuffer that meets our requirements was found!");
GLXFBConfig fbc = cfgs[0];
XFree(cfgs);
// Get the visual and set the window attributes
XVisualInfo *vi = glXGetVisualFromFBConfig(dpy, fbc);
if(!vi)
throw std::runtime_error("Could not get a visual from the fbconfig");
XSetWindowAttributes swa;
swa.colormap = platform->cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
swa.background_pixmap = None;
swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask; // TODO: Add more event masks as needed
// Create the window
Window win = platform->win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 800, 600, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &swa);
if(!win)
throw std::runtime_error("Could not create X11 window");
try { // everything from here on needs to close the window if there's an error
XStoreName(dpy, win, "Deferred Renderer Demo");
XMapWindow(dpy, win);
// Query if the needed GLX extensions are present
const char *glx_ext_string = glXQueryExtensionsString(dpy, DefaultScreen(dpy));
if(!isExtensionSupported(glx_ext_string, "GLX_ARB_create_context"))
throw std::runtime_error("GLX_ARB_create_context needs to be supported");
CreateContextAttribs = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((GLubyte*)"glXCreateContextAttribsARB");
if(!CreateContextAttribs)
throw std::runtime_error("Could not get context creation entrypoint from GLX");
GLXContext ctx = platform->ctx = CreateContextAttribs(dpy, fbc, 0, True, context_attribs);
if(!ctx)
throw std::runtime_error("Could not create the OpenGL context");
try {
if(!glXIsDirect(dpy, ctx))
throw std::runtime_error("Can only use a direct context");
glXMakeContextCurrent(dpy, win, win, ctx);
} catch (std::exception&) {
glXDestroyContext(dpy, ctx);
throw;
}
} catch (std::exception&) {
XDestroyWindow(dpy, win);
throw;
}
} catch (std::exception&) {
XCloseDisplay(dpy);
throw;
}
} catch(std::exception&) {
delete platform;
platform = 0;
throw;
}
}
void Pipeline::platformFinish() {
glXMakeContextCurrent(platform->dpy, 0, 0, 0);
glXDestroyContext(platform->dpy, platform->ctx);
XDestroyWindow(platform->dpy, platform->win);
XFreeColormap(platform->dpy, platform->cmap);
delete platform;
platform = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment