lyra-engine/src/game.rs

387 lines
12 KiB
Rust
Raw Normal View History

2023-09-01 01:34:58 +00:00
use std::sync::Arc;
2023-03-16 21:47:36 +00:00
use async_std::{task::block_on, sync::Mutex};
//use hecs::World;
use instant::Instant;
use tracing::{metadata::LevelFilter, info, debug, warn, error, Level};
use tracing_appender::non_blocking;
2023-03-16 21:47:36 +00:00
use tracing_subscriber::{
layer::{Layer, SubscriberExt},
filter::{FilterFn, self},
util::SubscriberInitExt, fmt,
2023-03-16 21:47:36 +00:00
};
use winit::{window::{WindowBuilder, Window}, event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, DeviceEvent}, event_loop::{EventLoop, ControlFlow}};
2023-03-16 21:47:36 +00:00
2023-09-01 15:41:54 +00:00
use crate::{render::renderer::{Renderer, BasicRenderer}, input_event::InputEvent, ecs::{SimpleSystem, SystemDispatcher, resources::{WindowState, Events, EventQueue}}, input::InputSystem};
pub struct Controls<'a> {
2023-09-01 01:34:58 +00:00
pub world: &'a mut edict::World,
}
2023-03-16 21:47:36 +00:00
struct TickCounter {
counter: u32,
last_second: Instant,
changed: bool,
tps: f32,
/// the time (in seconds) that passes between each tick
tick_time: f32
}
impl TickCounter {
fn new() -> Self {
Self {
counter: 0,
last_second: Instant::now(),
tps: 0.0,
changed: false,
tick_time: 0.0,
}
}
/// Returns true if the tps changed
fn tick(&mut self) -> bool {
self.counter += 1;
if self.last_second.elapsed().as_secs() > 0 {
self.tick_time = 1000.0 / self.counter as f32;
self.tps = self.counter as f32;
self.changed = true;
self.counter = 0;
self.last_second = Instant::now();
}
self.changed
}
/// Gets the change in ticks per second
fn get_change(&mut self) -> Option<f32> {
match self.changed {
true => {
self.changed = false;
Some(self.tps)
},
false => None,
}
}
/// Get the time (in seconds) between ticks
fn get_tick_time(&self) -> f32 {
self.tick_time
}
}
struct GameLoop {
2023-03-16 21:47:36 +00:00
window: Arc<Window>,
renderer: Box<dyn Renderer>,
2023-09-01 01:34:58 +00:00
world: Arc<Mutex<edict::World>>,
2023-06-29 03:18:44 +00:00
/// higher priority systems
engine_sys_dispatcher: SystemDispatcher,
user_sys_dispatcher: SystemDispatcher,
fps_counter: TickCounter,
2023-03-16 21:47:36 +00:00
}
impl GameLoop {
2023-09-01 01:34:58 +00:00
pub async fn new(window: Arc<Window>, world: Arc<Mutex<edict::World>>, user_systems: SystemDispatcher) -> GameLoop {
2023-03-16 21:47:36 +00:00
Self {
window: Arc::clone(&window),
renderer: Box::new(BasicRenderer::create_with_window(window).await),
world,
engine_sys_dispatcher: SystemDispatcher::new(),
user_sys_dispatcher: user_systems,
fps_counter: TickCounter::new(),
2023-03-16 21:47:36 +00:00
}
}
pub async fn on_resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
2023-09-01 01:34:58 +00:00
self.renderer.on_resize(new_size);
2023-03-16 21:47:36 +00:00
}
2023-09-01 15:41:54 +00:00
pub async fn on_init(&mut self) {
// Create the EventQueue resource in the world
let mut world = self.world.lock().await;
world.insert_resource(EventQueue::new());
}
2023-03-16 21:47:36 +00:00
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-06-03 17:36:39 +00:00
let mut world = self.world.lock().await;
2023-07-19 05:18:16 +00:00
let mut controls = Controls {
world: &mut world,
};
if let Err(e) = self.engine_sys_dispatcher.execute_mut(&mut controls) {
error!("Error when executing engine ecs systems: '{}'", e);
}
if let Err(e) = self.user_sys_dispatcher.execute_mut(&mut controls) {
error!("Error when executing user ecs systems: '{}'", e);
}
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)
},
// TODO: Create system for this? or maybe merge into input system, idk
InputEvent::CursorEntered { .. } => {
let mut world = self.world.lock().await;
2023-09-01 01:34:58 +00:00
let mut state = world.with_resource(|| WindowState::new());
state.is_cursor_inside_window = true;
None
},
InputEvent::CursorLeft { .. } => {
let mut world = self.world.lock().await;
2023-09-01 01:34:58 +00:00
let mut state = world.with_resource(|| WindowState::new());
state.is_cursor_inside_window = false;
None
}
2023-03-16 21:47:36 +00:00
_ => {
//debug!("Got unhandled input event: \"{:?}\"", event);
2023-03-16 21:47:36 +00:00
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::DeviceEvent { device_id, event } => match event {
// convert a MouseMotion event to an InputEvent
DeviceEvent::MouseMotion { delta } => {
let mut world = self.world.lock().await;
// make sure that the mouse is inside the window and the mouse has focus before reporting mouse motion
2023-09-01 01:34:58 +00:00
let trigger = match world.get_resource::<WindowState>() {
Some(window_state) if window_state.is_focused && window_state.is_cursor_inside_window => true,
_ => false,
};
if trigger {
2023-09-01 01:34:58 +00:00
let event_queue = world.with_resource(|| Events::<InputEvent>::new());
let input_event = InputEvent::MouseMotion { device_id, delta, };
event_queue.push_back(input_event);
}
},
_ => {}
},
2023-03-16 21:47:36 +00:00
Event::WindowEvent {
ref event,
window_id,
} if window_id == self.window.id() => {
2023-03-16 21:47:36 +00:00
// If try_from failed, that means that the WindowEvent is not an
// input related event.
if let Ok(input_event) = InputEvent::try_from(event) {
// Its possible to receive multiple input events before the update event for
// the InputSystem is called, so we must use a queue for the events.
{
2023-09-01 01:34:58 +00:00
let world = self.world.lock().await;
if let Some(mut event_queue) = world.get_resource_mut::<EventQueue>() {
event_queue.trigger_event(input_event.clone());
};
}
2023-03-16 21:47:36 +00:00
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;
},
WindowEvent::Focused(is_focused) => {
let mut world = self.world.lock().await;
2023-09-01 01:34:58 +00:00
let mut state = world.with_resource(|| WindowState::new());
state.is_focused = *is_focused;
},
2023-03-16 21:47:36 +00:00
_ => {}
}
}
},
Event::RedrawRequested(window_id) if window_id == self.window.id() => {
// Update the world
self.update().await;
/* self.fps_counter.tick();
if let Some(fps) = self.fps_counter.get_change() {
debug!("FPS: {}fps, {:.2}ms/frame", fps, self.fps_counter.get_tick_time());
} */
let mut world = self.world.lock().await;
2023-09-01 01:34:58 +00:00
self.renderer.as_mut().prepare(&mut world);
if let Some(mut event_queue) = world.get_resource_mut::<EventQueue>() {
event_queue.update_events();
}
drop(world);
2023-09-01 01:34:58 +00:00
match self.renderer.as_mut().render() {
2023-03-16 21:47:36 +00:00
Ok(_) => {}
// Reconfigure the surface if lost
2023-04-19 04:53:06 +00:00
Err(wgpu::SurfaceError::Lost) => self.on_resize(self.renderer.as_ref().surface_size()).await,
2023-03-16 21:47:36 +00:00
// 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();
},
_ => {}
}
}
}
pub struct Game {
2023-09-01 01:34:58 +00:00
world: Option<Arc<Mutex<edict::World>>>,
2023-09-01 15:41:54 +00:00
system_dispatcher: Option<SystemDispatcher>,
2023-03-16 21:47:36 +00:00
}
impl Default for Game {
fn default() -> Self {
Self {
world: None,
system_dispatcher: Some(SystemDispatcher::new()),
}
}
}
2023-03-16 21:47:36 +00:00
impl Game {
pub async fn initialize() -> Game {
/* let filter = FilterFn::new(|metadata| {
2023-03-16 21:47:36 +00:00
metadata.module_path()
.unwrap_or_else(|| metadata.target())
2023-04-20 06:07:11 +00:00
.starts_with("lyra_engine") && (LevelFilter::DEBUG >= metadata.level().to_owned())
2023-03-16 21:47:36 +00:00
});
let layer = tracing_subscriber::fmt::layer();
tracing_subscriber::registry()
.with(layer.with_filter(filter))
.init(); */
/* tracing_subscriber::registry()
.with(fmt::layer().with_writer(stdout_layer))
.with(filter::Targets::new()
.with_target("lyra_engine", Level::TRACE)
.with_default(Level::DEBUG))
.init(); */
/* tracing_subscriber::fmt()
.with_max_level(Level::DEBUG)
.init(); */
info!("dheiudfgbwehifwe");
2023-03-16 21:47:36 +00:00
2023-09-01 15:41:54 +00:00
let mut def = Self::default();
def.system_dispatcher.as_mut().unwrap().add_system("input", InputSystem::new(), &[]);
def
2023-03-16 21:47:36 +00:00
}
2023-09-01 01:34:58 +00:00
pub fn with_world(&mut self, world: edict::World) -> &mut Self {
self.world = Some(Arc::new(Mutex::new(world)));
2023-03-16 21:47:36 +00:00
self
2023-03-16 21:47:36 +00:00
}
2023-09-01 01:34:58 +00:00
pub fn with_world_arc(&mut self, world: Arc<Mutex<edict::World>>) -> &mut Self {
self.world = Some(world);
self
2023-03-16 21:47:36 +00:00
}
pub fn with_system<S>(&mut self, name: &str, system: S, depends: &[&str]) -> &mut Self
2023-06-29 03:18:44 +00:00
where
S: SimpleSystem + 'static
{
let dispatcher = self.system_dispatcher.as_mut().unwrap();
2023-09-01 15:41:54 +00:00
dispatcher.add_system(name, system, depends);
2023-06-29 03:18:44 +00:00
self
}
2023-03-16 21:47:36 +00:00
pub async fn run(&mut self) {
// init logging
let (stdout_layer, _stdout_nb) = non_blocking(std::io::stdout());
tracing_subscriber::registry()
.with(fmt::layer().with_writer(stdout_layer))
.with(filter::Targets::new()
.with_target("lyra_engine", Level::TRACE)
.with_default(Level::INFO))
.init();
let world = self.world.take().expect("ECS World was never given to Game!");
2023-03-16 21:47:36 +00:00
let event_loop = EventLoop::new();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
2023-03-16 21:47:36 +00:00
let systems = self.system_dispatcher.take().unwrap();
2023-06-29 03:18:44 +00:00
let mut g_loop = GameLoop::new(Arc::clone(&window), world, systems).await;
2023-03-16 21:47:36 +00:00
2023-09-01 15:41:54 +00:00
g_loop.on_init().await;
2023-03-16 21:47:36 +00:00
event_loop.run(move |event, _, control_flow| {
g_loop.run_sync(event, control_flow);
});
}
}