blue_engine_core/prelude/
mod.rs

1use downcast::{Any, downcast};
2/// re-exports from dependencies that are useful
3pub mod imports;
4pub use imports::*;
5/// contains definition for some 2D and 3D shapes. They are basic shapes and
6/// can be used as examples of how to create your own content.
7pub mod primitive_shapes;
8pub use crate::camera::{Camera, CameraContainer, Projection};
9pub use crate::definition::{
10    Pipeline, PipelineData, ShaderSettings, TextureData, TextureMode, VertexBuffers,
11    pixel_to_cartesian,
12};
13pub use crate::engine::{Engine, EngineSettings};
14pub use crate::objects::{
15    Instance, InstanceRaw, Object, ObjectSettings, ObjectStorage, RotateAmount, RotateAxis,
16};
17pub use crate::render::Renderer;
18#[cfg(all(feature = "window", not(feature = "headless")))]
19pub use crate::window::Window;
20
21/// The uint type used for indices and more
22#[cfg(not(feature = "u32"))]
23pub type UnsignedIntType = u16;
24#[cfg(feature = "u32")]
25pub type UnsignedIntType = u32;
26
27///
28pub mod macros {
29    macro_rules! impl_deref {
30        ($struct:ty,$type:ty) => {
31            impl std::ops::Deref for $struct {
32                type Target = $type;
33
34                fn deref(&self) -> &Self::Target {
35                    &self.0
36                }
37            }
38            impl std::ops::DerefMut for $struct {
39                fn deref_mut(&mut self) -> &mut Self::Target {
40                    &mut self.0
41                }
42            }
43        };
44    }
45
46    macro_rules! impl_deref_field {
47        ($struct:ty,$type:ty,$field:ident) => {
48            impl std::ops::Deref for $struct {
49                type Target = $type;
50
51                fn deref(&self) -> &Self::Target {
52                    &self.$field
53                }
54            }
55            impl std::ops::DerefMut for $struct {
56                fn deref_mut(&mut self) -> &mut Self::Target {
57                    &mut self.$field
58                }
59            }
60        };
61    }
62
63    pub(crate) use impl_deref;
64    pub(crate) use impl_deref_field;
65}
66
67/// Will contain all details about a vertex and will be sent to GPU
68// Will be turned to C code and sent to GPU
69#[repr(C)]
70#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
71pub struct Vertex {
72    /// Contains position data for the vertex in 3D space
73    pub position: [f32; 3],
74    /// Contains uv position data for the vertex
75    pub uv: [f32; 2],
76    /// Contains the normal face of the vertex
77    pub normal: [f32; 3],
78}
79impl Vertex {
80    pub(crate) fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
81        wgpu::VertexBufferLayout {
82            array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
83            step_mode: wgpu::VertexStepMode::Vertex,
84            attributes: &[
85                wgpu::VertexAttribute {
86                    offset: 0,
87                    shader_location: 0,
88                    format: wgpu::VertexFormat::Float32x3,
89                },
90                wgpu::VertexAttribute {
91                    // This should be replaced with `std::mem::size_of::<Vector3>() as wgpu::BufferAddress`
92                    offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
93                    shader_location: 1,
94                    format: wgpu::VertexFormat::Float32x2,
95                },
96                wgpu::VertexAttribute {
97                    offset: std::mem::size_of::<[f32; 5]>() as wgpu::BufferAddress,
98                    shader_location: 2,
99                    format: wgpu::VertexFormat::Float32x3,
100                },
101            ],
102        }
103    }
104}
105unsafe impl Send for Vertex {}
106unsafe impl Sync for Vertex {}
107
108/// Allows all events to be fetched directly, making it easier to add custom additions to the engine.
109pub trait Signal: Any {
110    /// This is ran as soon as the engine is properly initialized and all components are ready
111    #[allow(clippy::too_many_arguments)]
112    fn init(&mut self, _engine: &mut crate::Engine) {}
113
114    /// This is ran at the device events when available
115    #[allow(clippy::too_many_arguments)]
116    #[cfg(all(feature = "window", not(feature = "headless")))]
117    fn device_events(&mut self, _engine: &mut crate::Engine, _events: &crate::DeviceEvent) {}
118
119    /// This is ran at the window events when available
120    #[allow(clippy::too_many_arguments)]
121    #[cfg(all(feature = "window", not(feature = "headless")))]
122    fn window_events(&mut self, _engine: &mut crate::Engine, _events: &crate::WindowEvent) {}
123
124    /// ran before the frame is rendered
125    #[allow(clippy::too_many_arguments)]
126    fn frame(
127        &mut self,
128        _engine: &mut crate::Engine,
129        _encoder: &mut crate::CommandEncoder,
130        _view: &crate::TextureView,
131    ) {
132    }
133}
134// The engine needs to know the functions of Signal to do things internally,
135// so we use downcast and not the std::any::Any
136downcast!(dyn Signal);
137
138/// Handles the live events in the engine
139pub struct SignalStorage {
140    /// list of events with key and the event
141    pub events: Vec<(String, Box<dyn Signal>)>,
142}
143
144/// To hold the width and height of the engine frames
145pub type WindowSize = (u32, u32);