1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::ffi;
use static_assertions::{assert_eq_align, assert_eq_size};
use std::mem::transmute;

pub use crate::ffi::{CameraMode, CameraProjection};

/// Vector2, 2x f32 components
pub type Vector2 = mint::Vector2<f32>;
assert_eq_size!(Vector2, ffi::Vector2);
assert_eq_align!(Vector2, ffi::Vector2);

impl From<Vector2> for ffi::Vector2 {
    #[inline]
    fn from(val: Vector2) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Vector2> for Vector2 {
    #[inline]
    fn from(value: ffi::Vector2) -> Self {
        unsafe { transmute(value) }
    }
}

/// Vector3, 3x f32 components
pub type Vector3 = mint::Vector3<f32>;
assert_eq_size!(Vector3, ffi::Vector3);
assert_eq_align!(Vector3, ffi::Vector3);

impl From<Vector3> for ffi::Vector3 {
    #[inline]
    fn from(val: Vector3) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Vector3> for Vector3 {
    #[inline]
    fn from(value: ffi::Vector3) -> Self {
        unsafe { transmute(value) }
    }
}

/// Vector4, 4x f32 components
pub type Vector4 = mint::Vector4<f32>;
assert_eq_size!(Vector4, ffi::Vector4);
assert_eq_align!(Vector4, ffi::Vector4);

impl From<Vector4> for ffi::Vector4 {
    #[inline]
    fn from(val: Vector4) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Vector4> for Vector4 {
    #[inline]
    fn from(value: ffi::Vector4) -> Self {
        unsafe { transmute(value) }
    }
}

/// Quaternion, 4x f32 components
pub type Quaternion = mint::Quaternion<f32>;
assert_eq_size!(Quaternion, ffi::Quaternion);
assert_eq_align!(Quaternion, ffi::Quaternion);

impl From<Quaternion> for ffi::Vector4 {
    #[inline]
    fn from(val: Quaternion) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Vector4> for Quaternion {
    #[inline]
    fn from(value: ffi::Vector4) -> Self {
        unsafe { transmute(value) }
    }
}

/// Matrix, 4x4 f32 components, column major
pub type Matrix = mint::ColumnMatrix4<f32>;
assert_eq_size!(Matrix, ffi::Matrix);
assert_eq_align!(Matrix, ffi::Matrix);

impl From<Matrix> for ffi::Matrix {
    #[inline]
    fn from(val: Matrix) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Matrix> for Matrix {
    #[inline]
    fn from(value: ffi::Matrix) -> Self {
        unsafe { transmute(value) }
    }
}

/// Rectangle, 4 components
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rectangle {
    /// Rectangle top-left corner position x
    pub x: f32,
    /// Rectangle top-left corner position y
    pub y: f32,
    /// Rectangle width
    pub width: f32,
    /// Rectangle height
    pub height: f32,
}

assert_eq_size!(Rectangle, ffi::Rectangle);
assert_eq_align!(Rectangle, ffi::Rectangle);

impl Rectangle {
    /// Create new rectangle
    #[inline]
    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }
}

impl From<Rectangle> for ffi::Rectangle {
    #[inline]
    fn from(val: Rectangle) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Rectangle> for Rectangle {
    #[inline]
    fn from(value: ffi::Rectangle) -> Self {
        unsafe { transmute(value) }
    }
}

/// Ray, ray for raycasting
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Ray {
    /// Ray position (origin)
    pub position: Vector3,
    /// Ray direction
    pub direction: Vector3,
}

assert_eq_size!(Ray, ffi::Ray);
assert_eq_align!(Ray, ffi::Ray);

impl From<Ray> for ffi::Ray {
    #[inline]
    fn from(val: Ray) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Ray> for Ray {
    #[inline]
    fn from(value: ffi::Ray) -> Self {
        unsafe { transmute(value) }
    }
}

/// RayCollision, ray hit information
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RayCollision {
    /// Did the ray hit something?
    pub hit: bool,
    /// Distance to the nearest hit
    pub distance: f32,
    /// Point of the nearest hit
    pub point: Vector3,
    /// Surface normal of hit
    pub normal: Vector3,
}

assert_eq_size!(RayCollision, ffi::RayCollision);
assert_eq_align!(RayCollision, ffi::RayCollision);

impl From<RayCollision> for ffi::RayCollision {
    #[inline]
    fn from(val: RayCollision) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::RayCollision> for RayCollision {
    #[inline]
    fn from(value: ffi::RayCollision) -> Self {
        unsafe { transmute(value) }
    }
}

/// BoundingBox
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BoundingBox {
    /// Minimum vertex box-corner
    pub min: Vector3,
    /// Maximum vertex box-corner
    pub max: Vector3,
}

assert_eq_size!(Ray, ffi::BoundingBox);
assert_eq_align!(Ray, ffi::BoundingBox);

impl From<BoundingBox> for ffi::BoundingBox {
    #[inline]
    fn from(val: BoundingBox) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::BoundingBox> for BoundingBox {
    #[inline]
    fn from(value: ffi::BoundingBox) -> Self {
        unsafe { transmute(value) }
    }
}

/// Transform, vertex transformation data
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transform {
	/// Translation
	pub translation: Vector3,
	/// Rotation
	pub rotation: Quaternion,
	/// Scale
	pub scale: Vector3,
}

assert_eq_size!(Transform, ffi::Transform);
assert_eq_align!(Transform, ffi::Transform);

impl From<Transform> for ffi::Transform {
    #[inline]
    fn from(val: Transform) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Transform> for Transform {
    #[inline]
    fn from(value: ffi::Transform) -> Self {
        unsafe { transmute(value) }
    }
}

/// Camera2D, defines position/orientation in 2d space
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Camera2D {
    /// Camera offset (displacement from target)
    pub offset: Vector2,
    /// Camera target (rotation and zoom origin)
    pub target: Vector2,
    /// Camera rotation in degrees
    pub rotation: f32,
    /// Camera zoom (scaling), should be 1.0f by default
    pub zoom: f32,
}

assert_eq_size!(Camera2D, ffi::Camera2D);
assert_eq_align!(Camera2D, ffi::Camera2D);

impl Camera2D {
    /// Get camera 2d transform matrix
    #[inline]
    pub fn get_matrix(&self) -> Matrix {
        unsafe { ffi::GetCameraMatrix2D(self.clone().into()).into() }
    }

