Skip to content
Eric Laspe edited this page Jun 2, 2022 · 11 revisions

How to create runnable jar
How to disable exit behaviour on esc button
How to close sketch programmatically from outside
I cannot see sketch on OS X
How to use processing plugin

How to create runnable jar

Tutorial on runnable jars in Quil

How to disable exit behaviour on esc button

By default Quil closes sketches when esc button is pressed. To disable it we should change applet field that holds currently pressed key to 0 (or any other button except esc). It should be done from inside key-pressed handler. Example:

(defn key-pressed []
  (when (= 27 (q/key-code)) ; 27 is escape key.
    ; Preventing esc from closing the sketch by setting current key to 0.
    (set! (.key (quil.applet/current-applet)) (char 0))))
 
(q/defsketch my-sketch
  ...
  :key-pressed key-pressed)
How to close sketch programmatically from outside

Sometimes you might need to close sketch programmatically. For example sketch is a part of a system in Stuart Sierra's Component. Quil provides exit function but it works only from draw or other handlers. So it doesn't go with outside world very well. Alternatively you can call .exit method on sketch instance:

(defsketch my-sketch
  ...)

; Time to close.
(.exit my-sketch)
I cannot see sketch on OS X

When you launch sketch on OS X in some cases it is created behind your current window which is terminal or IDE. If the current window is fullscreen the chance is high that you won't see the sketch. As a workaround you can quit fullscreen. You can force your sketch to the front of all windows by adding the :features [:keep-on-top] argument to defsketch:

(defsketch my-sketch
  ...
  :features [:keep-on-top])
How to use Processing plugin

Quil is based on Processing so theoretically all plugins that you use in Processing can be used in Quil as well. First of all, you need to install them.

Installing

If they're available in Maven/Clojars - just add them to project.clj. Otherwise download plugin files and put them somewhere in in project directory. Then add path to the files in :resource-paths in project.clj.

Using

To use plugin you'll have to use java interop features as plugin was written in java so we're in Java world now. Let's assume that on plugin README you see following example:

import some.awesome.plugin.AwesomeFoo;

AwesomeFoo foo;

void setup() {
  size(400,400, P3D);
  // Initialize plugin
  foo = new AwesomeFoo(this);
}

void draw() {
  // draw...
  // call plugin
  foo.makeEverythingAwesome();
}

So we need to translate it into clojure/quil. It's going to look something like this:

(def foo (atom nil))
(def setup []
  ...
  (reset! foo (some.awesome.plugin.AwesomeFoo. (quil.applet/current-applet)))

(defn draw []
  ...
  (.makeEverythingAwesome @foo))

You can see that it is basically line-to-line translation of java example. The only big difference is that instead of this we used (quil.applet/current-applet).

If you use fun-mode then instead of having global atom with plugin object you can keep it in sketch state:

(def setup []
  ...
  {:foo (some.awesome.plugin.AwesomeFoo. (quil.applet/current-applet))})

(defn draw [state]
  (.makeEverythingAwesome (:foo state)))