From 44ed48d2eb82fc52679bec9f4fbeb17e22581b7d Mon Sep 17 00:00:00 2001 From: Natasha England-Elbro Date: Thu, 1 Feb 2024 20:59:37 +0000 Subject: [PATCH] feat: API for IVec --- citro3d/src/math.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/citro3d/src/math.rs b/citro3d/src/math.rs index 949bb42..fcc04c3 100644 --- a/citro3d/src/math.rs +++ b/citro3d/src/math.rs @@ -16,9 +16,49 @@ pub use projection::{ }; /// A 4-vector of `u8`s. +/// +/// # Layout +/// Uses the PICA layout of WZYX #[doc(alias = "C3D_IVec")] +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq, Eq)] pub struct IVec(citro3d_sys::C3D_IVec); +impl IVec { + pub fn new(x: u8, y: u8, z: u8, w: u8) -> Self { + Self(unsafe { citro3d_sys::IVec_Pack(x, y, z, w) }) + } + pub fn as_raw(&self) -> &citro3d_sys::C3D_IVec { + &self.0 + } + pub fn x(self) -> u8 { + self.0 as u8 + } + pub fn y(self) -> u8 { + (self.0 >> 8) as u8 + } + pub fn z(self) -> u8 { + (self.0 >> 16) as u8 + } + pub fn w(self) -> u8 { + (self.0 >> 24) as u8 + } +} + /// A quaternion, internally represented the same way as [`FVec`]. #[doc(alias = "C3D_FQuat")] pub struct FQuat(citro3d_sys::C3D_FQuat); + +#[cfg(test)] +mod tests { + use super::IVec; + + #[test] + fn ivec_getters_work() { + let iv = IVec::new(1, 2, 3, 4); + assert_eq!(iv.x(), 1); + assert_eq!(iv.y(), 2); + assert_eq!(iv.z(), 3); + assert_eq!(iv.w(), 4); + } +}