Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hadronized/awoo
https://github.com/hadronized/awoo
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/hadronized/awoo
- Owner: hadronized
- License: bsd-3-clause
- Created: 2019-05-07T15:37:08.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-07-26T10:50:16.000Z (over 5 years ago)
- Last Synced: 2024-11-12T20:49:48.200Z (about 1 month ago)
- Language: Rust
- Size: 43.9 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# awoo
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:- It doesn’t compose at all. If you want to do something in between α and β, you will have to
break the whole block and change every timings.
- Dynamic branching will get worse and worse as time passes, since you’re going to make more and
more tests.
- The code is just ugly!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`] 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.[`Window`]: crate::window::Window
[`MappedWindow<_>`]: crate::window::MappedWindow