This is a simple example using imgui 1.80 with rust. It is copied from somewhere (sorry!) and fixed such that it would work. The dependency mess is also needed beause... not sure why it is so broken without it.
Created
November 5, 2023 17:45
-
-
Save Nithanim/7c64f6b25b460af3625f34a72177c819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "imgui_test" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
imgui = "0.7.0" | |
#imgui-winit = { version = "0.1.2"} | |
imgui-winit-support = { version = "0.7.0" } | |
#winit = { version = "0.27.2", features = ["x11"] } | |
winit = { version = "0.24.0" } | |
imgui-glium-renderer = "0.7.0" | |
glium = { version = "0.29.1", default-features = true } | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ui.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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment