10000 Working Examples don't seem to work as stand alone code · Issue #637 · imgui-rs/imgui-rs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Open
RLogik opened this issue Apr 16, 2022 · 8 comments
Open

Working Examples don't seem to work as stand alone code #637

RLogik opened this issue Apr 16, 2022 · 8 comments

Comments

@RLogik
8000 Copy link
RLogik commented Apr 16, 2022

Hi, the current examples in the examples folder are imo much too complicated.

  • They involve boilerplates (cf. /support).
  • They seem to not really work as stand-alone code (by which I mean a separate rust project which just imports the imgui-rs etc. crate/s). I tried to take the examples folder and make it into a stand alone ‘project’, however there are dependency issues.

Here was what I tried:

.
├── assets # previously the contents of /resources
│   ├── LICENSE-Dokdo.txt
│   ├── LICENSE-Roboto.txt
│   ├── Lenna.jpg
│   ├── Dokdo-Regular.ttf
│   ├── mplus-1p-regular.ttf
│   └── Roboto-Regular.ttf
├── src # previously the contents of imgui-examples/examples
│   ├── support
│   │   ├── mod.rs
│   │   └── clipboard.rs
│   ├── collapsing_header.rs
│   ├── color_button.rs
│   ├── creating_windows.rs
│   ├── custom_textures.rs
│   ├── disablement.rs
│   ├── draw_list.rs
│   ├── hello_world.rs
│   ├── id_wrangling.rs
│   ├── keyboard.rs
│   ├── long_list.rs
│   ├── long_table.rs
│   ├── multiple_fonts.rs
│   ├── progress_bar.rs
│   ├── radio_button.rs
│   ├── slider.rs
│   ├── tables_api.rs
│   ├── test_drawing_channels_split.rs
│   ├── test_window.rs
│   ├── test_window_impl.rs
│   ├── text_callbacks.rs
│   ├── text_input.rs
│   ├── lib.rs # <- added this (with `pub mod support`)
│   └── main.rs # <- added this
└── Cargo.toml # <- made relative paths absolute references

The lib.rs file just contains the line pub mod support;
The main.rs file just contains the following lines (after making the function in hello_world public):

mod hello_world;
fn main() {
    hello_world::main();
}

I replaced the relative paths in Cargo.toml file by the absolute references:

[package]
name = "dearimgui"
version = "1.0.0"
edition = "2018"
authors = ["The imgui-rs Developers"]
description = "imgui crate examples using Glium backend"
homepage = "https://github.com/imgui-rs/imgui-rs"
repository = "https://github.com/imgui-rs/imgui-rs"
license = "MIT/Apache-2.0"
publish = false

[dependencies]
clipboard = "0.5"
glium = { version = "0.31", default-features = true }
image = { version = "0.24.1" }
imgui = { version = "0.8.2", features = ["tables-api"] }
imgui-glium-renderer = { version = "0.8.2" }
imgui-winit-support = { version = "0.8.2" }

Compilation then yields:

cargo 1.56.0 (4ed5d137b 2021-10-04)
   Compiling dearimgui v1.0.0 (/*****/imgui-rs-main)
error[E0308]: mismatched types
  --> src/support/mod.rs:60:48
   |
60 |         platform.attach_window(imgui.io_mut(), window, dpi_mode);
   |                                                ^^^^^^ expected struct `winit::window::Window`, found struct `glium::glutin::window::Window`
   |
   = note: expected reference `&winit::window::Window`
              found reference `&glium::glutin::window::Window`
   = note: perhaps two different versions of crate `winit` are being used?

error[E0277]: the trait bound `glium::Display: imgui_glium_renderer::glium::backend::Facade` is not satisfied
   --> src/support/mod.rs:102:47
    |
102 |     let renderer = Renderer::init(&mut imgui, &display).expect("Failed to initialize renderer");
    |                                               ^^^^^^^^ the trait `imgui_glium_renderer::glium::backend::Facade` is not implemented for `glium::Display`
    |
note: required by a bound in `Renderer::init`
   --> /*****/.cargo/registry/src/github.com-1ecc6299db9ec823/imgui-glium-renderer-0.8.2/src/lib.rs:140:20
    |
