使用XLib绘制XBM,得到黑色矩形

发布于 2024-10-12 12:43:02 字数 2649 浏览 4 评论 0原文

我正在尝试做一些我认为非常简单的事情:使用 XLib 在简单的 X 窗口上绘制硬编码的 XBM 图像。我正在使用以下代码,但只在窗口的左上角获得一个黑色矩形,而不是图像。帮助?

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

/* From http://commons.wikimedia.org/wiki/File:2008-07-25_Geese_over_00.svg
   Creative Commons BY-SA */
#define goose_width 32
#define goose_height 31
static const unsigned char goose_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x1c,
   0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x80, 0x0f,
   0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07,
   0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00, 0xfc, 0x01,
   0x00, 0x00, 0xfc, 0x01, 0x00, 0x07, 0xfe, 0x01, 0x10, 0x07, 0xff, 0x00,
   0x78, 0x0e, 0xff, 0x00, 0x80, 0x9f, 0x7f, 0x00, 0x00, 0xfe, 0x3f, 0x00,
   0x00, 0xfc, 0x1f, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0xf0, 0x0f, 0x00,
   0x00, 0xe0, 0x3f, 0x00, 0x00, 0x80, 0xff, 0x01, 0x00, 0x00, 0xf8, 0x01,
   0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00 };

void windowinit(Display *display, Window *window) {
    int screennum = DefaultScreen(display);
    long background = WhitePixel(display, screennum);
    long foreground = BlackPixel(display, screennum);
    *window = XCreateSimpleWindow(display, DefaultRootWindow(display), 10, 10, 800, 600, 2, foreground, background);
    XMapRaised(display, *window);
    XSync(display, False);

    /* Register for events */
    XSelectInput(display, *window, ButtonPressMask|ButtonReleaseMask|ExposureMask|ButtonMotionMask|KeyPressMask);
}

int main(void) {
    Display* display;
    Window window;
    GC gc;
    Pixmap i;

    /* Open display from $DISPLAY, handle errors */
    if(!(display = XOpenDisplay(NULL))) {
        fprintf(stderr, "Cannot connect to X display\n");
        return EXIT_FAILURE;
    }

    /* Set up window */
    windowinit(display, &window);

    /* Creat GC, handel errors */
    if((int)(gc = XCreateGC(display, window, 0, 0)) < 0) {
        fprintf(stderr, "XCreateGC: \n");
        return EXIT_FAILURE;
    }

    /* Draw image -- why does this draw a black rectangle ?? */
    i = XCreateBitmapFromData(display, DefaultRootWindow(display), goose_bits, goose_width, goose_height);
    XCopyPlane(display, i, window, gc, 0, 0, goose_width, goose_height, 0, 0, 1);

    XSync(display, False);
    sleep(3);

    /* Cleanup */
    XFreeGC(display, gc);
    XDestroyWindow(display, window);
    XCloseDisplay(display);

    return EXIT_SUCCESS;
}

I'm trying to do something I thought would be very simple: draw a hardcoded XBM image on a simple X window using XLib. I am using the following code, but only getting a black rectangle in the top left corner of the window instead of the image. Help?

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

/* From http://commons.wikimedia.org/wiki/File:2008-07-25_Geese_over_00.svg
   Creative Commons BY-SA */
#define goose_width 32
#define goose_height 31
static const unsigned char goose_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x1c,
   0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x80, 0x0f,
   0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07,
   0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00, 0xfc, 0x01,
   0x00, 0x00, 0xfc, 0x01, 0x00, 0x07, 0xfe, 0x01, 0x10, 0x07, 0xff, 0x00,
   0x78, 0x0e, 0xff, 0x00, 0x80, 0x9f, 0x7f, 0x00, 0x00, 0xfe, 0x3f, 0x00,
   0x00, 0xfc, 0x1f, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0xf0, 0x0f, 0x00,
   0x00, 0xe0, 0x3f, 0x00, 0x00, 0x80, 0xff, 0x01, 0x00, 0x00, 0xf8, 0x01,
   0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00 };

void windowinit(Display *display, Window *window) {
    int screennum = DefaultScreen(display);
    long background = WhitePixel(display, screennum);
    long foreground = BlackPixel(display, screennum);
    *window = XCreateSimpleWindow(display, DefaultRootWindow(display), 10, 10, 800, 600, 2, foreground, background);
    XMapRaised(display, *window);
    XSync(display, False);

    /* Register for events */
    XSelectInput(display, *window, ButtonPressMask|ButtonReleaseMask|ExposureMask|ButtonMotionMask|KeyPressMask);
}

int main(void) {
    Display* display;
    Window window;
    GC gc;
    Pixmap i;

    /* Open display from $DISPLAY, handle errors */
    if(!(display = XOpenDisplay(NULL))) {
        fprintf(stderr, "Cannot connect to X display\n");
        return EXIT_FAILURE;
    }

    /* Set up window */
    windowinit(display, &window);

    /* Creat GC, handel errors */
    if((int)(gc = XCreateGC(display, window, 0, 0)) < 0) {
        fprintf(stderr, "XCreateGC: \n");
        return EXIT_FAILURE;
    }

    /* Draw image -- why does this draw a black rectangle ?? */
    i = XCreateBitmapFromData(display, DefaultRootWindow(display), goose_bits, goose_width, goose_height);
    XCopyPlane(display, i, window, gc, 0, 0, goose_width, goose_height, 0, 0, 1);

    XSync(display, False);
    sleep(3);

    /* Cleanup */
    XFreeGC(display, gc);
    XDestroyWindow(display, window);
    XCloseDisplay(display);

    return EXIT_SUCCESS;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒中人 2024-10-19 12:43:02

我发现了问题。我需要调用:

XSetBackground(display, gc, WhitePixel(display, DefaultScreen(display)));

I found the problem. I needed to call:

XSetBackground(display, gc, WhitePixel(display, DefaultScreen(display)));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文