Skip to content

Commit

Permalink
better profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
tedsteen committed Nov 20, 2024
1 parent 1db4730 commit 8d6e28d
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 108 deletions.
2 changes: 2 additions & 0 deletions src/emulation/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ impl GuiComponent for EmulatorGui {

#[cfg(feature = "netplay")]
fn messages(&self) -> Option<Vec<String>> {
#[cfg(feature = "debug")]
puffin::profile_function!();
self.netplay_gui.messages(&self.nes_state.lock().unwrap())
}

Expand Down
236 changes: 128 additions & 108 deletions src/main_view/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,140 +137,160 @@ impl MainGui {
inputs_gui: &mut InputsGui,
emulator_gui: &mut EmulatorGui,
) {
if !self.visible() && esc_pressed(ctx) {
Self::set_main_menu_state(MainMenuState::Main);
}

match Self::main_menu_state() {
MainMenuState::Main => {
Self::ui_main_container(&self.window, None, ctx, |ui| {
if Self::menu_item_ui(ui, "BACK").clicked() || esc_pressed(ctx) {
Self::set_main_menu_state(MainMenuState::Closed);
}
{
#[cfg(feature = "debug")]
puffin::profile_scope!("Main ui");

if let Some(name) = emulator_gui.name() {
if Self::menu_item_ui(ui, name.to_uppercase()).clicked() {
Self::set_main_menu_state(MainMenuState::Netplay);
if !self.visible() && esc_pressed(ctx) {
Self::set_main_menu_state(MainMenuState::Main);
}
match Self::main_menu_state() {
MainMenuState::Main => {
Self::ui_main_container(&self.window, None, ctx, |ui| {
if Self::menu_item_ui(ui, "BACK").clicked() || esc_pressed(ctx) {
Self::set_main_menu_state(MainMenuState::Closed);
}
}

if Self::menu_item_ui(ui, "SETTINGS").clicked() {
Self::set_main_menu_state(MainMenuState::Settings);
}

#[cfg(feature = "debug")]
{
if Self::menu_item_ui(ui, "PROFILING").clicked() {
puffin::set_scopes_on(!puffin::are_scopes_on());
if let Some(name) = emulator_gui.name() {
if Self::menu_item_ui(ui, name.to_uppercase()).clicked() {
Self::set_main_menu_state(MainMenuState::Netplay);
}
}
}

if Self::menu_item_ui(ui, "QUIT GAME").clicked() {
std::process::exit(0);
}
});
}
MainMenuState::Settings => {
Self::ui_main_container(&self.window, Some("Settings"), ctx, |ui| {
ui.vertical(|ui| {
if let Some(name) = audio_gui.name() {
ui.vertical_centered(|ui| {
ui.heading(name);
});
audio_gui.ui(ui);
if Self::menu_item_ui(ui, "SETTINGS").clicked() {
Self::set_main_menu_state(MainMenuState::Settings);
}
ui.add_space(10.0);
ui.separator();
ui.add_space(10.0);
if let Some(name) = inputs_gui.name() {
ui.vertical_centered(|ui| {
ui.heading(name);
});
inputs_gui.ui(ui);

#[cfg(feature = "debug")]
{
if Self::menu_item_ui(ui, "PROFILING").clicked() {
puffin::set_scopes_on(!puffin::are_scopes_on());
}
}

if Bundle::current().config.supported_nes_regions.len() > 1 {
if Self::menu_item_ui(ui, "QUIT GAME").clicked() {
std::process::exit(0);
}
});
}
MainMenuState::Settings => {
Self::ui_main_container(&self.window, Some("Settings"), ctx, |ui| {
ui.vertical(|ui| {
if let Some(name) = audio_gui.name() {
ui.vertical_centered(|ui| {
ui.heading(name);
});
audio_gui.ui(ui);
}
ui.add_space(10.0);
ui.separator();
ui.vertical_centered(|ui| {
ui.heading("NES System");
});
ui.vertical(|ui| {
ui.label(
RichText::new("NOTE: changing this will restart the game")
.color(Color32::DARK_RED),
);
ui.add_space(10.0);
if let Some(name) = inputs_gui.name() {
ui.vertical_centered(|ui| {
ui.heading(name);
});
inputs_gui.ui(ui);
}

if Bundle::current().config.supported_nes_regions.len() > 1 {
ui.separator();
ui.vertical_centered(|ui| {
ui.heading("NES System");
});
ui.vertical(|ui| {
ui.label(
RichText::new("NOTE: changing this will restart the game")
.color(Color32::DARK_RED),
);

ui.horizontal(|ui| {
for supported_region in
&Bundle::current().config.supported_nes_regions
{
if ui
.radio_value(
Settings::current_mut().get_nes_region(),
supported_region.clone(),
format!("{:?}", supported_region),
)
.changed()
ui.horizontal(|ui| {
for supported_region in
&Bundle::current().config.supported_nes_regions
{
let _ =
self.emulator_tx.send(EmulatorCommand::Reset(true));
if ui
.radio_value(
Settings::current_mut().get_nes_region(),
supported_region.clone(),
format!("{:?}", supported_region),
)
.changed()
{
let _ = self
.emulator_tx
.send(EmulatorCommand::Reset(true));
}
}
}
});
});
});
}
}

ui.vertical_centered(|ui| {
ui.add_space(20.0);
if Button::new(RichText::new("Close").font(FontId::proportional(20.0)))
ui.vertical_centered(|ui| {
ui.add_space(20.0);
if Button::new(
RichText::new("Close").font(FontId::proportional(20.0)),
)
.ui(ui)
.clicked()
|| esc_pressed(ui.ctx())
{
Self::set_main_menu_state(MainMenuState::Main);
}
|| esc_pressed(ui.ctx())
{
Self::set_main_menu_state(MainMenuState::Main);
}
});
});
});
});
}
MainMenuState::Netplay => {
if emulator_gui.name().is_some() {
let name = emulator_gui.name().expect("a name").to_owned();
Self::ui_main_container(&self.window, Some(&name), ctx, |ui| {
emulator_gui.ui(ui);
});
}
MainMenuState::Netplay => {
if emulator_gui.name().is_some() {
let name = emulator_gui.name().expect("a name").to_owned();
Self::ui_main_container(&self.window, Some(&name), ctx, |ui| {
emulator_gui.ui(ui);
});
}
}
MainMenuState::Closed => {}
}
MainMenuState::Closed => {}
}
{
#[cfg(feature = "debug")]
puffin::profile_scope!("Messages");
egui::TopBottomPanel::top("messages")
.show_separator_line(false)
.frame(
egui::Frame::default()
.fill(Color32::TRANSPARENT)
.outer_margin(Margin::same(80.0))
.inner_margin(Margin::ZERO),
)
.show(ctx, |ui| {
ui.vertical_centered(|ui| {
let gui_components: &mut [&mut dyn GuiComponent] =
&mut [audio_gui, inputs_gui, emulator_gui];
for gui in gui_components.iter_mut() {
{
#[cfg(feature = "debug")]
puffin::profile_scope!(format!("Prepare {:?}", gui.name()));

egui::TopBottomPanel::top("messages")
.show_separator_line(false)
.frame(
egui::Frame::default()
.fill(Color32::TRANSPARENT)
.outer_margin(Margin::same(80.0))
.inner_margin(Margin::ZERO),
)
.show(ctx, |ui| {
ui.vertical_centered(|ui| {
let gui_components: &mut [&mut dyn GuiComponent] =
&mut [audio_gui, inputs_gui, emulator_gui];
for gui in gui_components.iter_mut() {
gui.prepare();
if gui.name().is_some() {
if let Some(messages) = gui.messages() {
for message in messages {
Self::message_ui(ui, message);
gui.prepare();
}
if gui.name().is_some() {
{
#[cfg(feature = "debug")]
puffin::profile_scope!(format!("Messages {:?}", gui.name()));

if let Some(messages) = gui.messages() {
for message in messages {
Self::message_ui(ui, message);
}
}
}
}
}
}
if self.start_time.elapsed() < Duration::from_secs(5) {
Self::message_ui(ui, "Press ESC for menu");
}
if self.start_time.elapsed() < Duration::from_secs(5) {
Self::message_ui(ui, "Press ESC for menu");
}
});
});
});
}
}

pub fn handle_event(
Expand Down
2 changes: 2 additions & 0 deletions src/netplay/gui/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ use super::NetplayGui;

impl NetplayGui {
pub fn prepare(&mut self, netplay_state_handler: &NetplayStateHandler) {
puffin::profile_function!();

if let Some(NetplayState::Connected(netplay)) = &netplay_state_handler.netplay {
let sess = &netplay.state.netplay_session.p2p_session;
if netplay.state.netplay_session.game_state.frame % 30 == 0 {
Expand Down
3 changes: 3 additions & 0 deletions src/netplay/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ impl NetplayGui {
None
}
pub fn messages(&self, netplay_state_handler: &NetplayStateHandler) -> Option<Vec<String>> {
#[cfg(feature = "debug")]
puffin::profile_function!();

if matches!(MainGui::main_menu_state(), MainMenuState::Netplay) {
// No need to show messages when the netplay menu is already showing status
return None;
Expand Down

0 comments on commit 8d6e28d

Please sign in to comment.