X11 - 图形渲染改进

时间:2017-02-22 10:32:54

标签: c gcc graphics rendering x11

我目前正在向窗口上的2D图像渲染无符号整数数组,但是,它对于我想用它完成的任务来说太慢了。这是我的代码:

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);

我想知道是否有人向我展示了一个更快的方法,同时仍然使用我的无符号整数数组作为基本图像绘制到窗口以及将其保留在X11 API中。我希望尽可能保持自由。我不想使用OpenGL,SDL或任何其他我不需要的额外图形库。谢谢。

1 个答案:

答案 0 :(得分:0)

我认为使用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);
}