From fb056f2d0af421e8c831364d298366b3e6cc53f3 Mon Sep 17 00:00:00 2001 From: fred Date: Wed, 11 May 2022 09:06:04 +0200 Subject: [PATCH] set tick rate, lower cpu usage & smoother gui --- src/egui_glutin.rs | 2 + src/main.rs | 106 +++++++++++++++++++++++++++------------------ 2 files changed, 65 insertions(+), 43 deletions(-) diff --git a/src/egui_glutin.rs b/src/egui_glutin.rs index 8ee6049..4843db5 100644 --- a/src/egui_glutin.rs +++ b/src/egui_glutin.rs @@ -423,6 +423,8 @@ fn write_cfg(egui_state: &EguiState, gui_state: &GuiState) { window_size: egui_state.window_size, + timer_ticks: gui_state.timer_ticks, + rank_window_pos: gui_state.graph.default_window_pos, //why is -12 necessary? probably doing something wrong rank_window_width: gui_state.graph.default_window_width - 12.0, diff --git a/src/main.rs b/src/main.rs index fb4c6c5..fcdbc44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,8 +24,8 @@ pub struct GuiState handle: HANDLE, offset: u64, - handle_timer: i8, - memory_read_timer: i8, + update_timer: i8, + timer_ticks: i8, graph: Graph, rank: u8, @@ -38,6 +38,8 @@ struct Save window_size: (u32, u32), // window_pos: (u32, u32), + timer_ticks: i8, + //rank graph rank_window_pos: (f32, f32), rank_window_width: f32, @@ -48,6 +50,27 @@ struct Save aspect: f32, } +impl Default for Save +{ + fn default() -> Self + { + Self + { + window_size: (1024, 768), + + timer_ticks: 100, + + rank_window_pos: (20.0, 20.0), + rank_window_width: 450.0, + data_points: 240, + color_r: ( 0, 255), + color_g: (255, 0), + color_b: ( 0, 0), + aspect: 3.7, + } + } +} + struct Graph { default_window_pos: (f32, f32), @@ -67,18 +90,7 @@ fn main() } else { - Save - { - window_size: (1024, 768), - - rank_window_pos: (20.0, 20.0), - rank_window_width: 450.0, - data_points: 240, - color_r: (0 , 255), - color_g: (255, 0), - color_b: (0 , 0), - aspect: 3.7, - } + Save::default() }; let el = EventLoop::new(); @@ -93,8 +105,8 @@ fn main() handle: HANDLE::default(), offset: 0, - handle_timer: 0, - memory_read_timer: 0, + update_timer: 0, + timer_ticks: save.timer_ticks, graph: Graph { @@ -116,7 +128,7 @@ fn main() el.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + *control_flow = ControlFlow::WaitUntil(std::time::Instant::now() + std::time::Duration::from_millis(2)); egui_glutin::event_handling(event, control_flow, &mut egui_state, &gui_state); @@ -124,14 +136,22 @@ fn main() frame_time += current_time - last_time; last_time = current_time; - while frame_time >= std::time::Duration::from_micros(33333) + let time = 20000; + + while frame_time >= std::time::Duration::from_micros(time) { - frame_time -= std::time::Duration::from_micros(33333); + frame_time -= std::time::Duration::from_micros(time); - match gui_state.current_game + gui_state.update_timer -= 1; + if gui_state.update_timer < 0 { - Some(_) => update(&mut gui_state), - None => find_game(&mut gui_state), + gui_state.update_timer = gui_state.timer_ticks; + + match gui_state.current_game + { + Some(_) => update(&mut gui_state), + None => find_game(&mut gui_state), + } } egui_state.ctx.begin_frame(egui_state.raw_input.take()); @@ -156,13 +176,6 @@ fn main() fn find_game(gui_state: &mut GuiState) { - if gui_state.handle_timer > 0 - { - gui_state.handle_timer -= 1; - return; - } - gui_state.handle_timer = 30; - let mut emu_info = None; let (pid_list, pid_count) = enum_processes(); @@ -255,8 +268,6 @@ fn find_game(gui_state: &mut GuiState) fn get_game_name(handle: &HANDLE, info: &MODULEINFO, emu: &game_data::Emulator) -> Option { - let mut raw_str = [0; 22]; - let game_name_offset = match emu { game_data::Emulator::Bsnes => 0xB151E8 as *const c_void, @@ -269,6 +280,8 @@ fn get_game_name(handle: &HANDLE, info: &MODULEINFO, emu: &game_data::Emulator) } }; + let mut raw_str = [0; 22]; + unsafe { let p_raw_str = raw_str.as_mut_ptr() as *mut _ as *mut c_void; @@ -321,13 +334,6 @@ fn get_mame_offset(handle: &HANDLE, dll_base: u64, offset_list: Vec) -> u64 fn update(gui_state: &mut GuiState) { - if gui_state.memory_read_timer > 0 - { - gui_state.memory_read_timer -= 1; - return; - } - gui_state.memory_read_timer = 60; //30 = 1s - //check if game window is closed. not perfect as user can load other game without closing the emulator //todo: check for string again probably let mut exit_code = 0; @@ -461,12 +467,26 @@ fn create_ui(ctx: &mut Context, gui_state: &mut GuiState) gui_state.graph.default_window_pos = (pos.min.x, pos.min.y); gui_state.graph.default_window_width = pos.max.x; } - else + + egui::Window::new("Game data reader").show(ctx, |ui| { - egui::Window::new("Game data reader").show(ctx, |ui| + ui.horizontal(|ui| { - ui.label("Searching for supported games..."); - ui.label("Once a game has been found, data will be shown automatically!"); + ui.add + ( + egui::DragValue::new(&mut gui_state.timer_ticks) + .speed(0.23) + .clamp_range(5 ..= 125) + .prefix("Ticks/update: ") + ); + + ui.label(format!("({:.2} updates/sec)", 50.0 / gui_state.timer_ticks as f32)); }); - } + + if gui_state.current_game.is_none() + { + ui.label("\nSearching for supported games..."); + ui.label("Once a game has been found, data will be shown automatically!"); + } + }); }