hello/
hello.rs

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