devotee_backend/lib.rs
1#![deny(missing_docs)]
2
3//! Backend stuff for the `devotee` engine.
4
5use std::num::NonZeroU32;
6
7use winit::dpi::PhysicalPosition;
8use winit::window::Window;
9
10pub use winit;
11
12/// Backend trait.
13/// It provides functions to:
14///
15/// - create new instance;
16/// - handle resize events;
17/// - to draw output;
18/// - recalculate pointer position to canvas space.
19pub trait Backend: Sized {
20 /// Create new backend instance.
21 fn new(window: &Window, resolution: (u32, u32), scale: u32) -> Option<Self>;
22
23 /// Handle resize event.
24 fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Option<()>;
25
26 /// Draw image on the backend.
27 fn draw_image<'a, P: 'a, I>(
28 &mut self,
29 image: &'a dyn BackendImage<'a, P, Iterator = I>,
30 converter: &dyn Converter<Palette = P>,
31 window: &Window,
32 background: u32,
33 ) -> Option<()>
34 where
35 I: Iterator<Item = &'a P>;
36
37 /// Recalculate pointer position to canvas space.
38 fn window_pos_to_inner(
39 &self,
40 position: PhysicalPosition<f64>,
41 window: &Window,
42 resolution: (u32, u32),
43 ) -> Result<(i32, i32), (i32, i32)>;
44}
45
46/// Converter from pallette value to `u32` value.
47pub trait Converter {
48 /// Palette to convert from.
49 type Palette;
50 /// Perform conversion.
51 /// The output is considered to be `0x00rrggbb`.
52 fn convert(&self, color: &Self::Palette) -> u32;
53}
54
55/// Trait to generalize images to be displayed on the backend.
56///
57/// Currently implemented for `Canvas` and `Sprite`.
58pub trait BackendImage<'a, P: 'a> {
59 /// Iterator to produce pixel values of the image, row-by-row.
60 type Iterator: Iterator<Item = &'a P>;
61
62 /// Get reference to specific pixel.
63 ///
64 /// # Safety
65 /// - `x` must be in bounds `[0; width)`;
66 /// - `y` must be in bounds `[0; height)`.
67 unsafe fn pixel_unsafe(&self, x: u32, y: u32) -> &P;
68
69 /// Get image width in pixels.
70 fn width(&self) -> u32;
71
72 /// Get image height in pixels.
73 fn height(&self) -> u32;
74
75 /// Get iterator over pixels.
76 ///
77 /// The iterator is considered to provide pixels row-by-row.
78 fn pixels(&'a self) -> Self::Iterator;
79}