summaryrefslogtreecommitdiff
path: root/engine/src/lib.rs
blob: a2f8c3464570996a1d4735c7f3f6e460de8068b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#![deny(clippy::all, clippy::pedantic)]
#![allow(clippy::needless_pass_by_value)]

use ecs::component::Sequence as ComponentSequence;
use ecs::event::component::TypeTransformComponentsToAddedEvents;
use ecs::event::{Event, Sequence as EventSequence};
use ecs::extension::Extension;
use ecs::sole::Sole;
use ecs::system::{Into, System};
use ecs::tuple::Reduce as TupleReduce;
use ecs::uid::Uid;
use ecs::{SoleAlreadyExistsError, World};

use crate::delta_time::{update as update_delta_time, DeltaTime, LastUpdate};
use crate::event::{
    Conclude as ConcludeEvent,
    PostPresent as PostPresentEvent,
    PreUpdate as PreUpdateEvent,
    Present as PresentEvent,
    Update as UpdateEvent,
};

mod opengl;
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 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,
    ConcludeEvent,
);

#[derive(Debug)]
pub struct Engine
{
    world: World,
}

impl Engine
{
    /// Returns a new `Engine`.
    #[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<Comps>(&mut self, components: Comps) -> Uid
    where
        Comps: ComponentSequence + TupleReduce<TypeTransformComponentsToAddedEvents>,
        Comps::Out: EventSequence,
    {
        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);
    }

    /// Runs the event loop.
    pub fn start(&self)
    {
        self.world.event_loop::<EventOrder>();
    }
}

impl Default for Engine
{
    fn default() -> Self
    {
        Self::new()
    }
}