140 |     pub fn init<F: Facade>(
    |                    ^^^^^^ required by this bound in `Renderer::init`

error[E0308]: mismatched types
   --> src/support/mod.rs:135:52
    |
135 |                     .prepare_frame(imgui.io_mut(), gl_window.window())
    |                                                    ^^^^^^^^^^^^^^^^^^ expected struct `winit::window::Window`, found struct `glium::glutin::window::Window`
    |
    = note: expected reference `&winit::window::Window`
               found reference `&glium::glutin::window::Window`
    = note: perhaps two different versions of crate `winit` are being used?

error[E0308]: mismatched types
   --> src/support/mod.rs:143:34
    |
143 |                 run_ui(&mut run, ui);
    |                                  ^^
    |                                  |
    |                                  expected `&mut Ui<'_>`, found struct `Ui`
    |                                  help: consider mutably borrowing here: `&mut ui`

error[E0308]: mismatched types
   --> src/support/mod.rs:151:41
    |
151 |                 platform.prepare_render(ui, gl_window.window());
    |                                         ^^
    |                                         |
    |                                         expected `&Ui<'_>`, found struct `Ui`
    |                                         help: consider borrowing here: `&ui`

error[E0308]: mismatched types
   --> src/support/mod.rs:151:45
    |
151 |                 platform.prepare_render(ui, gl_window.window());
    |                                             ^^^^^^^^^^^^^^^^^^ expected struct `winit::window::Window`, found struct `glium::glutin::window::Window`
    |
    = note: expected reference `&winit::window::Window`
               found reference `&glium::glutin::window::Window`
    = note: perhaps two different versions of crate `winit` are being used?

error[E0599]: no method named `render` found for struct `imgui::Context` in the current scope
   --> src/support/mod.rs:152:39
    |
152 |                 let draw_data = imgui.render();
    |                                       ^^^^^^ method not found in `imgui::Context`

error[E0277]: the trait bound `glium::Frame: imgui_glium_renderer::glium::Surface` is not satisfied
   --> src/support/mod.rs:154:22
    |
154 |                     .render(&mut target, draw_data)
    |                      ^^^^^^ the trait `imgui_glium_renderer::glium::Surface` is not implemented for `glium::Frame`

error[E0308]: mismatched types
   --> src/support/mod.rs:164:55
    |
164 |                 platform.handle_event(imgui.io_mut(), gl_window.window(), &event);
    |                                                       ^^^^^^^^^^^^^^^^^^ expected struct `winit::window::Window`, found struct `glium::glutin::window::Window`
    |
    = note: expected reference `&winit::window::Window`
               found reference `&glium::glutin::window::Window`
    = note: perhaps two different versions of crate `winit` are being used?

error[E0308]: mismatched types
   --> src/support/mod.rs:164:75
    |
164 |                 platform.handle_event(imgui.io_mut(), gl_window.window(), &event);
    |                                                                           ^^^^^^ expected enum `winit::event::Event`, found enum `Event`
    |
    = note: expected reference `&winit::event::Event<'_, _>`
               found reference `&Event<'_, ()>`
    = note: perhaps two different versions of crate `winit` are being used?

Some errors have detailed explanations: E0277, E0308, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `dearimgui` due to 10 previous errors
make: *** [build-no-checks] Error 101

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.

@RLogik RLogik changed the title Minimal Working Example Working Examples don't seem to work as stand alone code Apr 16, 2022
@dbr
Copy link
Contributor
dbr commented May 1, 2022

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

Some of the issues are down to methods being private or conflicts with the winit-crate.

A majority of the errors are indeed from conflicting winit(/glium) versions. I find it useful to check the output of cargo tree to debug such things (#638 will hopefully help simplfy things here a bit - in 0.8 we had various feature flags to support other versions of winit - now there is just a "correct version")

Some of the other errors are from using the example code from the main branch against the 0.8 crate, e.g let draw_data = imgui.render(); ... method not found in imgui::Context` is an API change between 0.8 and 0.9. Hard to avoid this, but hopefully these types of confusion will be lessened with the release of v0.9

@Pandabear314
Copy link

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.

Cargo.toml:

[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"

main.rs

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

@MazeOfEncryption
Copy link
MazeOfEncryption commented May 23, 2022

@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:
image

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?

@dbr
Copy link
Contributor
dbr commented May 23, 2022

I think you are just missing this line from the full example:

target.clear_color_srgb(1.0, 1.0, 1.0, 1.0);

(the renderer doesn't clear the buffer in case you are displaying imgui widgets over a buffer already containing a game or whatever)

@MazeOfEncryption
Copy link

@dbr This fixed it. Thank you!

I agree that it would be awesome to have some more "bare bones" examples

@deitux123
Copy link

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

@dbr
Copy link
Contributor
dbr commented Jun 2, 2022

but the imgui demo window source code translated to the rust version would help a lot

We do have the test_window_impl.rs which is exactly this (should probably rename it to imgui_demo_in_rust.rs or something more self-descriptive). It's not complete, so any PR's to improve it would be very welcome

@philipp-schmidt
Copy link

+1 to examples not being standalone.
it would be really helpful to have examples which work from scratch, e.g. cargo init into a bunch of cargo add and some copy-paste code to get started.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
0