rust: impl ncplane_as_rgba; refactor macros

- rename and refactor NcPlane method rgba() to as_rgba().
- add example `issue-rgba` that shows a problem when the plane has text.
- refactor `error_ref` & `error_ref_mut` macros to allow custom return values
pull/1516/head
joseLuís 4 years ago
parent 05082fc277
commit 60c874f835

@ -0,0 +1,20 @@
use libnotcurses_sys::*;
fn main() -> NcResult<()> {
let mut nc = FullMode::new()?;
let stdplane = nc.stdplane();
let plane = NcPlane::new_bound(stdplane, 0, 0, 2, 5)?;
plane.set_base("·", 0, NcChannelPair::with_rgb(0x224411, 0x229922))?;
// BUG FIXME: uncommenting this makes next as_rgba() call fail
// plane.putstr("PLANE")?;
rsleep![&mut nc, 0, 500];
let rgba = plane.as_rgba(NCBLIT_1x1, 0, 0, None, None)?;
println!("\n\n array: {:#?}", rgba);
rsleep![&mut nc, 3];
Ok(())
}

@ -395,7 +395,7 @@ pub use ffi::{
ncplane_resize_maximize,
ncplane_resize_realign,
ncplane_resizecb,
ncplane_rgba,
ncplane_as_rgba,
ncplane_rotate_ccw,
ncplane_rotate_cw,
ncplane_set_base,

@ -145,16 +145,19 @@ macro_rules! error {
/// `$msg` is optional. By default it will be an empty `&str` `""`.
#[macro_export]
macro_rules! error_ref {
($ptr:expr, $msg:expr) => {
($ptr:expr, $msg:expr, $ok:expr) => {
if $ptr != core::ptr::null() {
#[allow(unused_unsafe)]
return Ok(unsafe { &*$ptr });
return Ok(unsafe { $ok });
} else {
return Err(crate::NcError::with_msg(crate::NCRESULT_ERR, $msg));
}
};
($ptr:expr, $msg:expr) => {
error_ref![$ptr, $msg, &*$ptr ];
};
($ptr:expr) => {
error_ref![$ptr, ""];
error_ref![$ptr, "", &*$ptr ];
};
}
@ -168,16 +171,19 @@ macro_rules! error_ref {
/// `$msg` is optional. By default it will be an empty `&str` `""`.
#[macro_export]
macro_rules! error_ref_mut {
($ptr:expr, $msg:expr) => {
($ptr:expr, $msg:expr, $ok:expr) => {
if $ptr != core::ptr::null_mut() {
#[allow(unused_unsafe)]
return Ok(unsafe { &mut *$ptr });
return Ok(unsafe { $ok });
} else {
return Err(crate::NcError::with_msg(crate::NCRESULT_ERR, $msg));
}
};
($ptr:expr, $msg:expr) => {
error_ref_mut![$ptr, $msg, &mut *$ptr ];
};
($ptr:expr) => {
error_ref_mut![$ptr, ""];
error_ref_mut![$ptr, "", &mut *$ptr ];
};
}

@ -1407,7 +1407,7 @@ impl NcPlane {
]
}
/// Creates an RGBA flat array from the selected region of the ncplane.
/// Creates an RGBA flat array from the selected region of the plane.
///
/// Starts at the plane's `beg_y`x`beg_x` coordinate (which must lie on the
/// plane), continuing for `len_y`x`len_x` cells.
@ -1417,8 +1417,8 @@ impl NcPlane {
///
/// Only glyphs from the specified blitset may be present.
///
/// *C style function: [ncplane_rgba()][crate::ncplane_rgba].*
pub fn rgba(
/// *C style function: [ncplane_rgba()][crate::ncplane_as_rgba].*
pub fn as_rgba(
&mut self,
blitter: NcBlitter,
beg_y: NcDim,
@ -1440,31 +1440,19 @@ impl NcPlane {
len_x2 = -1;
}
// pixel geometry
let mut pxdimy = 0;
let mut pxdimx = 0;
let res_array = unsafe {
crate::ncplane_rgba(self, blitter, beg_y as i32, beg_x as i32, len_y2, len_x2)
crate::ncplane_as_rgba(self, blitter, beg_y as i32, beg_x as i32, len_y2, len_x2, &mut pxdimy, &mut pxdimx)
};
// calculates array length
let array_len_y;
let array_len_x;
if len_y2 == -1 {
array_len_y = self.dim_y() - beg_y;
} else {
array_len_y = len_y2 as u32;
}
if len_x2 == -1 {
array_len_x = self.dim_x() - beg_x;
} else {
array_len_x = len_x2 as u32;
}
let array_len = (array_len_y * array_len_x) as usize;
// returns the result
if res_array != null_mut() {
return Ok(unsafe { from_raw_parts_mut(res_array, array_len) });
} else {
Err(NcError::with_msg(NCRESULT_ERR, "NcPlane.rgba()"))
}
error_ref_mut![
res_array,
&format!["NcPlane.rgba({}, {}, {}, {:?}, {:?})", blitter, beg_y, beg_x, len_y, len_x],
from_raw_parts_mut(res_array, (pxdimy * pxdimx) as usize)
]
}
/// Realigns this NcPlane against its parent, using the alignment specified

Loading…
Cancel
Save