簡體   English   中英

使用幀緩沖區創建 xlib window 我可以直接繪制並使用 XPutImage

[英]Create xlib window with a frame buffer i can draw directly and use XPutImage

我正在嘗試創建一個 xlib window,創建一個深度為 32 的幀緩沖區並將該緩沖區繪制到 window,但是。 在調用 XPutImage 之前一切正常,window 永遠不會顯示並且控制台輸出:

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

如果我在 Expose 事件中注釋掉 XPutImage 行,那么我會得到一個 window,它具有所需的透明客戶區。 所以我正在尋找如何解決這個問題的答案。

注意我是 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;
}

為了讓它工作,我不得不更改代碼中的兩行。 您可能會不高興,因為要讓它工作,我必須將它從 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;
}

您在XCreateGC(dpy, parent, gcm, &gcv);中使用父級的 Drawable 而不是窗口自己的。

要使用 32 位顏色,請將其更改為XCreateGC(dpy, win, gcm, &gcv);

我知道我遲到了幾年,但我希望這對某人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM