hello/
hello.rs

1use std::path::Path;
2
3use fennel_engine::{EventHandler, Game, events, graphics};
4use sdl3::pixels::Color;
5use tokio::runtime::Handle;
6
7struct State {}
8
9#[async_trait::async_trait]
10impl EventHandler for State {
11    async fn update(&self, _game: &mut Game) -> anyhow::Result<()> {
12        Ok(())
13    }
14
15    async fn draw(&self, game: &mut Game) -> anyhow::Result<()> {
16        game.graphics.canvas.set_draw_color(Color::RGB(0, 0, 0));
17        game.graphics.canvas.clear();
18        game.graphics
19            .draw_image(
20                "examples/example.png".to_string(),
21                (0.0, 0.0),
22                &mut game.resource_manager,
23            )
24            .expect("failed to draw an image");
25        game.graphics.canvas.present();
26        Ok(())
27    }
28
29    fn key_down_event(
30        &self,
31        game: &mut Game,
32        _timestamp: u64,
33        _window_id: u32,
34        keycode: Option<sdl3::keyboard::Keycode>,
35        _scancode: Option<sdl3::keyboard::Scancode>,
36        _keymod: sdl3::keyboard::Mod,
37        _repeat: bool,
38        _which: u32,
39        _raw: u16,
40    ) -> anyhow::Result<()> {
41        println!("{:?}", keycode);
42        tokio::task::block_in_place(move || {
43            Handle::current().block_on(async move {
44                game.audio
45                    .play_audio(Path::new("examples/music.ogg"), false)
46                    .await
47                    .unwrap();
48            })
49        });
50        Ok(())
51    }
52}
53
54#[tokio::main]
55async fn main() {
56    let graphics = graphics::Graphics::new(String::from("my cool game"), (500, 500));
57    let mut game = fennel_engine::Game::new(
58        String::from("my cool game"),
59        String::from("wiltshire"),
60        graphics.unwrap(),
61    );
62    events::run(&mut game, Box::new(State {})).await;
63}