pub use winit;
use winit::dpi;
use std::fmt;
use crate::audio;
use crate::conf;
use crate::error::GameResult;
use crate::event::winit_event;
use crate::filesystem::Filesystem;
use crate::graphics::{self, Point2};
use crate::input::{gamepad, keyboard, mouse};
use crate::timer;
pub struct Context {
pub filesystem: Filesystem,
pub(crate) gfx_context: crate::graphics::context::GraphicsContext,
pub timer_context: timer::TimeContext,
pub audio_context: Box<dyn audio::AudioContext>,
pub keyboard_context: keyboard::KeyboardContext,
pub mouse_context: mouse::MouseContext,
pub gamepad_context: Box<dyn gamepad::GamepadContext>,
pub conf: conf::Conf,
pub continuing: bool,
#[allow(dead_code)]
debug_id: DebugId,
}
impl fmt::Debug for Context {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<Context: {:p}>", self)
}
}
impl Context {
fn from_conf(conf: conf::Conf, mut fs: Filesystem) -> GameResult<(Context, winit::EventsLoop)> {
let debug_id = DebugId::new();
let audio_context: Box<dyn audio::AudioContext> = if conf.modules.audio {
Box::new(audio::RodioAudioContext::new()?)
} else {
Box::new(audio::NullAudioContext::default())
};
let events_loop = winit::EventsLoop::new();
let timer_context = timer::TimeContext::new();
let backend_spec = graphics::GlBackendSpec::from(conf.backend);
let graphics_context = graphics::context::GraphicsContext::new(
&mut fs,
&events_loop,
&conf.window_setup,
conf.window_mode,
backend_spec,
debug_id,
)?;
let mouse_context = mouse::MouseContext::new();
let keyboard_context = keyboard::KeyboardContext::new();
let gamepad_context: Box<dyn gamepad::GamepadContext> = if conf.modules.gamepad {
Box::new(gamepad::GilrsGamepadContext::new()?)
} else {
Box::new(gamepad::NullGamepadContext::default())
};
let ctx = Context {
conf,
filesystem: fs,
gfx_context: graphics_context,
continuing: true,
timer_context,
audio_context,
keyboard_context,
gamepad_context,
mouse_context,
debug_id,
};
Ok((ctx, events_loop))
}
pub fn process_event(&mut self, event: &winit::Event) -> winit::Event {
let event = self.gfx_context.hack_event_hidpi(event);
match event.clone() {
winit_event::Event::WindowEvent { event, .. } => match event {
winit_event::WindowEvent::Resized(_) => {
self.gfx_context.resize_viewport();
}
winit_event::WindowEvent::CursorMoved {
position: dpi::LogicalPosition { x, y },
..
} => {
self.mouse_context
.set_last_position(Point2::new(x as f32, y as f32));
}
winit_event::WindowEvent::MouseInput { button, state, .. } => {
let pressed = match state {
winit_event::ElementState::Pressed => true,
winit_event::ElementState::Released => false,
};
self.mouse_context.set_button(button, pressed);
}
winit_event::WindowEvent::KeyboardInput {
input:
winit::KeyboardInput {
state,
virtual_keycode: Some(keycode),
modifiers,
..
},
..
} => {
let pressed = match state {
winit_event::ElementState::Pressed => true,
winit_event::ElementState::Released => false,
};
self.keyboard_context
.set_modifiers(keyboard::KeyMods::from(modifiers));
self.keyboard_context.set_key(keycode, pressed);
}
_ => (),
},
winit_event::Event::DeviceEvent { event, .. } => match event {
winit_event::DeviceEvent::MouseMotion { delta: (x, y) } => {
self.mouse_context
.set_last_delta(Point2::new(x as f32, y as f32));
}
_ => (),
},
_ => (),
};
event
}
}
use std::path;
#[derive(Debug, Clone, PartialEq)]
pub struct ContextBuilder {
pub(crate) game_id: &'static str,
pub(crate) author: &'static str,
pub(crate) conf: conf::Conf,
pub(crate) paths: Vec<path::PathBuf>,
pub(crate) load_conf_file: bool,
}
impl ContextBuilder {
pub fn new(game_id: &'static str, author: &'static str) -> Self {
Self {
game_id,
author,
conf: conf::Conf::default(),
paths: vec![],
load_conf_file: true,
}
}
pub fn window_setup(mut self, setup: conf::WindowSetup) -> Self {
self.conf.window_setup = setup;
self
}
pub fn window_mode(mut self, mode: conf::WindowMode) -> Self {
self.conf.window_mode = mode;
self
}
pub fn backend(mut self, backend: conf::Backend) -> Self {
self.conf.backend = backend;
self
}
pub fn modules(mut self, modules: conf::ModuleConf) -> Self {
self.conf.modules = modules;
self
}
pub fn conf(mut self, conf: conf::Conf) -> Self {
self.conf = conf;
self
}
pub fn add_resource_path<T>(mut self, path: T) -> Self
where
T: Into<path::PathBuf>,
{
self.paths.push(path.into());
self
}
pub fn with_conf_file(mut self, load_conf_file: bool) -> Self {
self.load_conf_file = load_conf_file;
self
}
pub fn build(self) -> GameResult<(Context, winit::EventsLoop)> {
let mut fs = Filesystem::new(self.game_id, self.author)?;
for path in &self.paths {
fs.mount(path, true);
}
let config = if self.load_conf_file {
fs.read_config().unwrap_or(self.conf)
} else {
self.conf
};
Context::from_conf(config, fs)
}
}
pub fn quit(ctx: &mut Context) {
ctx.continuing = false;
}
#[cfg(debug_assertions)]
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
#[cfg(debug_assertions)]
static DEBUG_ID_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg(debug_assertions)]
pub(crate) struct DebugId(u32);
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg(not(debug_assertions))]
pub(crate) struct DebugId;
#[cfg(debug_assertions)]
impl DebugId {
pub fn new() -> Self {
let id = DEBUG_ID_COUNTER.fetch_add(1, Ordering::SeqCst) as u32;
assert!(DEBUG_ID_COUNTER.load(Ordering::SeqCst) as u32 > id);
DebugId(id)
}
pub fn get(ctx: &Context) -> Self {
DebugId(ctx.debug_id.0)
}
pub fn assert(&self, ctx: &Context) {
if *self != ctx.debug_id {
panic!("Tried to use a resource with a Context that did not create it; this should never happen!");
}
}
}
#[cfg(not(debug_assertions))]
impl DebugId {
pub fn new() -> Self {
DebugId
}
pub fn get(_ctx: &Context) -> Self {
DebugId
}
pub fn assert(&self, _ctx: &Context) {
}
}