[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: making patterns the hard way



wilcox@cmns.think.com writes:
  I tried to make a pattern using make-record, but it didn't work. Although
  make-record macro-expanded into something reasonable, the resulting pattern
  was not correct. Why?
    (defvar *grid-x-pattern* (make-record :pattern
                                      (:array 0) 255
                                      (:array 1) 0
                                      (:array 2) 0
                                      (:array 3) 0
                                      (:array 4) 255
                                      (:array 5) 0
                                      (:array 6) 0
                                      (:array 7) 0
                                      ))
 
  I ended up doing it this way:
 
  ;;; modified from examples:View-Example.lisp
  (defun make-pattern (bytes)
    (let ((result (#_NewPtr 8))
          (i 0))
      (dolist (b bytes)
        (%put-byte result b i)
        (incf i))
      result))
 
  (defvar *grid-x-pattern* (make-pattern '(255 0 0 0 255 0 0 0)))
 

************ First a question:
Why does the following expression print the b7 field?
? (print-record *black-pattern* :pattern)
#<Record :PATTERN :W0 -1 :W1 -1 :W2 -1 :W3 -1 :B7 255>

Here is code that may do you want:

(defun get-pattern (pattern)
  "Given a pattern record,
returns a list with four integer values, representing the pattern"
   (list (rref pattern :pattern.b0)
         (rref pattern :pattern.b1)
         (rref pattern :pattern.b2)
         (rref pattern :pattern.b3)
         (rref pattern :pattern.b4)
         (rref pattern :pattern.b5)
         (rref pattern :pattern.b6)
         (rref pattern :pattern.b7)))

(defun get-pattern (pattern)
  "Given a pattern record,
returns a list with eight byte values, representing the pattern"
  (loop for i from 0 to (1- (record-length :pattern))
        collect (raref pattern :pattern.bytes i)))

(defun set-pattern (list-pattern &optional (pattern (make-record :pattern)))
  "Given a pattern, sets the eight byte fields of a pattern to the elements of the list"
  (loop for i from 0 to (1- (min (length list-pattern) 8))
        do (rarset pattern (nth i list-pattern) :pattern.bytes i))
  pattern)

(defvar *grid-x-pattern* (set-pattern '(255 0 0 0 255 0 0 0)))

?(get-pattern *grid-x-pattern*)
(255 0 0 0 255 0 0 0)

?(print-record *grid-x-pattern* :pattern)
#<Record :PATTERN :W0 -256 :W1 0 :W2 -256 :W3 0 :B7 0>

?(set-pattern '(1 2 3 4 5 6 7 8) *grid-x-pattern* )
? (get-pattern *grid-x-pattern*)
(1 2 3 4 5 6 7 8)
? (print-record *grid-x-pattern* :pattern)
#<Record :PATTERN :W0 258 :W1 772 :W2 1286 :W3 1800 :B7 8>
? (print-record *black-pattern* :pattern)
#<Record :PATTERN :W0 -1 :W1 -1 :W2 -1 :W3 -1 :B7 255>