X11 - 图形渲染改进
问题描述:
我目前正在将一个无符号整数数组呈现给窗口上的2D图像,但是,对于我想要完成的任务来说,这太慢了。这里是我的代码:X11 - 图形渲染改进
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或任何其他我不需要的额外图形库。谢谢。
答
我觉得用可以回答你的需要:看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'快,但要真快一个具有直接操作的像素。见例如[本](ftp://ftp.ccp4.ac.uk/ccp4/7.0/unpacked/checkout/gdk-pixbuf-2.28.1/contrib/gdk-pixbuf-xlib/gdk-pixbuf-xlib-drawable.c)作为直接像素操作的例子。这并不漂亮。 –
它的工作速度更快!我一定会看看你附上的这个代码文件,谢谢。 – SoleCore