2023-04-14 04:22:17 +00:00
|
|
|
use std::{sync::Arc};
|
2023-03-16 21:47:36 +00:00
|
|
|
|
|
|
|
use async_std::{task::block_on, sync::Mutex};
|
2023-04-14 04:22:17 +00:00
|
|
|
|
|
|
|
use specs::{Dispatcher, World};
|
|
|
|
|
|
|
|
use tracing::{metadata::LevelFilter, info, debug, warn};
|
2023-03-16 21:47:36 +00:00
|
|
|
use tracing_subscriber::{
|
|
|
|
layer::{Layer, SubscriberExt},
|
|
|
|
filter::FilterFn,
|
|
|
|
util::SubscriberInitExt,
|
|
|
|
};
|
|
|
|
|
|
|
|
use winit::{window::{WindowBuilder, Window}, event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode}, event_loop::{EventLoop, ControlFlow}};
|
|
|
|
|
|
|
|
use crate::{renderer::{Renderer, BasicRenderer}, input_event::InputEvent};
|
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
struct GameLoop<'a, 'b> {
|
2023-03-16 21:47:36 +00:00
|
|
|
window: Arc<Window>,
|
|
|
|
renderer: Box<dyn Renderer>,
|
2023-04-14 04:22:17 +00:00
|
|
|
|
|
|
|
world: Arc<Mutex<World>>,
|
|
|
|
system_dispatchers: Vec<Dispatcher<'a, 'b>>,
|
2023-03-16 21:47:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
impl<'a, 'b> GameLoop<'a, 'b> {
|
|
|
|
pub async fn new(window: Arc<Window>, world: Arc<Mutex<World>>, system_dispatchers: Vec<Dispatcher<'a, 'b>>) -> GameLoop<'a, 'b> {
|
2023-03-16 21:47:36 +00:00
|
|
|
Self {
|
|
|
|
window: Arc::clone(&window),
|
2023-04-14 04:22:17 +00:00
|
|
|
renderer: Box::new(BasicRenderer::create_with_window(window).await),
|
|
|
|
|
|
|
|
world,
|
|
|
|
system_dispatchers,
|
2023-03-16 21:47:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn on_resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
|
|
|
|
self.renderer.on_resize(new_size).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_sync(&mut self, event: Event<()>, control_flow: &mut ControlFlow) {
|
|
|
|
block_on(self.run_event_loop(event, control_flow))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn update(&mut self) {
|
2023-04-14 04:22:17 +00:00
|
|
|
let world = self.world.lock().await;
|
|
|
|
for dispatcher in self.system_dispatchers.iter_mut() {
|
|
|
|
dispatcher.dispatch(&world);
|
|
|
|
}
|
2023-03-16 21:47:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn input_update(&mut self, event: &InputEvent) -> Option<ControlFlow> {
|
|
|
|
match event {
|
|
|
|
InputEvent::KeyboardInput {
|
|
|
|
input:
|
|
|
|
KeyboardInput {
|
|
|
|
state: ElementState::Pressed,
|
|
|
|
virtual_keycode: Some(VirtualKeyCode::Escape),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
self.on_exit();
|
|
|
|
|
|
|
|
Some(ControlFlow::Exit)
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
debug!("Got unhandled input event: \"{:?}\"", event);
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_window() {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_item() {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_exit(&mut self) {
|
|
|
|
info!("On exit!");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_event_loop(&mut self, event: Event<'_, ()>, control_flow: &mut ControlFlow) {
|
|
|
|
*control_flow = ControlFlow::Poll;
|
|
|
|
match event {
|
|
|
|
Event::WindowEvent {
|
|
|
|
ref event,
|
|
|
|
window_id,
|
|
|
|
} if window_id == self.window.id() => {
|
|
|
|
// If try_from failed, that means that the WindowEvent is not an
|
|
|
|
// input related event.
|
|
|
|
if let Ok(input_event) = InputEvent::try_from(event) {
|
|
|
|
if let Some(new_control) = self.input_update(&input_event).await {
|
|
|
|
*control_flow = new_control;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match event {
|
|
|
|
WindowEvent::CloseRequested => {
|
|
|
|
self.on_exit();
|
|
|
|
|
|
|
|
*control_flow = ControlFlow::Exit
|
|
|
|
},
|
|
|
|
|
|
|
|
WindowEvent::Resized(physical_size) => {
|
|
|
|
self.on_resize(*physical_size).await;
|
|
|
|
},
|
|
|
|
|
|
|
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
|
|
|
self.on_resize(**new_inner_size).await;
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Event::RedrawRequested(window_id) if window_id == self.window.id() => {
|
2023-04-14 04:22:17 +00:00
|
|
|
self.update().await;
|
|
|
|
|
2023-03-16 21:47:36 +00:00
|
|
|
match self.renderer.as_mut().render().await {
|
|
|
|
Ok(_) => {}
|
|
|
|
// Reconfigure the surface if lost
|
|
|
|
Err(wgpu::SurfaceError::Lost) => self.on_resize(self.renderer.as_ref().size()).await,
|
|
|
|
// The system is out of memory, we should probably quit
|
|
|
|
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
|
|
|
// All other errors (Outdated, Timeout) should be resolved by the next frame
|
|
|
|
Err(e) => eprintln!("{:?}", e),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Event::MainEventsCleared => {
|
|
|
|
self.window.request_redraw();
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
pub struct Game<'a, 'b> {
|
|
|
|
world: Option<Arc<Mutex<World>>>,
|
|
|
|
system_dispatchers: Option<Vec<Dispatcher<'a, 'b>>>,
|
2023-03-16 21:47:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
impl<'a, 'b> Default for Game<'a, 'b> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
world: None,
|
|
|
|
system_dispatchers: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 21:47:36 +00:00
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
impl<'a, 'b> Game<'static, 'static> {
|
|
|
|
pub async fn initialize() -> Game<'static, 'static> {
|
2023-03-16 21:47:36 +00:00
|
|
|
let filter = FilterFn::new(|metadata| {
|
|
|
|
metadata.module_path()
|
|
|
|
.unwrap_or_else(|| metadata.target())
|
|
|
|
.starts_with("lyra_engine") && (LevelFilter::INFO >= metadata.level().to_owned())
|
|
|
|
});
|
|
|
|
|
|
|
|
let layer = tracing_subscriber::fmt::layer();
|
|
|
|
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
.with(layer.with_filter(filter))
|
|
|
|
.init();
|
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
Self::default()
|
2023-03-16 21:47:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
pub fn with_world(&mut self, world: World) -> &mut Self {
|
|
|
|
self.world = Some(Arc::new(Mutex::new(world)));
|
2023-03-16 21:47:36 +00:00
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
self
|
2023-03-16 21:47:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
pub fn with_world_arc(&mut self, world: Arc<Mutex<World>>) -> &mut Self {
|
|
|
|
self.world = Some(world);
|
|
|
|
|
|
|
|
self
|
2023-03-16 21:47:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
pub fn with_dispatchers(&mut self, dispatchers: Vec<Dispatcher<'static, 'static>>) -> &mut Self {
|
|
|
|
self.system_dispatchers = Some(dispatchers);
|
|
|
|
|
|
|
|
self
|
2023-03-16 21:47:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
pub fn with_dispatcher(&mut self, dispatcher: Dispatcher<'static, 'static>) -> &mut Self {
|
|
|
|
self.system_dispatchers.get_or_insert_with(|| Vec::new()).push(dispatcher);
|
|
|
|
|
|
|
|
self
|
2023-03-16 21:47:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(&mut self) {
|
2023-04-14 04:22:17 +00:00
|
|
|
let world = self.world.take().expect("ECS World was never given to Game!");
|
|
|
|
let system_dispatchers = self.system_dispatchers
|
|
|
|
.take().unwrap_or_else(|| Vec::new());
|
|
|
|
|
2023-03-16 21:47:36 +00:00
|
|
|
let event_loop = EventLoop::new();
|
2023-04-14 04:22:17 +00:00
|
|
|
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
|
2023-03-16 21:47:36 +00:00
|
|
|
|
2023-04-14 04:22:17 +00:00
|
|
|
let mut g_loop = GameLoop::new(Arc::clone(&window), world, system_dispatchers).await;
|
2023-03-16 21:47:36 +00:00
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
g_loop.run_sync(event, control_flow);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|