Struct blue_engine::Engine

source ·
pub struct Engine {
    pub renderer: Renderer,
    pub event_loop: EventLoop<()>,
    pub window: Window,
    pub objects: ObjectStorage,
    pub camera: Camera,
    pub plugins: Vec<Box<dyn EnginePlugin>>,
}
Expand description

The engine is the main starting point of using the Blue Engine. Everything that runs on Blue Engine will be under this struct. The structure of engine is monolithic, but the underlying data and the way it works is not. It gives a set of default data to work with, but also allow you to go beyond that and work as low level as you wish to.

You can also use the Engine to build you own custom structure the way you wish for it to be. Possibilities are endless!

To start using the Blue Engine, you can start by creating a new Engine like follows:

use blue_engine::header::{Engine, WindowDescriptor};

fn main() {
    let engine = Engine::new(WindowDescriptor::default()).expect("Couldn't create the engine");
}

The WindowDescriptor simply holds what features you would like for your window. If you are reading this on later version of the engine, you might be able to even run the engine in headless mode meaning there would not be a need for a window and the renders would come as image files.

If you so wish to have a window, you would need to start a window update loop. The update loop of window runs a frame every few milisecond, and gives you details of what is happening during this time, like input events. You can also modify existing parts of the engine during this update loop, such as changing camera to look differently, or creating a new object on the scene, or even changing window details!

The update loop is just a method of the Engine struct that have one argument which is a callback function.

[THE DATA HERE IS WORK IN PROGRESS!]

Fields§

§renderer: Renderer

The renderer does exactly what it is called. It works with the GPU to render frames according to the data you gave it.

§event_loop: EventLoop<()>

The event_loop handles the events of the window and inputs, so it’s used internally

§window: Window

The window handles everything about window and inputs. This includes ability to modify window and listen to input devices for changes.

§objects: ObjectStorage

The object system is a way to make it easier to work with the engine. Obviously you can work without it, but it’s for those who do not have the know-how, or wish to handle all the work of rendering data manually.

§camera: Camera

The camera handles the way the scene looks when rendered. You can modify everything there is to camera through this.

§plugins: Vec<Box<dyn EnginePlugin>>

Handles all engine plugins

Implementations§

source§

impl Engine

source

pub fn new() -> Result<Self>

Creates a new window in current thread using default settings.

