-
Notifications
You must be signed in to change notification settings - Fork 371
Working Examples don't seem to work as stand alone code #637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Oops thought I had commented on this already! I agree. The existing examples are okay at showing "how to use a button" or whatever, but they aren't good at showing how to get started structuring an application This is definitely something we want to improve - there's an existing issue about this #621
A majority of the errors are indeed from conflicting winit(/glium) versions. I find it useful to check the output of Some of the other errors are from using the example code from the |
For a short term solution I put together a minimal working example. I'm sure it's not perfect as I am just getting into imgui, but should allow anyone to get started.
[package]
name = "imgui_test"
version = "0.1.0"
edition = "2021"
[dependencies]
glium = { version = "0.30.2", default-features = true }
imgui = {version = "0.8.2", features = ["tables-api"]}
imgui-glium-renderer = "0.8.2"
imgui-winit-support = "0.8.2"
use glium;
use imgui;
use imgui_glium_renderer;
use imgui_winit_support;
fn main() {
let mut imgui_context = imgui::Context::create();
let event_loop = glium::glutin::event_loop::EventLoop::new();
let window_builder = glium::glutin::window::WindowBuilder::new()
.with_inner_size(glium::glutin::dpi::LogicalSize::new(1024.0, 768.0))
.with_title("Hello World");
let context_builder = glium::glutin::ContextBuilder::new();
let display = glium::Display::new(window_builder, context_builder, &event_loop).unwrap();
let mut platform = imgui_winit_support::WinitPlatform::init(&mut imgui_context);
{
let gl_window = display.gl_window();
let window = gl_window.window();
platform.attach_window(
imgui_context.io_mut(),
window,
imgui_winit_support::HiDpiMode::Default,
);
}
let mut renderer = imgui_glium_renderer::Renderer::init(&mut imgui_context, &display).unwrap();
let mut last_frame = std::time::Instant::now();
event_loop.run(move |event, _, control_flow| match event {
glium::glutin::event::Event::NewEvents(_) => {
imgui_context
.io_mut()
.update_delta_time(last_frame.elapsed());
last_frame = std::time::Instant::now();
}
glium::glutin::event::Event::MainEventsCleared => {
let gl_window = display.gl_window();
platform
.prepare_frame(imgui_context.io_mut(), gl_window.window())
.expect("Failed to prepare frame");
gl_window.window().request_redraw();
}
glium::glutin::event::Event::RedrawRequested(_) => {
let ui = imgui_context.frame();
let gl_window = display.gl_window();
let mut target = display.draw();
platform.prepare_render(&ui, gl_window.window());
let draw_data = ui.render();
renderer
.render(&mut target, draw_data)
.expect("UI rendering failed");
target.finish().expect("Failed to swap buffers");
}
glium::glutin::event::Event::WindowEvent {
event: glium::glutin::event::WindowEvent::CloseRequested,
..
} => {
*control_flow = glium::glutin::event_loop::ControlFlow::Exit;
}
event => {
let gl_window = display.gl_window();
platform.handle_event(imgui_context.io_mut(), gl_window.window(), &event);
}
});
} |
@Pandabear314 Thank you for writing this! This was insanely helpful. I'm having a problem, though: when I try to add a box containing some text, this happens when I drag the box around: Here's the portion I modified: glium::glutin::event::Event::RedrawRequested(_) => {
let ui = imgui_context.frame();
// Added this line to try to render some text
ui.text("test");
let gl_window = display.gl_window();
let mut target = display.draw();
platform.prepare_render(&ui, gl_window.window());
let draw_data = ui.render();
renderer
.render(&mut target, draw_data)
.expect("UI rendering failed");
target.finish().expect("Failed to swap buffers");
} What am I doing wrong here? |
I think you are just missing this line from the full example:
(the renderer doesn't clear the buffer in case you are displaying imgui widgets over a buffer already containing a game or whatever) |
@dbr This fixed it. Thank you! I agree that it would be awesome to have some more "bare bones" examples |
Also I'm not sure if this already exists, but the imgui demo window source code translated to the rust version would help a lot, as it serves kind of like documentation in the original project |
We do have the |
+1 to examples not being standalone. With current version 0.11.0 it is virtually impossible to get around all the conflicting packages and versions by looking at the example code. |
Hi, the current examples in the examples folder are imo much too complicated.
/support
).Here was what I tried:
The
lib.rs
file just contains the linepub mod support;
The
main.rs
file just contains the following lines (after making the function in hello_world public):I replaced the relative paths in
Cargo.toml
file by the absolute references:Compilation then yields:
Some of the issues are down to methods being private or conflicts with the winit-crate.
It would be nice if these issues were fixed or if the hello-world example were simpler to try out.
The text was updated successfully, but these errors were encountered: