summaryrefslogtreecommitdiff
path: root/engine/src/lib.rs
blob: 01c0701462d5ac1cdd2dd20b19f972541cb7fdbc (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
#![deny(clippy::all, clippy::pedantic)]
#![allow(clippy::needless_pass_by_value)]

use crate::asset::{Assets, Extension as AssetExtension};
use crate::delta_time::{update as update_delta_time, DeltaTime, LastUpdate};
use crate::ecs::error::HandlerFn as ErrorHandlerFn;
use crate::ecs::extension::Extension;
use crate::ecs::phase::PRE_UPDATE as PRE_UPDATE_PHASE;
use crate::ecs::system::initializable::Initializable;
use crate::ecs::system::Into;
use crate::ecs::World;
use crate::shader::Extension as ShaderExtension;

mod util;
mod work_queue;

pub mod asset;
pub mod camera;
pub mod collision;
pub mod data_types;
pub mod delta_time;
pub mod draw_flags;
pub mod file_format;
pub mod image;
pub mod input;
pub mod lighting;
pub mod material;
pub mod math;
pub mod mesh;
pub mod model;
pub mod projection;
pub mod reflection;
pub mod rendering;
pub mod shader;
pub mod texture;
pub mod transform;
pub mod ui;
pub mod windowing;

pub extern crate engine_ecs as ecs;

pub(crate) use crate::data_types::matrix;
pub use crate::data_types::{color, vector};

const INITIAL_ASSET_CAPACITY: usize = 128;

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

impl Engine
{
    /// Returns a new `Engine`.
    #[must_use]
    pub fn new() -> Self
    {
        #[cfg(windows)]
        nu_ansi_term::enable_ansi_support().unwrap();

        let mut world = World::new();

        world.add_sole(DeltaTime::default()).ok();

        world.register_system(
            *PRE_UPDATE_PHASE,
            update_delta_time
                .into_system()
                .initialize((LastUpdate::default(),)),
        );

        let mut assets = Assets::with_capacity(INITIAL_ASSET_CAPACITY);

        crate::model::asset::add_importers(&mut assets);
        crate::material::asset::add_importers(&mut assets);
        crate::shader::add_asset_importers(&mut assets);

        crate::texture::initialize(&mut assets);

        world.add_extension(AssetExtension { assets });
        world.add_extension(ShaderExtension);

        Self { world }
    }

    pub fn with_error_handler(mut self, error_handler: ErrorHandlerFn) -> Self
    {
        self.world.set_err_handler(error_handler);
        self
    }

    pub fn with_extension(mut self, extension: impl Extension) -> Self
    {
        self.world.add_extension(extension);
        self
    }

    /// Runs the event loop.
    pub fn start(&mut self)
    {
        self.world.start_loop();
    }
}

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