X11 - 图形渲染改进

X11 - Graphics Rendering Improvement

我目前正在 window 上将一组无符号整数渲染为二维图像,但是,对于我想要用它完成的任务来说,它太慢了。这是我的代码:

int x = 0;
int y = 0;

GC gc;
XGCValues gcv;
gc = XCreateGC(display, drawable, GCForeground, &gcv);

while (y < height) {
    while (x < width) {
            XSetForeground(display, gc, AlphaBlend(pixels[(width*y)+x], backcolor));
            XDrawPoint(display, drawable, gc, x, y);
            x++;
    }
    x = 0;
    y++;
}

XFlush(display);

我想知道是否有人向我展示了一种更快的方法来执行此操作,同时仍然使用我的无符号整数数组作为基础图像绘制到 window 并将其保持在 X11 API.我想让它尽可能独立。我不想使用 OpenGL、SDL 或任何其他我不需要的额外图形库。谢谢。

我认为使用 XImage 可以满足您的需求:请参阅 https://tronche.com/gui/x/xlib/graphics/images.html

XImage * s_image;

void init(...)
{
    /* data linked to image, 4 bytes per pixel */
    char *data = calloc(width * height, 4);
    /* image itself */
    s_image = XCreateImage(display, 
        DefaultVisual(display, screen),
        DefaultDepth(display, screen), 
        ZPixmap, 0, data, width, height, 32, 0);
}

void display(...)
{
    /* fill the image */    
    size_t offset = 0;
    y = 0;
    while (y < height) {  
        x = 0;
        while (x < width) {
            XPutPixel(s_image, x, y, AlphaBlend((pixels[offset++], backcolor));
            x++;
        }    
        y++;
    }

    /* put image on display */
    XPutImage(display, drawable, cg, s_image, 0, 0, 0, 0, width, height);

    XFlush(display);
}