From e22232774da538d205cfc9b67950ab45fafd3df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Wed, 12 Aug 2020 17:53:50 +0200 Subject: [PATCH] rust: add direct-image example --- rust/.cargo/config.toml | 2 ++ rust/examples/direct-image.png | Bin 0 -> 435 bytes rust/examples/direct-image.rs | 36 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 rust/examples/direct-image.png create mode 100644 rust/examples/direct-image.rs diff --git a/rust/.cargo/config.toml b/rust/.cargo/config.toml index ad5116026..bfd698fcf 100644 --- a/rust/.cargo/config.toml +++ b/rust/.cargo/config.toml @@ -2,6 +2,8 @@ d = "doc --no-deps" do = "doc --no-deps --open" +re = "run --example" + # TEST # fix IO errors: https://github.com/dankamongmen/notcurses/issues/766 t = "test -- --test-threads 1 --nocapture" diff --git a/rust/examples/direct-image.png b/rust/examples/direct-image.png new file mode 100644 index 0000000000000000000000000000000000000000..d1ff75f51ba6078cddb9e52c54c3a4a951f0fedd GIT binary patch literal 435 zcmV;k0ZjghP))#tTWWG0y@{gw3^g))?%0B=pa=ubCvj|=T%id{P%Dq-g_k4t@qIq~@7PplYh8X2xc_6?S6a8L;p@Fg$ib@eWc0 dQag;2G5|`NeZu-x_{abN002ovPDHLkV1iak(q8}o literal 0 HcmV?d00001 diff --git a/rust/examples/direct-image.rs b/rust/examples/direct-image.rs new file mode 100644 index 000000000..dbde19ab7 --- /dev/null +++ b/rust/examples/direct-image.rs @@ -0,0 +1,36 @@ +use std::ffi::CString; + +use libnotcurses_sys as nc; + +extern "C" { + fn libc_stdout() -> *mut nc::_IO_FILE; +} + +fn main() { + unsafe { + let _ = libc::setlocale(libc::LC_ALL, CString::new("").unwrap().as_ptr()); + let ncd: *mut nc::ncdirect = nc::ncdirect_init(std::ptr::null(), libc_stdout()); + + render_image(&mut *ncd, nc::ncblitter_e_NCBLIT_1x1); + render_image(&mut *ncd, nc::ncblitter_e_NCBLIT_2x1); + render_image(&mut *ncd, nc::ncblitter_e_NCBLIT_BRAILLE); + + nc::ncdirect_stop(ncd); + } +} + +fn render_image(ncd: &mut nc::ncdirect, blit: nc::ncblitter_e) { + unsafe { + if nc::ncdirect_render_image( + ncd, + CString::new("direct-image.png").unwrap().as_ptr(), + nc::ncalign_e_NCALIGN_CENTER, + blit, + nc::ncscale_e_NCSCALE_NONE, + ) != 0 + { + panic!("ERR: ncdirect_render_image. \ + Make sure you are running this example from the examples folder"); + } + } +}