QuantumBadger / Speedy2D

Rust library for hardware accelerated drawing of 2D shapes, images, and text, with an easy to use API.
Apache License 2.0
387 stars 41 forks source link

[Question] Interface with other UI packages #25

Open GDelevoye opened 3 years ago

GDelevoye commented 3 years ago

Hi,

Thank you for your package, which looks exactly like what I was looking for, in terms of simplicity and code design

I'm a somewhat experienced programmer, but I'm quite new in rust and also, very new to graphical programming. So my question will be really naive : Can I create widgets with another library (e.g druid, conrod fltk-rs etc), and then link them to a window created with Speedy2D ? And if yes, do you know if there is one extern library in particular that could be easier to use with Speedy2D ? Preferably in full rust ?

I don't need sophisticated widgets (see picture)

image-asset

wyhinton commented 3 years ago

Yes you can. I didn't add any buttons in my "Speedy2d Canvas" example, but it doing so would be really easy. Any UI library that provides a gl context should can control speedy2d via its widgets.

GDelevoye commented 3 years ago

Thank you very much @wyhinton for you answer :)

So, if I understand well, I could use Speedy2D with, say, egui for instance (https://github.com/emilk/egui), is that right ? Do you suggest that speedy2d uses an external openGL context, or rather that the UI library uses Speedy2D's context ?

Concerning the canvas you're talking about, is speedy2d::GLRenderer an interchangeable instance with any other instance of any OpenGL renderer from any other library ? I'm a bit confused on this part, I have only a very vague idea of what an openGL context is


Also if the canvas is yours, perhaps you'd like to be warned that it compiles well (once the cairo dev libraries are installed) but it panics:

guillaume@A320MA:~/GitHub/FLTK-RS-Examples/speedy2d$ RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/speedy2d_scroll_zoom_canvas`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:118:64
stack backtrace:
   0: rust_begin_unwind
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/panicking.rs:92:14
   2: core::option::expect_none_failed
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/option.rs:1300:5
   3: core::result::Result<T,E>::unwrap
             at /home/guillaume/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:1037:23
   4: speedy2d_scroll_zoom_canvas::CanvasImage::new
             at ./src/main.rs:118:40
   5: speedy2d_scroll_zoom_canvas::main
             at ./src/main.rs:259:22
   6: core::ops::function::FnOnce::call_once
             at /home/guillaume/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

This is due to the picture extensions being PNG and JPG instead of png and jpg in lowercases. I fixed it and it works now:

guillaume@A320MA:~/GitHub/FLTK-RS-Examples/speedy2d/imgs$ pwd
/home/guillaume/GitHub/FLTK-RS-Examples/speedy2d/imgs
guillaume@A320MA:~/GitHub/FLTK-RS-Examples/speedy2d/imgs$ ll
total 2820
drwxrwxr-x 2 guillaume guillaume    4096 avril 26 22:06 ./
drwxrwxr-x 6 guillaume guillaume    4096 avril 26 21:48 ../
-rw-rw-r-- 1 guillaume guillaume  138539 avril 26 21:48 2.jpg
-rw-rw-r-- 1 guillaume guillaume  449759 avril 26 21:48 chrome.jpg
-rw-rw-r-- 1 guillaume guillaume  488123 avril 26 21:48 forest.jpg
-rw-rw-r-- 1 guillaume guillaume 1726330 avril 26 21:48 IMG_0308.jpeg
-rw-rw-r-- 1 guillaume guillaume   60974 avril 26 21:48 rust-logo.png
-rw-rw-r-- 1 guillaume guillaume    5312 avril 26 21:48 smiley.png
QuantumBadger commented 3 years ago

@GDelevoye Thanks for the questions! Speedy2D needs a dedicated OpenGL context that isn't used by any other libraries. This is because a context contains global configuration, so multiple libraries using the same context would conflict with each other.

There are a few ways to combine Speedy2D with a UI library:

Each thread in a program can have one OpenGL context "globally active" at a time, so if you're using multiple contexts in a single thread, you'd need to manually switch between them.

To make use of egui, it sounds like you'd need to create an integration:

https://github.com/emilk/egui#integrations

This would mean that draw commands from egui were sent to Speedy2D, and input events from Speedy2D were sent to egui.

GDelevoye commented 3 years ago

Ok ! I think it's much clearer now

I'll give a try like this, and maybe I'll come back to you if I have other questions but it looks like a cool start :) Thank you very much