    /// Get the world space position for a 2d camera screen space position
    #[inline]
    pub fn screen_to_world(&self, position: Vector2) -> Vector2 {
        unsafe { ffi::GetScreenToWorld2D(position.into(), self.clone().into()).into() }
    }

    /// Get the screen space position for a 2d camera world space position
    #[inline]
    pub fn world_to_screen(&self, position: Vector2) -> Vector2 {
        unsafe { ffi::GetWorldToScreen2D(position.into(), self.clone().into()).into() }
    }
}

impl From<Camera2D> for ffi::Camera2D {
    #[inline]
    fn from(val: Camera2D) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Camera2D> for Camera2D {
    #[inline]
    fn from(value: ffi::Camera2D) -> Self {
        unsafe { transmute(value) }
    }
}

/// Camera, defines position/orientation in 3d space
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Camera3D {
    /// Camera position
    pub position: Vector3,
    /// Camera target it looks-at
    pub target: Vector3,
    /// Camera up vector (rotation over its axis)
    pub up: Vector3,
    /// Camera field-of-view aperture in Y (degrees) in perspective, used as near plane width in orthographic
    pub fovy: f32,
    /// Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
    pub projection: CameraProjection,
}

assert_eq_size!(Camera3D, ffi::Camera3D);
assert_eq_align!(Camera3D, ffi::Camera3D);

impl Camera3D {
    /// Update camera position for selected mode
    #[inline]
    pub fn update(&mut self, mode: CameraMode) {
        unsafe { ffi::UpdateCamera(self as *mut Camera3D as *mut ffi::Camera3D, mode as _) }
    }

    /// Update camera movement/rotation
    #[inline]
    pub fn update_pro(&mut self, movement: Vector3, rotation: Vector3, zoom: f32) {
        unsafe {
            ffi::UpdateCameraPro(
                self as *mut Camera3D as *mut ffi::Camera3D,
                movement.into(),
                rotation.into(),
                zoom,
            )
        }
    }

    /// Get a ray trace from mouse position
    #[inline]
    pub fn get_mouse_ray(&self, mouse_position: Vector2) -> Ray {
        unsafe { ffi::GetMouseRay(mouse_position.into(), self.clone().into()).into() }
    }

    /// Get camera transform matrix (view matrix)
    #[inline]
    pub fn get_matrix(&self) -> Matrix {
        unsafe { ffi::GetCameraMatrix(self.clone().into()).into() }
    }

    /// Get the screen space position for a 3d world space position
    #[inline]
    pub fn world_to_screen(&self, position: Vector3) -> Vector2 {
        unsafe { ffi::GetWorldToScreen(position.into(), self.clone().into()).into() }
    }

    /// Get size position for a 3d world space position
    #[inline]
    pub fn world_to_screen_ex(&self, position: Vector3, width: u32, height: u32) -> Vector2 {
        unsafe {
            ffi::GetWorldToScreenEx(
                position.into(),
                self.clone().into(),
                width as _,
                height as _,
            )
            .into()
        }
    }
}

impl From<Camera3D> for ffi::Camera3D {
    #[inline]
    fn from(val: Camera3D) -> Self {
        unsafe { transmute(val) }
    }
}

impl From<ffi::Camera3D> for Camera3D {
    #[inline]
    fn from(value: ffi::Camera3D) -> Self {
        if value.projection != CameraProjection::Orthographic as i32
            && value.projection != CameraProjection::Perspective as i32
        {
            panic!(
                "Attempted to convert a ffi::Camera3D with an invalid projection value: {}",
                value.projection
            );
        }

        unsafe { transmute(value) }
    }
}

/// Camera type fallback, defaults to Camera3D
pub type Camera = Camera3D;