c++ 创建带有帧缓冲区的xlib窗口,可以直接绘制并使用XPutImage

nc1teljy  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(189)

我尝试创建一个xlib窗口,创建一个深度为32的帧缓冲区,并将该缓冲区绘制到窗口中,然而,一切正常,直到XPutImage被调用,窗口从未显示,控制台输出:

Process returned -1 (0xFFFFFFFF) execution time : ?.??? s
Press ENTER to continue;

如果我在Expose事件中注解掉XPutImage行,那么我会得到一个拥有透明客户区的窗口,所以我正在寻找如何解决这个问题的答案。
注意我是Linux编程的新手,但是我已经做了很长时间的windows编程,所以我对Linux函数和协议还不熟悉;)
我在Fedora 32(64位)上使用Code::Blocks 20.03。
代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

int main(int argc, char **argv)
{
    Display *dpy;
    XVisualInfo vinfo;
    int depth;
    XVisualInfo *visual_list;
    XVisualInfo visual_template;
    int nxvisuals;
    int i;
    XSetWindowAttributes attrs;
    Window parent;
    Visual *visual;

    int width, height;
    Window win;
    int *framebuf;
    XImage *ximage;
    XEvent event;

    dpy = XOpenDisplay(NULL);

    nxvisuals = 0;
    visual_template.screen = DefaultScreen(dpy);
    visual_list = XGetVisualInfo (dpy, VisualScreenMask, &visual_template, &nxvisuals);

    for (i = 0; i < nxvisuals; ++i)
    {
        printf("  %3d: visual 0x%lx class %d (%s) depth %d\n",
               i,
               visual_list[i].visualid,
               visual_list[i].class,
               visual_list[i].class == TrueColor ? "TrueColor" : "unknown",
               visual_list[i].depth);
    }

    if (!XMatchVisualInfo(dpy, XDefaultScreen(dpy), 32, TrueColor, &vinfo))
    {
        fprintf(stderr, "no such visual\n");
        return 1;
    }

    printf("Matched visual 0x%lx class %d (%s) depth %d\n",
           vinfo.visualid,
           vinfo.class,
           vinfo.class == TrueColor ? "TrueColor" : "unknown",
           vinfo.depth);

    parent = XDefaultRootWindow(dpy);

    XSync(dpy, True);

    printf("creating RGBA child\n");

    visual = vinfo.visual;
    depth = vinfo.depth;

    attrs.colormap = XCreateColormap(dpy, XDefaultRootWindow(dpy), visual, AllocNone);
    attrs.background_pixel = 0;
    attrs.border_pixel = 0;

    width = 1000;
    height = 700;

    framebuf = malloc((width*height)*4);

    for (i = 0; i < (width*height); i++)
    {
        framebuf[i] = 0xFFFFFFFF;
    }

    win = XCreateWindow(dpy, parent, 100, 100, width, height, 0, depth, InputOutput,
                        visual, CWBackPixel | CWColormap | CWBorderPixel, &attrs);

    ximage = XCreateImage(dpy, vinfo.visual, 32, XYPixmap, 0, (char *)framebuf, width, height, 8, width*4);

    if (ximage == 0)
    {
        printf("ximage is null!\n");
    }

    XSync(dpy, True);

    XSelectInput(dpy, win, ExposureMask | KeyPressMask);

    XGCValues gcv;
    unsigned long gcm;
    GC NormalGC;

    //gcm = GCForeground | GCBackground | GCGraphicsExposures;
    //gcv.foreground = BlackPixel(dpy, parent);
    //gcv.background = WhitePixel(dpy, parent);
    gcm = GCGraphicsExposures;
    gcv.graphics_exposures = 0;
    NormalGC = XCreateGC(dpy, parent, gcm, &gcv);

    XMapWindow(dpy, win);

    while(!XNextEvent(dpy, &event))
    {
        switch(event.type)
        {
        case Expose:
            printf("I have been exposed!\n");
            XPutImage(dpy, win, NormalGC, ximage, 0, 0, 0, 0, width, height);
            break;
        }
    }

    printf("No error\n");

    return 0;
}
f2uvfpb9

