fennel_core/
lib.rs

1//! Core window-related API for Fennel engine
2use std::sync::{Arc, Mutex};
3
4use fennel_common::events::{
5    KeyboardEvent, MouseClickEvent, MouseMotionEvent, MouseWheelEvent, WindowEventHandler,
6};
7
8use crate::{audio::Audio, graphics::{Graphics, HasWindow}, resources::ResourceManager};
9
10/// Audio playback
11pub mod audio;
12/// Handling keyboard, window, mouse and other events
13pub mod events;
14/// Rendering layer and all the related things
15pub mod graphics;
16/// Resource management
17pub mod resources;
18/// Tests
19mod tests;
20
21unsafe impl Send for Window {}
22unsafe impl Sync for Window {}
23
24/// Main window struct
25///
26/// Holds basic metadata and a reference to the graphics subsystem.
27/// User must create a [`Window`] in order to feed it to the EventHandler
28pub struct Window {
29    /// Graphics subsystem used to render frames.
30    pub graphics: Graphics,
31    /// Audio subsystem
32    pub audio: Audio,
33    /// Resource manager
34    pub resource_manager: Arc<Mutex<ResourceManager>>,
35}
36
37impl Window {
38    /// Create a new [`Window`] instance.
39    ///
40    /// # Parameters
41    /// - `name`: title of the window
42    /// - `graphics`: initialized graphics subsystem
43    ///
44    /// # Returns
45    /// A [`Window`] instance ready to be used by an [`EventHandler`].
46    pub fn new(graphics: Graphics, resource_manager: Arc<Mutex<ResourceManager>>) -> Window {
47        Window {
48            graphics,
49            audio: Audio::new(),
50            resource_manager,
51        }
52    }
53}
54
55impl HasWindow for Window {
56    fn window_mut(&mut self) -> &mut Self {
57        self
58    }
59}
60
61pub struct CoreHandler;
62#[async_trait::async_trait]
63impl WindowEventHandler for CoreHandler {
64    type Host = Window;
65
66    fn update(&self, _window: &mut Window) -> anyhow::Result<()> {
67        Ok(())
68    }
69
70    fn draw(&self, _window: &mut Window) -> anyhow::Result<()> {
71        Ok(())
72    }
73
74    fn key_down_event(&self, _window: &mut Window, _event: KeyboardEvent) -> anyhow::Result<()> {
75        Ok(())
76    }
77
78    fn key_up_event(&self, _window: &mut Window, _event: KeyboardEvent) -> anyhow::Result<()> {
79        Ok(())
80    }
81
82    fn mouse_motion_event(
83        &self,
84        _window: &mut Window,
85        _event: MouseMotionEvent,
86    ) -> anyhow::Result<()> {
87        Ok(())
88    }
89
90    fn mouse_button_down_event(
91        &self,
92        _window: &mut Window,
93        _event: MouseClickEvent,
94    ) -> anyhow::Result<()> {
95        Ok(())
96    }
97
98    fn mouse_button_up_event(
99        &self,
100        _window: &mut Window,
101        _event: MouseClickEvent,
102    ) -> anyhow::Result<()> {
103        Ok(())
104    }
105
106    fn mouse_wheel_event(
107        &self,
108        _window: &mut Window,
109        _event: MouseWheelEvent,
110    ) -> anyhow::Result<()> {
111        Ok(())
112    }
113}
114
115pub type BoxedCoreHandler = Box<dyn WindowEventHandler<Host = Window> + Send + Sync>;