better example framework, moving towards winit 30

This commit is contained in:
Jonathan Spira 2024-05-21 23:31:37 -04:00
parent cb8beeb74c
commit 2e30300afd
24 changed files with 97 additions and 143 deletions

View File

@ -6,8 +6,7 @@ fn main() {
let mut state = State {
render_closable: true,
};
let system = support::init(file!());
system.main_loop(move |run, ui| {
support::simple_init(file!(), move |run, ui| {
let w = ui
.window("Collapsing header")
.opened(run)

View File

@ -4,8 +4,7 @@ mod support;
fn main() {
let mut state = State::default();
let system = support::init(file!());
system.main_loop(move |run, ui| {
support::simple_init(file!(), move |run, ui| {
example_selector(run, ui, &mut state);
match state.example {
1 => example_1(ui, &mut state),

View File

@ -1,9 +1,7 @@
mod support;
fn main() {
let system = support::init(file!());
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
// If we don't explicitly create a window before creating some kind of widget, then Dear Imgui will automatically create one
ui.text("This text will appear in a default window titled 'Debug'");

View File

@ -217,7 +217,8 @@ impl Lenna {
fn main() {
let mut my_app = CustomTexturesApp::default();
let mut system = support::init(file!());
let mut system = support::simple_init(file!());
my_app
.register_textures(system.display.get_context(), system.renderer.textures())
.expect("Failed to register textures");

View File

@ -5,14 +5,11 @@ use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
let mut edit_mode = true;
let mut safe_mode = true;
let mut click_count = 0;
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
ui.window("Disabling widgets")
.size([300.0, 200.0], Condition::FirstUseEver)
.build(|| {

View File

@ -17,8 +17,7 @@ fn draw_text_centered(
}
fn main() {
let system = support::init(file!());
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
// Get access to draw FG and BG draw lists.
let bg_draw_list = ui.get_background_draw_list();
let fg_draw_list = ui.get_foreground_draw_list();

View File

@ -1,9 +1,7 @@
mod support;
fn main() {
let system = support::init(file!());
system.main_loop(move |_, _ui| {
support::simple_init(file!(), move |_, _ui| {
// nothing! don't actually do any imgui funtimes
});
}

View File

@ -3,12 +3,9 @@ use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
let mut value = 0;
let choices = ["test test this is 1", "test test this is 2"];
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
ui.window("Hello world")
.size([300.0, 110.0], Condition::FirstUseEver)
.build(|| {

View File

@ -1,8 +1,7 @@
mod support;
fn main() {
let system = support::init(file!());
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
let items = vec!["a", "b", "c", "d"];
ui.window("Broken Example")

View File

@ -3,7 +3,6 @@ use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
let mut press_counter = 0u32;
let mut press_no_repeat_counter = 0u32;
let mut release_counter = 0u32;
@ -13,7 +12,7 @@ fn main() {
let mut f1_release_count = 0u32;
let mut text_buffer = String::new();
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
ui.window("Means of accessing key state")
.size([500.0, 300.0], Condition::FirstUseEver)
.build(|| {

View File

@ -13,8 +13,7 @@ mod support;
fn main() {
let lots_of_words: Vec<String> = (0..10000).map(|x| format!("Line {}", x)).collect();
let system = support::init(file!());
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
// Show the C++ style API
ui.window("Hello long world")
.size([100.0, 500.0], Condition::FirstUseEver)

View File

@ -3,9 +3,7 @@ use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
ui.show_demo_window(&mut true);
ui.window("Table with list clipper")

View File

@ -3,7 +3,7 @@ use imgui::*;
mod support;
fn main() {
let mut system = support::init(file!());
let mut system = support::simple_init(file!());
let dokdo = system.imgui.fonts().add_font(&[FontSource::TtfData {
data: include_bytes!("../../resources/Dokdo-Regular.ttf"),
size_pixels: system.font_size,

View File

@ -3,8 +3,7 @@ use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
system.main_loop(move |run, ui| {
support::simple_init(file!(), move |run, ui| {
let w = ui
.window("Progress bar")
.opened(run)

View File

@ -4,8 +4,7 @@ mod support;
fn main() {
let mut state = State::default();
let system = support::init(file!());
system.main_loop(move |run, ui| {
support::simple_init(file!(), move |run, ui| {
example_selector(run, ui, &mut state);
match state.example {
1 => example_1(ui, &mut state),

View File

@ -4,8 +4,7 @@ mod support;
fn main() {
let mut state = State::default();
let system = support::init(file!());
system.main_loop(move |run, ui| {
support::simple_init(file!(), move |run, ui| {
example_selector(run, ui, &mut state);
match state.example {
1 => example_1(ui, &mut state),

View File

@ -12,17 +12,9 @@ use std::time::Instant;
mod clipboard;
pub struct System {
pub event_loop: EventLoop<()>,
pub window: Window,
pub display: glium::Display<WindowSurface>,
pub imgui: Context,
pub platform: WinitPlatform,
pub renderer: Renderer,
pub font_size: f32,
}
pub fn simple_init<F: FnMut(&mut bool, &mut Ui) + 'static>(title: &str, mut run_ui: F) {
let mut imgui = create_context();
pub fn init(title: &str) -> System {
let title = match Path::new(&title).file_name() {
Some(file_name) => file_name.to_str().unwrap(),
None => title,
@ -35,9 +27,7 @@ pub fn init(title: &str) -> System {
let (window, display) = glium::backend::glutin::SimpleWindowBuilder::new()
.set_window_builder(builder)
.build(&event_loop);
let mut imgui = Context::create();
imgui.set_ini_filename(None);
let mut renderer = Renderer::init(&mut imgui, &display).expect("Failed to initialize renderer");
if let Some(backend) = clipboard::init() {
imgui.set_clipboard_backend(backend);
@ -60,13 +50,71 @@ pub fn init(title: &str) -> System {
platform.attach_window(imgui.io_mut(), &window, dpi_mode);
}
let mut last_frame = Instant::now();
event_loop
.run(move |event, window_target| match event {
Event::NewEvents(_) => {
let now = Instant::now();
imgui.io_mut().update_delta_time(now - last_frame);
last_frame = now;
}
Event::AboutToWait => {
platform
.prepare_frame(imgui.io_mut(), &window)
.expect("Failed to prepare frame");
window.request_redraw();
}
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
} => {
let ui = imgui.frame();
let mut run = true;
run_ui(&mut run, ui);
if !run {
window_target.exit();
}
let mut target = display.draw();
target.clear_color_srgb(1.0, 1.0, 1.0, 1.0);
platform.prepare_render(ui, &window);
let draw_data = imgui.render();
renderer
.render(&mut target, draw_data)
.expect("Rendering failed");
target.finish().expect("Failed to swap buffers");
}
Event::WindowEvent {
event: WindowEvent::Resized(new_size),
..
} => {
if new_size.width > 0 && new_size.height > 0 {
display.resize((new_size.width, new_size.height));
}
platform.handle_event(imgui.io_mut(), &window, &event);
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => window_target.exit(),
event => {
platform.handle_event(imgui.io_mut(), &window, &event);
}
})
.expect("EventLoop error");
}
/// Creates the imgui context
pub fn create_context() -> imgui::Context {
let mut imgui = Context::create();
// Fixed font size. Note imgui_winit_support uses "logical
// pixels", which are physical pixels scaled by the devices
// scaling factor. Meaning, 13.0 pixels should look the same size
// on two different screens, and thus we do not need to scale this
// value (as the scaling is handled by winit)
let font_size = 13.0;
imgui.fonts().add_font(&[
FontSource::TtfData {
data: include_bytes!("../../../resources/Roboto-Regular.ttf"),
@ -98,84 +146,16 @@ pub fn init(title: &str) -> System {
}),
},
]);
imgui.set_ini_filename(None);
let renderer = Renderer::init(&mut imgui, &display).expect("Failed to initialize renderer");
System {
event_loop,
window,
display,
imgui,
platform,
renderer,
font_size,
}
imgui
}
impl System {
pub fn main_loop<F: FnMut(&mut bool, &mut Ui) + 'static>(self, mut run_ui: F) {
let System {
event_loop,
window,
display,
mut imgui,
mut platform,
mut renderer,
..
} = self;
let mut last_frame = Instant::now();
event_loop
.run(move |event, window_target| match event {
Event::NewEvents(_) => {
let now = Instant::now();
imgui.io_mut().update_delta_time(now - last_frame);
last_frame = now;
}
Event::AboutToWait => {
platform
.prepare_frame(imgui.io_mut(), &window)
.expect("Failed to prepare frame");
window.request_redraw();
}
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
} => {
let ui = imgui.frame();
let mut run = true;
run_ui(&mut run, ui);
if !run {
window_target.exit();
}
let mut target = display.draw();
target.clear_color_srgb(1.0, 1.0, 1.0, 1.0);
platform.prepare_render(ui, &window);
let draw_data = imgui.render();
renderer
.render(&mut target, draw_data)
.expect("Rendering failed");
target.finish().expect("Failed to swap buffers");
}
Event::WindowEvent {
event: WindowEvent::Resized(new_size),
..
} => {
if new_size.width > 0 && new_size.height > 0 {
display.resize((new_size.width, new_size.height));
}
platform.handle_event(imgui.io_mut(), &window, &event);
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => window_target.exit(),
event => {
platform.handle_event(imgui.io_mut(), &window, &event);
}
})
.expect("EventLoop error");
}
/// Initialize the example with imgui and the renderer
pub fn init_with<F: FnMut(&mut bool, &mut Ui) + 'static>(
title: &str,
mut imgui: imgui::Context,
mut renderer: Renderer,
mut run_ui: F,
) {
}

View File

@ -3,8 +3,6 @@ use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
let mut humans = vec![
HumanData {
name: "Joonas",
@ -28,7 +26,7 @@ fn main() {
| TableFlags::RESIZABLE
| TableFlags::NO_BORDERS_IN_BODY;
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
ui.window("Input text callbacks")
.size([800.0, 400.0], Condition::FirstUseEver)
.build(|| {

View File

@ -4,8 +4,7 @@ const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
fn main() {
let system = support::init(file!());
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
let draw_list = ui.get_window_draw_list();
// Will draw channel 0 first, then channel 1, whatever the order of
// the calls in the code.

View File

@ -1,6 +1,5 @@
mod support;
fn main() {
let system = support::init(file!());
system.main_loop(move |run, ui| ui.show_demo_window(run));
support::simple_init(file!(), move |run, ui| ui.show_demo_window(run));
}

View File

@ -237,8 +237,9 @@ impl Default for CustomRenderingState {
fn main() {
let mut state = State::default();
let system = support::init(file!());
system.main_loop(move |run, ui| show_test_window(ui, &mut state, run));
support::simple_init(file!(), move |run, ui| {
show_test_window(ui, &mut state, run)
});
}
fn show_help_marker(ui: &Ui, desc: &str) {

View File

@ -3,10 +3,8 @@ use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
let mut buffers = [String::default(), String::default(), String::default()];
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
ui.window("Input text callbacks")
.size([500.0, 300.0], Condition::FirstUseEver)
.build(|| {

View File

@ -3,11 +3,10 @@ use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
let mut stable_str = String::new();
let mut callback_str = String::new();
system.main_loop(move |_, ui| {
support::simple_init(file!(), move |_, ui| {
if let Some(_window) = ui
.window("Input text callbacks")
.size([500.0, 300.0], Condition::FirstUseEver)

View File

@ -16,7 +16,7 @@ imgui = { version = "0.12.0", path = "../imgui" }
[dev-dependencies]
glium = { version = "0.34.0", default-features = false, features = ["glutin_backend"] }
imgui-winit-support = {path = "../imgui-winit-support"}
glutin = "0.31.1"
glutin = "0.31"
glutin-winit = "0.4.2"
winit = { version = "0.29.3", features = ["rwh_05"] }
raw-window-handle = "0.5.0"