2021-04-07 09:47:05 +00:00
|
|
|
//! pixel-cell example
|
2021-04-05 21:55:24 +00:00
|
|
|
//!
|
2021-04-06 10:30:32 +00:00
|
|
|
//! Shows how to get the size of a cell in pixels
|
|
|
|
//!
|
|
|
|
//! It works on the following terminals:
|
|
|
|
//! - kitty
|
|
|
|
//! - xterm (invoked with `xterm -ti vt340`)
|
2021-06-08 19:18:58 +00:00
|
|
|
//! - alacritty (WIP: https://github.com/ayosec/alacritty/tree/graphics)
|
2021-04-05 21:55:24 +00:00
|
|
|
|
|
|
|
use rand::{distributions::Uniform, Rng};
|
|
|
|
|
2021-04-06 10:30:32 +00:00
|
|
|
use libnotcurses_sys::*;
|
|
|
|
|
2021-04-05 21:55:24 +00:00
|
|
|
fn main() -> NcResult<()> {
|
2021-06-15 19:00:52 +00:00
|
|
|
let mut nc = Nc::new()?;
|
2021-04-05 21:55:24 +00:00
|
|
|
|
|
|
|
if !nc.check_pixel_support()? {
|
|
|
|
return Err(NcError::new_msg("Current terminal doesn't support pixels."));
|
|
|
|
}
|
|
|
|
|
2021-04-17 20:13:43 +00:00
|
|
|
let mut stdplane = nc.stdplane();
|
|
|
|
let pg = stdplane.pixelgeom();
|
2021-04-05 21:55:24 +00:00
|
|
|
|
|
|
|
// print visual delimiters around our pixelized cell
|
2021-04-06 10:30:32 +00:00
|
|
|
println!("0▗│▖\n│─ ─\n2▝│▘");
|
2021-04-17 20:13:43 +00:00
|
|
|
println!("a cell is {}x{} pixels", pg.cell_y, pg.cell_x);
|
2021-06-13 20:39:11 +00:00
|
|
|
println!("\ninterpolated not-interpolated interpolated not-interpolated");
|
|
|
|
println!(" SCALE SCALE RESIZE RESIZE");
|
2021-04-05 21:55:24 +00:00
|
|
|
|
|
|
|
// fill the buffer with random color pixels
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let range = Uniform::from(50..=180);
|
2021-04-17 20:13:43 +00:00
|
|
|
let mut buffer = Vec::<u8>::with_capacity((pg.cell_y * pg.cell_x * 4) as usize);
|
2021-04-05 21:55:24 +00:00
|
|
|
#[allow(unused_parens)]
|
2021-04-17 20:13:43 +00:00
|
|
|
for _byte in (0..={ pg.cell_y * pg.cell_x }) {
|
2021-04-05 21:55:24 +00:00
|
|
|
buffer.push(rng.sample(&range));
|
|
|
|
buffer.push(rng.sample(&range));
|
|
|
|
buffer.push(rng.sample(&range));
|
|
|
|
buffer.push(255);
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:30:32 +00:00
|
|
|
// show the newly created ncvisual delimited with the box drawing characters
|
2021-06-08 19:18:58 +00:00
|
|
|
let v1 = NcVisual::from_rgba(buffer.as_slice(), pg.cell_y, pg.cell_x * 4, pg.cell_x)?;
|
2021-04-17 20:13:43 +00:00
|
|
|
let voptions =
|
|
|
|
NcVisualOptions::without_plane(1, 2, 0, 0, pg.cell_y, pg.cell_x, NCBLIT_PIXEL, 0, 0);
|
2021-06-08 19:18:58 +00:00
|
|
|
v1.render(&mut nc, &voptions)?;
|
2021-04-18 09:33:58 +00:00
|
|
|
rsleep![&mut nc, 1];
|
2021-04-05 21:55:24 +00:00
|
|
|
|
2021-06-08 19:18:58 +00:00
|
|
|
// show the ncvisual, scaled with interpolated values
|
|
|
|
let mut vplane2 = NcPlane::new_bound(&mut stdplane, 7, 4, 5, 4)?;
|
2021-04-17 20:13:43 +00:00
|
|
|
let voptions2 = NcVisualOptions::with_plane(
|
|
|
|
&mut vplane2,
|
|
|
|
NCSCALE_SCALE,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
pg.cell_y,
|
|
|
|
pg.cell_x,
|
|
|
|
NCBLIT_PIXEL,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
);
|
2021-06-08 19:18:58 +00:00
|
|
|
v1.render(&mut nc, &voptions2)?;
|
2021-04-18 09:33:58 +00:00
|
|
|
rsleep![&mut nc, 1];
|
|
|
|
|
2021-06-08 19:18:58 +00:00
|
|
|
// show the ncvisual, scaled without using interpolation
|
|
|
|
let mut vplane3 = NcPlane::new_bound(&mut stdplane, 7, 19, 5, 4)?;
|
|
|
|
let voptions3 = NcVisualOptions::with_plane(
|
|
|
|
&mut vplane3,
|
|
|
|
NCSCALE_SCALE,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
pg.cell_y,
|
|
|
|
pg.cell_x,
|
|
|
|
NCBLIT_PIXEL,
|
|
|
|
NCVISUAL_OPTION_NOINTERPOLATE,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
v1.render(&mut nc, &voptions3)?;
|
|
|
|
rsleep![&mut nc, 1];
|
|
|
|
|
2021-06-13 20:39:11 +00:00
|
|
|
// resize the ncvisual (doesn't use interpolation)
|
2021-06-08 19:18:58 +00:00
|
|
|
let voptions4 =
|
2021-06-13 20:39:11 +00:00
|
|
|
NcVisualOptions::without_plane(7, 37, 0, 0, pg.cell_y, pg.cell_x, NCBLIT_PIXEL, 0, 0);
|
2021-06-13 11:07:00 +00:00
|
|
|
v1.resize_noninterpolative(pg.cell_y * 4, pg.cell_x * 4)?;
|
2021-06-08 19:18:58 +00:00
|
|
|
v1.render(&mut nc, &voptions4)?;
|
|
|
|
rsleep![&mut nc, 1];
|
|
|
|
|
|
|
|
// resize the ncvisual (uses interpolation)
|
|
|
|
let v5 = NcVisual::from_rgba(buffer.as_slice(), pg.cell_y, pg.cell_x * 4, pg.cell_x)?;
|
|
|
|
let voptions5 =
|
2021-06-13 20:39:11 +00:00
|
|
|
NcVisualOptions::without_plane(7, 51, 0, 0, pg.cell_y, pg.cell_x, NCBLIT_PIXEL, 0, 0);
|
2021-06-08 19:18:58 +00:00
|
|
|
v5.resize(18 * 4, 9 * 4)?; // FIXME: render function fails when downsizing (y<18 | x<9)
|
|
|
|
v5.render(&mut nc, &voptions5)?;
|
2021-04-18 11:56:36 +00:00
|
|
|
rsleep![&mut nc, 1];
|
|
|
|
|
2021-06-13 20:39:11 +00:00
|
|
|
sleep![2];
|
2021-04-05 21:55:24 +00:00
|
|
|
|
2021-06-08 19:18:58 +00:00
|
|
|
v1.destroy();
|
|
|
|
v5.destroy();
|
2021-05-05 17:33:00 +00:00
|
|
|
nc.stop()?;
|
2021-04-05 21:55:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|