1use std::{
2 path::Path,
3 sync::{Arc, Mutex},
4};
5
6use fennel_common::events::{KeyboardEvent, WindowEventHandler};
7use fennel_core::{Window, events, graphics, resources::ResourceManager};
8use sdl3::pixels::Color;
9use tokio::runtime::Handle;
10
11struct State;
12
13#[async_trait::async_trait]
14impl WindowEventHandler for State {
15 type Host = Window;
16 fn update(&self, _window: &mut Window) -> anyhow::Result<()> {
17 Ok(())
18 }
19
20 fn draw(&self, window: &mut Window) -> anyhow::Result<()> {
21 window.graphics.canvas.set_draw_color(Color::RGB(0, 0, 0));
22 window.graphics.canvas.clear();
23 window
24 .graphics
25 .draw_image("assets/example.png".to_string(), (0.0, 0.0))
26 .expect("failed to draw an image");
27 window.graphics.draw_text(
28 String::from("hi"),
29 (64.0, 64.0),
30 String::from("assets/terminus.ttf"),
31 Color::RGBA(255, 0, 0, 0),
32 16.0,
33 )?;
34 window.graphics.draw_text(
35 String::from("hi"),
36 (64.0, 150.0),
37 String::from("assets/terminus.ttf"),
38 Color::RGBA(255, 0, 0, 0),
39 128.0,
40 )?;
41 window.graphics.canvas.present();
42 Ok(())
43 }
44
45 fn key_down_event(&self, window: &mut Window, event: KeyboardEvent) -> anyhow::Result<()> {
46 println!("{:?}", event.keycode);
47 tokio::task::block_in_place(move || {
48 Handle::current().block_on(async move {
49 window
50 .audio
51 .play_audio(Path::new("assets/music.ogg"), false)
52 .await
53 .unwrap();
54 })
55 });
56 Ok(())
57 }
58}
59
60#[tokio::main]
61async fn main() {
62 let resource_manager = Arc::new(Mutex::new(ResourceManager::new()));
63 let graphics = graphics::Graphics::new(
64 String::from("my cool window"),
65 (500, 500),
66 resource_manager.clone(),
67 );
68 let mut window = Window::new(graphics.unwrap(), resource_manager);
69 events::run(&mut window, State).await;
70}