Crates.io | awoo |
lib.rs | awoo |
version | 0.2.0 |
source | src |
created_at | 2019-05-07 15:40:19.036575 |
updated_at | 2019-07-26 10:51:10.184197 |
description | Animate and schedule code. |
homepage | https://github.com/phaazon/awoo |
repository | https://github.com/phaazon/awoo |
max_upload_size | |
id | 132608 |
size | 21,534 |
Animate and schedule code.
This crate provides a very simple mechanism to animate some code. Consider:
let mut time = 0.; // time that passes
let a = 0.;
let b = 1.;
let c = 2.;
// main loop of our application
loop {
if time < a {
// do something until time passes a point in time a
} else if time < b {
// do something until time passes a point in time b
} else if time < c {
// do something until time passes a point in time c
} // etc. etc.
// time advances here
time += 0.1;
if time >= c {
break
}
}
That might sound surprising to you, but a lot of demoscene productions are written with that kind
of if / else if
blocks. It’s pretty bad and ugly though, I agree. For several reasons:
Instead of writing that kind of code, we can do better:
use awoo::window::Window;
let pre_alpha = Window::new(0., 1.); // do something until time passes 1.
let pre_beta = Window::new(1., 2.); // do something until time passes 2.
let pre_gamma = Window::new(2., 3.); // do something until time passes 3.
By default, Window<T>
is just a window of time T
. We can map actions to completely
do the same thing as above:
let pre_alpha = Window::new(0., 1.).map(|time| println!("time is {}", time));
The type of that MappedWindow<_>
is determined by what you return from your closure in the
map
call. Once all windows are created, you can schedule them:
use awoo::scheduler::RandomAccessScheduler;
use awoo::time::simple::SimpleF32TimeGenerator;
let mut scheduler = RandomAccessScheduler::new(
SimpleF32TimeGenerator::new(0., 0.1), // a generator that generates linear time starting at 0 and incrementing by 0.1
vec![pre_alpha, pre_beta, pre_gamma] // our mapped windows
).unwrap();
scheduler.schedule();
What’s interesting is that we can get the windows from a file, for instance, and map them on the fly.