I was writing a X-server display code to show movement of balls from one pixel location to next location, which can be visible at the new window opened up after execution of this code. While running the below code, I can get 5 red color filled circles (considered as balls). The code can run pretty fast, like real time computation is just like 2-3 ms, which is obvious, and on the window all the 5 balls are shown simultaneously.

I want to introduce some delay in between so that I can see the movement of ball from one pixel position to other. For doing this, what should I add to the given code? I hope some sort of timer or interrupt so that for some time the execution will be stopped for that time, while switching from one place to other and it can be visible at the window.

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

//Xserver variable declarations

 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);

    }
 }

As a first draft brute force instrument, perhaps you're looking for the usleep function, to be found in the unistd.h header?

http://unixhelp.ed.ac.uk/CGI/man-cgi?usleep+3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.