summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: f91e0c71326031e12296bee78b5e476aae8814e3 (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
use std::error::Error;
use std::path::Path;

use engine::camera::fly::{Extension as FlyCameraExtension, Options as FlyCameraOptions};
use engine::camera::Camera;
use engine::data_types::dimens::Dimens;
use engine::ecs::sole::Single;
use engine::event::Start as StartEvent;
use engine::input::Extension as InputExtension;
use engine::lighting::LightSourceBuilder;
use engine::material::Builder as MaterialBuilder;
use engine::renderer::Extension as RendererExtension;
use engine::shader::Program as ShaderProgram;
use engine::texture::{List as TextureList, Texture};
use engine::transform::Transform;
use engine::vector::{Vec2, Vec3};
use engine::vertex::Builder as VertexBuilder;
use engine::window::{CursorMode, Extension as WindowExtension, Window};
use engine::Engine;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

use crate::cube::{create_cube_mesh, Corner as CubeCorner, Side as CubeSide};

mod cube;

const WINDOW_SIZE: Dimens<u32> = Dimens { width: 1920, height: 1080 };

fn main() -> Result<(), Box<dyn Error>>
{
    let subscriber = FmtSubscriber::builder()
        .with_max_level(Level::TRACE)
        .finish();

    tracing::subscriber::set_global_default(subscriber)?;

    let mut engine = Engine::new();

    let mut transform = Transform::new();

    transform.set_position(Vec3 { x: 1.6, y: 0.0, z: 0.0 });

    let texture = Texture::open(Path::new("vent.png"))?;

    let texture_id = texture.id();

    engine.spawn((
        create_cube_mesh(cube_vertex_builder_cb),
        TextureList::from_iter([texture]),
        MaterialBuilder::new()
            .ambient_map(texture_id)
            .diffuse_map(texture_id)
            .specular_map(texture_id)
            .build(),
        transform,
        ShaderProgram::new()?,
    ));

    let light_source = LightSourceBuilder::new()
        .position(Vec3 { x: 1.2, y: 1.0, z: 1.5 })
        .build();

    engine.spawn((light_source,));

    engine.spawn((Camera { current: true, ..Default::default() },));

    engine.register_system(StartEvent, prepare_window);

    engine.add_extension(RendererExtension::default());

    engine.add_extension(
        WindowExtension::default()
            .window_title("Game")
            .window_size(WINDOW_SIZE),
    );

    engine.add_extension(FlyCameraExtension(FlyCameraOptions {
        camera_speed: 3.0,
        mouse_sensitivity: 0.2,
    }));

    engine.add_extension(InputExtension::default());

    engine.start();

    Ok(())
}

fn prepare_window(window: Single<Window>)
{
    window.set_cursor_mode(CursorMode::Disabled).unwrap();
}

fn cube_vertex_builder_cb(
    vertex_builder: VertexBuilder,
    _side: CubeSide,
    corner: CubeCorner,
) -> VertexBuilder
{
    vertex_builder.texture_coords(match corner {
        CubeCorner::TopRight => Vec2 { x: 1.0, y: 1.0 },
        CubeCorner::TopLeft => Vec2 { x: 0.0, y: 1.0 },
        CubeCorner::BottomRight => Vec2 { x: 1.0, y: 0.0 },
        CubeCorner::BottomLeft => Vec2 { x: 0.0, y: 0.0 },
    })
}