classicube_sys/
model.rs

1use crate::{bindings::*, std_types::c_float};
2
3#[allow(clippy::unnecessary_cast)]
4pub const MODEL_BOX_VERTICES: u32 = FACE_CONSTS_FACE_COUNT as u32 * MODEL_QUAD_VERTICES as u32;
5
6#[macro_export]
7macro_rules! BoxDesc_Dim {
8    ($p1:expr, $p2:expr) => {
9        {
10            if $p1 < $p2 {
11                $p2 - $p1
12            } else {
13                $p1 - $p2
14            }
15        } as u8
16    };
17}
18
19// Macros for making initialising a BoxDesc easier to understand. See Model.c for how these get used.
20
21#[macro_export]
22macro_rules! BoxDesc_Tex {
23    ($x:expr, $y:expr) => {
24        ($x, $y)
25    };
26}
27
28/// gives (x, y, z)
29#[macro_export]
30macro_rules! BoxDesc_Dims {
31    ($x1:expr, $y1:expr, $z1:expr, $x2:expr, $y2:expr, $z2:expr) => {
32        (
33            $crate::BoxDesc_Dim!($x1, $x2),
34            $crate::BoxDesc_Dim!($y1, $y2),
35            $crate::BoxDesc_Dim!($z1, $z2),
36        )
37    };
38}
39
40/// gives (x1, y1, z1, x2, y2, z2)
41#[cfg(feature = "no_std")]
42#[macro_export]
43macro_rules! BoxDesc_Bounds {
44    ($x1:expr, $y1:expr, $z1:expr, $x2:expr, $y2:expr, $z2:expr) => {
45        (
46            $x1 as ::libc::c_float / 16.0,
47            $y1 as ::libc::c_float / 16.0,
48            $z1 as ::libc::c_float / 16.0,
49            $x2 as ::libc::c_float / 16.0,
50            $y2 as ::libc::c_float / 16.0,
51            $z2 as ::libc::c_float / 16.0,
52        )
53    };
54}
55#[cfg(not(feature = "no_std"))]
56#[macro_export]
57macro_rules! BoxDesc_Bounds {
58    ($x1:expr, $y1:expr, $z1:expr, $x2:expr, $y2:expr, $z2:expr) => {
59        (
60            $x1 as ::std::os::raw::c_float / 16.0,
61            $y1 as ::std::os::raw::c_float / 16.0,
62            $z1 as ::std::os::raw::c_float / 16.0,
63            $x2 as ::std::os::raw::c_float / 16.0,
64            $y2 as ::std::os::raw::c_float / 16.0,
65            $z2 as ::std::os::raw::c_float / 16.0,
66        )
67    };
68}
69
70/// gives (x, y, z)
71#[macro_export]
72macro_rules! BoxDesc_Rot {
73    ($x:expr, $y:expr, $z:expr) => {
74        ($x / 16.0, $y / 16.0, $z / 16.0)
75    };
76}
77
78/// gives ((x, y, z), (x1, y1, z1, x2, y2, z2))
79#[macro_export]
80macro_rules! BoxDesc_Box {
81    ($x1:expr, $y1:expr, $z1:expr, $x2:expr, $y2:expr, $z2:expr) => {
82        (
83            $crate::BoxDesc_Dims!($x1, $y1, $z1, $x2, $y2, $z2),
84            $crate::BoxDesc_Bounds!($x1, $y1, $z1, $x2, $y2, $z2),
85        )
86    };
87}
88
89type BoxDescDimsReturn = (u8, u8, u8);
90type BoxDescBoundsReturn = (c_float, c_float, c_float, c_float, c_float, c_float);
91impl BoxDesc {
92    pub fn from_macros(
93        (texX, texY): (u16, u16),
94        ((sizeX, sizeY, sizeZ), (x1, y1, z1, x2, y2, z2)): (BoxDescDimsReturn, BoxDescBoundsReturn),
95    ) -> Self {
96        Self {
97            texX,
98            texY,
99            sizeX,
100            sizeY,
101            sizeZ,
102            x1,
103            y1,
104            z1,
105            x2,
106            y2,
107            z2,
108            rotX: 0.0,
109            rotY: 0.0,
110            rotZ: 0.0,
111        }
112    }
113}
114
115#[macro_export]
116macro_rules! Model_RetSize {
117    ($e:expr, $x:expr, $y:expr, $z:expr) => {
118        static P: $crate::Vec3 = $crate::Vec3::new(
119            $x/16.0,
120            $y/16.0,
121            $z/16.0
122        );
123        $e.Size = P;
124    };
125}
126
127#[macro_export]
128macro_rules! Model_RetAABB {
129    ($e:expr, $x1:expr, $y1:expr, $z1:expr, $x2:expr, $y2:expr, $z2:expr) => {
130        static BB: $crate::AABB = $crate::AABB {
131            Min: $crate::Vec3::new($x1 / 16.0, $y1 / 16.0, $z1 / 16.0),
132            Max: $crate::Vec3::new($x2 / 16.0, $y2 / 16.0, $z2 / 16.0),
133        };
134        $e.ModelAABB = BB;
135    };
136}
137
138#[test]
139fn test_model_macros() {
140    fn BoxDesc_BuildBox(_part: *mut ModelPart, desc: *const BoxDesc) {
141        #[cfg(not(feature = "no_std"))]
142        unsafe {
143            println!("{:#?}", *desc);
144        }
145    }
146
147    let mut part: ModelPart = unsafe { core::mem::zeroed() };
148    BoxDesc_BuildBox(
149        &mut part,
150        &BoxDesc::from_macros(BoxDesc_Tex!(0, 16), BoxDesc_Box!(-3, 1, -3, 3, 7, 3)),
151    );
152
153    let mut e: Entity = unsafe { core::mem::zeroed() };
154    Model_RetSize!(e, 0.0, 0.0, 0.0);
155
156    let mut e: Entity = unsafe { core::mem::zeroed() };
157    Model_RetAABB!(e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
158}
159
160pub fn ModelPart_Init(
161    part: &mut ModelPart,
162    offset: cc_uint16,
163    count: cc_uint16,
164    rotX: c_float,
165    rotY: c_float,
166    rotZ: c_float,
167) {
168    part.offset = offset;
169    part.count = count;
170    part.rotX = rotX;
171    part.rotY = rotY;
172    part.rotZ = rotZ;
173}