raylib-sys 6.0.0

Raw FFI bindings for Raylib
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use crate::{Matrix, Quaternion, Vector3};

// ─── Matrix ──────────────────────────────────────────────────────────────────

impl Matrix {
    /// Identity matrix. (raymath `MatrixIdentity`)
    #[inline]
    #[must_use]
    pub fn identity() -> Self {
        // SAFETY: MatrixIdentity is a pure value-out raymath fn; no preconditions.
        unsafe { crate::MatrixIdentity() }
    }

    /// Matrix determinant. (raymath `MatrixDeterminant`)
    #[inline]
    #[must_use]
    pub fn determinant(self) -> f32 {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixDeterminant(self) }
    }

    /// Trace of the matrix (sum of diagonal values). (raymath `MatrixTrace`)
    #[inline]
    #[must_use]
    pub fn trace(self) -> f32 {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixTrace(self) }
    }

    /// Transpose matrix. (raymath `MatrixTranspose`)
    #[inline]
    #[must_use]
    pub fn transpose(self) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixTranspose(self) }
    }

    /// Invert matrix. (raymath `MatrixInvert`)
    #[inline]
    #[must_use]
    pub fn invert(self) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixInvert(self) }
    }

    /// Multiply matrix by scalar. (raymath `MatrixMultiplyValue`)
    #[inline]
    #[must_use]
    pub fn mul_value(self, value: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixMultiplyValue(self, value) }
    }

    /// Translate matrix. (raymath `MatrixTranslate`)
    #[inline]
    #[must_use]
    pub fn translate(x: f32, y: f32, z: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixTranslate(x, y, z) }
    }

    /// Rotate matrix by axis and angle (radians). (raymath `MatrixRotate`)
    #[inline]
    #[must_use]
    pub fn rotate(axis: Vector3, angle: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixRotate(axis, angle) }
    }

    /// Rotate around X axis (radians). (raymath `MatrixRotateX`)
    #[inline]
    #[must_use]
    pub fn rotate_x(angle: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixRotateX(angle) }
    }

    /// Rotate around Y axis (radians). (raymath `MatrixRotateY`)
    #[inline]
    #[must_use]
    pub fn rotate_y(angle: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixRotateY(angle) }
    }

    /// Rotate around Z axis (radians). (raymath `MatrixRotateZ`)
    #[inline]
    #[must_use]
    pub fn rotate_z(angle: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixRotateZ(angle) }
    }

    /// Rotate around XYZ axes (radians, intrinsic order). (raymath `MatrixRotateXYZ`)
    #[inline]
    #[must_use]
    pub fn rotate_xyz(angle: Vector3) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixRotateXYZ(angle) }
    }

    /// Rotate around ZYX axes (radians, intrinsic order). (raymath `MatrixRotateZYX`)
    #[inline]
    #[must_use]
    pub fn rotate_zyx(angle: Vector3) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixRotateZYX(angle) }
    }

    /// Scale matrix. (raymath `MatrixScale`)
    #[inline]
    #[must_use]
    pub fn scale(x: f32, y: f32, z: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixScale(x, y, z) }
    }

    /// Frustum projection matrix. (raymath `MatrixFrustum`)
    #[inline]
    #[must_use]
    pub fn frustum(left: f64, right: f64, bottom: f64, top: f64, near: f64, far: f64) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixFrustum(left, right, bottom, top, near, far) }
    }

    /// Perspective projection matrix. (raymath `MatrixPerspective`)
    #[inline]
    #[must_use]
    pub fn perspective(fov_y: f64, aspect: f64, near: f64, far: f64) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixPerspective(fov_y, aspect, near, far) }
    }

    /// Orthographic projection matrix. (raymath `MatrixOrtho`)
    #[inline]
    #[must_use]
    pub fn ortho(left: f64, right: f64, bottom: f64, top: f64, near: f64, far: f64) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixOrtho(left, right, bottom, top, near, far) }
    }

    /// Look-at view matrix. (raymath `MatrixLookAt`)
    #[inline]
    #[must_use]
    pub fn look_at(eye: Vector3, target: Vector3, up: Vector3) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixLookAt(eye, target, up) }
    }

    /// Convert matrix to float array. (raymath `MatrixToFloatV`)
    #[inline]
    #[must_use]
    pub fn to_float_array(self) -> crate::float16 {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixToFloatV(self) }
    }

    /// Compose a matrix from translation, rotation quaternion, and scale. (raymath `MatrixCompose`)
    #[inline]
    #[must_use]
    pub fn compose(translation: Vector3, rotation: Quaternion, scale: Vector3) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::MatrixCompose(translation, rotation, scale) }
    }
}

