mirror of
https://github.com/doukutsu-rs/doukutsu-rs.git
synced 2026-08-24 13:02:49 -07:00
move GAME_SUSPENDED to Context
This commit is contained in:
@@ -30,7 +30,6 @@ use crate::framework::error::GameError;
|
||||
use crate::framework::render_opengl::GLContextType;
|
||||
use crate::game::shared_game_state::WindowMode;
|
||||
use crate::game::Game;
|
||||
use crate::game::GAME_SUSPENDED;
|
||||
use crate::input::touch_controls::TouchPoint;
|
||||
|
||||
trait WindowModeExt {
|
||||
@@ -316,8 +315,7 @@ impl BackendEventLoop for GlutinEventLoop {
|
||||
}
|
||||
Event::RedrawRequested(id) if id == window.window().id() => {
|
||||
{
|
||||
let mutex = GAME_SUSPENDED.lock().unwrap();
|
||||
if *mutex {
|
||||
if ctx.suspended {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -342,8 +340,7 @@ impl BackendEventLoop for GlutinEventLoop {
|
||||
}
|
||||
|
||||
{
|
||||
let mutex = GAME_SUSPENDED.lock().unwrap();
|
||||
if *mutex {
|
||||
if ctx.suspended {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,6 @@ use crate::framework::backend::get_scaled_size;
|
||||
use crate::framework::graphics::IndexData;
|
||||
use crate::game::shared_game_state::WindowMode;
|
||||
use crate::game::Game;
|
||||
use crate::game::GAME_SUSPENDED;
|
||||
use crate::input::touch_controls::TouchPoint;
|
||||
|
||||
fn handle_err_impl(result: GameResult, shutdown_requested: &mut bool) {
|
||||
@@ -505,9 +504,8 @@ impl BackendEventLoop for SDL2EventLoop {
|
||||
}
|
||||
|
||||
{
|
||||
let mutex = GAME_SUSPENDED.lock().unwrap();
|
||||
if *mutex {
|
||||
let event = self.event_pump.wait_event_timeout(10);
|
||||
if ctx.suspended {
|
||||
let event = self.event_pump.wait_event_timeout(1);
|
||||
if let Some(event) = event {
|
||||
Self::handle_event(event, &mut ctx, &mut game, &self.refs);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ use crate::game::Game;
|
||||
pub struct Context {
|
||||
pub headless: bool,
|
||||
pub shutdown_requested: bool,
|
||||
pub suspended: bool,
|
||||
pub window: WindowParams,
|
||||
pub flags: BackendFlag,
|
||||
pub(crate) imgui: Rc<RefCell<imgui::Context>>,
|
||||
@@ -35,6 +36,7 @@ impl Context {
|
||||
Context {
|
||||
headless: false,
|
||||
shutdown_requested: false,
|
||||
suspended: false,
|
||||
window: WindowParams::default(),
|
||||
flags: BackendFlag::new(),
|
||||
imgui: init_imgui(),
|
||||
|
||||
+5
-18
@@ -100,10 +100,6 @@ impl LaunchOptions {
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref GAME_SUSPENDED: Mutex<bool> = Mutex::new(false);
|
||||
}
|
||||
|
||||
pub struct Game {
|
||||
pub(crate) scene: RefCell<Option<Box<dyn Scene>>>,
|
||||
pub(crate) state: RefCell<SharedGameState>,
|
||||
@@ -291,10 +287,7 @@ impl Game {
|
||||
self.ui.draw(state_ref, ctx, scene)?;
|
||||
}
|
||||
|
||||
let is_suspended = *GAME_SUSPENDED.lock().unwrap();
|
||||
if !is_suspended {
|
||||
graphics::present(ctx)?;
|
||||
}
|
||||
graphics::present(ctx)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -306,26 +299,20 @@ impl BackendCallbacks for Game {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_focus_gained(&mut self, _ctx: &mut Context) -> GameResult {
|
||||
fn on_focus_gained(&mut self, ctx: &mut Context) -> GameResult {
|
||||
let state_ref = self.state.get_mut();
|
||||
if state_ref.settings.pause_on_focus_loss {
|
||||
{
|
||||
let mut mutex = GAME_SUSPENDED.lock().unwrap();
|
||||
*mutex = false;
|
||||
}
|
||||
|
||||
ctx.suspended = false;
|
||||
state_ref.sound_manager.resume();
|
||||
self.loops = 0;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_focus_lost(&mut self, _ctx: &mut Context) -> GameResult {
|
||||
fn on_focus_lost(&mut self, ctx: &mut Context) -> GameResult {
|
||||
let state_ref = self.state.get_mut();
|
||||
if state_ref.settings.pause_on_focus_loss {
|
||||
let mut mutex = GAME_SUSPENDED.lock().unwrap();
|
||||
*mutex = true;
|
||||
|
||||
ctx.suspended = true;
|
||||
state_ref.sound_manager.pause();
|
||||
}
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user