سؤال

Here is the code snippet (Xlib):

void update_screen()
{
  int X,Y;
  for (X = 0; X < Wnd_X; X++)
    for (Y = 0; Y < Wnd_Y; Y++)
    {
      XDrawPoint(dsp, win, gc_2, X, Y);
      usleep(100);
    }

  return;
}

But it'll be like this:

and then:

Why dots cannot be drawn smoothly with XDrawPoint?

And how to fill the window with green color fluently?

Full code here:

#include <X11/Xlib.h> // must precede most other headers!
#include <stdlib.h>

int Wnd_X=500;
int Wnd_Y=500;

void update_screen();

GC gc_2;
Window win;
Display *dsp;

int main()
{
  dsp = XOpenDisplay(NULL);
  int screen = DefaultScreen(dsp);
  win = XCreateWindow(dsp, DefaultRootWindow(dsp), 0, 0, Wnd_X, Wnd_Y, 0, 0, 0, 0, 0, 0);
      //XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), 0, 0, Wnd_X, Wnd_Y, 0, 0x000000, 0xFFFFFF);
  Atom wmDelete = XInternAtom(dsp, "WM_DELETE_WINDOW", True);
  XSetWMProtocols(dsp, win, &wmDelete, 1);

  XGCValues gcvalues_2;
  gcvalues_2.function = GXcopy;
  gcvalues_2.plane_mask = AllPlanes;
  gcvalues_2.foreground = 0x00FF00;
  gcvalues_2.background = 0xFFFFFF;
  gc_2 = XCreateGC(dsp, win, GCFunction|GCPlaneMask|GCForeground|GCBackground, &gcvalues_2);

  XEvent evt;
  long eventMask = StructureNotifyMask;
  eventMask |= ButtonPressMask|ButtonReleaseMask|KeyPressMask|KeyReleaseMask;
  XSelectInput(dsp, win, eventMask);

  XMapWindow(dsp, win);

  // wait until window appears
  do { XNextEvent(dsp,&evt); } while (evt.type != MapNotify);

  update_screen();
  XDestroyWindow(dsp, win);
  XCloseDisplay(dsp);

  return 0;
}

void update_screen()
{
  int X,Y;
  for (X = 0; X < Wnd_X; X++)
    for (Y = 0; Y < Wnd_Y; Y++)
    {
      XDrawPoint(dsp, win, gc_2, X, Y);
      usleep(100);
    }

  return;
}
هل كانت مفيدة؟

المحلول

You need to flush the updates to the X server. Try this:

XDrawPoint(dsp, win, gc_2, X, Y);
XFlush(dsp);
usleep(100);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top