Now using glXChooseFBConfig

This commit is contained in:
Pierre Krieger 2014-08-07 17:35:26 +02:00
parent ed3ed47ad8
commit c7aa219ef1
2 changed files with 41 additions and 17 deletions

View file

@ -13,7 +13,7 @@ pub type Drawable = XID; // TODO: not sure
pub type GLXContext = *const (); pub type GLXContext = *const ();
pub type GLXContextID = XID; pub type GLXContextID = XID;
pub type GLXDrawable = XID; pub type GLXDrawable = XID;
pub type GLXFBConfig = (); pub type GLXFBConfig = *const ();
pub type GLXPbuffer = XID; pub type GLXPbuffer = XID;
pub type GLXPixmap = XID; pub type GLXPixmap = XID;
pub type GLXWindow = XID; pub type GLXWindow = XID;
@ -1348,6 +1348,7 @@ extern "C" {
pub fn XDestroyWindow(display: *mut Display, w: Window); pub fn XDestroyWindow(display: *mut Display, w: Window);
pub fn XFilterEvent(event: *mut XEvent, w: Window) -> Bool; pub fn XFilterEvent(event: *mut XEvent, w: Window) -> Bool;
pub fn XFlush(display: *mut Display); pub fn XFlush(display: *mut Display);
pub fn XFree(data: *const libc::c_void);
pub fn XGetGeometry(display: *mut Display, d: Drawable, root_return: *mut Window, pub fn XGetGeometry(display: *mut Display, d: Drawable, root_return: *mut Window,
x_return: *mut libc::c_int, y_return: *mut libc::c_int, x_return: *mut libc::c_int, y_return: *mut libc::c_int,
width_return: *mut libc::c_uint, height_return: *mut libc::c_uint, width_return: *mut libc::c_uint, height_return: *mut libc::c_uint,
@ -1386,10 +1387,11 @@ extern "C" {
shareList: GLXContext, direct: Bool) -> GLXContext; shareList: GLXContext, direct: Bool) -> GLXContext;
pub fn glXDestroyContext(dpy: *mut Display, ctx: GLXContext); pub fn glXDestroyContext(dpy: *mut Display, ctx: GLXContext);
pub fn glXChooseFBConfig(dpy: *mut Display, screen: libc::c_int, pub fn glXChooseFBConfig(dpy: *mut Display, screen: libc::c_int,
attrib_list: *const libc::c_int, nelements: *mut libc::c_int); attrib_list: *const libc::c_int, nelements: *mut libc::c_int) -> *mut GLXFBConfig;
pub fn glXChooseVisual(dpy: *mut Display, screen: libc::c_int, pub fn glXChooseVisual(dpy: *mut Display, screen: libc::c_int,
attribList: *const libc::c_int) -> *const XVisualInfo; attribList: *const libc::c_int) -> *const XVisualInfo;
pub fn glXGetProcAddress(procName: *const libc::c_uchar) -> *const (); pub fn glXGetProcAddress(procName: *const libc::c_uchar) -> *const ();
pub fn glXGetVisualFromFBConfig(dpy: *mut Display, config: GLXFBConfig) -> *mut XVisualInfo;
pub fn glXMakeCurrent(dpy: *mut Display, drawable: GLXDrawable, pub fn glXMakeCurrent(dpy: *mut Display, drawable: GLXDrawable,
ctx: GLXContext) -> Bool; ctx: GLXContext) -> Bool;
pub fn glXSwapBuffers(dpy: *mut Display, drawable: GLXDrawable); pub fn glXSwapBuffers(dpy: *mut Display, drawable: GLXDrawable);
@ -1398,7 +1400,6 @@ extern "C" {
/* /*
GLXFBConfig *glXGetFBConfigs (Display *dpy, int screen, int *nelements); GLXFBConfig *glXGetFBConfigs (Display *dpy, int screen, int *nelements);
int glXGetFBConfigAttrib (Display *dpy, GLXFBConfig config, int attribute, int *value); int glXGetFBConfigAttrib (Display *dpy, GLXFBConfig config, int attribute, int *value);
XVisualInfo *glXGetVisualFromFBConfig (Display *dpy, GLXFBConfig config);
GLXWindow glXCreateWindow (Display *dpy, GLXFBConfig config, Window win, const int *attrib_list); GLXWindow glXCreateWindow (Display *dpy, GLXFBConfig config, Window win, const int *attrib_list);
void glXDestroyWindow (Display *dpy, GLXWindow win); void glXDestroyWindow (Display *dpy, GLXWindow win);
GLXPixmap glXCreatePixmap (Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attrib_list); GLXPixmap glXCreatePixmap (Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attrib_list);

View file

@ -51,23 +51,46 @@ impl Window {
display display
}; };
// TODO: set error handler // TODO: set error handler?
static VISUAL_ATTRIBUTES: [libc::c_int, ..5] = [ // getting the FBConfig
ffi::GLX_RGBA, let fb_config = unsafe {
ffi::GLX_DEPTH_SIZE, static VISUAL_ATTRIBUTES: [libc::c_int, ..23] = [
24, ffi::GLX_X_RENDERABLE, 1,
ffi::GLX_DOUBLEBUFFER, ffi::GLX_DRAWABLE_TYPE, ffi::GLX_WINDOW_BIT,
0 ffi::GLX_RENDER_TYPE, ffi::GLX_RGBA_BIT,
]; ffi::GLX_X_VISUAL_TYPE, ffi::GLX_TRUE_COLOR,
ffi::GLX_RED_SIZE, 8,
ffi::GLX_GREEN_SIZE, 8,
ffi::GLX_BLUE_SIZE, 8,
ffi::GLX_ALPHA_SIZE, 8,
ffi::GLX_DEPTH_SIZE, 24,
ffi::GLX_STENCIL_SIZE, 8,
ffi::GLX_DOUBLEBUFFER, 1,
0
];
let mut num_fb: libc::c_int = mem::uninitialized();
let fb = ffi::glXChooseFBConfig(display, ffi::XDefaultScreen(display),
VISUAL_ATTRIBUTES.as_ptr(), &mut num_fb);
if fb.is_null() {
return Err(format!("glXChooseFBConfig failed"));
}
let preferred_fb = *fb; // TODO: choose more wisely
ffi::XFree(fb as *const libc::c_void);
preferred_fb
};
// getting the visual infos // getting the visual infos
let visual_infos = unsafe { let visual_infos = unsafe {
let vi = ffi::glXChooseVisual(display, 0, VISUAL_ATTRIBUTES.as_ptr()); let vi = ffi::glXGetVisualFromFBConfig(display, fb_config);
if vi.is_null() { if vi.is_null() {
return Err(format!("glXChooseVisual failed")); return Err(format!("glXChooseVisual failed"));
} }
vi let vi_copy = *vi;
ffi::XFree(vi as *const libc::c_void);
vi_copy
}; };
// getting the root window // getting the root window
@ -76,7 +99,7 @@ impl Window {
// creating the color map // creating the color map
let cmap = unsafe { let cmap = unsafe {
let cmap = ffi::XCreateColormap(display, root, let cmap = ffi::XCreateColormap(display, root,
(*visual_infos).visual, ffi::AllocNone); visual_infos.visual, ffi::AllocNone);
// TODO: error checking? // TODO: error checking?
cmap cmap
}; };
@ -97,8 +120,8 @@ impl Window {
let dimensions = dimensions.unwrap_or((800, 600)); let dimensions = dimensions.unwrap_or((800, 600));
let win = ffi::XCreateWindow(display, root, 50, 50, dimensions.val0() as libc::c_uint, let win = ffi::XCreateWindow(display, root, 50, 50, dimensions.val0() as libc::c_uint,
dimensions.val1() as libc::c_uint, 0, (*visual_infos).depth, ffi::InputOutput, dimensions.val1() as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput,
(*visual_infos).visual, ffi::CWColormap | ffi::CWEventMask, visual_infos.visual, ffi::CWColormap | ffi::CWEventMask,
&mut set_win_attr); &mut set_win_attr);
win win
}; };
@ -142,7 +165,7 @@ impl Window {
// creating GL context // creating GL context
let context = unsafe { let context = unsafe {
ffi::glXCreateContext(display, visual_infos, ptr::null(), 1) ffi::glXCreateContext(display, &visual_infos, ptr::null(), 1)
}; };
// returning // returning