kubi/src/main.rs

123 lines
3 KiB
Rust
Raw Normal View History

2023-01-19 17:45:46 -06:00
use shipyard::{
World, Workload, IntoWorkload,
UniqueView, UniqueViewMut,
NonSendSync, Unique
};
2023-01-19 16:28:07 -06:00
use glium::{
Surface,
glutin::{
event_loop::{EventLoop, ControlFlow},
event::{Event, WindowEvent}
}
};
2023-01-19 17:45:46 -06:00
use glam::vec3;
use std::time::{Instant, Duration};
2023-01-19 14:27:34 -06:00
2023-01-19 14:58:49 -06:00
mod logging;
2023-01-19 17:45:46 -06:00
pub(crate) mod rendering;
pub(crate) mod world;
2023-01-21 20:24:28 -06:00
pub(crate) mod player;
2023-01-19 19:54:41 -06:00
pub(crate) mod prefabs;
2023-01-20 13:40:08 -06:00
pub(crate) mod transform;
2023-01-20 19:39:55 -06:00
pub(crate) mod settings;
2023-01-21 20:24:28 -06:00
pub(crate) mod camera;
2023-01-19 17:45:46 -06:00
use rendering::{Renderer, RenderTarget, BackgroundColor, clear_background};
2023-01-22 19:34:25 -06:00
use world::{loading::update_loaded_world_around_player, render::draw_world, init_world};
2023-01-20 14:55:02 -06:00
use player::spawn_player;
2023-01-19 19:54:41 -06:00
use prefabs::load_prefabs;
2023-01-20 20:40:23 -06:00
use settings::GameSettings;
2023-01-21 20:24:28 -06:00
use camera::compute_cameras;
2023-01-19 16:28:07 -06:00
2023-01-19 17:45:46 -06:00
#[derive(Unique)]
pub(crate) struct DeltaTime(Duration);
2023-01-19 16:28:07 -06:00
2023-01-20 14:55:02 -06:00
fn startup() -> Workload {
2023-01-19 17:45:46 -06:00
(
2023-01-20 14:55:02 -06:00
spawn_player,
2023-01-19 17:45:46 -06:00
).into_workload()
2023-01-19 16:28:07 -06:00
}
fn update() -> Workload {
2023-01-20 14:55:02 -06:00
(
2023-01-21 20:24:28 -06:00
update_loaded_world_around_player,
compute_cameras,
2023-01-20 14:55:02 -06:00
).into_workload()
}
fn render() -> Workload {
(
clear_background,
2023-01-22 10:01:54 -06:00
draw_world,
2023-01-21 20:24:28 -06:00
).into_sequential_workload()
2023-01-19 16:28:07 -06:00
}
2023-01-19 14:58:49 -06:00
2023-01-14 13:08:14 -06:00
fn main() {
2023-01-19 14:58:49 -06:00
logging::init();
2023-01-19 16:28:07 -06:00
//Create event loop
2023-01-19 14:58:49 -06:00
let event_loop = EventLoop::new();
2023-01-19 16:28:07 -06:00
//Create a shipyard world
let world = World::new();
2023-01-20 10:42:04 -06:00
//Add systems and uniques, Init and load things
2023-01-21 18:51:23 -06:00
world.add_unique_non_send_sync(Renderer::init(&event_loop));
2023-01-19 19:54:41 -06:00
world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.)));
world.add_unique(DeltaTime(Duration::default()));
2023-01-20 20:40:23 -06:00
world.add_unique(GameSettings::default());
2023-01-21 18:51:23 -06:00
load_prefabs(&world);
2023-01-22 19:34:25 -06:00
init_world(&world);
2023-01-20 14:55:02 -06:00
//Register workloads
world.add_workload(startup);
2023-01-19 16:28:07 -06:00
world.add_workload(update);
world.add_workload(render);
2023-01-20 14:55:02 -06:00
//Run startup systems
world.run_workload(startup).unwrap();
2023-01-19 16:28:07 -06:00
//Run the event loop
2023-01-19 17:45:46 -06:00
let mut last_update = Instant::now();
2023-01-19 14:58:49 -06:00
event_loop.run(move |event, _, control_flow| {
2023-01-19 16:28:07 -06:00
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent { event, .. } => match event {
2023-01-20 09:28:34 -06:00
WindowEvent::Resized(size) => {
// todo ...
}
2023-01-19 16:28:07 -06:00
WindowEvent::CloseRequested => {
2023-01-19 17:45:46 -06:00
log::info!("exit requested");
2023-01-19 16:28:07 -06:00
*control_flow = ControlFlow::Exit;
},
_ => (),
},
Event::MainEventsCleared => {
2023-01-19 17:45:46 -06:00
//Update delta time (maybe move this into a system?)
{
let mut dt_view = world.borrow::<UniqueViewMut<DeltaTime>>().unwrap();
let now = Instant::now();
dt_view.0 = now - last_update;
last_update = now;
}
//Run update workflow
2023-01-19 16:28:07 -06:00
world.run_workload(update).unwrap();
2023-01-19 17:45:46 -06:00
2023-01-19 20:08:09 -06:00
//Start rendering (maybe use custom views for this?)
2023-01-22 20:19:26 -06:00
let target = {
2023-01-21 18:51:23 -06:00
let renderer = world.borrow::<NonSendSync<UniqueView<Renderer>>>().unwrap();
2023-01-19 16:28:07 -06:00
renderer.display.draw()
};
world.add_unique_non_send_sync(RenderTarget(target));
2023-01-19 17:45:46 -06:00
//Run render workflow
2023-01-19 16:28:07 -06:00
world.run_workload(render).unwrap();
2023-01-19 17:45:46 -06:00
//Finish rendering
2023-01-19 16:28:07 -06:00
let target = world.remove_unique::<RenderTarget>().unwrap();
target.0.finish().unwrap();
},
_ => (),
};
2023-01-19 14:58:49 -06:00
});
2023-01-14 13:08:14 -06:00
}