From 38f11442c9fafd3b3540f0fb2bdc1ca9f465a0d6 Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 4 Jan 2023 19:00:56 +1030 Subject: [PATCH] Self-contained example for glutin [#691] --- imgui-glium-renderer/Cargo.toml | 3 + .../examples/glium_01_basic.rs | 98 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 imgui-glium-renderer/examples/glium_01_basic.rs diff --git a/imgui-glium-renderer/Cargo.toml b/imgui-glium-renderer/Cargo.toml index 6338966..65d9cac 100644 --- a/imgui-glium-renderer/Cargo.toml +++ b/imgui-glium-renderer/Cargo.toml @@ -12,3 +12,6 @@ categories = ["gui", "rendering"] [dependencies] glium = { version = "0.32.1", default-features = false } imgui = { version = "0.9.0", path = "../imgui" } + +[dev-dependencies] +imgui-winit-support = {path = "../imgui-winit-support"} diff --git a/imgui-glium-renderer/examples/glium_01_basic.rs b/imgui-glium-renderer/examples/glium_01_basic.rs new file mode 100644 index 0000000..2e06b65 --- /dev/null +++ b/imgui-glium-renderer/examples/glium_01_basic.rs @@ -0,0 +1,98 @@ +use glium::glutin::event::{Event, WindowEvent}; +use glium::glutin::event_loop::{ControlFlow, EventLoop}; +use glium::Surface; + +const TITLE: &str = "Hello, imgui-rs!"; + +fn main() { + // Common setup for creating a winit window and imgui context, not specifc + // to this renderer at all except that glutin is used to create the window + // since it will give us access to a GL context + let (event_loop, display) = create_window(); + let (mut winit_platform, mut imgui_context) = imgui_init(&display); + + // Create renderer from this crate + let mut renderer = imgui_glium_renderer::Renderer::init(&mut imgui_context, &display).expect("Failed to initialize renderer"); + + // Timer for FPS calculation + let mut last_frame = std::time::Instant::now(); + + // Standard winit event loop + event_loop.run(move |event, _, control_flow| match event { + Event::NewEvents(_) => { + let now = std::time::Instant::now(); + imgui_context.io_mut().update_delta_time(now - last_frame); + last_frame = now; + } + Event::MainEventsCleared => { + let gl_window = display.gl_window(); + winit_platform + .prepare_frame(imgui_context.io_mut(), gl_window.window()) + .expect("Failed to prepare frame"); + gl_window.window().request_redraw(); + } + Event::RedrawRequested(_) => { + // Create frame for the all important `&imgui::Ui` + let ui = imgui_context.frame(); + + // Draw our example content + ui.show_demo_window(&mut true); + + // Setup for drawing + let gl_window = display.gl_window(); + let mut target = display.draw(); + + // Renderer doesn't automatically clear window + target.clear_color_srgb(1.0, 1.0, 1.0, 1.0); + + // Perform rendering + winit_platform.prepare_render(ui, gl_window.window()); + let draw_data = imgui_context.render(); + renderer + .render(&mut target, draw_data) + .expect("Rendering failed"); + target.finish().expect("Failed to swap buffers"); + } + Event::WindowEvent { + event: WindowEvent::CloseRequested, + .. + } => *control_flow = ControlFlow::Exit, + event => { + let gl_window = display.gl_window(); + winit_platform.handle_event(imgui_context.io_mut(), gl_window.window(), &event); + } + }); +} + +fn create_window() -> (EventLoop<()>, glium::Display) { + let event_loop = EventLoop::new(); + let context = glium::glutin::ContextBuilder::new().with_vsync(true); + let builder = glium::glutin::window::WindowBuilder::new() + .with_title(TITLE.to_owned()) + .with_inner_size(glium::glutin::dpi::LogicalSize::new(1024f64, 768f64)); + let display = + glium::Display::new(builder, context, &event_loop).expect("Failed to initialize display"); + + (event_loop, display) +} + + +fn imgui_init(display: &glium::Display) -> (imgui_winit_support::WinitPlatform, imgui::Context) { + let mut imgui_context = imgui::Context::create(); + imgui_context.set_ini_filename(None); + + let mut winit_platform = imgui_winit_support::WinitPlatform::init(&mut imgui_context); + + let gl_window = display.gl_window(); + let window = gl_window.window(); + + let dpi_mode = imgui_winit_support::HiDpiMode::Default; + + winit_platform.attach_window(imgui_context.io_mut(), window, dpi_mode); + + imgui_context + .fonts() + .add_font(&[imgui::FontSource::DefaultFontData { config: None }]); + + (winit_platform, imgui_context) +}