diff --git a/README.org b/README.org index 0787b8e..fe9e1db 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. Alternatively, you can do `(run-sketch 'NAMEHERE)`. #+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/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 diff --git a/src/sketch.lisp b/src/sketch.lisp index da10b4d..222d7ad 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,10 +332,18 @@ ,@(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)))) +(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 ()