Split matrix-loops into separate do-for-cell macro

This commit is contained in:
Jaidyn Ann 2023-06-03 22:09:23 -05:00
parent ce3dcebbb4
commit 6fd48fd2dc

View File

@ -35,75 +35,77 @@ Borrowed from https://github.com/gorozhin/chlorophyll/
(format stream "~C[J" #\Esc)) (format stream "~C[J" #\Esc))
(defmacro do-for-cell (matrix &body body)
"Given a 2d-array (matrix), execute the body for every cell.
The body has access to 4 variables:
* i/j The current row/column.
* dimensions Dimensions of the given matrix.
* cell The value of the current cell."
`(let* ((dimensions (array-dimensions matrix))
(max-i (car dimensions))
(max-j (cadr dimensions))
(i 0) (j 0))
(loop
(let ((cell (ignore-errors (aref matrix i j))))
(cond
((< i max-i)
(cond
((< j max-j)
,@body
(incf j))
((eq j max-j)
(setf j 0)
(incf i))))
((eq i max-i)
(return)))))))
(defun matrix-delta (a b) (defun matrix-delta (a b)
"Given two 2D matrices, return a listcontaining on the cells that change between ab in the following "Given two 2D matrices, return a listcontaining on the cells that change between ab in the following
format: format:
only the cells that change between ab all others are nil." only the cells that change between ab all others are nil."
(let* ((dimensions (array-dimensions a)) (let ((delta (make-array (array-dimensions a))))
(delta (make-array dimensions :initial-element nil)) (do-for-cell a
(max-i (car dimensions)) (when (not (eq cell
(max-j (cadr dimensions))
(i 0) (j 0))
(loop
(cond
((< i max-i)
(cond
((< j max-j)
(if (not (eq (aref a i j)
(aref b i j)))
(setf (aref delta i j)
(aref b i j))) (aref b i j)))
(incf j)) (setf (aref delta i j)
((eq j max-j) (aref b i j))))
(setf j 0) delta))
(incf i))))
((eq i max-i)
(return))))
delta))
(defun print-screen-matrix (array) (defun print-screen-matrix (matrix)
"Given a matrix of characters, print each element to standard output." "Given a matrix of characters, print each element to standard output."
(let* ((dimensions (array-dimensions array)) (do-for-cell matrix
(max-i (car dimensions)) (when (characterp cell)
(max-j (cadr dimensions)) (move-cursor (+ i 1) (+ j 1))
(i 0) (j 0)) (write-char cell))))
(loop
(cond
((< i max-i)
(cond
((< j max-j)
(if (characterp (aref array i j))
(progn
(move-cursor i j)
(write-char (aref array i j))))
(incf j))
((eq j max-j)
(setf j 0)
(incf i))))
((eq i max-i)
(return))))))
(defun screen-matrix-set-map (array map-path) (defun make-screen-matrix ()
"Create a screen matrix that is, a 2D array representing the
72x20 grid of characters we can print to the terminal."
(make-array '(20 72) :initial-element #\space))
(defun screen-matrix-set-map (matrix map-path)
"Draw a Tiled-format tilemap to the 2D array." "Draw a Tiled-format tilemap to the 2D array."
(mapcar (lambda (layer) (screen-matrix-set-map-layer array layer)) (mapcar (lambda (layer) (screen-matrix-set-map-layer matrix layer))
(cl-tiled:map-layers (cl-tiled:load-map map-path))) (cl-tiled:map-layers (cl-tiled:load-map map-path)))
array) matrix)
(defun screen-matrix-set-map-layer (array tile-layer) (defun screen-matrix-set-map-layer (matrix tile-layer)
"Set an array's elements to those corresponding the given Tiled "Set an array's elements to those corresponding the given Tiled
tile-layer's cells. a Tiled tile-layer to the screen." tile-layer's cells. a Tiled tile-layer to the screen."
(mapcar (lambda (cell) (screen-matrix-set-char-cell array cell)) (mapcar (lambda (cell) (screen-matrix-set-char-cell matrix cell))
(cl-tiled:layer-cells tile-layer)) (cl-tiled:layer-cells tile-layer))
array) matrix)
(defun screen-matrix-set-char-cell (array cell) (defun screen-matrix-set-char-cell (matrix cell)
"Set a 2D array's element corresponding to a Tiled cell's "Set a matrice's (2d array's) element corresponding to
character-value, using it's column and row." a Tiled cell's character-value, using it's column and row."
(setf (aref array (setf (aref matrix
(cl-tiled:cell-row cell) (cl-tiled:cell-row cell)
(cl-tiled:cell-column cell)) (cl-tiled:cell-column cell))
(tile-character (tile-character
@ -118,5 +120,3 @@ with 15 characters-per-line."
(+ (* (cl-tiled:tile-row tile) 15) (+ (* (cl-tiled:tile-row tile) 15)
(cl-tiled:tile-column tile) (cl-tiled:tile-column tile)
32))) 32)))
(main)