diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:24 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:24 +0100 |
commit | 09a777d42d6301028cd3a8967ff6a82e703c8800 (patch) | |
tree | 79ac93287a15e8b0f90e1ac5d6981fa6eaeb6229 /examples/render-quad/src/main.rs | |
parent | 6367a9ba5a549b62f01da61fb50323877b9f52ff (diff) |
refactor(all): remove levels and render
Diffstat (limited to 'examples/render-quad/src/main.rs')
-rw-r--r-- | examples/render-quad/src/main.rs | 252 |
1 files changed, 50 insertions, 202 deletions
diff --git a/examples/render-quad/src/main.rs b/examples/render-quad/src/main.rs index c5d52a9..1955b51 100644 --- a/examples/render-quad/src/main.rs +++ b/examples/render-quad/src/main.rs @@ -1,82 +1,29 @@ //! Renders ./example.bsp geometry: (), texture_idx: () geometry: (), texture_idx: () -#[macro_use] -extern crate stockton_input_codegen; +extern crate gfx_hal as hal; #[macro_use] extern crate legion; -use std::{ - collections::BTreeMap, - path::Path, - sync::{Arc, RwLock}, -}; - -use stockton_contrib::{delta_time::*, flycam::*}; -use stockton_input::{Axis, InputManager, Mouse}; -use stockton_levels::{ - parts::{data::{Geometry, Vertex}, FsResolver}, - types::Rgba, -}; -use stockton_render::{ - level::{LevelDrawPass, LevelDrawPassConfig}, - ui::UiDrawPass, - window::{process_window_events_system, UiState, WindowEvent, WindowFlow}, -}; -use stockton_skeleton::{ - draw_passes::ConsDrawPass, error::full_error_display, Renderer, - Session, types::*, - components::{CameraSettings, Transform}, -}; - use anyhow::{Context, Result}; -use egui::{containers::CentralPanel, Frame}; use log::warn; +use stockton_skeleton::{error::full_error_display, Renderer, Session}; use winit::{ - event::Event, + event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; -mod level; -use level::*; - -type Dp<'a> = ConsDrawPass<LevelDrawPass<'a, DemoLevel>, UiDrawPass<'a>>; +mod draw_pass; +mod system; +use draw_pass::*; +use system::*; -#[derive(InputManager, Default, Clone, Debug)] -struct MovementInputs { - #[axis] - x: Axis, - - #[axis] - y: Axis, - - #[axis] - z: Axis, -} - -impl FlycamInput for MovementInputs { - fn get_x_axis(&self) -> &Axis { - &self.x - } - fn get_y_axis(&self) -> &Axis { - &self.y - } - fn get_z_axis(&self) -> &Axis { - &self.z - } -} - -#[system] -fn hello_world(#[resource] ui: &mut UiState) { - CentralPanel::default() - .frame(Frame::none()) - .show(ui.ctx(), |ui| { - ui.heading("Hello, World!"); - }); -} +/// Alias for our drawpass +type Dp<'a> = ExampleDrawPass<'a>; fn main() { + // Wrap for full error display if let Err(err) = try_main() { eprintln!("{}", full_error_display(err)); } @@ -101,165 +48,66 @@ fn try_main() -> Result<()> { .build(&event_loop) .context("Error creating window")?; + // Grab cursor + window.set_cursor_visible(false); if window.set_cursor_grab(true).is_err() { warn!("warning: cursor not grabbed"); } - window.set_cursor_visible(false); - - // TODO: Parse the map file - let map = Arc::new(RwLock::new(DemoLevel { - faces: vec![Face { - geometry: Geometry::Vertices( - Vertex { - position: Vector3::new(-128.0, 128.0, 128.0), - tex: Vector2::new(0.0, 0.0), - color: Rgba::from_slice(&[0, 0, 0, 1]), - }, - Vertex { - position: Vector3::new(-128.0, -128.0, 128.0), - tex: Vector2::new(0.0, 1.0), - color: Rgba::from_slice(&[0, 0, 0, 1]), - }, - Vertex { - position: Vector3::new(128.0, 128.0, 128.0), - tex: Vector2::new(1.0, 0.0), - color: Rgba::from_slice(&[0, 0, 0, 1]), - }, - ), - texture_idx: 0, - }] - .into_boxed_slice(), - textures: vec![Texture { - name: "example_texture".to_string(), - }] - .into_boxed_slice(), - })); - - // Create the UI State - let ui = UiState::default(); - - // Create the input manager - let manager = { - use stockton_input::InputMutation::*; - use MovementInputsFields::*; - - let mut actions = BTreeMap::new(); - actions.insert(17, (Z, PositiveAxis)); // W - actions.insert(30, (X, NegativeAxis)); // A - actions.insert(31, (Z, NegativeAxis)); // S - actions.insert(32, (X, PositiveAxis)); // D - actions.insert(29, (Y, NegativeAxis)); // Ctrl - actions.insert(57, (Y, PositiveAxis)); // Space - - MovementInputsManager::new(actions) - }; - - // Load everything into the session + // Our game world let mut session = Session::new(move |schedule| { - schedule - .add_system(update_deltatime_system()) - .add_system(process_window_events_system::<MovementInputsManager>()) - .flush() - .add_system(hello_world_system()) - .add_system(flycam_move_system::<MovementInputsManager>()); + schedule.add_system(mutate_state_system()); }); - session.resources.insert(map.clone()); - session.resources.insert(manager); - session.resources.insert(Timing::default()); - session.resources.insert(Mouse::default()); - session.resources.insert(ui); - - // Add our player entity - let player = session.world.push(( - Transform { - position: Vector3::new(0.0, 0.0, 0.0), - rotation: Vector3::new(0.0, 0.0, 0.0), - }, - CameraSettings { - far: 1024.0, - fov: 90.0, - near: 0.1, - }, - FlycamControlled::new(512.0, 400.0), - )); + // An entity to keep track of our state + let state_ent = session.world.push((ExampleState::default(),)); // Create the renderer - let renderer = Renderer::<Dp<'static>>::new( - &window, - &mut session, - ( - LevelDrawPassConfig { - active_camera: player, - tex_resolver: FsResolver::new(Path::new("./examples/render-quad/textures"), map), - }, - (), - ), - )?; - - let new_control_flow = Arc::new(RwLock::new(ControlFlow::Poll)); - let (window_flow, tx) = WindowFlow::new(new_control_flow.clone()); - session.resources.insert(window_flow); - - // Populate the initial UI state - { - let ui = &mut session.resources.get_mut::<UiState>().unwrap(); - ui.populate_initial_state(&renderer); - } + let renderer = + Renderer::<Dp<'static>>::new(&window, &mut session, ExampleDrawPassConfig { state_ent })?; + // We'll be moving it in/out of here, so we need an Option for safety. let mut renderer = Some(renderer); // Done loading - This is our main loop. // It just communicates events to the session and continuously ticks - event_loop.run(move |event, _, flow| { - match event { - Event::MainEventsCleared => { - window.request_redraw(); - } - Event::RedrawRequested(_) => { - session.do_update(); - let r = renderer.take().unwrap(); - match r.render(&session) { - Ok(r) => { - renderer = Some(r); - } - Err(e) => { - println!("Error drawing: {}", full_error_display(e)); - - // TODO: Not really sound - *(new_control_flow.write().unwrap()) = ControlFlow::Exit; - } + event_loop.run(move |event, _, flow| match event { + Event::MainEventsCleared => { + window.request_redraw(); + } + Event::RedrawRequested(_) => { + session.do_update(); + + // Render + let r = renderer.take().unwrap(); + match r.render(&session) { + Ok(r) => { + renderer = Some(r); } - } - _ => { - if let Some(we) = WindowEvent::from(&event) { - tx.send(we).unwrap(); + Err(e) => { + println!("Error drawing: {}", full_error_display(e)); - if let WindowEvent::SizeChanged(_, _) = we { - let r = renderer.take().unwrap(); - match r.recreate_surface(&session) { - Ok(r) => { - renderer = Some(r); - } - Err(e) => { - println!("Error resizing: {}", full_error_display(e)); - - // TODO: Not really sound - *(new_control_flow.write().unwrap()) = ControlFlow::Exit; - } - } - } + *flow = ControlFlow::Exit; } } } + Event::WindowEvent { + window_id: _, + event: WindowEvent::Resized(_), + } => { + // (Attempt) resize + let r = renderer.take().unwrap(); + match r.recreate_surface(&session) { + Ok(r) => { + renderer = Some(r); + } + Err(e) => { + println!("Error resizing: {}", full_error_display(e)); - // Update the control flow if the session has requested it. - { - let new_control_flow = new_control_flow.read().unwrap(); - if *new_control_flow != *flow { - *flow = *new_control_flow; + *flow = ControlFlow::Exit; + } } - }; + } + _ => (), }); } |