#![deny(clippy::all, clippy::pedantic)] #![allow(clippy::needless_pass_by_value)] use ecs::component::Sequence as ComponentSequence; use ecs::entity::Uid as EntityUid; use ecs::event::Event; use ecs::extension::Extension; use ecs::sole::Sole; use ecs::system::{Into, System}; use ecs::{SoleAlreadyExistsError, World}; use crate::delta_time::{update as update_delta_time, DeltaTime, LastUpdate}; use crate::event::{ PostPresent as PostPresentEvent, PreUpdate as PreUpdateEvent, Present as PresentEvent, Start as StartEvent, Update as UpdateEvent, }; mod opengl; mod shader_preprocessor; mod util; pub mod camera; pub mod data_types; pub mod delta_time; pub mod draw_flags; pub mod event; pub mod file_format; pub mod input; pub mod lighting; pub mod material; pub mod math; pub mod mesh; pub mod projection; pub mod renderer; pub mod shader; pub mod texture; pub mod transform; pub mod vertex; pub mod window; pub extern crate ecs; pub(crate) use crate::data_types::matrix; pub use crate::data_types::{color, vector}; type EventOrder = (PreUpdateEvent, UpdateEvent, PresentEvent, PostPresentEvent); #[derive(Debug)] pub struct Engine { world: World, } impl Engine { /// Creates and initializes a new engine. /// /// # Errors /// Will return `Err` if window creation or window configuration fails. #[must_use] pub fn new() -> Self { let mut world = World::new(); world.add_sole(DeltaTime::default()).ok(); world.register_system( PreUpdateEvent, update_delta_time .into_system() .initialize((LastUpdate::default(),)), ); Self { world } } pub fn spawn(&mut self, components: Comps) -> EntityUid where Comps: ComponentSequence, { self.world.create_entity(components) } pub fn register_system<'this, SystemImpl>( &'this mut self, event: impl Event, system: impl System<'this, SystemImpl>, ) { self.world.register_system(event, system); } /// Adds a globally shared singleton value. /// /// # Errors /// Returns `Err` if this [`Sole`] has already been added. pub fn add_sole(&mut self, sole: impl Sole) -> Result<(), SoleAlreadyExistsError> { self.world.add_sole(sole) } pub fn add_extension(&mut self, extension: impl Extension) { self.world.add_extension(extension); } /// Starts the engine. /// /// # Errors /// Will return `Err` if updating the window fails. pub fn start(&self) { self.world.emit(StartEvent); self.world.event_loop::(); } } impl Default for Engine { fn default() -> Self { Self::new() } }