-
Notifications
You must be signed in to change notification settings - Fork 2
Timers
Normally when you enter Scheme in the evaluator pane; and execute it; it runs right away.
Only one thing ever runs in the Scheme engine at a time; if one script is running; other scripts will have to wait for it to finish; this is also true of any Scheme functions scheduled by timers; they all wait to run.
There is a timer function called after that can be used to run something later.
Where this command :-
(display "hello") (newline)
Runs right now; you could also schedule commands to run in 20 seconds time like this:-
(after 20000 (lambda ()
(display "hello")
(newline)))
You could write a function that runs; and then reschedules itself to run again later in two seconds time.
(define repeating
(lambda () (after 2000 repeating)
(display "hello")(newline)))
(repeating)
You can continue running other short functions interactively; and the timer function will keep repeating seemingly in the background; running whenever Scheme is not evaluating anything else.
This would keep running until you redefine the function not to repeat; when it stops.
(define repeating (lambda () '()))
Given that only one Scheme thread is ever running; this may seem to be of limited use; however it can be useful when you are also doing animation with the timers below.
This is neither concurrency (two things seem to run at once) or parallelism (two processers run things at the same time) it is really only scheduling.
There is a special timer related entirely to animation of the image view; called the every-function.
The general idea with computer animation is that you display the latest frame; while the viewer is looking at that and deciding what key to press; you draw the next frame; behind the scenes.
If part of an animation is the app displaying the latest frame; and part is a Scheme function creating the next frame; those functions can partially overlap with each other if we are careful.
There is only one repeating every-function; it is intended to run a drawing function every N milliseconds; allowing an animation to create the next frame; it rotates the active and display images; and optionally batches together a list of graphical commands; it is entirely animation related.
This "step-function" is a single step in a game-loop. Everything needed to display the next frame has to be completed by this function; in not very many milliseconds.
The function needs to update the game state; which is essentially data processing; and then display the game; which is essentially bit-blitting image data to the screen or starting a sound sample.
The workspace application is interactive.
You can work on and edit your animation in the evaluator pane; while the animation is still running in the image pane; making changes for example to a simple game; while it is running.
Please save your work as you go; to a separate editor. A mistake in a function being executed at 60fps; can certainly cause a crash.
The modes used in the show command, 0-2 apply also to the every function.
You have a choice of running the graphics commands in the every function (doing the painting); or just adding commands to a list of commands that are run when the function ends; (this is render mode 2). In render mode 2; the step function just adds commands to a list; and the painting is done when the function ends.
This Mode (2) is typically the most efficient; as the command list of graphics operations are sent to the graphics card in large batches.
Also the Scheme engine is free to run while the graphics commands are being executed; allowing for slightly better use of multiple processors.
In this mode you should do data processing at the top of the step-function; move the positions of objects; check for collisions; trigger explosions etc; all the logic that is unrelated to painting. Then add all the resulting graphics commands towards the end of the step-function.
Otherwise the function is more likely to have to wait for the graphics command list to be drained; before it can add more commands for the next frame.