Events Xlib应用程序更新中的问题

Events Xlib应用程序更新中的问题,events,x11,xlib,Events,X11,Xlib,我正在Ubuntu上使用Xlib开发一个光线跟踪应用程序,这些应用程序可以正常工作,但当系统响应输入事件时,图像更新存在问题 问题是图像更新和频繁的输入事件(如按住键和拖动鼠标)之间存在明显的延迟。当我拖动鼠标时,图像没有更新,而过了一会儿,窗口上的图像会根据之前的事件进行更新。我想原因是执行图像更新的位置不合适,但我真的不知道如何修复这个错误。这是我的密码: int main(int argc,char*argv[]){ g_Display=XOpenDisplay(NULL); 如果(g_D

我正在Ubuntu上使用Xlib开发一个光线跟踪应用程序,这些应用程序可以正常工作,但当系统响应输入事件时,图像更新存在问题

问题是图像更新和频繁的输入事件(如按住键和拖动鼠标)之间存在明显的延迟。当我拖动鼠标时,图像没有更新,而过了一会儿,窗口上的图像会根据之前的事件进行更新。我想原因是执行图像更新的位置不合适,但我真的不知道如何修复这个错误。这是我的密码:

int main(int argc,char*argv[]){

g_Display=XOpenDisplay(NULL);
如果(g_Display==NULL)
{
fprintf(stderr,“无法打开显示\n”);
出口(-1);
}
//设置摄像机
g_摄像头设置(0.0f、0.0f、1.0f);
g_摄像头设置位置(10.0f、0.0f、0.0f);
g_摄像头设置焦距(0.0f、0.0f、0.0f);
g_摄像机。SetFovAngle(45.0f);
g_摄像头设置剪辑(0.1f、100.0f);
/*创建一个简单的窗口,作为屏幕的直接子窗口*/
/*根窗口。使用屏幕的白色作为背景*/
/*窗口的颜色。放置新窗口的左上角*/
/*在给定的“x,y”坐标处*/
g_窗口=CreateSimpleWindow(g_显示、g_渲染器宽度、g_渲染器高度、g_位置X、g_位置Y);
g_TextWindow=CreateTextWindow(g_显示,g_窗口,g_TextWinWidth,g_TextWinHeight,0,0);
/*为窗口中的图形分配新的GC(图形上下文)*/
g_GraphicsContext=CreateGraphicsContext(g_显示,g_窗口,0);
XSync(g_显示,错误);
MeshLoad(“模型/模型/cow.obj”);
HighResClock::HighResClock::time_point lastFrame=HighResClock::HighResClock::now();
int nbFrame=0;
浮动平均时间=0.0f;
//voxlize网格
体素化客户端();
X选择输入(g|U显示、g|U窗口、曝光蒙版|按键蒙版|按键释放蒙版
|按钮提示任务|按钮提示任务|指针提示任务);
XMapWindow(g_显示,g_窗口);
//创建bmp图像
CameraControl();
而(1)
{
//渲染
CameraControl();
XNextEvent(g_显示和g_事件);
if(g_dirty)
{
//设置CPU时间显示
//启动计时器
std::chrono::微秒l_ElapsedC=std::chrono::duration_cast(g_endC-g_start);
std::chrono::微秒l_经过=std::chrono::持续时间(g_结束-g_开始);
avgTime+=l_已用时间。计数();
nbFrame++;
if(std::chrono::duration\u cast(g\u end-lastFrame).count()>500)
{
lastFrame=g_端;
std::stringstream;
流动
g_Display = XOpenDisplay(NULL);
if(g_Display == NULL)
{
    fprintf(stderr, "Cannot open display\n");
    exit(-1);

}

//Set up camera
g_Camera.SetUp(0.0f, 0.0f, 1.0f);
g_Camera.SetPosition(10.0f, 0.0f, 0.0f);
g_Camera.SetFocal(0.0f, 0.0f, 0.0f);
g_Camera.SetFovAngle(45.0f);
g_Camera.SetClipping(0.1f, 100.0f);

/* create a simple window, as a direct child of the screen's   */
/* root window. Use the screen's white color as the background */
/* color of the window. Place the new window's top-left corner */
/* at the given 'x,y' coordinates.                             */
g_Window = CreateSimpleWindow(g_Display, g_RendererWidth, g_RendererHeight, g_PositionX, g_PositionY);

g_TextWindow = CreateTextWindow(g_Display, g_Window, g_TextWinWidth, g_TextWinHeight, 0, 0);



/* allocate a new GC (graphics context) for drawing in the window. */
g_GraphicsContext = CreateGraphicsContext(g_Display, g_Window, 0);
XSync(g_Display, False);

MeshLoad("Model/Models/cow.obj");

HighResClock::HighResClock::time_point  lastFrame = HighResClock::HighResClock::now();
int nbFrame = 0;
float avgTime = 0.0f;

//voxlize mesh
VoxelizeOnClient();

XSelectInput(g_Display, g_Window, ExposureMask | KeyPressMask | KeyReleaseMask
        | ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
XMapWindow(g_Display, g_Window);

//create bmp image
CameraControl();

while(1)
{
    //render
    CameraControl();
    XNextEvent(g_Display, &g_Event);

    if(g_dirty)
    {
        //Set CPU time display
        //start timer
        std::chrono::microseconds l_ElapsedC = std::chrono::duration_cast<std::chrono::microseconds>(g_endC - g_start);
        std::chrono::microseconds l_Elapsed = std::chrono::duration_cast<std::chrono::microseconds>(g_end - g_start);

        avgTime += l_Elapsed.count();
        nbFrame++;

        if(std::chrono::duration_cast<std::chrono::milliseconds>(g_end - lastFrame).count() > 500)
        {
            lastFrame = g_end;
            std::stringstream stream;

            stream << "CPU time: " << l_ElapsedC.count() << " micros    ";
            stream << "FPS: "   << 1000000.0f*nbFrame / avgTime;

            DisplayText(g_Display, stream);

            nbFrame = 0;
            avgTime = 0.0f;
        }

        g_dirty = false;

    }

    //key events
    if(g_Event.type == KeyPress)
    {
        float x = 0, y = 0;
        switch (g_Event.xkey.keycode)
        {
                // key ESC
                case 0x09:
                {
                    exit(0);
                }
                    break;

                default:
                    break;

            }        

        }

    //mouse press event
    if(g_Event.type == ButtonPress)
    {              
        switch(g_Event.xbutton.button)
        {   
            //left button
            case Button1:
            {
                g_MouseLeft = true;
                g_PositionX = g_Event.xbutton.x;
                g_PositionY = g_Event.xbutton.y;
            }
                break;


            //right button
            case Button3:
            {
                g_MouseRight = true;
                g_PositionX = g_Event.xbutton.x;
                g_PositionY = g_Event.xbutton.y;
            }
                break;
        }
    }

    //mouse release event
    if(g_Event.type == ButtonRelease)
    {
        switch(g_Event.xbutton.button)
        {   
            //left button
            case Button1:
            {
                g_MouseLeft = false;
                g_PositionX = g_Event.xbutton.x;
                g_PositionY = g_Event.xbutton.y;
            }
                break;

            //right button
            case Button3:
            {
                g_MouseRight = false;
                g_PositionX = g_Event.xbutton.x;
                g_PositionY = g_Event.xbutton.y;
            }
                break;
        }
    }

    //mouse motion
    if(g_Event.type == MotionNotify)
    {            
        if(g_MouseLeft)
        {   

            if(g_Event.type == KeyPress && g_Event.xkey.keycode == 0x25)
            {
                /*Do something*/
            }
        }

        if(g_MouseRight)
        {            
            if(g_Event.type == KeyPress && g_Event.xkey.keycode == 0x25)
            {
                /* Update Camera */
            }
            else
            {
               /* Update Camera */
            }

            printf("Pressing mouse's right button and moving mouse. \n");

        }

        g_PositionX = g_Event.xbutton.x;
        g_PositionY = g_Event.xbutton.y;
    }

    XFlush(g_Display); 

    CameraControl();
}


XCloseDisplay(g_Display);

return 0;