From 9e3cd1681cf913f5759c03b6dd158696e90d2295 Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Sat, 29 Jun 2019 13:30:13 +0300 Subject: [PATCH] Renderers accept DrawData directly There's no need to pass Ui, which makes this alternative much more flexible. --- imgui-examples/examples/support/mod.rs | 5 +++- .../examples/support_gfx/mod.rs | 27 ++++++++++++------- imgui-gfx-renderer/src/lib.rs | 7 +++-- imgui-glium-renderer/src/lib.rs | 7 +++-- imgui-winit-support/src/lib.rs | 3 ++- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/imgui-examples/examples/support/mod.rs b/imgui-examples/examples/support/mod.rs index 6709f67..bf591e2 100644 --- a/imgui-examples/examples/support/mod.rs +++ b/imgui-examples/examples/support/mod.rs @@ -109,7 +109,10 @@ impl System { let mut target = display.draw(); target.clear_color_srgb(1.0, 1.0, 1.0, 1.0); platform.prepare_render(&ui, &window); - renderer.render(&mut target, ui).expect("Rendering failed"); + let draw_data = ui.render(); + renderer + .render(&mut target, draw_data) + .expect("Rendering failed"); target.finish().expect("Failed to swap buffers"); } } diff --git a/imgui-gfx-examples/examples/support_gfx/mod.rs b/imgui-gfx-examples/examples/support_gfx/mod.rs index 8272fa5..fbf8ae9 100644 --- a/imgui-gfx-examples/examples/support_gfx/mod.rs +++ b/imgui-gfx-examples/examples/support_gfx/mod.rs @@ -1,8 +1,8 @@ use gfx::Device; use glutin::{Event, WindowEvent}; -use imgui::{FontGlyphRanges, FontConfig, FontSource, Context, Ui}; +use imgui::{Context, FontConfig, FontGlyphRanges, FontSource, Ui}; use imgui_gfx_renderer::{GfxRenderer, Shaders}; -use imgui_winit_support::{WinitPlatform, HiDpiMode}; +use imgui_winit_support::{HiDpiMode, WinitPlatform}; use std::time::Instant; type ColorFormat = gfx::format::Rgba8; @@ -17,7 +17,7 @@ pub fn run bool>(title: String, clear_color: [f32; 4], mut run_ .with_dimensions(glutin::dpi::LogicalSize::new(1024f64, 768f64)); let (windowed_context, mut device, mut factory, mut main_color, mut main_depth) = gfx_window_glutin::init::(builder, context, &events_loop) - .expect("Failed to initialize graphics"); + .expect("Failed to initialize graphics"); let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into(); let shaders = { @@ -60,7 +60,11 @@ pub fn run bool>(title: String, clear_color: [f32; 4], mut run_ imgui.set_ini_filename(None); let mut platform = WinitPlatform::init(&mut imgui); - platform.attach_window(imgui.io_mut(), &windowed_context.window(), HiDpiMode::Rounded); + platform.attach_window( + imgui.io_mut(), + &windowed_context.window(), + HiDpiMode::Rounded, + ); let hidpi_factor = platform.hidpi_factor(); let font_size = (13.0 * hidpi_factor) as f32; @@ -96,9 +100,11 @@ pub fn run bool>(title: String, clear_color: [f32; 4], mut run_ if let Event::WindowEvent { event, .. } = event { match event { - WindowEvent::Resized(_) => { - gfx_window_glutin::update_views(&windowed_context, &mut main_color, &mut main_depth) - }, + WindowEvent::Resized(_) => gfx_window_glutin::update_views( + &windowed_context, + &mut main_color, + &mut main_depth, + ), WindowEvent::CloseRequested => quit = true, _ => (), } @@ -109,7 +115,9 @@ pub fn run bool>(title: String, clear_color: [f32; 4], mut run_ } let io = imgui.io_mut(); - platform.prepare_frame(io, &windowed_context.window()).expect("Failed to start frame"); + platform + .prepare_frame(io, &windowed_context.window()) + .expect("Failed to start frame"); last_frame = io.update_delta_time(last_frame); let ui = imgui.frame(); @@ -118,8 +126,9 @@ pub fn run bool>(title: String, clear_color: [f32; 4], mut run_ } encoder.clear(&main_color, clear_color); + let draw_data = ui.render(); renderer - .render(&mut factory, &mut encoder, &mut main_color, ui) + .render(&mut factory, &mut encoder, &mut main_color, draw_data) .expect("Rendering failed"); encoder.flush(&mut device); windowed_context.swap_buffers().unwrap(); diff --git a/imgui-gfx-renderer/src/lib.rs b/imgui-gfx-renderer/src/lib.rs index 1b6d0a1..56034c9 100644 --- a/imgui-gfx-renderer/src/lib.rs +++ b/imgui-gfx-renderer/src/lib.rs @@ -6,7 +6,7 @@ use gfx::texture::{FilterMethod, SamplerInfo, WrapMode}; use gfx::traits::FactoryExt; use gfx::{CommandBuffer, Encoder, Factory, IntoIndexBuffer, Rect, Resources, Slice}; use imgui::internal::RawWrapper; -use imgui::{DrawCmd, DrawCmdParams, DrawIdx, DrawVert, ImString, TextureId, Textures, Ui}; +use imgui::{DrawCmd, DrawCmdParams, DrawData, DrawIdx, DrawVert, ImString, TextureId, Textures}; use std::usize; #[derive(Clone, Debug)] @@ -170,14 +170,13 @@ where pub fn textures(&mut self) -> &mut Textures> { &mut self.textures } - pub fn render<'ui, F: Factory, C: CommandBuffer>( + pub fn render, C: CommandBuffer>( &mut self, factory: &mut F, encoder: &mut Encoder, target: &mut RenderTargetView, - ui: Ui<'ui>, + draw_data: &DrawData, ) -> Result<(), GfxRendererError> { - let draw_data = ui.render(); let fb_width = draw_data.display_size[0] * draw_data.framebuffer_scale[0]; let fb_height = draw_data.display_size[1] * draw_data.framebuffer_scale[1]; if !(fb_width > 0.0 && fb_height > 0.0) { diff --git a/imgui-glium-renderer/src/lib.rs b/imgui-glium-renderer/src/lib.rs index e7001d9..4983961 100644 --- a/imgui-glium-renderer/src/lib.rs +++ b/imgui-glium-renderer/src/lib.rs @@ -8,7 +8,7 @@ use glium::{ Surface, Texture2d, VertexBuffer, }; use imgui::internal::RawWrapper; -use imgui::{DrawCmd, DrawCmdParams, ImString, TextureId, Textures, Ui}; +use imgui::{DrawCmd, DrawCmdParams, DrawData, ImString, TextureId, Textures}; use std::borrow::Cow; use std::fmt; use std::rc::Rc; @@ -112,12 +112,11 @@ impl GliumRenderer { Err(GliumRendererError::BadTexture(texture_id)) } } - pub fn render<'ui, T: Surface>( + pub fn render( &mut self, target: &mut T, - ui: Ui<'ui>, + draw_data: &DrawData, ) -> Result<(), GliumRendererError> { - let draw_data = ui.render(); let fb_width = draw_data.display_size[0] * draw_data.framebuffer_scale[0]; let fb_height = draw_data.display_size[1] * draw_data.framebuffer_scale[1]; if !(fb_width > 0.0 && fb_height > 0.0) { diff --git a/imgui-winit-support/src/lib.rs b/imgui-winit-support/src/lib.rs index b435377..b7f1c33 100644 --- a/imgui-winit-support/src/lib.rs +++ b/imgui-winit-support/src/lib.rs @@ -57,7 +57,8 @@ //! //! platform.prepare_render(&ui, &window); // step 5 //! // render the UI with a renderer -//! // renderer.render(..., ui).expect("UI rendering failed"); +//! let draw_data = ui.render(); +//! // renderer.render(..., draw_data).expect("UI rendering failed"); //! //! // application-specific rendering *over the UI* //! }