From 55acc817ad919c47de39e0febe8630304792793f Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Fri, 15 Jul 2022 22:08:13 -0500 Subject: [example] tweaking canvas example --- src/interactive/canvas.lisp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/interactive/canvas.lisp b/src/interactive/canvas.lisp index 2fa341c..ce7829b 100644 --- a/src/interactive/canvas.lisp +++ b/src/interactive/canvas.lisp @@ -12,6 +12,9 @@ :element-type 'unsigned-byte :initial-element 255)))) +(defun pixel-offset (x y pixels) + (* 4 (+ x (* y (pixel-width pixels))))) + (let ((cached-pixel)) (defun pixel (pixels x y &optional (use-cached t)) "When USE-CACHED is NIL, return a fresh array displaced to @@ -50,8 +53,7 @@ (,a (aref ,pixel-var 3))) ,@body)))) -(defun pixel-offset (x y pixels) - (* 4 (+ x (* y (pixel-width pixels))))) + (defmacro with-pixels-rect ((x y r g b a) (pixels &key left right top bottom) &body body) "Executes BODY on all pixels in the box bounded by LEFT RIGHT TOP and BOTTOM of PIXELS. @@ -73,11 +75,15 @@ (loop for ,x from (if ,lv ,lv 0) below (if ,rv ,rv (pixel-width ,pxs)) do (loop for ,y from (if ,bv ,bv 0) below (if ,tv ,tv (pixel-height ,pxs)) do (progn - (setf ,px (adjust-array ,px 4 - :displaced-to (pixels-data ,pxs) - :displaced-index-offset (pixel-offset ,x ,y ,pxs))) + (setf ,px (adjust-array ,px 4 + :displaced-to (pixels-data ,pxs) + :displaced-index-offset (pixel-offset ,x ,y ,pxs))) ,@body))))))) +(defun clear-canvas (canvas &key (r 0) (g 0) (b 0) (a 255)) + (with-pixels-rect (x y pr pg pb pa) (canvas) + (setf pr r pg g pb b pa a))) + (defmacro with-pixels-line ((x y r g b a) (pixels start-x start-y end-x end-y) &body body) "A convenience macro for doing something to a whole line of pixels - e.g., drawing a line in a particular color." -- cgit v1.2.3