pub(crate) mod input;
pub(crate) mod window_size;
use std::sync::{atomic::AtomicBool, Arc, Mutex};
use async_trait::async_trait;
use thiserror::Error;
use winit::{
dpi::PhysicalPosition,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
window::{Window, WindowBuilder},
};
use crate::LfLimitsExt;
use self::input::{InputMap, MouseInputType, VectorInputActivation, VectorInputType};
#[derive(Debug, Error)]
pub enum GameInitialisationFailure {
#[error("failed to find an adapter (GPU) that supports the render surface")]
AdapterError,
#[error("failed to request a device from the adapter chosen: {0}")]
DeviceError(wgpu::RequestDeviceError),
}
#[derive(Clone)]
pub struct ExitFlag {
inner: Arc<AtomicBool>,
}
impl ExitFlag {
fn new() -> Self {
Self {
inner: Arc::new(AtomicBool::new(false)),
}
}
pub fn get(&self) -> bool {
self.inner.load(std::sync::atomic::Ordering::SeqCst)
}
fn set(&self) {
self.inner.store(true, std::sync::atomic::Ordering::SeqCst)
}
}
#[derive(Debug, Clone, Copy)]
pub enum InputMode {
Exclusive,
UI,
Unified,
}
impl InputMode {
fn should_hide_cursor(self) -> bool {
match self {
InputMode::Exclusive => true,
InputMode::UI => false,
InputMode::Unified => false,
}
}
fn should_handle_input(self) -> bool {
match self {
InputMode::Exclusive => true,
InputMode::UI => false,
InputMode::Unified => true,
}
}
fn should_propogate_raw_input(self) -> bool {
match self {
InputMode::Exclusive => false,
InputMode::UI => true,
InputMode::Unified => true,
}
}
fn should_lock_cursor(self) -> bool {
match self {
InputMode::Exclusive => true,
InputMode::UI => false,
InputMode::Unified => false,
}
}
}
pub enum GameCommand {
Exit,
SetInputMode(InputMode),
SetMouseSensitivity(f32),
}
pub struct GameData {
pub command_sender: flume::Sender<GameCommand>,
pub surface_format: wgpu::TextureFormat,
pub limits: wgpu::Limits,
pub config: wgpu::SurfaceConfiguration,
pub size: winit::dpi::PhysicalSize<u32>,
pub window: Window,
pub device: wgpu::Device,
pub queue: wgpu::Queue,
pub exit_flag: ExitFlag,
}
#[async_trait]
pub trait Game: Sized {
type InitData;
type LinearInputType;
type VectorInputType;
fn target_limits() -> wgpu::Limits {
wgpu::Limits::downlevel_webgl2_defaults()
}
fn default_inputs() -> InputMap<Self::LinearInputType, Self::VectorInputType>;
async fn init(data: &GameData, init: Self::InitData) -> Self;
fn on_init_failure(error: GameInitialisationFailure) -> ! {
let error = format!("failed to initialise: {}", error);
#[cfg(target_arch = "wasm32")]
{
alert(&error);
}
panic!("{}", error);
}
fn process_raw_event<'a, T>(&mut self, event: Event<'a, T>) -> Option<Event<'a, T>> {
Some(event)
}
fn window_resize(&mut self, data: &GameData, new_size: winit::dpi::PhysicalSize<u32>);
fn handle_linear_input(
&mut self,
data: &GameData,
input: &Self::LinearInputType,
activation: input::LinearInputActivation,
);
fn handle_vector_input(
&mut self,
data: &GameData,
input: &Self::VectorInputType,
activation: input::VectorInputActivation,
);
fn render_to(&mut self, data: &GameData, view: wgpu::TextureView);
fn user_exit_requested(&mut self, data: &GameData) {
let _ = data.command_sender.send(GameCommand::Exit);
}
fn finished(self, _: GameData) {}
}
pub(crate) struct GameState<T: Game> {
data: GameData,
current_modifiers: input::ModifiersState,
game: T,
input_map: input::InputMap<T::LinearInputType, T::VectorInputType>,
command_receiver: flume::Receiver<GameCommand>,
surface: Mutex<wgpu::Surface>,
input_mode: InputMode,
last_cursor_position: PhysicalPosition<f64>,
mouse_sensitivity: f32,
}
impl<T: Game + 'static> GameState<T> {
async fn new(init: T::InitData, window_target: &EventLoopWindowTarget<()>) -> Self {
let window = WindowBuilder::new().build(window_target).unwrap();
let size = window.inner_size();
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::util::backend_bits_from_env().unwrap_or(wgpu::Backends::all()),
dx12_shader_compiler: wgpu::util::dx12_shader_compiler_from_env().unwrap_or_default(),
});
let surface = unsafe { instance.create_surface(&window) }.unwrap();
let adapter = match instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
force_fallback_adapter: false,
compatible_surface: Some(&surface),
})
.await
{
Some(adapter) => adapter,
None => T::on_init_failure(GameInitialisationFailure::AdapterError),
};
let available_limits = if cfg!(target_arch = "wasm32") {
wgpu::Limits::downlevel_defaults()
} else {
adapter.limits()
};
let target_limits = T::target_limits();
let limits = available_limits.intersection(&target_limits);
let mut features = wgpu::Features::empty();
if adapter
.features()
.contains(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS)
&& matches!(
adapter.get_info().device_type,
wgpu::DeviceType::IntegratedGpu
| wgpu::DeviceType::Cpu
| wgpu::DeviceType::VirtualGpu
)
{
features |= wgpu::Features::MAPPABLE_PRIMARY_BUFFERS;
}
features |= adapter.features().intersection(
wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_PASSES,
);
let (device, queue) = match adapter
.request_device(
&wgpu::DeviceDescriptor {
features,
limits: limits.clone(),
label: None,
},
None,
)
.await
{
Ok(vs) => vs,
Err(e) => T::on_init_failure(GameInitialisationFailure::DeviceError(e)),
};
let mut surface_config = surface
.get_default_config(&adapter, size.width, size.height)
.expect("got an adapter based on the surface");
surface_config.present_mode = wgpu::PresentMode::AutoVsync;
surface.configure(&device, &surface_config);
let surface_caps = surface.get_capabilities(&adapter);
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.is_srgb())
.next()
.unwrap_or(surface_caps.formats[0]);
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: surface_format,
width: size.width,
height: size.height,
present_mode: surface_caps.present_modes[0],
alpha_mode: surface_caps.alpha_modes[0],
view_formats: vec![],
};
surface.configure(&device, &config);
let (command_sender, command_receiver) = flume::unbounded();
command_sender
.try_send(GameCommand::SetInputMode(InputMode::Unified))
.expect("unbounded queue helf by this thread should send immediately");
let data = GameData {
command_sender,
surface_format,
limits,
config,
size,
window,
device,
queue,
exit_flag: ExitFlag::new(),
};
let game = T::init(&data, init).await;
let input_map = T::default_inputs();
Self {
data,
current_modifiers: input::ModifiersState::default(),
game,
surface: Mutex::new(surface),
command_receiver,
input_map,
input_mode: InputMode::Unified,
last_cursor_position: PhysicalPosition { x: 0.0, y: 0.0 },
mouse_sensitivity: 0.01,
}
}
pub(crate) fn run(init: T::InitData) {
let event_loop = EventLoop::new();
let mut state: Option<Self> = None;
let mut init = Some(init);
event_loop.run(move |event, window_target, control_flow| {
if event == Event::LoopDestroyed {
state.take().expect("loop is destroyed once").finished();
return;
}
if state.is_none() && event == Event::Resumed {
state = Some(pollster::block_on(Self::new(
init.take().expect("should only initialise once"),
window_target,
)));
}
let state = match state.as_mut() {
None => return,
Some(state) => state,
};
state.receive_event(event, window_target, control_flow);
});
}
fn is_input_event(event: &Event<'_, ()>) -> bool {
match event {
winit::event::Event::WindowEvent { event, .. } => match event {
winit::event::WindowEvent::CursorMoved { .. }
| winit::event::WindowEvent::CursorEntered { .. }
| winit::event::WindowEvent::CursorLeft { .. }
| winit::event::WindowEvent::MouseWheel { .. }
| winit::event::WindowEvent::MouseInput { .. }
| winit::event::WindowEvent::TouchpadRotate { .. }
| winit::event::WindowEvent::TouchpadPressure { .. }
| winit::event::WindowEvent::AxisMotion { .. }
| winit::event::WindowEvent::Touch(_)
| winit::event::WindowEvent::ReceivedCharacter(_)
| winit::event::WindowEvent::KeyboardInput { .. }
| winit::event::WindowEvent::ModifiersChanged(_)
| winit::event::WindowEvent::Ime(_)
| winit::event::WindowEvent::TouchpadMagnify { .. }
| winit::event::WindowEvent::SmartMagnify { .. } => true,
_ => false,
},
winit::event::Event::DeviceEvent { event, .. } => match event {
winit::event::DeviceEvent::MouseMotion { .. }
| winit::event::DeviceEvent::MouseWheel { .. }
| winit::event::DeviceEvent::Motion { .. }
| winit::event::DeviceEvent::Button { .. }
| winit::event::DeviceEvent::Key(_)
| winit::event::DeviceEvent::Text { .. } => true,
_ => false,
},
_ => false,
}
}
fn receive_event(
&mut self,
mut event: Event<'_, ()>,
window_target: &EventLoopWindowTarget<()>,
control_flow: &mut ControlFlow,
) {
event = match event {
Event::WindowEvent { window_id, .. } if window_id != self.window().id() => return,
event => event,
};
let should_send_input = self.input_mode.should_propogate_raw_input();
if should_send_input || !Self::is_input_event(&event) {
event = match self.game.process_raw_event(event) {
None => return,
Some(event) => event,
};
}
self.process_event(event, window_target, control_flow)
}
fn process_event(
&mut self,
event: Event<'_, ()>,
_window_target: &EventLoopWindowTarget<()>,
control_flow: &mut ControlFlow,
) {
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested | WindowEvent::Destroyed => self.request_exit(),
WindowEvent::Resized(winit::dpi::PhysicalSize {
width: 0,
height: 0,
}) => {}
WindowEvent::Resized(physical_size) => {
self.resize(physical_size);
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
self.resize(*new_inner_size);
}
WindowEvent::ModifiersChanged(modifiers) => {
let changed = self.set_current_input_modifiers(modifiers);
for (key, activation) in changed {
self.linear_input(input::LinearInputType::KnownKeyboard(key), activation);
}
}
WindowEvent::KeyboardInput {
device_id: _device_id,
input,
is_synthetic,
} if !is_synthetic => {
if let Some(key) = input.virtual_keycode {
let activation = match input.state {
winit::event::ElementState::Pressed => 1.0,
winit::event::ElementState::Released => 0.0,
};
let activation =
input::LinearInputActivation::try_from(activation).expect("from const");
self.linear_input(
input::LinearInputType::KnownKeyboard(key.into()),
activation,
);
} else {
eprintln!("unknown key code, scan code: {:?}", input.scancode)
}
}
WindowEvent::CursorMoved {
device_id: _device_id,
position,
..
} => {
let delta_x = position.x - self.last_cursor_position.x;
let delta_y = position.y - self.last_cursor_position.y;
if delta_x.abs() > 2.0 || delta_y.abs() > 2.0 {
self.process_linear_mouse_movement(delta_x, delta_y);
}
self.vector_input(
VectorInputType::MouseMove,
VectorInputActivation::clamp(
delta_x as f32 * self.mouse_sensitivity,
delta_y as f32 * self.mouse_sensitivity,
),
);
self.last_cursor_position = position.cast();
let should_lock_cursor = self.input_mode.should_lock_cursor();
if should_lock_cursor {
let mut center = self.data.window.inner_size();
center.width /= 2;
center.height /= 2;
let old_pos = position.cast::<u32>();
let new_pos = PhysicalPosition::new(center.width, center.height);
if old_pos != new_pos {
let _ = self.data.window.set_cursor_position(new_pos);
}
self.last_cursor_position = new_pos.cast();
}
}
_ => {}
},
Event::RedrawRequested(window_id) if window_id == self.window().id() => {
self.data.device.poll(wgpu::MaintainBase::Poll);
self.pre_frame_update();
if self.data.exit_flag.get() {
*control_flow = ControlFlow::Exit;
}
match self.render() {
Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => self.resize(self.data.size),
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
Err(e) => eprintln!("{:?}", e),
}
}
Event::MainEventsCleared => {
self.window().request_redraw();
}
_ => {}
}
}
pub fn window(&self) -> &Window {
&self.data.window
}
fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.data.size = new_size;
self.data.config.width = new_size.width;
self.data.config.height = new_size.height;
let lock = self.surface.lock().unwrap();
let (s, r) = flume::bounded(1);
self.data
.queue
.on_submitted_work_done(move || s.send(()).unwrap());
self.data.device.poll(wgpu::Maintain::Wait);
let _ = r.recv().unwrap();
lock.configure(&self.data.device, &self.data.config);
self.game.window_resize(&self.data, new_size)
}
}
fn set_current_input_modifiers(
&mut self,
modifiers: winit::event::ModifiersState,
) -> Vec<(input::KeyCode, input::LinearInputActivation)> {
let mut changed = Vec::new();
let mut check = |old, new, key| {
if old != new {
let activation = if old {
input::LinearInputActivation::try_from(0.0).unwrap()
} else {
input::LinearInputActivation::try_from(1.0).unwrap()
};
changed.push((key, activation))
}
};
check(
self.current_modifiers.shift,
modifiers.shift(),
input::KeyCode::Shift,
);
check(
self.current_modifiers.ctrl,
modifiers.ctrl(),
input::KeyCode::Ctrl,
);
check(
self.current_modifiers.alt,
modifiers.alt(),
input::KeyCode::Alt,
);
check(
self.current_modifiers.logo,
modifiers.logo(),
input::KeyCode::Logo,
);
self.current_modifiers = input::ModifiersState {
shift: modifiers.shift(),
ctrl: modifiers.ctrl(),
alt: modifiers.alt(),
logo: modifiers.logo(),
};
return changed;
}
fn process_linear_mouse_movement(&mut self, delta_x: f64, delta_y: f64) {
if delta_x.abs() > delta_y.abs() {
if delta_x > 0.0 {
self.linear_input(
input::LinearInputType::Mouse(MouseInputType::MoveRight),
input::LinearInputActivation::clamp(delta_x as f32 * self.mouse_sensitivity),
);
} else {
self.linear_input(
input::LinearInputType::Mouse(MouseInputType::MoveLeft),
input::LinearInputActivation::clamp(-delta_x as f32 * self.mouse_sensitivity),
);
}
} else {
if delta_y > 0.0 {
self.linear_input(
input::LinearInputType::Mouse(MouseInputType::MoveUp),
input::LinearInputActivation::clamp(delta_y as f32 * self.mouse_sensitivity),
);
} else {
self.linear_input(
input::LinearInputType::Mouse(MouseInputType::MoveDown),
input::LinearInputActivation::clamp(-delta_y as f32 * self.mouse_sensitivity),
);
}
}
}
fn linear_input(
&mut self,
inputted: input::LinearInputType,
activation: input::LinearInputActivation,
) {
if !self.input_mode.should_handle_input() {
return;
}
let input_value = self.input_map.get_linear(&inputted);
if let Some(input_value) = input_value {
self.game
.handle_linear_input(&self.data, input_value, activation)
}
}
fn vector_input(
&mut self,
inputted: input::VectorInputType,
activation: input::VectorInputActivation,
) {
if !self.input_mode.should_handle_input() {
return;
}
let input_value = self.input_map.get_vector(&inputted);
if let Some(input_value) = input_value {
self.game
.handle_vector_input(&self.data, input_value, activation)
}
}
fn request_exit(&mut self) {
self.data.exit_flag.set();
self.game.user_exit_requested(&self.data);
}
fn pre_frame_update(&mut self) {
while let Ok(cmd) = self.command_receiver.try_recv() {
match cmd {
GameCommand::Exit => self.data.exit_flag.set(),
GameCommand::SetInputMode(input_mode) => {
self.input_mode = input_mode;
let should_show_cursor = !input_mode.should_hide_cursor();
self.data.window.set_cursor_visible(should_show_cursor);
}
GameCommand::SetMouseSensitivity(new_sensitivity) => {
self.mouse_sensitivity = new_sensitivity;
}
}
}
}
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let lock = self.surface.lock().unwrap();
let was_suboptimal = {
let output = lock.get_current_texture()?;
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
self.game.render_to(&self.data, view);
let was_suboptimal = output.suboptimal;
output.present();
was_suboptimal
};
if was_suboptimal {
return Err(wgpu::SurfaceError::Lost);
}
Ok(())
}
fn finished(self) {
self.game.finished(self.data)
}
}