Examples found in repository?
examples/shapes/square.rs (line 51)
50
51
52
53
54
55
56
57
58
fn main() {
    let mut engine = Engine::new().expect("win");

    let _ = square("Square", &mut engine).unwrap();

    engine
        .update_loop(move |_, _, _, _, _, _| {})
        .expect("Error during update loop");
}
More examples
Hide additional examples
examples/shapes/triangle.rs (line 15)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
pub fn main() {
    let mut engine = Engine::new().expect("win");

    triangle(
        "Triangle",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .unwrap();

    engine
        .update_loop(move |_, _, _, _, _, _| {})
        .expect("Error during update loop");
}
examples/shapes/cube.rs (line 10)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let mut engine = Engine::new().expect("win");

    cube("Cube", &mut engine.renderer, &mut engine.objects).unwrap();
    engine
        .objects
        .get_mut("Cube")
        .unwrap()
        .set_color(0f32, 0f32, 1f32, 1f32)
        .unwrap();

    let radius = 5f32;
    let start = std::time::SystemTime::now();
    engine
        .update_loop(move |_, _, _, _, camera, _| {
            let camx = start.elapsed().unwrap().as_secs_f32().sin() * radius;
            let camy = start.elapsed().unwrap().as_secs_f32().sin() * radius;
            let camz = start.elapsed().unwrap().as_secs_f32().cos() * radius;
            camera
                .set_position(camx, camy, camz)
                .expect("Couldn't update the camera eye");
        })
        .expect("Error during update loop");
}
examples/camera/rotate_around.rs (line 14)
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
fn main() {
    // Create the engine
    let mut engine = Engine::new().expect("win");

    // create a square
    square(
        // let's give it a name
        "Rotating Square",
        ObjectSettings {
            // and set the size
            // we need it to not cull it's back face so that it's visible on both side
            shader_settings: ShaderSettings {
                cull_mode: None,
                ..Default::default()
            },
            // and have default settings for the rest
            ..Default::default()
        },
        &mut engine.renderer,
        &mut engine.objects,
    )
    .unwrap();

    let radius = 2f32;
    let start = std::time::SystemTime::now();

    engine
        .update_loop(move |_, _, _, _, camera, _| {
            let camx = start.elapsed().unwrap().as_secs_f32().sin() * radius;
            let camz = start.elapsed().unwrap().as_secs_f32().cos() * radius;
            camera
                .set_position(camx, 0.0, camz)
                .expect("Couldn't update the camera eye");
        })
        .expect("Error during update loop");
}
examples/utils/instancing.rs (line 17)
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
pub fn main() {
    // start the engine
    let mut engine = Engine::new().expect("window not created");

    // create a triangle
    triangle(
        "Triangle",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .unwrap();

    // update the triangle
    engine.objects.update_object("Triangle", |object| {
        // set the position of the main triangle
        object.set_position(0f32, 0f32, -3f32);

        // a function to make instance creation easier
        let create_instance = |x: f32, y: f32, z: f32| {
            Instance::new(
                [x, y, z].into(),
                [0f32, 0f32, 0f32].into(),
                [1f32, 1f32, 1f32].into(),
            )
        };

        // add an instance
        object.add_instance(create_instance(2f32, 1f32, -2f32));
        object.add_instance(create_instance(2f32, -1f32, -2f32));
        object.add_instance(create_instance(-2f32, 1f32, -2f32));
        object.add_instance(create_instance(-2f32, -1f32, -2f32));
    });

    // we manually update the instance buffer before the next frame starts
    // this is due to the object updates happening after every frame, hence
    // for the first frame, we need to update it ourselves.
    engine
        .objects
        .get_mut("Triangle")
        .expect("Couldn't get the triangle")
        .update_instance_buffer(&mut engine.renderer)
        .expect("Couldn't update instance buffer");

    // run the loop as normal
    engine
        .update_loop(move |_, _, _, _, _, _| {})
        .expect("Error during update loop");
}
examples/utils/resource_sharing.rs (line 5)
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
fn main() {
    // Start the engine
    let mut engine = Engine::new().expect("window not initialized");

    // build a texture as an example of resource to be shared
    let texture = engine
        .renderer
        .build_texture(
            "background",
            TextureData::Path("resources/BlueLogoDiscord.png".to_string()),
            blue_engine::TextureMode::Clamp,
        )
        .unwrap();

    // build your main object with the texture
    square(
        "main",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .expect("Error during creation of main square");

    // add the texture to the main object as normally would
    engine
        .objects
        .get_mut("main")
        .unwrap()
        .set_texture(texture)
        .expect("Error during inserting texture to the main square");
    // set position to make it visible
    engine
        .objects
        .get_mut("main")
        .expect("Error during setting the position of the main square")
        .set_position(-1.5f32, 0f32, 0f32);

    // create another object where you want to get resources shared with
    square(
        "alt",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .expect("Error during creation of alt square");

    // here you can use `reference_texture` to reference the texture from the main object
    engine
        .objects
        .get_mut("alt")
        .expect("Error during copying texture of the main square")
        .reference_texture("main");
    // setting position again to make it visible
    engine
        .objects
        .get_mut("alt")
        .expect("Error during setting the position of the alt square")
        .set_position(1.5f32, 0f32, 0f32);

    engine
        .update_loop(move |_, _, _, _, _, _| {})
        .expect("Error during update loop");
}
source

pub fn new_config(settings: WindowDescriptor) -> Result<Self>

Creates a new window in current thread using provided settings.

source

pub fn update_loop<F: 'static + FnMut(&mut Renderer, &mut Window, &mut ObjectStorage, &WinitInputHelper, &mut Camera, &mut Vec<Box<dyn EnginePlugin>>)>( self, update_function: F ) -> Result<()>

Runs the block of code that you pass to it every frame. The update code is used to modify the engine on the fly thus creating interactive graphics and making things happy in the engine!

Renderer, window, vec of objects, events, and camera are passed to the update code.

Examples found in repository?
examples/shapes/square.rs (line 56)
50
51
52
53
54
55
56
57
58
fn main() {
    let mut engine = Engine::new().expect("win");

    let _ = square("Square", &mut engine).unwrap();

    engine
        .update_loop(move |_, _, _, _, _, _| {})
        .expect("Error during update loop");
}
More examples
Hide additional examples
examples/shapes/triangle.rs (line 26)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
pub fn main() {
    let mut engine = Engine::new().expect("win");

    triangle(
        "Triangle",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .unwrap();

    engine
        .update_loop(move |_, _, _, _, _, _| {})
        .expect("Error during update loop");
}
examples/shapes/cube.rs (lines 23-30)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let mut engine = Engine::new().expect("win");

    cube("Cube", &mut engine.renderer, &mut engine.objects).unwrap();
    engine
        .objects
        .get_mut("Cube")
        .unwrap()
        .set_color(0f32, 0f32, 1f32, 1f32)
        .unwrap();

    let radius = 5f32;
    let start = std::time::SystemTime::now();
    engine
        .update_loop(move |_, _, _, _, camera, _| {
            let camx = start.elapsed().unwrap().as_secs_f32().sin() * radius;
            let camy = start.elapsed().unwrap().as_secs_f32().sin() * radius;
            let camz = start.elapsed().unwrap().as_secs_f32().cos() * radius;
            camera
                .set_position(camx, camy, camz)
                .expect("Couldn't update the camera eye");
        })
        .expect("Error during update loop");
}
examples/camera/rotate_around.rs (lines 39-45)
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
fn main() {
    // Create the engine
    let mut engine = Engine::new().expect("win");

    // create a square
    square(
        // let's give it a name
        "Rotating Square",
        ObjectSettings {
            // and set the size
            // we need it to not cull it's back face so that it's visible on both side
            shader_settings: ShaderSettings {
                cull_mode: None,
                ..Default::default()
            },
            // and have default settings for the rest
            ..Default::default()
        },
        &mut engine.renderer,
        &mut engine.objects,
    )
    .unwrap();

    let radius = 2f32;
    let start = std::time::SystemTime::now();

    engine
        .update_loop(move |_, _, _, _, camera, _| {
            let camx = start.elapsed().unwrap().as_secs_f32().sin() * radius;
            let camz = start.elapsed().unwrap().as_secs_f32().cos() * radius;
            camera
                .set_position(camx, 0.0, camz)
                .expect("Couldn't update the camera eye");
        })
        .expect("Error during update loop");
}
examples/utils/instancing.rs (line 61)
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
pub fn main() {
    // start the engine
    let mut engine = Engine::new().expect("window not created");

    // create a triangle
    triangle(
        "Triangle",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .unwrap();

    // update the triangle
    engine.objects.update_object("Triangle", |object| {
        // set the position of the main triangle
        object.set_position(0f32, 0f32, -3f32);

        // a function to make instance creation easier
        let create_instance = |x: f32, y: f32, z: f32| {
            Instance::new(
                [x, y, z].into(),
                [0f32, 0f32, 0f32].into(),
                [1f32, 1f32, 1f32].into(),
            )
        };

        // add an instance
        object.add_instance(create_instance(2f32, 1f32, -2f32));
        object.add_instance(create_instance(2f32, -1f32, -2f32));
        object.add_instance(create_instance(-2f32, 1f32, -2f32));
        object.add_instance(create_instance(-2f32, -1f32, -2f32));
    });

    // we manually update the instance buffer before the next frame starts
    // this is due to the object updates happening after every frame, hence
    // for the first frame, we need to update it ourselves.
    engine
        .objects
        .get_mut("Triangle")
        .expect("Couldn't get the triangle")
        .update_instance_buffer(&mut engine.renderer)
        .expect("Couldn't update instance buffer");

    // run the loop as normal
    engine
        .update_loop(move |_, _, _, _, _, _| {})
        .expect("Error during update loop");
}
examples/utils/resource_sharing.rs (line 63)
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
fn main() {
    // Start the engine
    let mut engine = Engine::new().expect("window not initialized");

    // build a texture as an example of resource to be shared
    let texture = engine
        .renderer
        .build_texture(
            "background",
            TextureData::Path("resources/BlueLogoDiscord.png".to_string()),
            blue_engine::TextureMode::Clamp,
        )
        .unwrap();

    // build your main object with the texture
    square(
        "main",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .expect("Error during creation of main square");

    // add the texture to the main object as normally would
    engine
        .objects
        .get_mut("main")
        .unwrap()
        .set_texture(texture)
        .expect("Error during inserting texture to the main square");
    // set position to make it visible
    engine
        .objects
        .get_mut("main")
        .expect("Error during setting the position of the main square")
        .set_position(-1.5f32, 0f32, 0f32);

    // create another object where you want to get resources shared with
    square(
        "alt",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .expect("Error during creation of alt square");

    // here you can use `reference_texture` to reference the texture from the main object
    engine
        .objects
        .get_mut("alt")
        .expect("Error during copying texture of the main square")
        .reference_texture("main");
    // setting position again to make it visible
    engine
        .objects
        .get_mut("alt")
        .expect("Error during setting the position of the alt square")
        .set_position(1.5f32, 0f32, 0f32);

    engine
        .update_loop(move |_, _, _, _, _, _| {})
        .expect("Error during update loop");
}

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Any for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

source§

fn type_name(&self) -> &'static str

source§

impl<T> AnySync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSync for T
where T: Sync,