Skip to main content

simple/
simple.rs

1use figs::prelude::*;
2
3fn main() -> FigResult<()> {
4    // Initialize the canvas and input
5    let mut canvas = Canvas::new("simple example", (320, 320), 60)?;
6    let mut input = InputManager::new();
7
8    // Load assets from assets.tgz
9    let mut assets = AssetLoader::from_tar("examples/assets.tgz");
10    // You can also load assets from the assets directory
11    // let mut assets = AssetLoader::from_dir("examples/assets");
12
13    // Create some entities, using assets from the tarball
14    let mut bop = Entity::new((200.0, 200.0), vec![assets.load_png("bop.png")?], (8, 8), 20);
15    let mut blob = Entity::new((100.0, 100.0), assets.load_png_dir("blob")?, (8, 8), 20);
16
17    // We want to control blob with WASD or arrow keys
18    input.wasd();
19    input.arrows();
20
21    // We would also like to exit when ESC is pressed
22    input.add(Key::Escape);
23
24    // Main event loop (do something more interesting here)
25    while canvas.is_open() {
26        // Handle keypress events
27        for key in &input.keys {
28            if canvas.key_down(*key) {
29                match key {
30                    Key::W | Key::Up => blob.mov(0.0, -1.0),
31                    Key::A | Key::Left => blob.mov(-1.0, 0.0),
32                    Key::S | Key::Down => blob.mov(0.0, 1.0),
33                    Key::D | Key::Right => blob.mov(1.0, 0.0),
34                    Key::Escape => return Ok(()),
35                    _ => (),
36                }
37            }
38        }
39
40        // Update entities (advance animation, etc)
41        bop.update();
42        blob.update();
43
44        // Update the canvas with the entities
45        canvas.clear();
46        canvas.draw(&bop);
47        canvas.draw(&blob);
48        canvas.update()?;
49    }
50
51    Ok(())
52}