f2uvfpb91#

为了让它工作,我不得不改变代码中的两行。你可能会不高兴,因为为了让它工作,我不得不把它从RGBA改为BGRX。每当我使用xlib时,我总是不得不使用24位深度,即使数据是以32位存储的。它也是以BGRX而不是RGBX存储的...
下面是更改后的代码。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

int main(int argc, char **argv)
{
    Display *dpy;
    XVisualInfo vinfo;
    int depth;
    XVisualInfo *visual_list;
    XVisualInfo visual_template;
    int nxvisuals;
    int i;
    XSetWindowAttributes attrs;
    Window parent;
    Visual *visual;

    int width, height;
    Window win;
    int *framebuf;
    XImage *ximage;
    XEvent event;

    dpy = XOpenDisplay(NULL);

    nxvisuals = 0;
    visual_template.screen = DefaultScreen(dpy);
    visual_list = XGetVisualInfo (dpy, VisualScreenMask, &visual_template, &nxvisuals);

    //Change to this line
    //if (!XMatchVisualInfo(dpy, XDefaultScreen(dpy), 32, TrueColor, &vinfo))
    if (!XMatchVisualInfo(dpy, XDefaultScreen(dpy), 24, TrueColor, &vinfo))
    {
        fprintf(stderr, "no such visual\n");
        return 1;
    }

    parent = XDefaultRootWindow(dpy);

    XSync(dpy, True);

    printf("creating RGBA child\n");

    visual = vinfo.visual;
    depth = vinfo.depth;

    attrs.colormap = XCreateColormap(dpy, XDefaultRootWindow(dpy), visual, AllocNone);
    attrs.background_pixel = 0;
    attrs.border_pixel = 0;

    width = 1000;
    height = 700;

    framebuf = (int *) malloc((width*height)*4);

    for (i = 0; i < (width*height); i++)
    {
        framebuf[i] = 0xFF00FFFF;
    }

    win = XCreateWindow(dpy, parent, 100, 100, width, height, 0, depth, InputOutput,
                        visual, CWBackPixel | CWColormap | CWBorderPixel, &attrs);

    //Change to this line
    //ximage = XCreateImage(dpy, vinfo.visual, 32, XYPixmap, 0, (char *)framebuf, width, height, 8, width*4);
    ximage = XCreateImage(dpy, vinfo.visual, depth, ZPixmap, 0, (char *)framebuf, width, height, 8, width*4);

    if (ximage == 0)
    {
        printf("ximage is null!\n");
    }

    XSync(dpy, True);

    XSelectInput(dpy, win, ExposureMask | KeyPressMask);

    XGCValues gcv;
    unsigned long gcm;
    GC NormalGC;

    //gcm = GCForeground | GCBackground | GCGraphicsExposures;
    //gcv.foreground = BlackPixel(dpy, parent);
    //gcv.background = WhitePixel(dpy, parent);
    gcm = GCGraphicsExposures;
    gcv.graphics_exposures = 0;
    NormalGC = XCreateGC(dpy, parent, gcm, &gcv);

    XMapWindow(dpy, win);

    while(!XNextEvent(dpy, &event))
    {
        switch(event.type)
        {
        case Expose:
            printf("I have been exposed!\n");
            XPutImage(dpy, win, NormalGC, ximage, 0, 0, 0, 0, width, height);
            break;
        }
    }

    printf("No error\n");

    return 0;
}
3qpi33ja

3qpi33ja2#

您正在XCreateGC(dpy, parent, gcm, &gcv);中使用父级的Drawable,而不是窗口自己的Drawable。
要使用32位颜色,请将其更改为XCreateGC(dpy, win, gcm, &gcv);
我知道我晚了几年,但我希望这能帮助到一些人。

相关问题