fennel_engine/
lib.rs

1//! A small 2D game framework I'm building just-for-fun and to learn Rust a little bit deeper
2
3use sdl3::{
4    keyboard::{Keycode, Mod, Scancode},
5    mouse::{MouseButton, MouseState, MouseWheelDirection},
6};
7
8use crate::graphics::Graphics;
9
10/// Handling keyboard, window, mouse and other events
11pub mod events;
12/// Rendering layer and all the related things
13pub mod graphics;
14
15/// Main game struct
16///
17/// Holds basic metadata and a reference to the graphics subsystem.
18/// User must create a [`Game`] in order to feed it to the EventHandler
19pub struct Game {
20    /// Human-readable game title.
21    pub name: String,
22    /// Author or owner of the game.
23    pub author: String,
24    /// Graphics subsystem used to render frames.
25    pub graphics: Graphics,
26}
27
28impl Game {
29    /// Create a new [`Game`] instance.
30    ///
31    /// # Parameters
32    /// - `name`: title of the game
33    /// - `author`: author/owner name
34    /// - `graphics`: initialized graphics subsystem
35    ///
36    /// # Returns
37    /// A [`Game`] instance ready to be used by an [`EventHandler`].
38    pub fn new(name: String, author: String, graphics: Graphics) -> Game {
39        Game {
40            name,
41            author,
42            graphics,
43        }
44    }
45}
46
47/// Trait that must be implemented by user's game state struct
48/// - `update` is called to advance game state (physics, AI, input processing).
49/// - `draw` is called to render the current state using `game.graphics`.
50///
51/// Both return `anyhow::Result<()>`
52pub trait EventHandler {
53    fn update(&self, game: &mut Game) -> anyhow::Result<()>;
54    fn draw(&self, game: &mut Game) -> anyhow::Result<()>;
55    fn key_down_event(
56        &self,
57        _game: &mut Game,
58        _timestamp: u64,
59        _window_id: u32,
60        _keycode: Option<Keycode>,
61        _scancode: Option<Scancode>,
62        _keymod: Mod,
63        _repeat: bool,
64        _which: u32,
65        _raw: u16,
66    ) -> anyhow::Result<()> {
67        Ok(())
68    }
69
70    fn key_up_event(
71        &self,
72        _game: &mut Game,
73        _timestamp: u64,
74        _window_id: u32,
75        _keycode: Option<Keycode>,
76        _scancode: Option<Scancode>,
77        _keymod: Mod,
78        _repeat: bool,
79        _which: u32,
80        _raw: u16,
81    ) -> anyhow::Result<()> {
82        Ok(())
83    }
84
85    fn mouse_motion_event(
86        &self,
87        _game: &mut Game,
88        _timestamp: u64,
89        _window_id: u32,
90        _which: u32,
91        _mousestate: MouseState,
92        _x: f32,
93        _y: f32,
94        _xrel: f32,
95        _yrel: f32,
96    ) -> anyhow::Result<()> {
97        Ok(())
98    }
99
100    fn mouse_button_down_event(
101        &self,
102        _game: &mut Game,
103        _timestamp: u64,
104        _window_id: u32,
105        _which: u32,
106        _mouse_btn: MouseButton,
107        _clicks: u8,
108        _x: f32,
109        _y: f32,
110    ) -> anyhow::Result<()> {
111        Ok(())
112    }
113
114    fn mouse_button_up_event(
115        &self,
116        _game: &mut Game,
117        _timestamp: u64,
118        _window_id: u32,
119        _which: u32,
120        _mouse_btn: MouseButton,
121        _clicks: u8,
122        _x: f32,
123        _y: f32,
124    ) -> anyhow::Result<()> {
125        Ok(())
126    }
127
128    fn mouse_wheel_event(
129        &self,
130        _game: &mut Game,
131        _timestamp: u64,
132        _window_id: u32,
133        _which: u32,
134        _x: f32,
135        _y: f32,
136        _direction: MouseWheelDirection,
137        _mouse_x: f32,
138        _mouse_y: f32,
139    ) -> anyhow::Result<()> {
140        Ok(())
141    }
142}