2017-02-22 264 views
0

我目前正在將一個無符號整數數組呈現給窗口上的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或任何其他我不需要的額外圖形庫。謝謝。

回答

0

我覺得用​​可以回答你的需要:看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); 
} 
+0

'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)作爲直接像素操作的例子。這並不漂亮。 –

+0

它的工作速度更快!我一定會看看你附上的這個代碼文件,謝謝。 – SoleCore