繁体   English   中英

X11窗口未自动更新/绘制

[英]X11 Window isn't updating/drawing automatically

我正在使用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 ,然后重新组织循环(首先处理所有事件,然后处理)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM