summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 358b15b2e3b8eaa164b4253ff62c327c5583d767 (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
128
129
130
131
132
133
134
use std::error::Error;
use std::path::Path;

use engine::camera::fly::{Extension as FlyCameraExtension, Options as FlyCameraOptions};
use engine::camera::Camera;
use engine::color::Color;
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::{GlobalLight, LightSource};
use engine::material::Builder as MaterialBuilder;
use engine::renderer::Extension as RendererExtension;
use engine::shader::Program as ShaderProgram;
use engine::texture::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 transform = Transform {
        position: Vec3 { x: 1.6, y: 0.0, z: 0.0 },
        ..Default::default()
    };

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

    let texture_id = texture.id();

    engine.spawn((
        create_cube_mesh(cube_vertex_builder_cb),
        MaterialBuilder::new()
            .textures([texture])
            .diffuse(Color::WHITE_F32)
            .ambient(Color::WHITE_F32)
            .specular(Color::WHITE_F32)
            .ambient_map(texture_id)
            .diffuse_map(texture_id)
            .specular_map(texture_id)
            .build(),
        transform,
        ShaderProgram::new()?,
    ));

    let light_source_texture = Texture::open(Path::new("yellow.jpg"))?;

    let light_source_texture_id = light_source_texture.id();

    engine.spawn((
        LightSource::default(),
        Transform {
            position: Vec3 { x: -2.0, y: 3.0, z: 3.0 },
            ..Default::default()
        },
        create_cube_mesh(cube_vertex_builder_cb),
        MaterialBuilder::new()
            .textures([light_source_texture])
            .diffuse(Color::WHITE_F32)
            .ambient(Color::WHITE_F32 * 3.0)
            .specular(Color::WHITE_F32)
            .ambient_map(light_source_texture_id)
            .diffuse_map(light_source_texture_id)
            .specular_map(light_source_texture_id)
            .build(),
        ShaderProgram::new()?,
    ));

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

    engine.add_sole(
        GlobalLight::builder()
            .ambient_offset(-Color::WHITE_F32)
            .build(),
    )?;

    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 },
    })
}