修復:錯誤很簡單,因爲我退出應用程序而不使用XCloseDisplay()關閉顯示。而我沒有得到任何東西出現在屏幕上的原因是因爲width和height werent的值實際上是設置的,所以當它設置投影時,它將其設置爲0或0。由於datenwolf幫助我:)Xlib致命IO錯誤:11(資源暫時不可用)
運行我的代碼,當我得到這個錯誤:
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
after 58 requests (58 known processed) with 0 events remaining.
我已經到處找在互聯網上的文檔中,但我無法找到這意味着什麼什麼?
當我運行代碼時,我看到了窗口,但沒有在裏面畫,當我退出窗口時,我得到了上述錯誤。
我不想做任何事情。我已經設置了X窗口系統了OpenGL的
我有這樣一個基本程序:
#include <GL/gl.h>
#include <GL/glu.h>
// create window
bool success = paDisplay::init(640, 480);
if (!success)
std::cout << "oh noes";
while (1)
{
// do pre-frame tasks
paDisplay::stepBegin();
// process the screen and draw
// do post-frame tasks
paDisplay::stepEnd();
}
這裏是爲paDisplay類的代碼:
#include <X11/X.h>
#include <X11/Xlib.h>
#include <GL/glx.h>
class paDisplay
{
private:
// window information
static Display *dpy;
static Window win;
static GLXContext glc;
static XWindowAttributes gwa;
static XEvent xev;
static int width, height;
static int frame;
// event information
static bool EV_Resize, EV_KeyPress;
static KeySym EV_Key;
// functions
static void destroyWindow();
public:
static bool init(int, int);
static void stepBegin();
static void stepEnd();
// window
static bool createWindow(int, int);
static void setTitle(std::string);
// events
static void pollEvents();
static bool eventResize() { return EV_Resize; }
static bool eventKeyPress() { return EV_KeyPress; }
static unsigned int getKey() { return EV_Key; }
// opengl
static void initOpenGL();
static void swapBuffers();
// quit
static void quit();
// window dimensions
static int getWidth() { return width; }
static int getHeight() { return height; }
};
// static variables
// window information
Display* paDisplay::dpy;
Window paDisplay::win;
GLXContext paDisplay::glc;
XWindowAttributes paDisplay::gwa;
XEvent paDisplay::xev;
int paDisplay::width;
int paDisplay::height;
int paDisplay::frame;
// event information
bool paDisplay::EV_Resize;
bool paDisplay::EV_KeyPress;
KeySym paDisplay::EV_Key;
// implementation
bool paDisplay::init(int w, int h)
{
if (createWindow(w, h))
{
initOpenGL();
time_t start = time(NULL);
srand(start);
// initialize frame counter
frame = 0;
return true;
}
else
return false;
}
void paDisplay::stepBegin()
{
// clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
pollEvents();
}
void paDisplay::stepEnd()
{
// start processing buffered opengl routines
glFlush();
// swap buffers
swapBuffers();
// implement dumb frames per second
usleep(1000000/60);
// increase frame counter
frame ++;
}
void paDisplay::initOpenGL()
{
// init OpenGL
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
glShadeModel(GL_FLAT);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, (GLdouble) width, 0, (GLdouble) height);
}
void paDisplay::setTitle(std::string title)
{
XStoreName(dpy, win, title.c_str());
}
void paDisplay::quit()
{
destroyWindow();
exit(0);
}
void paDisplay::pollEvents()
{
// set defaults
EV_Resize = false;
EV_KeyPress = false;
while (XPending(dpy) > 0)
{
// get next event
XNextEvent(dpy, &xev);
switch (xev.type)
{
// window resized
case Expose:
/*EV_Resize = true;
XGetWindowAttributes(dpy, win, &gwa);
width = gwa.width;
height = gwa.height;
// resize OpenGL projection function
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, (GLdouble) width, 0, (GLdouble) height);*/
break;
// a key was pressed
case KeyPress:
EV_KeyPress = true;
EV_Key = XLookupKeysym(&xev.xkey, 0);
break;
}
}
}
void paDisplay::swapBuffers()
{
glXSwapBuffers(dpy, win);
}
bool paDisplay::createWindow(int w, int h)
{
Window root;
GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
dpy = XOpenDisplay(NULL);
if (dpy == NULL)
return false;
XSynchronize(dpy, true);
root = DefaultRootWindow(dpy);
vi = glXChooseVisual(dpy, 0, att);
if (vi == NULL)
return false;
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
swa.colormap = cmap;
swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask;
win = XCreateWindow(dpy, root, 0, 0, w, h, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
XMapWindow(dpy, win);
XStoreName(dpy, win, "Pineapple");
glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
glXMakeCurrent(dpy, win, glc);
return true;
}
void paDisplay::destroyWindow()
{
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
該paDisplay類看起來很奇怪。爲什麼要使一切都靜止另外它使用OpenGL的方式是粗略的。例如,GL上下文可能會被程序的其他部分解開/反彈,所以在處理OpenGL的函數之前調用glXMakeCurrent,或者更好的是OpenGL上下文應該由單個類來管理。 initOpenGL函數通過渲染代碼完成顯示函數中的任務。設置視口和投影是渲染的一部分,而不是窗口設置! – datenwolf 2011-04-06 07:30:05
我讓所有東西都是靜態的,因爲它比一個類更多的是函數和變量的集合。即在我的程序中,你不應該能夠創建多個窗口。在程序運行的整個過程中,OpenGL上下文將始終被綁定。但我確實認爲你的想法很好,可以分離出更多的東西,也許可以使它更具可讀性和結構良好。你是說設置vierport和投影應該可能被移動到paDisplay :: stepBegin()函數。對不起,它看起來很糟糕我只是想從主程序中隱藏大部分複雜的代碼。 – 2011-04-06 07:53:40
我只是試圖將視口和投影功能移動到渲染,但問題仍然存在。 究竟發生了什麼事情是窗口創建成功,但沒有顯示裏面,它不是白色的顏色,它只是窗口後面仍然存在的內容。 – 2011-04-06 07:54:37