2009-11-16 57 views
7

我最近開始學習Lisp,想寫一個使用gtk接口的程序。我已經安裝了lambda-gtk綁定(在CMUCL上)。我想在pixbuf上使用putpixel/getpixel功能。但是我發現我無法直接訪問內存。 (或者只是不知道如何)Lisp指針?

函數(gdk:pixbuf-get-pixels pixbuf)給我一個數字 - 內存地址,我猜。在C++中,我可以輕鬆地獲取所需的像素。有沒有辦法在Lisp中編寫我自己的putpixel?

回答

7

在Lisp中,訪問C庫和直接內存訪問的現代便攜方式是CFFI

您可以使用它像這樣:

>(defparameter *p* (cffi:foreign-alloc :unsigned-char :count 10)) 
;; allocate 10 bytes 
*P* 
> (setf (cffi:mem-aref *p* :unsigned-char 0) 10) 
;; access *p* as an array of bytes and set its 0th element to 10 
10 
> (cffi:mem-aref *p* :unsigned-char 0) 
;; access *p* as an array of bytes and take its 0th element 
10 
> (cffi:make-pointer 123) 
;; make a pointer that points to given address 
#.(SB-SYS:INT-SAP #X0000007B)