【问题标题】:X11 - Graphics Rendering ImprovementX11 - 图形渲染改进
【发布时间】:2017-02-22 10:32:54
【问题描述】:

我目前正在将一个无符号整数数组渲染到一个窗口上的 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 或任何其他我不需要的额外图形库。谢谢。

【问题讨论】:

    标签: c gcc graphics rendering x11


    【解决方案1】:

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

    【讨论】:

    • XPutPixel 当然比XDrawPoint 快,但要真正快,必须直接操作像素。参见例如this 用于直接像素操作的示例。不漂亮。
    • 它工作得更快!我一定会看看你附上的这个代码文件,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2018-08-10
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2013-09-29
    相关资源
    最近更新 更多