mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-11 09:31:34 +11:00
add docs
This commit is contained in:
parent
5e8a50159e
commit
c09c0b77f4
|
@ -1,3 +1,23 @@
|
||||||
|
#![warn(missing_docs)]
|
||||||
|
//! # Affine matricies for the Game Boy Advance
|
||||||
|
//!
|
||||||
|
//! An affine matrix represents an affine transformation, an affine
|
||||||
|
//! transformation being one which preserves parallel lines (note that this
|
||||||
|
//! therefore cannot represent perspective seen in games like Super Mario Kart).
|
||||||
|
//! Affine matricies are used in two places on the GBA, for affine backgrounds
|
||||||
|
//! and for affine objects.
|
||||||
|
//!
|
||||||
|
//! # Linear Algebra basics
|
||||||
|
//! As a matrix, they can be manipulated using linear algebra, although you
|
||||||
|
//! shouldn't need to know linear algebra to use this apart from a few things
|
||||||
|
//!
|
||||||
|
//! If `A` and `B` are matricies, then matrix `C = A * B` represents the
|
||||||
|
//! transformation `A` performed on `B`, or alternatively `C` is transformation
|
||||||
|
//! `B` followed by transformation `A`.
|
||||||
|
//!
|
||||||
|
//! Additionally matrix multiplication is not commutative, meaning swapping the
|
||||||
|
//! order changes the result, or `A * B ≢ B * A`.
|
||||||
|
|
||||||
use core::{
|
use core::{
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
ops::{Mul, MulAssign},
|
ops::{Mul, MulAssign},
|
||||||
|
@ -8,6 +28,8 @@ use agb_fixnum::{Num, Vector2D};
|
||||||
type AffineMatrixElement = Num<i32, 8>;
|
type AffineMatrixElement = Num<i32, 8>;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
|
/// An affine matrix stored in a way that is efficient for the GBA to perform
|
||||||
|
/// operations on. This implements multiplication.
|
||||||
pub struct AffineMatrix {
|
pub struct AffineMatrix {
|
||||||
a: AffineMatrixElement,
|
a: AffineMatrixElement,
|
||||||
b: AffineMatrixElement,
|
b: AffineMatrixElement,
|
||||||
|
@ -18,10 +40,14 @@ pub struct AffineMatrix {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
/// The error emitted upon a conversion that could not be performed due to
|
||||||
|
/// overflowing the destination data size
|
||||||
pub struct OverflowError(pub(crate) ());
|
pub struct OverflowError(pub(crate) ());
|
||||||
|
|
||||||
impl AffineMatrix {
|
impl AffineMatrix {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
/// The Identity matrix. The identity matrix can be thought of as 1 and is
|
||||||
|
/// represented by `I`. For a matrix `A`, `A ≡ A * I ≡ I * A`.
|
||||||
pub fn identity() -> Self {
|
pub fn identity() -> Self {
|
||||||
AffineMatrix {
|
AffineMatrix {
|
||||||
a: 1.into(),
|
a: 1.into(),
|
||||||
|
@ -34,6 +60,7 @@ impl AffineMatrix {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
/// Generates the matrix that represents a rotation
|
||||||
pub fn from_rotation<const N: usize>(angle: Num<i32, N>) -> Self {
|
pub fn from_rotation<const N: usize>(angle: Num<i32, N>) -> Self {
|
||||||
fn from_rotation(angle: Num<i32, 28>) -> AffineMatrix {
|
fn from_rotation(angle: Num<i32, 28>) -> AffineMatrix {
|
||||||
let cos = angle.cos().change_base();
|
let cos = angle.cos().change_base();
|
||||||
|
@ -52,6 +79,7 @@ impl AffineMatrix {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identity for rotation / scale / skew
|
// Identity for rotation / scale / skew
|
||||||
|
/// Generates the matrix that represents a translation by the position
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn from_position(position: Vector2D<Num<i32, 8>>) -> Self {
|
pub fn from_position(position: Vector2D<Num<i32, 8>>) -> Self {
|
||||||
AffineMatrix {
|
AffineMatrix {
|
||||||
|
@ -65,10 +93,13 @@ impl AffineMatrix {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
/// The position fields of the matrix
|
||||||
pub fn position(&self) -> Vector2D<Num<i32, 8>> {
|
pub fn position(&self) -> Vector2D<Num<i32, 8>> {
|
||||||
(self.x, self.y).into()
|
(self.x, self.y).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attempts to convert the matrix to one which can be used in affine
|
||||||
|
/// backgrounds.
|
||||||
pub fn try_to_background(&self) -> Result<AffineMatrixBackground, OverflowError> {
|
pub fn try_to_background(&self) -> Result<AffineMatrixBackground, OverflowError> {
|
||||||
Ok(AffineMatrixBackground {
|
Ok(AffineMatrixBackground {
|
||||||
a: self.a.to_raw().try_into().map_err(|_| OverflowError(()))?,
|
a: self.a.to_raw().try_into().map_err(|_| OverflowError(()))?,
|
||||||
|
@ -81,6 +112,8 @@ impl AffineMatrix {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
/// Converts the matrix to one which can be used in affine backgrounds
|
||||||
|
/// wrapping any value which is too large to be represented there.
|
||||||
pub fn to_background_wrapping(&self) -> AffineMatrixBackground {
|
pub fn to_background_wrapping(&self) -> AffineMatrixBackground {
|
||||||
AffineMatrixBackground {
|
AffineMatrixBackground {
|
||||||
a: self.a.to_raw() as i16,
|
a: self.a.to_raw() as i16,
|
||||||
|
@ -95,6 +128,7 @@ impl AffineMatrix {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
#[repr(C, packed(4))]
|
#[repr(C, packed(4))]
|
||||||
|
/// An affine matrix that can be used in affine backgrounds
|
||||||
pub struct AffineMatrixBackground {
|
pub struct AffineMatrixBackground {
|
||||||
// Internally these can be thought of as Num<i16, 8>
|
// Internally these can be thought of as Num<i16, 8>
|
||||||
a: i16,
|
a: i16,
|
||||||
|
@ -116,6 +150,8 @@ impl TryFrom<AffineMatrix> for AffineMatrixBackground {
|
||||||
|
|
||||||
impl AffineMatrixBackground {
|
impl AffineMatrixBackground {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
/// Converts to the affine matrix that is usable in performing efficient
|
||||||
|
/// calculations.
|
||||||
pub fn to_affine_matrix(&self) -> AffineMatrix {
|
pub fn to_affine_matrix(&self) -> AffineMatrix {
|
||||||
AffineMatrix {
|
AffineMatrix {
|
||||||
a: Num::from_raw(self.a.into()),
|
a: Num::from_raw(self.a.into()),
|
||||||
|
|
Loading…
Reference in a new issue