2024-05-28 22:55:57 +00:00
|
|
|
use crate::framework::backend::{init_backend, BackendRenderer};
|
2021-01-28 22:33:43 +00:00
|
|
|
use crate::framework::error::GameResult;
|
2021-01-27 18:20:47 +00:00
|
|
|
use crate::framework::filesystem::Filesystem;
|
2022-07-20 13:07:24 +00:00
|
|
|
use crate::framework::gamepad::GamepadContext;
|
2022-11-19 17:20:03 +00:00
|
|
|
use crate::framework::graphics::VSyncMode;
|
2021-01-28 22:33:43 +00:00
|
|
|
use crate::framework::keyboard::KeyboardContext;
|
2022-11-19 17:20:03 +00:00
|
|
|
use crate::game::Game;
|
2021-01-27 18:20:47 +00:00
|
|
|
|
|
|
|
pub struct Context {
|
2021-10-14 04:54:11 +00:00
|
|
|
pub headless: bool,
|
2021-10-16 12:59:27 +00:00
|
|
|
pub size_hint: (u16, u16),
|
2021-01-27 18:20:47 +00:00
|
|
|
pub(crate) filesystem: Filesystem,
|
2021-01-28 22:33:43 +00:00
|
|
|
pub(crate) renderer: Option<Box<dyn BackendRenderer>>,
|
2022-07-20 13:07:24 +00:00
|
|
|
pub(crate) gamepad_context: GamepadContext,
|
2021-01-28 22:33:43 +00:00
|
|
|
pub(crate) keyboard_context: KeyboardContext,
|
2021-10-14 07:43:17 +00:00
|
|
|
pub(crate) real_screen_size: (u32, u32),
|
2021-01-28 22:33:43 +00:00
|
|
|
pub(crate) screen_size: (f32, f32),
|
2021-02-24 08:28:47 +00:00
|
|
|
pub(crate) screen_insets: (f32, f32, f32, f32),
|
2022-04-15 00:51:48 +00:00
|
|
|
pub(crate) vsync_mode: VSyncMode,
|
2021-01-27 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
pub fn new() -> Context {
|
|
|
|
Context {
|
2021-10-14 04:54:11 +00:00
|
|
|
headless: false,
|
2021-10-16 12:59:27 +00:00
|
|
|
size_hint: (640, 480),
|
2021-01-27 18:20:47 +00:00
|
|
|
filesystem: Filesystem::new(),
|
2021-01-28 22:33:43 +00:00
|
|
|
renderer: None,
|
2022-07-20 13:07:24 +00:00
|
|
|
gamepad_context: GamepadContext::new(),
|
2021-01-28 22:33:43 +00:00
|
|
|
keyboard_context: KeyboardContext::new(),
|
2021-10-14 07:43:17 +00:00
|
|
|
real_screen_size: (320, 240),
|
2021-01-28 22:33:43 +00:00
|
|
|
screen_size: (320.0, 240.0),
|
2021-02-24 08:28:47 +00:00
|
|
|
screen_insets: (0.0, 0.0, 0.0, 0.0),
|
2022-04-15 00:51:48 +00:00
|
|
|
vsync_mode: VSyncMode::Uncapped,
|
2021-01-27 18:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 22:33:43 +00:00
|
|
|
pub fn run(&mut self, game: &mut Game) -> GameResult {
|
2021-10-16 12:59:27 +00:00
|
|
|
let backend = init_backend(self.headless, self.size_hint)?;
|
2022-08-01 00:18:43 +00:00
|
|
|
let mut event_loop = backend.create_event_loop(self)?;
|
2022-04-15 00:51:48 +00:00
|
|
|
self.renderer = Some(event_loop.new_renderer(self as *mut Context)?);
|
2021-01-28 22:33:43 +00:00
|
|
|
|
|
|
|
event_loop.run(game, self);
|
|
|
|
|
|
|
|
Ok(())
|
2021-01-27 18:20:47 +00:00
|
|
|
}
|
2021-01-28 22:33:43 +00:00
|
|
|
}
|