XLib draw pixel problem - Xwindows

This is a discussion on XLib draw pixel problem - Xwindows ; Hi All void drawPixel(int x, int y, long color) { gc = XCreateGC(display, window, 0, 0); //XAllocColor(display, DefaultColormap(display, XDefaultScreen(display)), &color2); XSetForeground(display, gc, color); // XFillRectangle(display, window, gc, 0, 0, 400, 400); XDrawPoint(display, window, gc, x, y); //XFlush(display); } Why i ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: XLib draw pixel problem

  1. XLib draw pixel problem

    Hi All
    void drawPixel(int x, int y, long color)
    {
    gc = XCreateGC(display, window, 0, 0);

    //XAllocColor(display, DefaultColormap(display,
    XDefaultScreen(display)), &color2);
    XSetForeground(display, gc, color);
    // XFillRectangle(display, window, gc, 0, 0, 400, 400);
    XDrawPoint(display, window, gc, x, y);
    //XFlush(display);
    }

    Why i need to call XCreateGC everytime i call my drawPixel? If i don't
    do that, some pixels will not be drawn on the screen.
    thanks
    from Peter (cmk128@hotmail.com)


  2. Re: XLib draw pixel problem

    In comp.windows.x, cmk128@hotmail.com

    wrote
    on 15 Sep 2006 21:41:08 -0700
    <1158381668.275823.228020@b28g2000cwb.googlegroups. com>:
    > Hi All
    > void drawPixel(int x, int y, long color)
    > {
    > gc = XCreateGC(display, window, 0, 0);
    >
    > //XAllocColor(display, DefaultColormap(display,
    > XDefaultScreen(display)), &color2);
    > XSetForeground(display, gc, color);
    > // XFillRectangle(display, window, gc, 0, 0, 400, 400);
    > XDrawPoint(display, window, gc, x, y);
    > //XFlush(display);
    > }
    >
    > Why i need to call XCreateGC everytime i call my drawPixel?


    You don't. However, it might depend on how the program
    is handling events and the GC.

    Basically, XDrawPoint() and XFillRectangle() translate into
    packets in Xlib, which are queued until flushed. Your
    explicit flush is commented out, and is usually not necessary
    as a flush is done whenever XNextEvent() finds no pending
    events.

    The following is a simplified event loop; this draws a
    fixed line whenever (part of) the window needs to be redrawn.

    #include
    #include
    int main()
    {
    Display * dpy = XOpenDisplay(NULL);
    int scr = DefaultScreen(dpy);
    unsigned long blackp = BlackPixel(dpy, scr);
    unsigned long whitep = WhitePixel(dpy, scr);
    Window root = RootWindow(dpy, scr);
    Window win = XCreateSimpleWindow(dpy, root, 0, 0, 400, 300, 1,
    blackp, whitep);
    GC gc = XCreateGC(dpy, win, 0, 0);
    XSelectInput(dpy, win, ExposureMask);
    XMapRaised(dpy, win);

    /* begin event loop */
    while(1)
    {
    XEvent ev;
    XNextEvent(dpy, &ev); // implicit XFlush() if no events pending
    switch(ev.type)
    {
    case Expose:
    {
    /* the following is optional but recommended */
    XRectangle r;
    XSetForeground(dpy, gc, blackp);
    r.x = ev.xexpose.x;
    r.y = ev.xexpose.y;
    r.width = ev.xexpose.width;
    r.height = ev.xexpose.height;
    XSetClipRectangles(dpy, gc, 0, 0, &r, 1, Unsorted);

    /* on with the actual drawing! */
    XDrawLine(dpy, win, gc, 100, 100, 200, 200);
    /* ... */

    /* optional -- reset the clip mask to the entire window */

    XSetClipMask(dpy, gc, None);
    }
    break;
    /* ... */
    }
    }
    }

    (Note that this program has no explicitly coded exit.
    If one hits the close button -- usually on the upper right
    of the window in most window managers -- X will complain
    and then exit. There are ways around that but they go
    beyond the scope of the current problem.)

    Animation is possible if you use XFlush() judiciously. You
    might also be having clipping issues; man XSetClipMask()
    or XSetClipRectangles() for details. In the code above the
    gc will retain the clip region indefinitely until reset,
    which could mean that if you, say, press a key (a KeyPress
    event) and reuse that gc, that gc will clip to the last
    exposed window area, which is probably not what you want.

    HTH

    > If i don't
    > do that, some pixels will not be drawn on the screen.
    > thanks
    > from Peter (cmk128@hotmail.com)
    >



    --
    #191, ewill3@earthlink.net
    Windows Vista. Because it's time to refresh your hardware. Trust us.

  3. Re: XLib draw pixel problem


    The Ghost In The Machine 寫道:

    > In comp.windows.x, cmk128@hotmail.com
    >
    > wrote
    > on 15 Sep 2006 21:41:08 -0700
    > <1158381668.275823.228020@b28g2000cwb.googlegroups. com>:
    > > Hi All
    > > void drawPixel(int x, int y, long color)
    > > {
    > > gc = XCreateGC(display, window, 0, 0);
    > >
    > > //XAllocColor(display, DefaultColormap(display,
    > > XDefaultScreen(display)), &color2);
    > > XSetForeground(display, gc, color);
    > > // XFillRectangle(display, window, gc, 0, 0, 400, 400);
    > > XDrawPoint(display, window, gc, x, y);
    > > //XFlush(display);
    > > }
    > >
    > > Why i need to call XCreateGC everytime i call my drawPixel?

    >
    > You don't. However, it might depend on how the program
    > is handling events and the GC.
    >
    > Basically, XDrawPoint() and XFillRectangle() translate into
    > packets in Xlib, which are queued until flushed. Your
    > explicit flush is commented out, and is usually not necessary
    > as a flush is done whenever XNextEvent() finds no pending
    > events.
    >
    > The following is a simplified event loop; this draws a
    > fixed line whenever (part of) the window needs to be redrawn.
    >
    > #include
    > #include
    > int main()
    > {
    > Display * dpy = XOpenDisplay(NULL);
    > int scr = DefaultScreen(dpy);
    > unsigned long blackp = BlackPixel(dpy, scr);
    > unsigned long whitep = WhitePixel(dpy, scr);
    > Window root = RootWindow(dpy, scr);
    > Window win = XCreateSimpleWindow(dpy, root, 0, 0, 400, 300, 1,
    > blackp, whitep);
    > GC gc = XCreateGC(dpy, win, 0, 0);
    > XSelectInput(dpy, win, ExposureMask);
    > XMapRaised(dpy, win);
    >
    > /* begin event loop */
    > while(1)
    > {
    > XEvent ev;
    > XNextEvent(dpy, &ev); // implicit XFlush() if no events pending
    > switch(ev.type)
    > {
    > case Expose:
    > {
    > /* the following is optional but recommended */
    > XRectangle r;
    > XSetForeground(dpy, gc, blackp);
    > r.x = ev.xexpose.x;
    > r.y = ev.xexpose.y;
    > r.width = ev.xexpose.width;
    > r.height = ev.xexpose.height;
    > XSetClipRectangles(dpy, gc, 0, 0, &r, 1, Unsorted);
    >
    > /* on with the actual drawing! */
    > XDrawLine(dpy, win, gc, 100, 100, 200, 200);
    > /* ... */
    >
    > /* optional -- reset the clip mask to the entire window */
    >
    > XSetClipMask(dpy, gc, None);
    > }
    > break;
    > /* ... */
    > }
    > }
    > }
    >
    > (Note that this program has no explicitly coded exit.
    > If one hits the close button -- usually on the upper right
    > of the window in most window managers -- X will complain
    > and then exit. There are ways around that but they go
    > beyond the scope of the current problem.)
    >
    > Animation is possible if you use XFlush() judiciously. You
    > might also be having clipping issues; man XSetClipMask()
    > or XSetClipRectangles() for details. In the code above the
    > gc will retain the clip region indefinitely until reset,
    > which could mean that if you, say, press a key (a KeyPress
    > event) and reuse that gc, that gc will clip to the last
    > exposed window area, which is probably not what you want.
    >
    > HTH
    >
    > > If i don't
    > > do that, some pixels will not be drawn on the screen.
    > > thanks
    > > from Peter (cmk128@hotmail.com)
    > >

    >
    >
    > --
    > #191, ewill3@earthlink.net
    > Windows Vista. Because it's time to refresh your hardware. Trust us.



    Thanks
    And i found this way will fix my problem, i execute the following
    code before i draw anything to display, i guess we should wait for the
    MapNotify signal before the XMapWindow function call.
    for(; {
    XEvent e;
    XNextEvent(display, &e);
    if (e.type == MapNotify)
    break;
    }
    Thanks again!!!!
    from Peter


  4. Re: XLib draw pixel problem

    In comp.windows.x, cmk128@hotmail.com

    wrote
    on 16 Sep 2006 21:22:38 -0700
    <1158466958.860149.326780@e3g2000cwe.googlegroups.c om>:
    >
    > The Ghost In The Machine ???
    >
    >> In comp.windows.x, cmk128@hotmail.com
    >>
    >> wrote
    >> on 15 Sep 2006 21:41:08 -0700
    >> <1158381668.275823.228020@b28g2000cwb.googlegroups. com>:
    >> > Hi All
    >> > void drawPixel(int x, int y, long color)
    >> > {
    >> > gc = XCreateGC(display, window, 0, 0);
    >> >
    >> > //XAllocColor(display, DefaultColormap(display,
    >> > XDefaultScreen(display)), &color2);
    >> > XSetForeground(display, gc, color);
    >> > // XFillRectangle(display, window, gc, 0, 0, 400, 400);
    >> > XDrawPoint(display, window, gc, x, y);
    >> > //XFlush(display);
    >> > }
    >> >
    >> > Why i need to call XCreateGC everytime i call my drawPixel?

    >>
    >> You don't. However, it might depend on how the program
    >> is handling events and the GC.
    >>
    >> Basically, XDrawPoint() and XFillRectangle() translate into
    >> packets in Xlib, which are queued until flushed. Your
    >> explicit flush is commented out, and is usually not necessary
    >> as a flush is done whenever XNextEvent() finds no pending
    >> events.
    >>
    >> The following is a simplified event loop; this draws a
    >> fixed line whenever (part of) the window needs to be redrawn.
    >>
    >> #include
    >> #include
    >> int main()
    >> {
    >> Display * dpy = XOpenDisplay(NULL);
    >> int scr = DefaultScreen(dpy);
    >> unsigned long blackp = BlackPixel(dpy, scr);
    >> unsigned long whitep = WhitePixel(dpy, scr);
    >> Window root = RootWindow(dpy, scr);
    >> Window win = XCreateSimpleWindow(dpy, root, 0, 0, 400, 300, 1,
    >> blackp, whitep);
    >> GC gc = XCreateGC(dpy, win, 0, 0);
    >> XSelectInput(dpy, win, ExposureMask);
    >> XMapRaised(dpy, win);
    >>
    >> /* begin event loop */
    >> while(1)
    >> {
    >> XEvent ev;
    >> XNextEvent(dpy, &ev); // implicit XFlush() if no events pending
    >> switch(ev.type)
    >> {
    >> case Expose:
    >> {
    >> /* the following is optional but recommended */
    >> XRectangle r;
    >> XSetForeground(dpy, gc, blackp);
    >> r.x = ev.xexpose.x;
    >> r.y = ev.xexpose.y;
    >> r.width = ev.xexpose.width;
    >> r.height = ev.xexpose.height;
    >> XSetClipRectangles(dpy, gc, 0, 0, &r, 1, Unsorted);
    >>
    >> /* on with the actual drawing! */
    >> XDrawLine(dpy, win, gc, 100, 100, 200, 200);
    >> /* ... */
    >>
    >> /* optional -- reset the clip mask to the entire window */
    >>
    >> XSetClipMask(dpy, gc, None);
    >> }
    >> break;
    >> /* ... */
    >> }
    >> }
    >> }
    >>
    >> (Note that this program has no explicitly coded exit.
    >> If one hits the close button -- usually on the upper right
    >> of the window in most window managers -- X will complain
    >> and then exit. There are ways around that but they go
    >> beyond the scope of the current problem.)
    >>
    >> Animation is possible if you use XFlush() judiciously. You
    >> might also be having clipping issues; man XSetClipMask()
    >> or XSetClipRectangles() for details. In the code above the
    >> gc will retain the clip region indefinitely until reset,
    >> which could mean that if you, say, press a key (a KeyPress
    >> event) and reuse that gc, that gc will clip to the last
    >> exposed window area, which is probably not what you want.
    >>
    >> HTH
    >>
    >> > If i don't
    >> > do that, some pixels will not be drawn on the screen.
    >> > thanks
    >> > from Peter (cmk128@hotmail.com)
    >> >

    >>
    >>
    >> --
    >> #191, ewill3@earthlink.net
    >> Windows Vista. Because it's time to refresh your hardware. Trust us.

    >
    >
    > Thanks
    > And i found this way will fix my problem, i execute the following
    > code before i draw anything to display, i guess we should wait for the
    > MapNotify signal before the XMapWindow function call.
    > for(; {
    > XEvent e;
    > XNextEvent(display, &e);
    > if (e.type == MapNotify)
    > break;
    > }
    > Thanks again!!!!
    > from Peter
    >


    Erm...MapNotify *is* a result of the XMapWindow/XMapRaised function
    call, if one's enabled the event mask for it. It might be appropriate to
    wait for it if you want to ensure that your window's being seen, though.

    --
    #191, ewill3@earthlink.net
    Windows Vista. Because it's time to refresh your hardware. Trust us.

+ Reply to Thread