impl core::ops::Mul for Matrix {
    type Output = Matrix;
    #[inline]
    fn mul(self, rhs: Matrix) -> Matrix {
        // SAFETY: MatrixMultiply is a pure value-in/out raymath fn; no preconditions.
        unsafe { crate::MatrixMultiply(self, rhs) }
    }
}
impl core::ops::MulAssign for Matrix {
    #[inline]
    fn mul_assign(&mut self, rhs: Matrix) {
        *self = *self * rhs;
    }
}

impl core::ops::Add for Matrix {
    type Output = Matrix;
    #[inline]
    fn add(self, rhs: Matrix) -> Matrix {
        // SAFETY: MatrixAdd is a pure value-in/out raymath fn; no preconditions.
        unsafe { crate::MatrixAdd(self, rhs) }
    }
}
impl core::ops::AddAssign for Matrix {
    #[inline]
    fn add_assign(&mut self, rhs: Matrix) {
        *self = *self + rhs;
    }
}

impl core::ops::Sub for Matrix {
    type Output = Matrix;
    #[inline]
    fn sub(self, rhs: Matrix) -> Matrix {
        // SAFETY: MatrixSubtract is a pure value-in/out raymath fn; no preconditions.
        unsafe { crate::MatrixSubtract(self, rhs) }
    }
}
impl core::ops::SubAssign for Matrix {
    #[inline]
    fn sub_assign(&mut self, rhs: Matrix) {
        *self = *self - rhs;
    }
}

/// Decompose a matrix into translation, rotation quaternion, and scale.
/// (raymath `MatrixDecompose`)
pub fn matrix_decompose(mat: Matrix) -> (Vector3, Quaternion, Vector3) {
    let mut translation = Vector3 {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    };
    let mut rotation = Quaternion {
        x: 0.0,
        y: 0.0,
        z: 0.0,
        w: 0.0,
    };
    let mut scale = Vector3 {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    };
    // SAFETY: MatrixDecompose writes through non-null, properly aligned out-ptrs;
    // all three are distinct stack allocations with no aliasing.
    unsafe { crate::MatrixDecompose(mat, &mut translation, &mut rotation, &mut scale) };
    (translation, rotation, scale)
}

// ─── Quaternion ──────────────────────────────────────────────────────────────

impl Quaternion {
    /// Identity quaternion (0, 0, 0, 1). (raymath `QuaternionIdentity`)
    #[inline]
    #[must_use]
    pub fn identity() -> Self {
        // SAFETY: QuaternionIdentity is a pure value-out raymath fn; no preconditions.
        unsafe { crate::QuaternionIdentity() }
    }

    /// Add scalar to each component. (raymath `QuaternionAddValue`)
    #[inline]
    #[must_use]
    pub fn add_value(self, add: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionAddValue(self, add) }
    }

    /// Subtract scalar from each component. (raymath `QuaternionSubtractValue`)
    #[inline]
    #[must_use]
    pub fn sub_value(self, sub: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionSubtractValue(self, sub) }
    }

    /// Quaternion length. (raymath `QuaternionLength`)
    #[inline]
    #[must_use]
    pub fn length(self) -> f32 {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionLength(self) }
    }

    /// Normalize quaternion. (raymath `QuaternionNormalize`)
    #[inline]
    #[must_use]
    pub fn normalize(self) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionNormalize(self) }
    }

    /// Invert quaternion. (raymath `QuaternionInvert`)
    #[inline]
    #[must_use]
    pub fn invert(self) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionInvert(self) }
    }

    /// Linear interpolation. (raymath `QuaternionLerp`)
    #[inline]
    #[must_use]
    pub fn lerp(self, other: Quaternion, amount: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionLerp(self, other, amount) }
    }

    /// Normalized linear interpolation. (raymath `QuaternionNlerp`)
    #[inline]
    #[must_use]
    pub fn nlerp(self, other: Quaternion, amount: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionNlerp(self, other, amount) }
    }

    /// Spherical linear interpolation. (raymath `QuaternionSlerp`)
    #[inline]
    #[must_use]
    pub fn slerp(self, other: Quaternion, amount: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionSlerp(self, other, amount) }
    }

    /// Cubic Hermite spline interpolation. (raymath `QuaternionCubicHermiteSpline`)
    #[inline]
    #[must_use]
    pub fn cubic_hermite_spline(
        self,
        out_tangent1: Quaternion,
        q2: Quaternion,
        in_tangent2: Quaternion,
        t: f32,
    ) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionCubicHermiteSpline(self, out_tangent1, q2, in_tangent2, t) }
    }

    /// Quaternion for rotation from one vector to another. (raymath `QuaternionFromVector3ToVector3`)
    #[inline]
    #[must_use]
    pub fn from_vector3_to_vector3(from: Vector3, to: Vector3) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionFromVector3ToVector3(from, to) }
    }

    /// Quaternion from rotation matrix. (raymath `QuaternionFromMatrix`)
    #[inline]
    #[must_use]
    pub fn from_matrix(mat: Matrix) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionFromMatrix(mat) }
    }

    /// Rotation matrix from quaternion. (raymath `QuaternionToMatrix`)
    #[inline]
    #[must_use]
    pub fn to_matrix(self) -> Matrix {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionToMatrix(self) }
    }

    /// Quaternion from axis and angle (radians). (raymath `QuaternionFromAxisAngle`)
    #[inline]
    #[must_use]
    pub fn from_axis_angle(axis: Vector3, angle: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionFromAxisAngle(axis, angle) }
    }

    /// Quaternion from Euler angles (pitch, yaw, roll in radians). (raymath `QuaternionFromEuler`)
    #[inline]
    #[must_use]
    pub fn from_euler(pitch: f32, yaw: f32, roll: f32) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionFromEuler(pitch, yaw, roll) }
    }

    /// Convert quaternion to Euler angles (roll, pitch, yaw in radians). (raymath `QuaternionToEuler`)
    #[inline]
    #[must_use]
    pub fn to_euler(self) -> Vector3 {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionToEuler(self) }
    }

    /// Transform quaternion by matrix. (raymath `QuaternionTransform`)
    #[inline]
    #[must_use]
    pub fn transform(self, mat: Matrix) -> Self {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionTransform(self, mat) }
    }

    /// Approximate equality (uses raymath epsilon). (raymath `QuaternionEquals`)
    #[inline]
    #[must_use]
    pub fn equals(self, other: Quaternion) -> bool {
        // SAFETY: pure value-in/out, no preconditions.
        unsafe { crate::QuaternionEquals(self, other) != 0 }
    }
}

