From df7ccae78ce80ddb3d71ce479c3f1a3992d7a880 Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 4 Jan 2023 18:31:30 +1030 Subject: [PATCH 01/11] [#691] Update "main library crates" doc - sdl2 wasn't mentioned - mention examples folder (..pending for a few of the crates) - mention the imgui-examples folder --- README.markdown | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/README.markdown b/README.markdown index 0ba81ac..94c74dc 100644 --- a/README.markdown +++ b/README.markdown @@ -25,12 +25,25 @@ ui.window("Hello world") ## Main library crates -- imgui: High-level safe API -- imgui-winit-support: Backend platform implementation that uses the `winit` - crate (latest by default, but earlier versions are supported via feature flags) -- imgui-glow-renderer: Renderer implementation that uses the `glow` crate -- imgui-glium-renderer: Renderer implementation that uses the `glium` crate -- imgui-sys: Low-level unsafe API (automatically generated) +The core of imgui-rs consists of: + +- `imgui`: High-level safe API +- `imgui-sys`: Low-level unsafe API (automatically generated) + +Next, we provide two renderers, and two backend platform implementations: + +- `imgui-winit-support`: Backend platform implementation that uses the `winit` crate +- `imgui-sdl2-support`: Backend platform using SDL2 +- `imgui-glow-renderer`: Renderer implementation that uses the `glow` crate +- `imgui-glium-renderer`: Renderer implementation that uses the `glium` crate + +Each of these contain an `examples` folder showing their usage. Check +the `[dev-dependencies]` section of their respective `Cargo.toml` to +find the versions of dependencies used (e.g `imgui-glow-renderer/Cargo.toml`) + +Finally the `imgui-examples` folder contains examples of how to use +the `imgui` crate itself - things like how to show text, how to create +buttons, etc etc. ## Features From ba9cb7f052bd7b6ebdcd5174605b0ef71a3520c7 Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 4 Jan 2023 18:31:40 +1030 Subject: [PATCH 02/11] Update features section --- README.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 94c74dc..113d99b 100644 --- a/README.markdown +++ b/README.markdown @@ -52,8 +52,8 @@ buttons, etc etc. - Builder structs for use cases where the original C++ library uses optional function parameters - Easy integration with `glow`/ `glium` -- Easy integration with winit (backend platform) -- Optional support for the freetype font rasterizer +- Easy integration with winit and sdl2 (backend platform) +- Optional support for the freetype font rasterizer and the docking branch ## Minimum Support Rust Version (MSRV) From 38f11442c9fafd3b3540f0fb2bdc1ca9f465a0d6 Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 4 Jan 2023 19:00:56 +1030 Subject: [PATCH 03/11] 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) +} From 34453a90812998bf9d7f0a4b251a74ebd2d5a105 Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 4 Jan 2023 19:05:15 +1030 Subject: [PATCH 04/11] Placeholder for imgui-winit-support A standalone example would either be missing a renderer (of minimal usefulness), or an exact duplicate of the linked examples --- imgui-winit-support/examples/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 imgui-winit-support/examples/README.md diff --git a/imgui-winit-support/examples/README.md b/imgui-winit-support/examples/README.md new file mode 100644 index 0000000..82b9a95 --- /dev/null +++ b/imgui-winit-support/examples/README.md @@ -0,0 +1,5 @@ +See the examples for [`imgui-glium-renderer`][glium] or [`imgui-glow-renderer`][glow] +for simple examples of this platform backend with different renderers + +[glium]: ../../imgui-glium-renderer/examples +[glow]: ../../imgui-glow-renderer/examples From 6d08cb502cff5c5ea68a397f57b55bd492b3657d Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 4 Jan 2023 19:07:01 +1030 Subject: [PATCH 05/11] Tweak wording to make it clearer(?) there are other renderers --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 113d99b..4c37fc8 100644 --- a/README.markdown +++ b/README.markdown @@ -30,7 +30,7 @@ The core of imgui-rs consists of: - `imgui`: High-level safe API - `imgui-sys`: Low-level unsafe API (automatically generated) -Next, we provide two renderers, and two backend platform implementations: +Next, we provide two example renderers, and two example backend platform implementations: - `imgui-winit-support`: Backend platform implementation that uses the `winit` crate - `imgui-sdl2-support`: Backend platform using SDL2 From e96b2f1a04a32b5f68060021a1bf540a440f94bb Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 4 Jan 2023 19:11:54 +1030 Subject: [PATCH 06/11] Tweak wording about imgui-examples --- README.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 4c37fc8..6a03b17 100644 --- a/README.markdown +++ b/README.markdown @@ -42,8 +42,8 @@ the `[dev-dependencies]` section of their respective `Cargo.toml` to find the versions of dependencies used (e.g `imgui-glow-renderer/Cargo.toml`) Finally the `imgui-examples` folder contains examples of how to use -the `imgui` crate itself - things like how to show text, how to create -buttons, etc etc. +the `imgui` crate itself - covers general topics like how to show +text, how to create buttons, etc etc. ## Features From 2a5ffdfc3faf946845ba3c5c4a8caac7bb93f341 Mon Sep 17 00:00:00 2001 From: dbr Date: Thu, 5 Jan 2023 10:28:59 +1030 Subject: [PATCH 07/11] More rewording --- README.markdown | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index 6a03b17..92eeda8 100644 --- a/README.markdown +++ b/README.markdown @@ -38,12 +38,15 @@ Next, we provide two example renderers, and two example backend platform impleme - `imgui-glium-renderer`: Renderer implementation that uses the `glium` crate Each of these contain an `examples` folder showing their usage. Check -the `[dev-dependencies]` section of their respective `Cargo.toml` to -find the versions of dependencies used (e.g `imgui-glow-renderer/Cargo.toml`) +their respective `Cargo.toml` to find compatible versions (e.g +`imgui-glow-renderer/Cargo.toml` the `[dependencies]` describes the +compatible `glow` version and `[dev-dependencies]` describes the +compatible `glutin` version) Finally the `imgui-examples` folder contains examples of how to use -the `imgui` crate itself - covers general topics like how to show -text, how to create buttons, etc etc. +the `imgui` crate itself - this covers general topics like how to show +text, how to create buttons, etc - and should be applicable to usage +with any backend/renderer. ## Features @@ -97,6 +100,7 @@ Additionally, there are other libraries which provide other kinds of renderers, 2. [`imgui-d3d12-renderer`](https://github.com/curldivergence/imgui-d3d12-renderer) 3. [`imgui-dx11-renderer`](https://github.com/veykril/imgui-dx11-renderer) 4. [`imgui-gfx-renderer`](https://github.com/imgui-rs/imgui-gfx-renderer): Deprecated (no longer maintained beyond imgui-rs v0.8). Renderer implementation that uses the `gfx` crate (_not the new gfx-hal crate_) + 5. Many more can be found on [crates.io](https://crates.io) either using search or the ["dependents" page](https://crates.io/crates/imgui/reverse_dependencies) (the "depends on" text indicates if the crate has been updated for current versions of imgui-rs) You can also write your own support code if you have a more advanced use case, because **imgui-rs is not tied to any specific graphics / OS API**. From 81f8e0b48af3f9d488f8bab6ed2f792480b065f6 Mon Sep 17 00:00:00 2001 From: dbr Date: Thu, 5 Jan 2023 10:41:03 +1030 Subject: [PATCH 08/11] fmt --- .../examples/glium_01_basic.rs | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/imgui-glium-renderer/examples/glium_01_basic.rs b/imgui-glium-renderer/examples/glium_01_basic.rs index 2e06b65..9c1fdb4 100644 --- a/imgui-glium-renderer/examples/glium_01_basic.rs +++ b/imgui-glium-renderer/examples/glium_01_basic.rs @@ -12,55 +12,56 @@ fn main() { 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"); + 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(); + 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); + // Draw our example content + ui.show_demo_window(&mut true); - // Setup for drawing - let gl_window = display.gl_window(); - let mut target = display.draw(); + // 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); + // 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); - } + // 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); + } }); } @@ -76,7 +77,6 @@ fn create_window() -> (EventLoop<()>, glium::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); From fb6aa53235f51d8f72215195a3c063ec987dfb6b Mon Sep 17 00:00:00 2001 From: dbr Date: Thu, 5 Jan 2023 12:00:38 +1030 Subject: [PATCH 09/11] Linkify crate names --- README.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.markdown b/README.markdown index 92eeda8..9bbe387 100644 --- a/README.markdown +++ b/README.markdown @@ -27,15 +27,15 @@ ui.window("Hello world") The core of imgui-rs consists of: -- `imgui`: High-level safe API -- `imgui-sys`: Low-level unsafe API (automatically generated) +- [`imgui`](./imgui): High-level safe API +- [`imgui-sys`](./imgui-sys): Low-level unsafe API (automatically generated) Next, we provide two example renderers, and two example backend platform implementations: -- `imgui-winit-support`: Backend platform implementation that uses the `winit` crate -- `imgui-sdl2-support`: Backend platform using SDL2 -- `imgui-glow-renderer`: Renderer implementation that uses the `glow` crate -- `imgui-glium-renderer`: Renderer implementation that uses the `glium` crate +- [`imgui-winit-support`](./imgui-winit-support): Backend platform implementation that uses the `winit` crate +- [`imgui-sdl2-support`])(./imgui-sdl2-support): Backend platform using SDL2 +- [`imgui-glow-renderer`](./imgui-glow-renderer): Renderer implementation that uses the `glow` crate +- [`imgui-glium-renderer`](./imgui-glium-renderer): Renderer implementation that uses the `glium` crate Each of these contain an `examples` folder showing their usage. Check their respective `Cargo.toml` to find compatible versions (e.g From e8cb3897d04b72bb665a12b61180c1015cfb1ab9 Mon Sep 17 00:00:00 2001 From: dbr Date: Thu, 5 Jan 2023 12:01:40 +1030 Subject: [PATCH 10/11] Fix Markdown syntax error --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 9bbe387..bcbc113 100644 --- a/README.markdown +++ b/README.markdown @@ -33,7 +33,7 @@ The core of imgui-rs consists of: Next, we provide two example renderers, and two example backend platform implementations: - [`imgui-winit-support`](./imgui-winit-support): Backend platform implementation that uses the `winit` crate -- [`imgui-sdl2-support`])(./imgui-sdl2-support): Backend platform using SDL2 +- [`imgui-sdl2-support`](./imgui-sdl2-support): Backend platform using SDL2 - [`imgui-glow-renderer`](./imgui-glow-renderer): Renderer implementation that uses the `glow` crate - [`imgui-glium-renderer`](./imgui-glium-renderer): Renderer implementation that uses the `glium` crate From 84dd23a95965cb58d5aefd43606dfd64fdbb0b73 Mon Sep 17 00:00:00 2001 From: dbr Date: Thu, 5 Jan 2023 12:05:10 +1030 Subject: [PATCH 11/11] Linkify imgui-examples also --- README.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index bcbc113..a94bb96 100644 --- a/README.markdown +++ b/README.markdown @@ -43,10 +43,10 @@ their respective `Cargo.toml` to find compatible versions (e.g compatible `glow` version and `[dev-dependencies]` describes the compatible `glutin` version) -Finally the `imgui-examples` folder contains examples of how to use -the `imgui` crate itself - this covers general topics like how to show -text, how to create buttons, etc - and should be applicable to usage -with any backend/renderer. +Finally the [`imgui-examples`](./imgui-examples) folder contains +examples of how to use the `imgui` crate itself - this covers general +topics like how to show text, how to create buttons, etc - and should +be applicable to usage with any backend/renderer. ## Features