Search code examples
common-lispx11

Drawing a rectangle using CLX


I am trying to draw a rectangle inside a window using Common Lisp X Interface:

(asdf:load-system "clx")

(in-package :xlib)

(defun main ()
  (let* ((display (open-default-display))
         (screen (display-default-screen display))
         (colormap (screen-default-colormap screen))
         (font (open-font display "fixed")))
    (let* ((window (create-window
                    :parent (screen-root screen)
                    :x 0
                    :y 0
                    :width 512
                    :height 512
            :border (screen-black-pixel screen)
            :border-width 2
            :bit-gravity :center
            :colormap colormap
                    :background (alloc-color colormap
                                             (lookup-color colormap
                                                                     "white"))))
           (foreground (create-gcontext
                        :drawable window
                        :fill-style :solid
                        :background (screen-white-pixel screen)
                        :foreground (alloc-color colormap
                                                      (lookup-color
                                                       colormap
                                                       "red"))
                        :font font)))
      (map-window window)
      (unwind-protect
       (progn
         (draw-rectangle window foreground 0 0 100 100 :fill-p) ;; no effect
         (display-force-output display)
         (display-finish-output display)
         (sleep 2))
    (CLOSE-DISPLAY display)))))

What I get is just an empty window. Could you please tell me what I am doing wrong. Thanks.


Solution

  • Interesting. After playing with your code a considerable time, I made it work within a loop. It seems like clx wants you to perform the drawing at least once? Here, it works if you replace your progn with a loop for example:

    ...
    (unwind-protect
           (loop repeat 3
                 do
             (draw-rectangle window foreground 0 0 100 100 :fill-p) ;; no effect
             (display-force-output display)
             ;;(display-finish-output display)
             (sleep 1))
        (CLOSE-DISPLAY display)))))