From c2419bfee851810a27672b40c77c829e4b4c2829 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 14 Apr 2024 12:36:31 +0200 Subject: chore: use new engine architecture --- src/main.rs | 299 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 174 insertions(+), 125 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 321009e..0cbe754 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,166 +2,228 @@ use std::error::Error; use std::path::Path; use engine::camera::Camera; -use engine::lighting::LightSourceBuilder; +use engine::delta_time::DeltaTime; +use engine::ecs::sole::Single; +use engine::ecs::Query; +use engine::event::Update as UpdateEvent; +use engine::input::{Extension as InputExtension, Keys}; +use engine::lighting::{LightSource, LightSourceBuilder}; use engine::material::Builder as MaterialBuilder; -use engine::object::Id as ObjectId; -use engine::texture::{Id as TextureId, Texture}; +use engine::renderer::Extension as RendererExtension; +use engine::shader::{Kind as ShaderKind, Program as ShaderProgram, Shader}; +use engine::texture::{Id as TextureId, Map as TextureMap, Texture}; +use engine::transform::Transform; use engine::vector::{Vec2, Vec3}; -use engine::{Engine, Key, WindowSettingsBuilder, WindowSize}; +use engine::vertex::Builder as VertexBuilder; +use engine::window::{Extension as WindowExtension, Key, KeyState}; +use engine::Engine; use tracing::Level; use tracing_subscriber::FmtSubscriber; -use crate::cube::{create_cube, Corner as CubeCorner}; +use crate::cube::{create_cube_mesh, Corner as CubeCorner, Side as CubeSide}; mod cube; -const WINDOW_SIZE: WindowSize = WindowSize { width: 500, height: 600 }; +const VERTEX_SHADER_FILE: &str = "vertex.glsl"; +const FRAGMENT_SHADER_FILE: &str = "fragment.glsl"; -fn main() -> Result<(), Box> +const SHADER_DIR: &str = "engine"; + +const CAM_SPEED: f32 = 3.0; + +const LIGHT_SOURCE_SPEED: f32 = 1.2; + +fn lmao( + camera_query: Query<(Camera,)>, + light_source_query: Query<(LightSource,)>, + keys: Single, + delta_time: Single, +) { - let subscriber = FmtSubscriber::builder() - .with_max_level(Level::TRACE) - .finish(); + let (mut camera,) = camera_query.iter().next().expect("No camera"); - tracing::subscriber::set_global_default(subscriber)?; + let (mut light_source,) = light_source_query.iter().next().expect("No light source"); - let mut engine = Engine::::new( - &WindowSettingsBuilder::new().build(WINDOW_SIZE, "Yaaay lmao"), - LookAtCamera::new(), - )?; + let delta_time = delta_time.duration; - let texture = Texture::open(Path::new("vent.png"))?; + if matches!(keys.get_key_state(Key::W), KeyState::Pressed) { + let cam_target_direction = camera.target_direction().clone(); - let mut textured_cube = create_cube(|vertex_builder, _, corner| { - 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 }, - }) - }) - .texture(TextureId::new(0), texture) - .material( - MaterialBuilder::new() - .ambient_map(TextureId::new(0)) - .diffuse_map(TextureId::new(0)) - .specular_map(TextureId::new(0)) - .build(), - ) - .build(ObjectId::new(4))?; + camera.position += cam_target_direction * CAM_SPEED * delta_time.as_secs_f32(); + } - textured_cube.translate(Vec3 { x: 1.6, y: 0.0, z: 0.0 }); + if matches!(keys.get_key_state(Key::S), KeyState::Pressed) { + let rev_cam_target_direction = -camera.target_direction().clone(); - engine.add_object(textured_cube); + camera.position += + rev_cam_target_direction * CAM_SPEED * delta_time.as_secs_f32(); + } - let light_source = LightSourceBuilder::new() - .position(Vec3 { x: 1.2, y: 1.0, z: 1.5 }) - .build(); + if matches!(keys.get_key_state(Key::A), KeyState::Pressed) { + let cam_left = camera.left().clone(); - engine.set_light_source(light_source); + // Camera speed adjusted to be same no matter how far the distance is to the + // camera target + let cam_speed_dist_adj = + CAM_SPEED * (camera.position.clone() - camera.target.clone()).length(); - let cam_speed = 3.0; + camera.position += cam_left * cam_speed_dist_adj * delta_time.as_secs_f32(); + } - let light_source_speed = 1.2; + if matches!(keys.get_key_state(Key::D), KeyState::Pressed) { + let cam_right = camera.right().clone(); - engine.start(|engine| { - let delta_time = *engine.delta_time(); + // Camera speed adjusted to be same no matter how far the distance is to the + // camera target + let cam_speed_dist_adj = + CAM_SPEED * (camera.position.clone() - camera.target.clone()).length(); - if engine.is_key_pressed(Key::W).unwrap() { - let cam_target_direction = engine.camera().target_direction().clone(); + camera.position += cam_right * cam_speed_dist_adj * delta_time.as_secs_f32(); + } - engine.camera_mut().position += - cam_target_direction * cam_speed * delta_time.as_secs_f32(); - } - if engine.is_key_pressed(Key::S).unwrap() { - let rev_cam_target_direction = -engine.camera().target_direction().clone(); + if matches!(keys.get_key_state(Key::K), KeyState::Pressed) { + let cam_up = camera.up().clone(); - engine.camera_mut().position += - rev_cam_target_direction * cam_speed * delta_time.as_secs_f32(); - } - if engine.is_key_pressed(Key::A).unwrap() { - let cam_left = engine.camera().left().clone(); + // Camera speed adjusted to be same no matter how far the distance is to the + // camera target + let cam_speed_dist_adj = + CAM_SPEED * (camera.position.clone() - camera.target.clone()).length(); - // Camera speed adjusted to be same no matter how far the distance is to the - // camera target - let cam_speed_dist_adj = cam_speed - * (engine.camera().position() - engine.camera().target()).length(); + camera.position += cam_up * cam_speed_dist_adj * delta_time.as_secs_f32(); + } - engine.camera_mut().position += - cam_left * cam_speed_dist_adj * delta_time.as_secs_f32(); - } - if engine.is_key_pressed(Key::D).unwrap() { - let cam_right = engine.camera().right().clone(); + if matches!(keys.get_key_state(Key::J), KeyState::Pressed) { + let cam_up = camera.down().clone(); - // Camera speed adjusted to be same no matter how far the distance is to the - // camera target - let cam_speed_dist_adj = cam_speed - * (engine.camera().position() - engine.camera().target()).length(); + // Camera speed adjusted to be same no matter how far the distance is to the + // camera target + let cam_speed_dist_adj = + CAM_SPEED * (camera.position.clone() - camera.target.clone()).length(); - engine.camera_mut().position += - cam_right * cam_speed_dist_adj * delta_time.as_secs_f32(); - } - if engine.is_key_pressed(Key::K).unwrap() { - let cam_up = engine.camera().up().clone(); + camera.position += cam_up * cam_speed_dist_adj * delta_time.as_secs_f32(); + } - // Camera speed adjusted to be same no matter how far the distance is to the - // camera target - let cam_speed_dist_adj = cam_speed - * (engine.camera().position() - engine.camera().target()).length(); + if matches!(keys.get_key_state(Key::O), KeyState::Pressed) { + let front_right = Vec3 { x: 1.0, y: 0.0, z: 1.0 }; - engine.camera_mut().position += - cam_up * cam_speed_dist_adj * delta_time.as_secs_f32(); - } - if engine.is_key_pressed(Key::J).unwrap() { - let cam_up = engine.camera().down().clone(); + light_source + .translate(front_right * LIGHT_SOURCE_SPEED * delta_time.as_secs_f32()); + } - // Camera speed adjusted to be same no matter how far the distance is to the - // camera target - let cam_speed_dist_adj = cam_speed - * (engine.camera().position() - engine.camera().target()).length(); + if matches!(keys.get_key_state(Key::L), KeyState::Pressed) { + let back_left = Vec3 { x: -1.0, y: 0.0, z: -1.0 }; - engine.camera_mut().position += - cam_up * cam_speed_dist_adj * delta_time.as_secs_f32(); - } + light_source.translate(back_left * LIGHT_SOURCE_SPEED * delta_time.as_secs_f32()); + } +} - if engine.is_key_pressed(Key::O).unwrap() { - let light_source = engine.light_source_mut().unwrap(); +fn main() -> Result<(), Box> +{ + let subscriber = FmtSubscriber::builder() + .with_max_level(Level::TRACE) + .finish(); - let front_right = Vec3 { x: 1.0, y: 0.0, z: 1.0 }; + tracing::subscriber::set_global_default(subscriber)?; - light_source - .translate(front_right * light_source_speed * delta_time.as_secs_f32()); - } + let mut engine = Engine::new(); - if engine.is_key_pressed(Key::L).unwrap() { - let light_source = engine.light_source_mut().unwrap(); + let texture = Texture::open(Path::new("vent.png"))?; - let back_left = Vec3 { x: -1.0, y: 0.0, z: -1.0 }; + let material = MaterialBuilder::new() + .ambient_map(TextureId::new(0)) + .diffuse_map(TextureId::new(0)) + .specular_map(TextureId::new(0)) + .build(); + + /* + let mut textured_cube = create_cube(cube_vertex_builder_cb) + .texture(TextureId::new(0), texture.clone()) + .material(material.clone()) + .build(ObjectId::new(4))?; + + textured_cube.translate(Vec3 { x: 1.6, y: 0.0, z: 0.0 }); - light_source - .translate(back_left * light_source_speed * delta_time.as_secs_f32()); - } - })?; + engine.add_object(textured_cube); + */ + + let mut transform = Transform::new(); + + transform.set_position(Vec3 { x: 1.6, y: 0.0, z: 0.0 }); + + let mut shader_program = ShaderProgram::new(); + + shader_program.push_shader( + Shader::read_shader_file( + ShaderKind::Vertex, + &Path::new(SHADER_DIR).join(VERTEX_SHADER_FILE), + )? + .preprocess()?, + ); + + shader_program.push_shader( + Shader::read_shader_file( + ShaderKind::Fragment, + &Path::new(SHADER_DIR).join(FRAGMENT_SHADER_FILE), + )? + .preprocess()?, + ); + + engine.spawn(( + create_cube_mesh(cube_vertex_builder_cb), + TextureMap::from_iter([(TextureId::new(0), texture)]), + material, + transform, + shader_program, + )); + + 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(UpdateEvent, lmao); + + engine.add_extension(RendererExtension::default()); + engine.add_extension(WindowExtension::default().window_title("Game")); + engine.add_extension(InputExtension::default()); + + engine.start(); Ok(()) } -#[derive(Debug)] -pub struct LookAtCamera +fn cube_vertex_builder_cb( + vertex_builder: VertexBuilder, + _side: CubeSide, + corner: CubeCorner, +) -> VertexBuilder { - position: Vec3, - target: Vec3, + 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 }, + }) } -impl LookAtCamera +trait CameraExt { - fn new() -> Self - { - let position = Vec3 { x: 0.0, y: 0.0, z: 3.0 }; + fn target_direction(&self) -> Vec3; - Self { position, target: Vec3::default() } - } + fn right(&self) -> Vec3; + + fn left(&self) -> Vec3; + + fn up(&self) -> Vec3; + fn down(&self) -> Vec3; +} + +impl CameraExt for Camera +{ fn target_direction(&self) -> Vec3 { -(&self.position - &self.target).normalize() @@ -191,16 +253,3 @@ impl LookAtCamera -self.up() } } - -impl Camera for LookAtCamera -{ - fn position(&self) -> Vec3 - { - self.position.clone() - } - - fn target(&self) -> Vec3 - { - self.target.clone() - } -} -- cgit v1.2.3-18-g5258