-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwinit.rs
More file actions
95 lines (83 loc) · 3.12 KB
/
Copy pathwinit.rs
File metadata and controls
95 lines (83 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use egui::Ui;
use egui::Vec2;
use egui::ViewportCommand;
use egui_demo_lib::ColorTest;
use egui_demo_lib::DemoWindows;
use egui_software_backend::SoftwareRenderCaching;
use egui_software_backend::{SoftwareBackend, SoftwareBackendAppConfiguration};
struct EguiApp {
demo: DemoWindows,
color_test: ColorTest,
frame_times: Vec<f32>,
}
impl EguiApp {
fn new(context: egui::Context) -> Self {
egui_extras::install_image_loaders(&context);
EguiApp {
demo: DemoWindows::default(),
color_test: ColorTest::default(),
frame_times: Vec::new(),
}
}
fn ui(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |_ui| {
self.demo.ui(ctx);
egui::Window::new("Color Test").show(ctx, |ui| {
egui::ScrollArea::both().auto_shrink(false).show(ui, |ui| {
self.color_test.ui(ui);
});
});
});
}
}
impl eframe::App for EguiApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |_ui| {
self.ui(ctx);
});
}
}
fn software_backend_ui(backend: &mut SoftwareBackend, ui: &mut Ui) {
let old = backend.caching();
let mut new = old;
egui::ComboBox::from_label("SoftwareRenderCaching")
.selected_text(format!("{old:?}"))
.show_ui(ui, |ui| {
ui.selectable_value(&mut new, SoftwareRenderCaching::BlendTiled, "BlendTiled");
ui.selectable_value(&mut new, SoftwareRenderCaching::MeshTiled, "MeshTiled");
ui.selectable_value(&mut new, SoftwareRenderCaching::Mesh, "Mesh");
ui.selectable_value(&mut new, SoftwareRenderCaching::Direct, "Direct");
});
if new != old {
backend.set_caching(new);
}
}
impl egui_software_backend::App for EguiApp {
fn update(&mut self, ctx: &egui::Context, backend: &mut SoftwareBackend) {
egui::CentralPanel::default().show(ctx, |_ui| {
self.ui(ctx);
#[cfg(feature = "raster_stats")]
egui::Window::new("Stats").show(ctx, |ui| {
backend.display_stats(ui);
});
egui::Window::new("Software Backend").show(ctx, |ui| software_backend_ui(backend, ui));
if self.frame_times.len() < 100 {
self.frame_times
.push(backend.last_frame_time().unwrap_or_default().as_secs_f32());
} else {
let avg =
(self.frame_times.iter().sum::<f32>() / self.frame_times.len() as f32) * 1000.0;
ctx.send_viewport_cmd(ViewportCommand::Title(format!("Frame Time {avg:.2}ms")));
self.frame_times.clear();
}
});
}
}
fn main() {
let settings = SoftwareBackendAppConfiguration::new()
.inner_size(Some(Vec2::new(1600.0, 900.0)))
.title(Some(String::from("egui software backend")));
egui_software_backend::run_app_with_software_backend(settings, EguiApp::new)
//Can fail if winit fails to create the window
.expect("Failed to run app")
}