如何在事件驱动的X-lib程序中引入可实现的延迟?

时间:2015-07-02 09:30:49

标签: c graphics x11 xlib xserver

我正在编写一个X服务器显示代码,用于显示从一个像素位置到下一个位置的球的移动,这可以在执行以下代码后打开的新窗口中看到。在运行代码时,我可以获得5个红色填充圆圈(被视为球)。代码可以运行得非常快,实时计算就像2-3毫秒,很明显,并且在窗口上所有5个球同时显示。

我想在两者之间引入一些延迟,以便我可以看到球从一个像素位置移动到另一个像素位置。为此,我应该将什么添加到给定的代码中?我希望某种计时器或中断,以便一段时间执行将停止,同时从一个地方切换到另一个地方,它可以在窗口看到。

代码:

#include <stdio.h>
#include <X11/Xlib.h>
#include <math.h>

// Xserver变量声明

 Display *display;
 Window  window;
 XSetWindowAttributes attributes;
 XGCValues gr_values;
 XFontStruct *fontinfo;
 GC gr_context;
 Visual *visual;
 int depth;
 int screen;
 XEvent event;
 XColor    color, dummy;

 main (argc, argv)
 char   *argv[];
 int     argc;
  { 
  display = XOpenDisplay(NULL);
  screen = DefaultScreen(display);
  visual = DefaultVisual(display,screen);
  depth  = DefaultDepth(display,screen);
  attributes.background_pixel = XWhitePixel(display,screen);

  window = XCreateWindow( display,XRootWindow(display,screen),
                        200, 200, 750, 200, 5, depth,  InputOutput,
                        visual ,CWBackPixel, &attributes);
  XSetStandardProperties(display,window,"Welcome","Hi",None,NULL,0,NULL);
 XSelectInput(display,window,ExposureMask | KeyPressMask) ;
 fontinfo = XLoadQueryFont(display,"6x10");

 XAllocNamedColor(display, DefaultColormap(display, screen),"red",
                  &color,&dummy);

 gr_values.font = fontinfo->fid;
 gr_values.foreground = color.pixel;

 gr_context=XCreateGC(display,window,GCFont+GCForeground, &gr_values);
 XFlush(display);
 XMapWindow(display,window);
 XFlush(display);
 int i,j,a,a1,a2,a4,a5,b1,b2,b4,b5,b,h,w,angle1,angle2;
 while(1){
    XNextEvent(display,&event);

    switch(event.type){
    case Expose:
 //Make a circle using arc, and then fill it with a color

    a = 200; a1=520; a2=750;a4=400;a5=1050;
    b = 200;b1=100; b2=300;b4=400;b5=500;
    h = 10, w = 10;
    angle1 = 0, angle2 = 360*64;

//Some delay I may need to introduce here ??

 XFillArc(display, window, gr_context, a-(w/2), b-(h/2), w, h, angle1, angle2);

      XFillArc(display, window, gr_context, a1-(w/2), b1-(h/2), w, h, angle1, angle2);

     XFillArc(display, window, gr_context, a2-(w/2), b2-(h/2), w, h, angle1, angle2);

      XFillArc(display, window, gr_context, a4-(w/2), b4-(h/2), w, h, angle1, angle2);

      XFillArc(display, window, gr_context, a5-(w/2), b5-(h/2), w, h, angle1, angle2);
break;

    case KeyPress: 
         XCloseDisplay(display); 
         exit(0);

    }
 }

}

0 个答案:

没有答案