rename parts of objects

This commit is contained in:
Corwin 2023-04-04 17:35:53 +01:00
parent d183b8373d
commit d6006c7808
No known key found for this signature in database
10 changed files with 60 additions and 59 deletions

View file

@ -4,7 +4,7 @@
use agb::{ use agb::{
display::tiled::{TileFormat, TileSet, TileSetting, TiledMap}, display::tiled::{TileFormat, TileSet, TileSetting, TiledMap},
display::{ display::{
object::{OAMManager, Object, Size, Sprite}, object::{OamManager, Object, Size, Sprite},
palette16::Palette16, palette16::Palette16,
tiled::RegularBackgroundSize, tiled::RegularBackgroundSize,
HEIGHT, WIDTH, HEIGHT, WIDTH,
@ -145,7 +145,7 @@ fn main(mut gba: agb::Gba) -> ! {
fn update_chicken_object( fn update_chicken_object(
chicken: &'_ mut Character<'_>, chicken: &'_ mut Character<'_>,
gfx: &OAMManager, gfx: &OamManager,
state: State, state: State,
frame_count: u32, frame_count: u32,
) { ) {

View file

@ -5,7 +5,7 @@ extern crate alloc;
use agb::display::{ use agb::display::{
affine::AffineMatrix, affine::AffineMatrix,
object::{self, Graphics, OAMManager, Sprite, TagMap}, object::{self, Graphics, OamManager, Sprite, TagMap},
}; };
use agb::fixnum::num; use agb::fixnum::num;
use agb_fixnum::Num; use agb_fixnum::Num;
@ -20,14 +20,14 @@ const GRAPHICS: &Graphics = agb::include_aseprite!(
const SPRITES: &[Sprite] = GRAPHICS.sprites(); const SPRITES: &[Sprite] = GRAPHICS.sprites();
const TAG_MAP: &TagMap = GRAPHICS.tags(); const TAG_MAP: &TagMap = GRAPHICS.tags();
fn all_sprites(gfx: &OAMManager, rotation_speed: Num<i32, 16>) { fn all_sprites(gfx: &OamManager, rotation_speed: Num<i32, 16>) {
let mut input = agb::input::ButtonController::new(); let mut input = agb::input::ButtonController::new();
let mut objs = Vec::new(); let mut objs = Vec::new();
let mut rotation: Num<i32, 16> = num!(0.); let mut rotation: Num<i32, 16> = num!(0.);
let rotation_matrix = AffineMatrix::from_rotation(rotation); let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrix::new(rotation_matrix.to_object_wrapping()); let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for y in 0..9 { for y in 0..9 {
for x in 0..14 { for x in 0..14 {
@ -55,7 +55,7 @@ fn all_sprites(gfx: &OAMManager, rotation_speed: Num<i32, 16>) {
rotation += rotation_speed; rotation += rotation_speed;
let rotation_matrix = AffineMatrix::from_rotation(rotation); let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrix::new(rotation_matrix.to_object_wrapping()); let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for obj in objs.iter_mut() { for obj in objs.iter_mut() {
obj.set_affine_matrix(matrix.clone()); obj.set_affine_matrix(matrix.clone());
@ -75,7 +75,7 @@ fn all_sprites(gfx: &OAMManager, rotation_speed: Num<i32, 16>) {
} }
} }
fn all_tags(gfx: &OAMManager) { fn all_tags(gfx: &OamManager) {
let mut input = agb::input::ButtonController::new(); let mut input = agb::input::ButtonController::new();
let mut objs = Vec::new(); let mut objs = Vec::new();

View file

@ -6,7 +6,7 @@ use video::Video;
use self::{ use self::{
blend::Blend, blend::Blend,
object::{initilise_oam, OAMManager, StaticSpriteLoader, UnmanagedOAM}, object::{initilise_oam, OamManager, OamUnmanaged, SpriteLoader},
window::Windows, window::Windows,
}; };
@ -84,14 +84,14 @@ pub struct Display {
pub struct ObjectDistribution; pub struct ObjectDistribution;
impl ObjectDistribution { impl ObjectDistribution {
pub fn get_unmanaged(&mut self) -> (UnmanagedOAM<'_>, StaticSpriteLoader) { pub fn get_unmanaged(&mut self) -> (OamUnmanaged<'_>, SpriteLoader) {
unsafe { initilise_oam() }; unsafe { initilise_oam() };
(UnmanagedOAM::new(), StaticSpriteLoader::new()) (OamUnmanaged::new(), SpriteLoader::new())
} }
pub fn get_managed(&mut self) -> OAMManager<'_> { pub fn get_managed(&mut self) -> OamManager<'_> {
unsafe { initilise_oam() }; unsafe { initilise_oam() };
OAMManager::new() OamManager::new()
} }
} }

View file

@ -4,13 +4,12 @@ mod sprites;
mod unmanaged; mod unmanaged;
pub use sprites::{ pub use sprites::{
include_aseprite, DynamicSprite, Graphics, Size, Sprite, SpriteVram, StaticSpriteLoader, Tag, include_aseprite, DynamicSprite, Graphics, Size, Sprite, SpriteLoader, SpriteVram, Tag, TagMap,
TagMap,
}; };
pub use affine::AffineMatrix; pub use affine::AffineMatrixInstance;
pub use managed::{OAMManager, Object}; pub use managed::{OamManager, Object};
pub use unmanaged::{AffineMode, OAMIterator, OAMSlot, UnmanagedOAM, UnmanagedObject}; pub use unmanaged::{AffineMode, OamIterator, OamSlot, OamUnmanaged, ObjectUnmanaged};
use super::DISPLAY_CONTROL; use super::DISPLAY_CONTROL;

View file

@ -15,14 +15,14 @@ struct AffineMatrixData {
pub(crate) struct AffineMatrixVram(Rc<AffineMatrixData>); pub(crate) struct AffineMatrixVram(Rc<AffineMatrixData>);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AffineMatrix { pub struct AffineMatrixInstance {
location: AffineMatrixVram, location: AffineMatrixVram,
} }
impl AffineMatrix { impl AffineMatrixInstance {
#[must_use] #[must_use]
pub fn new(affine_matrix: AffineMatrixObject) -> AffineMatrix { pub fn new(affine_matrix: AffineMatrixObject) -> AffineMatrixInstance {
AffineMatrix { AffineMatrixInstance {
location: AffineMatrixVram(Rc::new(AffineMatrixData { location: AffineMatrixVram(Rc::new(AffineMatrixData {
frame_count: Cell::new(u32::MAX), frame_count: Cell::new(u32::MAX),
location: Cell::new(u32::MAX), location: Cell::new(u32::MAX),
@ -72,8 +72,8 @@ mod tests {
#[test_case] #[test_case]
fn niche_optimisation(_gba: &mut crate::Gba) { fn niche_optimisation(_gba: &mut crate::Gba) {
assert_eq!( assert_eq!(
core::mem::size_of::<AffineMatrix>(), core::mem::size_of::<AffineMatrixInstance>(),
core::mem::size_of::<Option<AffineMatrix>>() core::mem::size_of::<Option<AffineMatrixInstance>>()
); );
} }
} }

View file

@ -6,7 +6,8 @@ use slotmap::{new_key_type, SlotMap};
use crate::display::Priority; use crate::display::Priority;
use super::{ use super::{
AffineMatrix, AffineMode, Sprite, SpriteVram, StaticSpriteLoader, UnmanagedOAM, UnmanagedObject, AffineMatrixInstance, AffineMode, OamUnmanaged, ObjectUnmanaged, Sprite, SpriteLoader,
SpriteVram,
}; };
new_key_type! {struct ObjectKey; } new_key_type! {struct ObjectKey; }
@ -18,7 +19,7 @@ struct Ordering {
} }
struct ObjectItem { struct ObjectItem {
object: UnsafeCell<UnmanagedObject>, object: UnsafeCell<ObjectUnmanaged>,
z_order: Cell<Ordering>, z_order: Cell<Ordering>,
z_index: Cell<i32>, z_index: Cell<i32>,
} }
@ -71,7 +72,7 @@ impl Store {
true true
} }
fn insert_object(&self, object: UnmanagedObject) -> Object { fn insert_object(&self, object: ObjectUnmanaged) -> Object {
let object_item = ObjectItem { let object_item = ObjectItem {
object: UnsafeCell::new(object), object: UnsafeCell::new(object),
z_order: Cell::new(Ordering { z_order: Cell::new(Ordering {
@ -121,21 +122,21 @@ impl Store {
} }
} }
pub struct OAMManager<'gba> { pub struct OamManager<'gba> {
object_store: Store, object_store: Store,
sprite_loader: UnsafeCell<StaticSpriteLoader>, sprite_loader: UnsafeCell<SpriteLoader>,
unmanaged: UnsafeCell<UnmanagedOAM<'gba>>, unmanaged: UnsafeCell<OamUnmanaged<'gba>>,
} }
impl OAMManager<'_> { impl OamManager<'_> {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {
object_store: Store { object_store: Store {
store: UnsafeCell::new(SlotMap::with_key()), store: UnsafeCell::new(SlotMap::with_key()),
first_z: Cell::new(None), first_z: Cell::new(None),
}, },
sprite_loader: UnsafeCell::new(StaticSpriteLoader::new()), sprite_loader: UnsafeCell::new(SpriteLoader::new()),
unmanaged: UnsafeCell::new(UnmanagedOAM::new()), unmanaged: UnsafeCell::new(OamUnmanaged::new()),
} }
} }
@ -143,7 +144,7 @@ impl OAMManager<'_> {
/// Do not reenter or recurse or otherwise use sprite loader cell during this. /// Do not reenter or recurse or otherwise use sprite loader cell during this.
unsafe fn do_work_with_sprite_loader<C, T>(&self, c: C) -> T unsafe fn do_work_with_sprite_loader<C, T>(&self, c: C) -> T
where where
C: Fn(&mut StaticSpriteLoader) -> T, C: Fn(&mut SpriteLoader) -> T,
{ {
let sprite_loader = unsafe { &mut *self.sprite_loader.get() }; let sprite_loader = unsafe { &mut *self.sprite_loader.get() };
@ -164,13 +165,13 @@ impl OAMManager<'_> {
// safety: not reentrant // safety: not reentrant
unsafe { unsafe {
self.do_work_with_sprite_loader(StaticSpriteLoader::garbage_collect); self.do_work_with_sprite_loader(SpriteLoader::garbage_collect);
} }
} }
pub fn add_object(&self, sprite: SpriteVram) -> Object<'_> { pub fn add_object(&self, sprite: SpriteVram) -> Object<'_> {
self.object_store self.object_store
.insert_object(UnmanagedObject::new(sprite)) .insert_object(ObjectUnmanaged::new(sprite))
} }
pub fn get_vram_sprite(&self, sprite: &'static Sprite) -> SpriteVram { pub fn get_vram_sprite(&self, sprite: &'static Sprite) -> SpriteVram {
@ -335,13 +336,13 @@ impl Object<'_> {
/// Safety: /// Safety:
/// Only have *ONE* of these at a time, do not call any functions that modify the slot map while having this. /// Only have *ONE* of these at a time, do not call any functions that modify the slot map while having this.
unsafe fn object(&mut self) -> &mut UnmanagedObject { unsafe fn object(&mut self) -> &mut ObjectUnmanaged {
unsafe { &mut *self.store.get_object(self.me).object.get() } unsafe { &mut *self.store.get_object(self.me).object.get() }
} }
/// Safety: /// Safety:
/// Don't have a mutable one of these while having one of these, do not call any functions that modify the slot map while having this. /// Don't have a mutable one of these while having one of these, do not call any functions that modify the slot map while having this.
unsafe fn object_shared(&self) -> &UnmanagedObject { unsafe fn object_shared(&self) -> &ObjectUnmanaged {
unsafe { &*self.store.get_object(self.me).object.get() } unsafe { &*self.store.get_object(self.me).object.get() }
} }
@ -414,7 +415,7 @@ impl Object<'_> {
self self
} }
pub fn set_affine_matrix(&mut self, affine_matrix: AffineMatrix) -> &mut Self { pub fn set_affine_matrix(&mut self, affine_matrix: AffineMatrixInstance) -> &mut Self {
// safety: only have one of these, doesn't modify slotmap // safety: only have one of these, doesn't modify slotmap
unsafe { self.object().set_affine_matrix(affine_matrix) }; unsafe { self.object().set_affine_matrix(affine_matrix) };

View file

@ -4,4 +4,4 @@ mod sprite_allocator;
const BYTES_PER_TILE_4BPP: usize = 32; const BYTES_PER_TILE_4BPP: usize = 32;
pub use sprite::{include_aseprite, Graphics, Size, Sprite, Tag, TagMap}; pub use sprite::{include_aseprite, Graphics, Size, Sprite, Tag, TagMap};
pub use sprite_allocator::{DynamicSprite, SpriteVram, StaticSpriteLoader}; pub use sprite_allocator::{DynamicSprite, SpriteLoader, SpriteVram};

View file

@ -53,7 +53,7 @@ impl PaletteId {
} }
/// This holds loading of static sprites and palettes. /// This holds loading of static sprites and palettes.
pub struct StaticSpriteLoader { pub struct SpriteLoader {
static_palette_map: HashMap<PaletteId, Weak<PaletteVramData>>, static_palette_map: HashMap<PaletteId, Weak<PaletteVramData>>,
static_sprite_map: HashMap<SpriteId, Weak<SpriteVramData>>, static_sprite_map: HashMap<SpriteId, Weak<SpriteVramData>>,
} }
@ -159,7 +159,7 @@ impl SpriteVram {
} }
} }
impl StaticSpriteLoader { impl SpriteLoader {
fn create_sprite_no_insert( fn create_sprite_no_insert(
palette_map: &mut HashMap<PaletteId, Weak<PaletteVramData>>, palette_map: &mut HashMap<PaletteId, Weak<PaletteVramData>>,
sprite: &'static Sprite, sprite: &'static Sprite,
@ -245,7 +245,7 @@ impl StaticSpriteLoader {
} }
} }
impl Default for StaticSpriteLoader { impl Default for SpriteLoader {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }

View file

@ -2,4 +2,4 @@ mod attributes;
mod object; mod object;
pub use attributes::AffineMode; pub use attributes::AffineMode;
pub use object::{OAMIterator, OAMSlot, UnmanagedOAM, UnmanagedObject}; pub use object::{OamIterator, OamSlot, OamUnmanaged, ObjectUnmanaged};

View file

@ -5,7 +5,8 @@ use alloc::vec::Vec;
use crate::display::{ use crate::display::{
object::{ object::{
affine::AffineMatrixVram, sprites::SpriteVram, AffineMatrix, OBJECT_ATTRIBUTE_MEMORY, affine::AffineMatrixVram, sprites::SpriteVram, AffineMatrixInstance,
OBJECT_ATTRIBUTE_MEMORY,
}, },
Priority, Priority,
}; };
@ -20,24 +21,24 @@ struct OamFrameModifyables {
affine_matrix_count: u32, affine_matrix_count: u32,
} }
pub struct UnmanagedOAM<'gba> { pub struct OamUnmanaged<'gba> {
phantom: PhantomData<&'gba ()>, phantom: PhantomData<&'gba ()>,
frame_data: UnsafeCell<OamFrameModifyables>, frame_data: UnsafeCell<OamFrameModifyables>,
previous_frame_sprites: Vec<SpriteVram>, previous_frame_sprites: Vec<SpriteVram>,
} }
pub struct OAMIterator<'oam> { pub struct OamIterator<'oam> {
index: usize, index: usize,
frame_data: &'oam UnsafeCell<OamFrameModifyables>, frame_data: &'oam UnsafeCell<OamFrameModifyables>,
} }
pub struct OAMSlot<'oam> { pub struct OamSlot<'oam> {
slot: usize, slot: usize,
frame_data: &'oam UnsafeCell<OamFrameModifyables>, frame_data: &'oam UnsafeCell<OamFrameModifyables>,
} }
impl OAMSlot<'_> { impl OamSlot<'_> {
pub fn set(&mut self, object: &UnmanagedObject) { pub fn set(&mut self, object: &ObjectUnmanaged) {
let mut attributes = object.attributes; let mut attributes = object.attributes;
// SAFETY: This function is not reentrant and we currently hold a mutable borrow of the [UnmanagedOAM]. // SAFETY: This function is not reentrant and we currently hold a mutable borrow of the [UnmanagedOAM].
let frame_data = unsafe { &mut *self.frame_data.get() }; let frame_data = unsafe { &mut *self.frame_data.get() };
@ -72,8 +73,8 @@ impl OAMSlot<'_> {
} }
} }
impl<'oam> Iterator for OAMIterator<'oam> { impl<'oam> Iterator for OamIterator<'oam> {
type Item = OAMSlot<'oam>; type Item = OamSlot<'oam>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let idx = self.index; let idx = self.index;
@ -82,7 +83,7 @@ impl<'oam> Iterator for OAMIterator<'oam> {
if idx >= 128 { if idx >= 128 {
None None
} else { } else {
Some(OAMSlot { Some(OamSlot {
slot: idx, slot: idx,
frame_data: self.frame_data, frame_data: self.frame_data,
}) })
@ -90,7 +91,7 @@ impl<'oam> Iterator for OAMIterator<'oam> {
} }
} }
impl Drop for OAMIterator<'_> { impl Drop for OamIterator<'_> {
fn drop(&mut self) { fn drop(&mut self) {
let last_written = unsafe { &*self.frame_data.get() }.up_to; let last_written = unsafe { &*self.frame_data.get() }.up_to;
@ -105,8 +106,8 @@ impl Drop for OAMIterator<'_> {
} }
} }
impl UnmanagedOAM<'_> { impl OamUnmanaged<'_> {
pub fn iter(&mut self) -> OAMIterator<'_> { pub fn iter(&mut self) -> OamIterator<'_> {
let frame_data = self.frame_data.get_mut(); let frame_data = self.frame_data.get_mut();
frame_data.up_to = -1; frame_data.up_to = -1;
frame_data.frame = frame_data.frame.wrapping_add(1); frame_data.frame = frame_data.frame.wrapping_add(1);
@ -120,7 +121,7 @@ impl UnmanagedOAM<'_> {
&mut self.previous_frame_sprites, &mut self.previous_frame_sprites,
); );
OAMIterator { OamIterator {
index: 0, index: 0,
frame_data: &self.frame_data, frame_data: &self.frame_data,
} }
@ -136,13 +137,13 @@ impl UnmanagedOAM<'_> {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct UnmanagedObject { pub struct ObjectUnmanaged {
attributes: Attributes, attributes: Attributes,
sprite: SpriteVram, sprite: SpriteVram,
affine_matrix: Option<AffineMatrixVram>, affine_matrix: Option<AffineMatrixVram>,
} }
impl UnmanagedObject { impl ObjectUnmanaged {
#[must_use] #[must_use]
pub fn new(sprite: SpriteVram) -> Self { pub fn new(sprite: SpriteVram) -> Self {
let sprite_location = sprite.location(); let sprite_location = sprite.location();
@ -226,7 +227,7 @@ impl UnmanagedObject {
self self
} }
pub fn set_affine_matrix(&mut self, affine_matrix: AffineMatrix) -> &mut Self { pub fn set_affine_matrix(&mut self, affine_matrix: AffineMatrixInstance) -> &mut Self {
let vram = affine_matrix.vram(); let vram = affine_matrix.vram();
self.affine_matrix = Some(vram); self.affine_matrix = Some(vram);