fennel_core/
lib.rs

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