impl core::ops::Add for Quaternion {
    type Output = Quaternion;
    #[inline]
    fn add(self, rhs: Quaternion) -> Quaternion {
        // SAFETY: QuaternionAdd is a pure value-in/out raymath fn; no preconditions.
        unsafe { crate::QuaternionAdd(self, rhs) }
    }
}
impl core::ops::AddAssign for Quaternion {
    #[inline]
    fn add_assign(&mut self, rhs: Quaternion) {
        *self = *self + rhs;
    }
}

impl core::ops::Sub for Quaternion {
    type Output = Quaternion;
    #[inline]
    fn sub(self, rhs: Quaternion) -> Quaternion {
        // SAFETY: QuaternionSubtract is a pure value-in/out raymath fn; no preconditions.
        unsafe { crate::QuaternionSubtract(self, rhs) }
    }
}
impl core::ops::SubAssign for Quaternion {
    #[inline]
    fn sub_assign(&mut self, rhs: Quaternion) {
        *self = *self - rhs;
    }
}

impl core::ops::Mul for Quaternion {
    type Output = Quaternion;
    #[inline]
    fn mul(self, rhs: Quaternion) -> Quaternion {
        // SAFETY: QuaternionMultiply is a pure value-in/out raymath fn; no preconditions.
        unsafe { crate::QuaternionMultiply(self, rhs) }
    }
}
impl core::ops::MulAssign for Quaternion {
    #[inline]
    fn mul_assign(&mut self, rhs: Quaternion) {
        *self = *self * rhs;
    }
}

impl core::ops::Mul<f32> for Quaternion {
    type Output = Quaternion;
    #[inline]
    fn mul(self, rhs: f32) -> Quaternion {
        // SAFETY: QuaternionScale is a pure value-in/out raymath fn; no preconditions.
        unsafe { crate::QuaternionScale(self, rhs) }
    }
}
impl core::ops::MulAssign<f32> for Quaternion {
    #[inline]
    fn mul_assign(&mut self, rhs: f32) {
        *self = *self * rhs;
    }
}

impl core::ops::Div for Quaternion {
    type Output = Quaternion;
    #[inline]
    fn div(self, rhs: Quaternion) -> Quaternion {
        // SAFETY: QuaternionDivide is a pure value-in/out raymath fn; no preconditions.
        unsafe { crate::QuaternionDivide(self, rhs) }
    }
}
impl core::ops::DivAssign for Quaternion {
    #[inline]
    fn div_assign(&mut self, rhs: Quaternion) {
        *self = *self / rhs;
    }
}

/// Convert quaternion to axis-angle representation.
/// Returns `(axis, angle_radians)`. (raymath `QuaternionToAxisAngle`)
pub fn quaternion_to_axis_angle(q: Quaternion) -> (Vector3, f32) {
    let mut out_axis = Vector3 {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    };
    let mut out_angle: f32 = 0.0;
    // SAFETY: QuaternionToAxisAngle writes through non-null, properly aligned out-ptrs;
    // both are distinct stack allocations with no aliasing.
    unsafe { crate::QuaternionToAxisAngle(q, &mut out_axis, &mut out_angle) };
    (out_axis, out_angle)
}