From 333ee35652fa72464e9453e445c5530b290a357f Mon Sep 17 00:00:00 2001 From: Kevin Galligan Date: Wed, 17 Apr 2024 13:30:15 +0100 Subject: [PATCH 1/3] Make defsketch generate a function for running sketches. This provides better abstraction for sketches, the CLOS details of their implementation don't leak as much. --- README.org | 19 ++++++++++--------- src/sketch.lisp | 10 ++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/README.org b/README.org index 0787b8e..8828d00 100644 --- a/README.org +++ b/README.org @@ -88,13 +88,14 @@ If you did everything correctly, you should be able to =(ql:quickload :sketch)= #+BEGIN_SRC lisp CL-USER> (ql:quickload :sketch-examples) -CL-USER> (make-instance 'sketch-examples:hello-world) -CL-USER> (make-instance 'sketch-examples:sinewave) -CL-USER> (make-instance 'sketch-examples:brownian) -CL-USER> (make-instance 'sketch-examples:life) ; Click to toggle cells, - ; any key to toggle iteration -CL-USER> (make-instance 'sketch-examples:input) -CL-USER> (make-instance 'sketch-examples:stars) +CL-USER> (in-package sketch-examples) +SKETCH-EXAMPLES> (run-hello-world) +SKETCH-EXAMPLES> (run-sinewave) +SKETCH-EXAMPLES> (run-brownian) +SKETCH-EXAMPLES> (run-life) ; Click to toggle cells, + ; any key to toggle iteration +SKETCH-EXAMPLES> (run-input) +SKETCH-EXAMPLES> (run-stars) #+END_SRC *** Running example code from this page @@ -108,11 +109,11 @@ TUTORIAL> ;; ready #+END_SRC ** Tutorial -Defining sketches is done with the =defsketch= macro, which is essentially a wrapper for =defclass=. +Defining sketches is done with the =defsketch= macro. This defines a function called `run-NAMEHERE` for starting your sketch. #+BEGIN_SRC lisp (defsketch tutorial ()) - (make-instance 'tutorial) + (run-tutorial) #+END_SRC If all goes well, this should give you an unremarkable gray window. From now on, assuming you're using Emacs + SLIME, or a similarly capable environment, you can just re-evaluate =(defsketch tutorial () )= and the sketch will be restarted without you having to close the window or make another instance of the class. diff --git a/src/sketch.lisp b/src/sketch.lisp index da10b4d..64fc56b 100644 --- a/src/sketch.lisp +++ b/src/sketch.lisp @@ -315,6 +315,15 @@ collect `(,(binding-accessor b) *sketch*) collect (binding-name b))))) +(defun define-sketch-run-function (name bindings) + `(defun ,(alexandria:symbolicate 'run- name) + (&rest args + &key ,@(loop for b in bindings + collect (list (binding-name b) (binding-initform b))) + &allow-other-keys) + (declare (ignore ,@(loop for b in bindings collect (binding-name b)))) + (apply #'make-instance ',name args))) + (defmacro defsketch (sketch-name binding-forms &body body) (let ((bindings (parse-bindings sketch-name binding-forms (class-bindings (find-class 'sketch))))) @@ -323,6 +332,7 @@ ,@(define-sketch-channel-observers bindings) ,(define-sketch-prepare-method sketch-name bindings) ,(define-sketch-draw-method sketch-name bindings body) + ,(define-sketch-run-function sketch-name bindings) (make-instances-obsolete ',sketch-name) (find-class ',sketch-name)))) From 5afd17e1fee93941af71d41dd375c37d401b43a5 Mon Sep 17 00:00:00 2001 From: Kevin Galligan Date: Mon, 22 Apr 2024 16:23:40 +0100 Subject: [PATCH 2/3] Add a shared `run-sketch` function. --- src/sketch.lisp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/sketch.lisp b/src/sketch.lisp index 64fc56b..222d7ad 100644 --- a/src/sketch.lisp +++ b/src/sketch.lisp @@ -337,6 +337,13 @@ (make-instances-obsolete ',sketch-name) (find-class ',sketch-name)))) +(defun run-sketch (name &rest args) + (let ((cls (find-class name nil))) + (when (or (null cls) + (not (c2mop:subclassp cls (find-class 'sketch)))) + (error (format nil "Couldn't find a sketch called ~a" name))) + (apply #'make-instance name args))) + ;;; Control flow (defun stop-loop () From 29abf59bf1164c3ef71864e0e0c961c26be34187 Mon Sep 17 00:00:00 2001 From: Kevin Galligan Date: Mon, 22 Apr 2024 16:28:24 +0100 Subject: [PATCH 3/3] Update package.lisp and README. --- README.org | 2 +- src/package.lisp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.org b/README.org index 8828d00..fe9e1db 100644 --- a/README.org +++ b/README.org @@ -109,7 +109,7 @@ TUTORIAL> ;; ready #+END_SRC ** Tutorial -Defining sketches is done with the =defsketch= macro. This defines a function called `run-NAMEHERE` for starting your sketch. +Defining sketches is done with the =defsketch= macro. This defines a function called `run-NAMEHERE` for starting your sketch. Alternatively, you can do `(run-sketch 'NAMEHERE)`. #+BEGIN_SRC lisp (defsketch tutorial ()) diff --git a/src/package.lisp b/src/package.lisp index 8b10f07..6fbeb28 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -15,6 +15,7 @@ :draw :defsketch + :run-sketch :sketch-title :sketch-width