imgui_sdl3/
lib.rs

1pub mod platform;
2pub mod renderer;
3pub mod utils;
4use platform::Platform;
5use renderer::Renderer;
6use sdl3::gpu::*;
7
8pub struct ImGuiSdl3 {
9    imgui_context: imgui::Context,
10    platform: Platform,
11    renderer: Renderer,
12}
13
14impl ImGuiSdl3 {
15    pub fn new<T>(device: &sdl3::gpu::Device, window: &sdl3::video::Window, ctx_configure: T) -> Self
16    where
17        T: Fn(&mut imgui::Context),
18    {
19        let mut imgui_context = imgui::Context::create();
20        ctx_configure(&mut imgui_context);
21
22        let platform = Platform::new(&mut imgui_context);
23        let renderer = Renderer::new(device, window, &mut imgui_context).unwrap();
24
25        Self {
26            imgui_context,
27            platform,
28            renderer,
29        }
30    }
31
32    pub fn handle_event(&mut self, event: &sdl3::event::Event) {
33        self.platform.handle_event(&mut self.imgui_context, event);
34    }
35
36    #[allow(clippy::too_many_arguments)]
37    pub fn render<T>(
38        &mut self,
39        sdl_context: &mut sdl3::Sdl,
40        device: &sdl3::gpu::Device,
41        window: &sdl3::video::Window,
42        event_pump: &sdl3::EventPump,
43        command_buffer: &mut CommandBuffer,
44        color_targets: &mut [ColorTargetInfo],
45        mut draw_callback: T,
46    ) where
47        T: FnMut(&mut imgui::Ui),
48    {
49        self.platform
50            .prepare_frame(sdl_context, &mut self.imgui_context, window, event_pump);
51
52        let ui = self.imgui_context.new_frame();
53        draw_callback(ui);
54
55        self.renderer
56            .render(device, command_buffer, color_targets, &mut self.imgui_context)
57            .unwrap();
58    }
59}