gpt4 book ai didi

c++ - X11 窗口不会自动更新/绘图

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:09 25 4
gpt4 key购买 nike

我使用 x11(在 slackware linux 中)作为使用 OpenGL 绘图的窗口。

一切正常,但不知何故我的 x11 窗口没有自动更新/绘制。

它只会在我调整窗口大小时更新/绘制。

x11 窗口代码:

int main()
{

/*************** VARIABLE DEFINITION SECTION ***************/

Display *dpy;
Window root;
GLint att[] = {GLX_RGBA,GLX_DEPTH_SIZE,24,GLX_DOUBLEBUFFER,None};
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;

enum GAMEOBJECTS {USER, ENEMY}; //Object Enum - to identify the objects, object must be added in same enumeration you've created the objects

Renderer *renderer = new Renderer(); //Renderer Object


/*************** LINUX WINDOW SETTINGS ***************/


dpy = XOpenDisplay(NULL);
if(dpy == NULL)
{
cout << "\ncan't connect to x server\n" << endl;
exit(0);
}

root = DefaultRootWindow(dpy);
vi = glXChooseVisual(dpy,0,att);


if(vi == NULL)
{
cout << "\n no appropriate visual found \n" << endl;
exit(0);
}
else
{
cout << "visual " << (void *)vi->visualid << "selected";
}


cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
swa.colormap = cmap;
swa.event_mask = ExposureMask | KeyPressMask;

win = XCreateWindow(dpy,root,0,0,800,600,0,vi->depth,InputOutput,vi->visual,CWColormap | CWEventMask, &swa);


XMapWindow(dpy,win);

XStoreName(dpy,win,"VERY SIMPLE APP");

glc = glXCreateContext(dpy,vi,NULL,GL_TRUE);
glXMakeCurrent(dpy,win,glc);

cout << "ENUM: " << USER << endl;
cout << "ENUM: " << ENEMY << endl;

/*************** START RENDERER AND ADD OBJECTS ***************/

renderer->initGL();

renderer->addObject(100,100); // USER
renderer->move(USER,0,-100); //move to startposition of USER

renderer->addObject(100,100); // ENEMY
renderer->move(ENEMY,0,100); //move to startposition of ENEMY

glEnable(GL_DEPTH_TEST);


/*************** GAME LOOP += GAME LOOP NEEDS A OWN GAME LOOP ***************/
int count = 0;
while(1)
{
XNextEvent(dpy,&xev);
//glViewport(0,0,gwa.width,gwa.height);
//renderer->drawGL();

renderer->move(ENEMY,count,100);
if(xev.type == Expose)
{
XGetWindowAttributes(dpy,win,&gwa);
glViewport(0,0,gwa.width,gwa.height);
renderer->drawGL(); // DRAW OPENGL
glXSwapBuffers(dpy,win);
}

else if(xev.type == KeyPress)
{
switch(XLookupKeysym(&xev.xkey,0))
{
case XK_Left: cout << "LEFT KEY PRESSED" << endl;
renderer->move(USER,-1.0f,0.0f); //Move USER to the right
xev.xkey.send_event;
break;

case XK_Right: cout << "RIGHT KEY PRESSED" << endl;
renderer->move(USER,1.0f,0.0f); //Move USER to the right
break;

case XK_Escape: glXMakeCurrent(dpy,None,NULL);
glXDestroyContext(dpy,glc);
XDestroyWindow(dpy,win);
XCloseDisplay(dpy);
exit(0);
break;

case XK_P: cout << "P KEY PRESSEED" << endl;
break;

case ConfigureNotify: renderer->resizeGL(xev.xconfigure.width,xev.xconfigure.height);
break;
}

}
renderer->drawGL(); //DRAW OPENGL
glXSwapBuffers(dpy,win);
count++;
}

return 0;
}

有人可以帮忙吗?

最佳答案

   The XNextEvent function copies the first event from the event queue into the
specified XEvent structure and then removes it from the queue. If the event
queue is empty, XNextEvent flushes the output buffer and blocks until an event
is received.

因此,您对 XNextEvent 的调用会一直等到某个事件发生,然后才重绘。更糟糕的是,它会在每个事件上重绘,因此如果发生多个事件,您只能在几帧之后才知道。

您应该改用XCheckWindowEvent,并重新组织您的循环(首先处理所有事件,然后呈现)。

关于c++ - X11 窗口不会自动更新/绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com