我尝试在网上搜索,但我必须注意,找到关于X编程这方面的材料并不容易。
我使用X和GLX来创建OpenGL上下文。我已经知道我目前的显卡驱动程序仅支持OpenGL API版本3.3,但我希望我的应用程序能够尝试使用任何类型的版本创建上下文(因为它可以在其他计算机上运行)。我的代码是这样的:
glXCreateContextAttribsARB
glXCreateContext
我的代码没问题,但是我希望在检索X / GLX启动的错误的方式上更加干净,就像我使用glXCreateContextAttribARB
创建4.4版本一样(请记住) ,我的显卡只支持3.3),我显然得到:
X Error of failed request: BadMatch (invalid parameter attributes)
Major opcode of failed request: 153 (GLX)
Minor opcode of failed request: 34 ()
Serial number of failed request: 33
Current serial number in output stream: 34
我想在我的代码中插入X的错误处理来处理它。 X是C,而不是C ++,异常在此阶段不可用。这是我创建上下文的地方(我有意删除了不直接创建上下文的内容):
// Notes :
// mGLXContext : The GLX context we want to create
// vDepthSize, vAntialiasingLevel, vStencilSize are here to customize mGLXContext
// vTryVersion : a pointer to the API version {major, minor} we want to create
// vSharedContext : a pointer to an other (existing) context for data sharing.
// mXDisplay : the X Display
// Get the proc
const GLubyte* vProcName = reinterpret_cast<const GLubyte*>("glXCreateContextAttribsARB");
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB =
reinterpret_cast<PFNGLXCREATECONTEXTATTRIBSARBPROC>(glXGetProcAddress(vProcName));
if(glXCreateContextAttribsARB) {
// Create the FB attributes
int vFBAttributes[] = {
GLX_DEPTH_SIZE, (int)(vDepthSize),
GLX_STENCIL_SIZE, (int)(vStencilSize),
GLX_SAMPLE_BUFFERS, vAntialiasingLevel > 0,
GLX_SAMPLES, (int)(vAntialiasingLevel),
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, pDepth == 32 ? 8 : 0,
GLX_DOUBLEBUFFER, True,
GLX_X_RENDERABLE, True,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_CONFIG_CAVEAT, GLX_NONE,
None
};
// Get the FB configs
int vNbXConfigs = 0;
::GLXFBConfig* vGLXFBConfig = glXChooseFBConfig(mXDisplay, DefaultScreen(mXDisplay), vFBAttributes, &vNbXConfigs);
if(vGLXFBConfig && vNbXConfigs) {
// Create the context attributes
int vAttributes[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, static_cast<int>(vTryVersion->major),
GLX_CONTEXT_MINOR_VERSION_ARB, static_cast<int>(vTryVersion->minor),
0, 0
};
// Create the context : Error is generated by GLX here
mGLXContext = glXCreateContextAttribsARB(mXDisplay, vGLXFBConfig[0], vSharedContext, true, vAttributes);
}
}
所以我的问题是如何捕获X错误并查询它们?
感谢您阅读:)
答案 0 :(得分:2)
您需要使用XSetErrorHandler
来指定错误处理程序,例如
XSetErrorHandler(handler);
错误处理程序是
int handler(Display * d, XErrorEvent * e)
{
std::cerr << "Error code: " << e->error_code << std::endl;
return 0;
}