Skip to content

Commit

Permalink
feat: auto-save cfg at a set interval (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukexor authored Jun 7, 2024
1 parent 93fa6d2 commit e6941d8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
31 changes: 16 additions & 15 deletions tetanes/src/nes/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,21 @@ impl Running {
self.run_state = RunState::Running;
self.nes_event(EmulationEvent::RunState(self.run_state));
}
} else if self
.renderer
.window(window_id)
.and_then(|win| win.is_minimized())
.unwrap_or(false)
{
self.repaint_times.remove(&window_id);
if self.renderer.rom_loaded() {
self.run_state = RunState::Paused;
self.nes_event(EmulationEvent::RunState(self.run_state));
} else {
if let Err(err) = self.renderer.save(&self.cfg) {
error!("failed to save rendererer state: {err:?}");
}
if self
.renderer
.window(window_id)
.and_then(|win| win.is_minimized())
.unwrap_or(false)
{
self.repaint_times.remove(&window_id);
if self.renderer.rom_loaded() {
self.run_state = RunState::Paused;
self.nes_event(EmulationEvent::RunState(self.run_state));
}
}
}
}
Expand Down Expand Up @@ -463,14 +468,10 @@ impl Running {
#[cfg(feature = "profiling")]
puffin::set_scopes_on(false);

if let Err(err) = self.renderer.save() {
if let Err(err) = self.renderer.save(&self.cfg) {
error!("failed to save rendererer state: {err:?}");
}
self.renderer.destroy();

if let Err(err) = self.cfg.save() {
error!("failed to save configuration: {err:?}");
}
}
_ => (),
}
Expand Down
31 changes: 20 additions & 11 deletions tetanes/src/nes/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub struct Renderer {
render_state: Option<RenderState>,
texture: Texture,
first_frame: bool,
last_save_time: Instant,
}

impl std::fmt::Debug for Renderer {
Expand Down Expand Up @@ -279,6 +280,7 @@ impl Renderer {
render_state: Some(render_state),
texture,
first_frame: true,
last_save_time: Instant::now(),
})
}

Expand Down Expand Up @@ -676,12 +678,23 @@ impl Renderer {
Ok(())
}

pub fn save(&self) -> anyhow::Result<()> {
pub fn auto_save(&mut self, cfg: &Config) -> anyhow::Result<()> {
let time_since_last_save = Instant::now() - self.last_save_time;
if time_since_last_save > Duration::from_secs(30) {
self.save(cfg)?;
}
Ok(())
}

pub fn save(&mut self, cfg: &Config) -> anyhow::Result<()> {
cfg.save()?;

let path = Config::default_config_dir().join("gui.dat");
self.ctx.memory(|mem| {
let data = bincode::serialize(&mem).context("failed to serialize gui memory")?;
fs::save_raw(path, &data).context("failed to save gui memory")
})?;
self.last_save_time = Instant::now();

info!("Saved UI state");

Expand Down Expand Up @@ -709,12 +722,6 @@ impl Renderer {

let window_builder =
egui_winit::create_winit_window_builder(ctx, event_loop, viewport_builder.clone());
#[cfg(target_os = "macos")]
let window_builder = {
use winit::platform::macos::{OptionAsAlt, WindowBuilderExtMacOS};
window_builder.with_option_as_alt(OptionAsAlt::Both)
};

let window = window_builder
.with_platform(Config::WINDOW_TITLE)
.build(event_loop)?;
Expand Down Expand Up @@ -1059,7 +1066,7 @@ impl Renderer {
&mut self,
window_id: WindowId,
event_loop: &EventLoopWindowTarget<NesEvent>,
inputs: &mut Gamepads,
gamepads: &mut Gamepads,
cfg: &mut Config,
) -> anyhow::Result<()> {
#[cfg(feature = "profiling")]
Expand Down Expand Up @@ -1181,9 +1188,7 @@ impl Renderer {
if let Some(viewport_ui_cb) = viewport_ui_cb {
viewport_ui_cb(ctx);
}
{
self.gui.ui(ctx, inputs, cfg);
}
self.gui.ui(ctx, gamepads, cfg);
});

{
Expand Down Expand Up @@ -1258,6 +1263,10 @@ impl Renderer {
}
}

if let Err(err) = self.auto_save(cfg) {
error!("failed to auto save UI state: {err:?}");
}

Ok(())
}
}
Expand Down

0 comments on commit e6941d8

Please sign in to comment.