kubi/src/main.rs

108 lines
2.6 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 player;
pub(crate) mod world;
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-19 17:45:46 -06:00
use rendering::{Rederer, RenderTarget, BackgroundColor, clear_background};
2023-01-20 10:42:04 -06:00
use world::GameWorld;
2023-01-19 19:54:41 -06:00
use prefabs::load_prefabs;
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
fn render() -> Workload {
2023-01-19 17:45:46 -06:00
(
clear_background,
).into_workload()
2023-01-19 16:28:07 -06:00
}
fn update() -> Workload {
(||()).into_workload()
}
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-19 16:28:07 -06:00
world.add_unique_non_send_sync(
Rederer::init(&event_loop)
);
2023-01-19 19:54:41 -06:00
load_prefabs(&world);
2023-01-20 10:42:04 -06:00
world.add_unique_non_send_sync(
GameWorld::new()
);
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-19 16:28:07 -06:00
world.add_workload(update);
world.add_workload(render);
//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-19 16:28:07 -06:00
let mut target = {
let renderer = world.borrow::<NonSendSync<UniqueView<Rederer>>>().unwrap();
renderer.display.draw()
};
target.clear_color_and_depth((0., 0., 0., 1.), 1.);
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
}