If anything's idiomatic clojure, it's probably this (I think this is
how the ants worked in the ant demo, for example), whether it's
lightweight enough is another story, and probably hard to tell at this
point.  You could always send-off an infinitely-looping function to an
agent, then look at a ref or atom to check if you need to stop, that
way you avoid the overhead of queueing stuff for the agent, but it's
difficult to tell how problematic that overhead even is, if it is at
all.

The reason your set! doesn't work is because defs can only be changed
inside a (binding ...) clause, and even if you used one, it wouldn't
propogate to another thread, so you'll have to use another type of
reference (atoms maybe) to do your counter if you want it to be thread
safe.

On Apr 3, 10:05 pm, Curran Kelleher <curran.kelle...@gmail.com> wrote:
> Hi all,
>
> I'm trying to get started with an OpenGL (JOGL) application in
> Clojure, and one of the first orders of business is establishing a
> thread that repeatedly calls the display function - the game loop. I'm
> not sure what the idiomatic way to do this in Clojure is.
>
> The below code works, but is it good Clojure style and lightweight
> enough for a game loop running at ~60 fps? I would greatly appreciate
> any tips!
>
> ;;Prints incrementing integers every 500 ms in a separate thread
> ;is this the Clojure way?
> (def counter 0)
> (def animator (agent false));agent state = running flag
> (defn animation [running]
>   (when running
>     (send-off *agent* #'animation))
>   (def counter (+ 1 counter))
> ; (set! counter (+ 1 counter)) <-- just doesn't work!
>   (println counter)
>   (. Thread sleep 500)
>   running)
>
> (defn start-animation []
>   (send animator (fn [x] true));should this be send-off? why?
>   (send-off animator animation))
>
> (defn stop-animation []
>   (send animator (fn [x] false)))
>
> ;for SLIME
> (comment
>   (start-animation)
>   (stop-animation)
> )
>
> Thanks a lot! Clojure is a blast!
>
> Best,
> Curran Kelleher
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to