[Fluxus] commits
Dave Griffiths
dave at pawfal.org
Tue Mar 31 08:55:58 PDT 2009
Hi all,
I removed my hooks and replaced them with Scott's tasks, as they are
better (see below).
I also added (spawn-timed-task time thunk) which will schedule some code
to run at a specific time, and updated fluxa to use these new things.
The every-frame function now creates a task called 'every-frame-task
which is removed in (clear).
It all seems to be working ok, but it's likely I've broken something as
I did it in a bit of a hurry (it's all needed for a slub rehersal
tomorrow)
cheers,
dave
On Fri, 2009-03-27 at 16:25 -0700, Scott wrote:
> Hah! I did almost the exact same thing, except I put the id (symbol)
> at the end and it's optional (I gensym one up if you don't specify
> one). I also put an exception handler around the execution of the
> thunk incase of an error.
>
> Some samples:
> (spawn-task thunk 'taskname)
> (ls-tasks) ; lists all named tasks
> (rm-task 'taskname)
>
> I have almost the same thing for key-hooks (also runs with error
> handling), that looks like this:
> (add-key-hook 'right (lambda (n) (rotcam #(-1 0 0))))
> (add-key-hook 'left (lambda (n) (rotcam #(1 0 0))))
> (add-key-hook "m" (lambda (n) (play-movie)))
> (add-key-hook "M" (lambda (n) (stop-movie)))
>
>
> Here's tasks.ss:
> ;; Provide a named safe tasks
> ;;
> ;; Currently tasks are limited to "thunks" but may be extended to
> support continuations
> ;;
> ;; Tasks:
> ;; * execute once per graphic frame
> ;; * are called in lexigraphical order by name
> ;; * have unique names and if the same name is used the old task is
> ;; removed prior to the new task being added
> ;; * a task that returns #f will remove itself after executing
> ;;
>
> ;; run-tasks puts itself on the frame-hooks list defined in scratchpad
>
> ;; LATER: if task returns a thunk, then that thunk will replace the old one
> ;; LATER: if a task returns a continuation, then that continuation
> will be called next frame
>
> ;;
> ;; (ls-tasks)
> ;; (spawn-task thunk)
> ;; (spawn-task thunk 'name)
> ;; (rm-task 'name)
> ;;
> #lang scheme/base
>
> (provide spawn-task ls-tasks rm-task rm-all-tasks)
>
> (define task-list '()) ; alist of tasks - maintained in sorted order
>
> (define (spawn-task thunk . args)
> (let ([name (if (null? args) (string->symbol (symbol->string
> (gensym))) (car args))])
> (rm-task name) ; incase it already exists - replace it
> (set! task-list (sort (cons (cons name thunk) task-list)
> #:key (lambda (l) (symbol->string (car l)))
> string<?)
> )))
>
> (define (rm-task name)
> (set! task-list (remove name task-list (lambda (a b) (eq? a (car b)))))
> )
>
> (define (rm-all-tasks)
> (set! task-list '()))
>
> (define (ls-tasks)
> (for-each (lambda (t)
> (printf "task: ~a ~a~%" (car t) (cdr t)))
> task-list))
>
> (define (thunk? t) (let ([arity (procedure-arity t)])
> (or (eq? arity 0)
> (and (list? arity) (eq? (car arity) 0)))))
>
> (define (call-task task)
> (cond [(thunk? task) (task)]
> ;; [(continuation? task) (task 'resume)]
> [else (error "Non-thunk or continuation passed to call-task")]))
>
> (define (run-tasks)
> (for-each
> (lambda (task)
> (let/ec out
> ;; handle errors by reporting and removing task in error
> (let ([task-error
> (lambda (e)
> (printf "Error in Task '~a - Task removed.~% Error: ~a ~%"
> (car task) e)
> (rm-task (car task))
> (out #t))])
> (call-with-exception-handler task-error
> (lambda ()
> (unless (call-task (cdr task))
> (rm-task (car task)))))
> ))
> )
> task-list))
>
> (require fluxus-016/scratchpad)
> (add-frame-hook run-tasks)
>
>
>
> On 3/27/09, Dave Griffiths <dave at pawfal.org> wrote:
> > Hi all,
> >
> > I've extended the way that every-frame works, so you can add and manage
> > your own frame hooks. This means you can run multiple scripts at the
> > same time, or have things running in the background unaffected by
> > (clear) or (every-frame). Fluxa uses this, and some other stuff might in
> > the future.
> >
> > You add your own with:
> > (add-frame-hook! id thunk)
> >
> > Which adds a hook with a given id - the every-frame id is 0, so
> > (every-frame (draw-cube))
> > is the same as doing:
> > (add-frame-hook 0 (lambda () (draw-cube)))
> >
> > calling (clear) only clears hook 0, so you can add your own with
> > different id's and they'll get left around until you call:
> > (remove-frame-hook! id)
> > or
> > (clear-frame-hooks!)
> >
> > to get rid of them all.
> >
> > I'va also added (locator-bounding-radius n) which allows you to set the
> > size of the locator bounding box (maybe it should be possible to set the
> > dimensions too....
> >
> > I've also added the missile command script as an example. It shows how
> > to embed glsl shaders into fluxus scripts.
> >
> > cheers,
> >
> > dave
> >
> >
More information about the Fluxus
mailing list