Renderers accept DrawData directly

There's no need to pass Ui, which makes this alternative much more
flexible.
This commit is contained in:
Joonas Javanainen 2019-06-29 13:30:13 +03:00
parent d094c6ee73
commit 9e3cd1681c
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
5 changed files with 30 additions and 19 deletions

View File

@ -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");
}
}

View File

@ -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<F: FnMut(&Ui) -> 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::<ColorFormat, DepthFormat>(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<F: FnMut(&Ui) -> 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<F: FnMut(&Ui) -> 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<F: FnMut(&Ui) -> 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<F: FnMut(&Ui) -> 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();

View File

@ -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<Texture<R>> {
&mut self.textures
}
pub fn render<'ui, F: Factory<R>, C: CommandBuffer<R>>(
pub fn render<F: Factory<R>, C: CommandBuffer<R>>(
&mut self,
factory: &mut F,
encoder: &mut Encoder<R, C>,
target: &mut RenderTargetView<R, Cf>,
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) {

View File

@ -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<T: Surface>(
&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) {

View File

@ -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*
//! }