From 5a8842669b71d4eef81bdfa281b31f197a0a2480 Mon Sep 17 00:00:00 2001 From: Alula <6276139+alula@users.noreply.github.com> Date: Sat, 3 Aug 2024 03:13:49 +0200 Subject: [PATCH] Move scene handling code to Game, doesn't make sense to have it in backends --- src/framework/backend_horizon.rs | 8 -------- src/framework/backend_null.rs | 8 -------- src/framework/backend_sdl2.rs | 8 -------- src/framework/backend_winit.rs | 9 --------- src/framework/vfs.rs | 3 ++- src/game/mod.rs | 13 ++++++++++++- 6 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/framework/backend_horizon.rs b/src/framework/backend_horizon.rs index 25e3e9c..3b03348 100644 --- a/src/framework/backend_horizon.rs +++ b/src/framework/backend_horizon.rs @@ -402,14 +402,6 @@ impl BackendEventLoop for HorizonEventLoop { break; } - if state_ref.next_scene.is_some() { - mem::swap(&mut game.scene, &mut state_ref.next_scene); - state_ref.next_scene = None; - game.scene.as_mut().unwrap().init(state_ref, ctx).unwrap(); - game.loops = 0; - state_ref.frame_time = 0.0; - } - game.draw(ctx).unwrap(); } } diff --git a/src/framework/backend_null.rs b/src/framework/backend_null.rs index bdcaa18..a99cb9d 100644 --- a/src/framework/backend_null.rs +++ b/src/framework/backend_null.rs @@ -46,15 +46,7 @@ impl BackendEventLoop for NullEventLoop { break; } - if game.state.get_mut().next_scene.is_some() { - mem::swap(game.scene.get_mut(), &mut game.state.get_mut().next_scene); - game.state.get_mut().next_scene = None; - game.scene.borrow_mut().as_mut().unwrap().init(game.state.get_mut(), ctx).unwrap(); - game.loops = 0; - game.state.get_mut().frame_time = 0.0; - } std::thread::sleep(std::time::Duration::from_millis(10)); - game.draw(ctx).unwrap(); } } diff --git a/src/framework/backend_sdl2.rs b/src/framework/backend_sdl2.rs index 3af2490..aba6b17 100644 --- a/src/framework/backend_sdl2.rs +++ b/src/framework/backend_sdl2.rs @@ -430,14 +430,6 @@ impl BackendEventLoop for SDL2EventLoop { game.update(ctx).unwrap(); - if game.state.get_mut().next_scene.is_some() { - let mut state = game.state.borrow_mut(); - *game.scene.get_mut() = mem::take(&mut state.next_scene); - game.scene.get_mut().as_mut().unwrap().init(&mut state, ctx).unwrap(); - game.loops = 0; - state.frame_time = 0.0; - } - imgui_sdl2.prepare_frame( imgui.io_mut(), self.refs.deref().borrow().window.window(), diff --git a/src/framework/backend_winit.rs b/src/framework/backend_winit.rs index a11367b..0911570 100644 --- a/src/framework/backend_winit.rs +++ b/src/framework/backend_winit.rs @@ -281,15 +281,6 @@ impl ApplicationHandler for WinitEventLoop { } window.request_redraw(); - - if game.state.get_mut().next_scene.is_some() { - let mut state_ref = game.state.borrow_mut(); - mem::swap(game.scene.get_mut(), &mut state_ref.next_scene); - state_ref.next_scene = None; - game.scene.borrow_mut().as_mut().unwrap().init(&mut state_ref, ctx).unwrap(); - game.loops = 0; - state_ref.frame_time = 0.0; - } } } diff --git a/src/framework/vfs.rs b/src/framework/vfs.rs index 5cd0517..85032ab 100644 --- a/src/framework/vfs.rs +++ b/src/framework/vfs.rs @@ -254,10 +254,11 @@ impl PhysicalFS { continue; } + let node_lower = node.to_ascii_lowercase(); if let Ok(entries) = root_path2.read_dir() { for entry in entries.flatten() { let name = entry.file_name(); - if name.to_ascii_lowercase() != node.to_ascii_lowercase() { + if name.to_ascii_lowercase() != node_lower { continue; } diff --git a/src/game/mod.rs b/src/game/mod.rs index 9fe5319..ca62930 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -77,8 +77,9 @@ impl Game { } pub(crate) fn update(&mut self, ctx: &mut Context) -> GameResult { + let state_ref = self.state.get_mut(); + if let Some(scene) = self.scene.get_mut() { - let state_ref = self.state.get_mut(); let speed = if state_ref.textscript_vm.mode == ScriptMode::Map && state_ref.textscript_vm.flags.cutscene_skip() @@ -127,6 +128,16 @@ impl Game { } } } + + let next_scene = std::mem::take(&mut state_ref.next_scene); + if let Some(mut next_scene) = next_scene { + next_scene.init(state_ref, ctx)?; + *self.scene.get_mut() = Some(next_scene); + + self.loops = 0; + state_ref.frame_time = 0.0; + } + Ok(()) }