From 04e4b246fe2992b0c19d274dbe4034b0accb85c7 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 29 Jan 2023 01:21:28 +0100 Subject: [PATCH] minor refactor --- src/delta_time.rs | 12 +++++++++++ src/fly_controller.rs | 2 +- src/main.rs | 14 ++++++------- src/rendering/crosshair.rs | 0 src/rendering/selection_box.rs | 38 ++++++++++++++++++++-------------- 5 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 src/delta_time.rs create mode 100644 src/rendering/crosshair.rs diff --git a/src/delta_time.rs b/src/delta_time.rs new file mode 100644 index 0000000..e9e393d --- /dev/null +++ b/src/delta_time.rs @@ -0,0 +1,12 @@ +use std::time::Duration; + +use shipyard::{Unique, AllStoragesView}; + +#[derive(Unique, Default)] +pub(crate) struct DeltaTime(pub Duration); + +pub fn init_delta_time( + storages: AllStoragesView +) { + storages.add_unique(DeltaTime::default()) +} diff --git a/src/fly_controller.rs b/src/fly_controller.rs index a8972ea..07e0010 100644 --- a/src/fly_controller.rs +++ b/src/fly_controller.rs @@ -1,7 +1,7 @@ use glam::{Vec3, Mat4, Quat, EulerRot, Vec2}; use shipyard::{Component, View, ViewMut, IntoIter, UniqueView, Workload, IntoWorkload}; use std::f32::consts::PI; -use crate::{transform::Transform, input::Inputs, settings::GameSettings, DeltaTime}; +use crate::{transform::Transform, input::Inputs, settings::GameSettings, delta_time::DeltaTime}; #[derive(Component)] pub struct FlyController; diff --git a/src/main.rs b/src/main.rs index 2cda345..ee70afd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use shipyard::{ World, Workload, IntoWorkload, UniqueView, UniqueViewMut, - NonSendSync, Unique + NonSendSync }; use glium::{ glutin::{ @@ -10,7 +10,7 @@ use glium::{ } }; use glam::vec3; -use std::time::{Instant, Duration}; +use std::time::Instant; mod logging; @@ -25,6 +25,7 @@ pub(crate) mod events; pub(crate) mod input; pub(crate) mod fly_controller; pub(crate) mod block_placement; +pub(crate) mod delta_time; use rendering::{ Renderer, @@ -45,17 +46,17 @@ use events::{clear_events, process_glutin_events}; use input::{init_input, process_inputs}; use fly_controller::update_controllers; use rendering::{ - selection_box::render_selection_box, + selection_box::{render_selection_box, init_selection_box_buffers}, world::draw_world, }; use block_placement::block_placement_system; - -#[derive(Unique)] -pub(crate) struct DeltaTime(Duration); +use delta_time::{DeltaTime, init_delta_time}; fn startup() -> Workload { ( + init_delta_time, load_prefabs, + init_selection_box_buffers, init_input, init_game_world, spawn_player, @@ -96,7 +97,6 @@ fn main() { //Add systems and uniques, Init and load things world.add_unique_non_send_sync(Renderer::init(&event_loop)); world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.))); - world.add_unique(DeltaTime(Duration::default())); world.add_unique(GameSettings::default()); //Register workloads diff --git a/src/rendering/crosshair.rs b/src/rendering/crosshair.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/rendering/selection_box.rs b/src/rendering/selection_box.rs index febfe47..7f0ad56 100644 --- a/src/rendering/selection_box.rs +++ b/src/rendering/selection_box.rs @@ -1,4 +1,4 @@ -use shipyard::{View, IntoIter, NonSendSync, UniqueViewMut, UniqueView}; +use shipyard::{View, IntoIter, NonSendSync, UniqueViewMut, UniqueView, AllStoragesView, Unique}; use glium::{ Surface, implement_vertex, @@ -43,6 +43,9 @@ const fn box_vertices() -> [SelBoxVertex; CUBE_VERTICES.len() / 3] { } const BOX_VERTICES: &[SelBoxVertex] = &box_vertices(); +#[derive(Unique)] +pub struct SelectionBoxBuffers(VertexBuffer, IndexBuffer); + //wip pub fn render_selection_box( lookat: View, @@ -50,27 +53,16 @@ pub fn render_selection_box( mut target: NonSendSync>, display: NonSendSync>, program: NonSendSync>, + buffers: NonSendSync>, ) { let camera = camera.iter().next().unwrap(); let Some(lookat) = lookat.iter().next() else { return }; let Some(lookat) = lookat.0 else { return }; - //this may be slow but the amount of vertices is very low - let vert = VertexBuffer::new( - &display.display, - BOX_VERTICES - ).unwrap(); - - let index = IndexBuffer::new( - &display.display, - PrimitiveType::TrianglesList, - CUBE_INDICES - ).unwrap(); - //Darken block target.0.draw( - &vert, - &index, + &buffers.0, + &buffers.1, &program.0, &uniform! { u_color: [0., 0., 0., 0.5_f32], @@ -89,3 +81,19 @@ pub fn render_selection_box( } ).unwrap(); } + +pub fn init_selection_box_buffers( + storages: AllStoragesView, + display: NonSendSync> +) { + let vert = VertexBuffer::new( + &display.display, + BOX_VERTICES + ).unwrap(); + let index = IndexBuffer::new( + &display.display, + PrimitiveType::TrianglesList, + CUBE_INDICES + ).unwrap(); + storages.add_unique_non_send_sync(SelectionBoxBuffers(vert, index)); +}