libX11:XPutImage第一次调用

时间:2015-06-16 07:36:30

标签: c x11

我使用XCreateImage创建了XImage并使用XPutImage在窗口上显示它,但XPutImage仅在第二次调用时显示此图片。为什么会这样?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

void draw(char *rgb_out, int w, int h)
{
        int i = 0;

        for (i = 0;i < w*h;i += 4) {
                rgb_out[i + 1] = 0;
                rgb_out[i + 2] = 0;
                rgb_out[i + 3] = 0;
        }

        return;
}

XImage *create_ximage(Display *display, Visual *visual, int width, int height)
{
        char *image32 = (char *)malloc(width * height * 4);
        draw(image32, width, height);
        return XCreateImage(display, visual, 24,
                            ZPixmap, 0, image32,
                            width, height, 32, 0);
}

int main(int argc, char **argv)
{
        int win_b_color;
        int win_w_color;
        XEvent xev;
        Window window;
        GC gc;
        Display *display = XOpenDisplay(NULL);
        Visual *visual;
        XImage *ximage;

        win_b_color = BlackPixel(display, DefaultScreen(display));
        win_w_color = BlackPixel(display, DefaultScreen(display));
        window = XCreateSimpleWindow(display,
                                DefaultRootWindow(display),
                                0, 0, 600, 400, 0,
                                win_b_color, win_w_color);

        gc = XCreateGC(display, window, 0, NULL);
        visual = DefaultVisual(display, 0);

        XMapWindow(display, window);
        XFlush(display);
        ximage = create_ximage(display, visual, 100, 100);
        while (1) {
                int r;

                r = XPutImage(display, window,
                        gc, ximage, 0, 0, 0, 0,
                        100, 100);
                printf("RES: %i\n", r);
                XSync(display, 1);
                XFlush(display);
                getchar();
        }

        return 0;
}

1 个答案:

答案 0 :(得分:2)

诀窍是等待窗口映射。你可以通过Expose事件来做到这一点。

int main(int argc, char **argv)
{
        int win_b_color;
        int win_w_color;
        XEvent xev;
        Window window;
        GC gc;
        Display *display = XOpenDisplay(NULL);
        Visual *visual;
        XImage *ximage;

        win_b_color = BlackPixel(display, DefaultScreen(display));
        win_w_color = BlackPixel(display, DefaultScreen(display));
        window = XCreateSimpleWindow(display,
                                DefaultRootWindow(display),
                                0, 0, 600, 400, 0,
                                win_b_color, win_w_color);

        visual = DefaultVisual(display, 0);

        XSelectInput(display, window, ExposureMask | KeyPressMask);

        XMapWindow(display, window);
        XFlush(display);
        gc = XCreateGC(display, window, 0, NULL);
        ximage = create_ximage(display, visual, 100, 100);
        XEvent event;
        bool exit = false;

        while (!exit) {
                int r;

                XNextEvent(display, &event);

                if (event.type == Expose)
                {
                    r = XPutImage(display, window,
                            gc, ximage, 0, 0, 0, 0,
                            100, 100);
                    printf("RES: %i\n", r);
                }
                else if (event.type == KeyPress)
                    exit = true;
        }

        return 0;
}