move GAME_SUSPENDED to Context

This commit is contained in:
Alula
2026-04-17 07:56:02 +02:00
parent a1b5ae5fc8
commit 58d5ca97c4
4 changed files with 11 additions and 27 deletions
+2 -5
View File
@@ -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;
}
}
+2 -4
View File
@@ -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);
}
+2
View File
@@ -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
View File
@@ -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(())