From dbd3931769feb84c86dc6c18eedac91816673edf Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Thu, 29 Nov 2018 22:43:16 +0100 Subject: [PATCH] [#15] Use looping control functions in mandelbrot example --- src/doodler/core.clje | 3 ++- src/doodler/examples/mandelbrot.clje | 29 ++++++++++++++++++++-------- src/doodler/sketch.clje | 11 +++++++---- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/doodler/core.clje b/src/doodler/core.clje index b23af93..f86bfca 100644 --- a/src/doodler/core.clje +++ b/src/doodler/core.clje @@ -180,7 +180,8 @@ (defn start-loop [] - (u/swap-var! *sketch* assoc :looping? true)) + (u/swap-var! *sketch* assoc :looping? true) + (u/send-message (:pid *sketch*) :refresh)) (defmacro defsketch [name & opts] diff --git a/src/doodler/examples/mandelbrot.clje b/src/doodler/examples/mandelbrot.clje index a331ab1..13dd5a8 100644 --- a/src/doodler/examples/mandelbrot.clje +++ b/src/doodler/examples/mandelbrot.clje @@ -15,12 +15,12 @@ minx -2.5 miny -2.0 wh 4.0] - [(+ minx (* wh (/ x w))) - (+ miny (* wh (/ y h)))])) + #erl[(+ minx (* wh (/ x w))) + (+ miny (* wh (/ y h)))])) (defn ->color [n max-iters] - (int (* 255 (/ n max-iters)))) + (erlang/div (* 255 n) max-iters)) (defn mandelbrot [x y max-iters] (loop [a 0.0 @@ -35,10 +35,19 @@ (inc n))))) (defn mandelbrot-point [x y w h max-iters] - (let [[i j] (->complex x y w h) - n (mandelbrot i j max-iters) - color (->color n max-iters)] - [x y color])) + (let* [#erl[i j] (->complex x y w h) + n (mandelbrot i j max-iters) + color (->color n max-iters)] + #erl[x y color])) + +(defn on-mouse-pressed [state handler] + (if-not (d/looping?) + (do + (d/start-loop) + (d/background 0) + {:points (points) + :m-points nil}) + state)) (defn setup [] @@ -48,7 +57,10 @@ (defn update-state [{points :points :as state}] - (when points + (if-not points + (do + (d/no-loop) + state) (let [max-iters 100 w (d/width) h (d/height)] @@ -73,6 +85,7 @@ :size [300 300] :setup setup :update update-state + :mouse-pressed on-mouse-pressed :draw draw :bgcolor [0] :features [:keep-on-top] diff --git a/src/doodler/sketch.clje b/src/doodler/sketch.clje index 943fb84..81d4be9 100644 --- a/src/doodler/sketch.clje +++ b/src/doodler/sketch.clje @@ -226,6 +226,7 @@ :frame-delay frame-period :current-frame-rate default-frame-rate + :looping? true :frame-count 0 :epoch (u/time-mark) :p-time-mark (u/time-mark) @@ -280,10 +281,12 @@ refresh u/noreply)) ([#erl[:draw time-mark] sketch] - (-> sketch - (on-draw (:draw-fn sketch)) - (next-refresh time-mark) - u/noreply)) + (u/noreply + (if (:looping? sketch) + (-> sketch + (on-draw (:draw-fn sketch)) + (next-refresh time-mark)) + sketch))) ([wx sketch] (u/noreply sketch)))