From a595ed8d9fbe6ff4a7a6f13c279b5412aeb41ab4 Mon Sep 17 00:00:00 2001 From: LawnCable Date: Fri, 15 Apr 2022 08:37:05 +0200 Subject: layer tiles, draft for object parsing and more --- src/lowlevel/layer_tile.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/lowlevel/layer_tile.rs (limited to 'src/lowlevel/layer_tile.rs') diff --git a/src/lowlevel/layer_tile.rs b/src/lowlevel/layer_tile.rs new file mode 100644 index 0000000..bca5daf --- /dev/null +++ b/src/lowlevel/layer_tile.rs @@ -0,0 +1,73 @@ +// Bits on the far end of the 32-bit global tile ID are used for tile flags +const FLIPPED_HORIZONTALLY_FLAG: u32 = 0x80000000; +const FLIPPED_VERTICALLY_FLAG: u32 = 0x40000000; +const FLIPPED_DIAGONALLY_FLAG: u32 = 0x20000000; + +#[derive(Debug, Default, PartialEq)] +pub struct TileFlip { + // whether tile is flipped diagonally (should be the first operation) + pub diagonally: bool, + // whether tile is flipped horizontally (should be the second operation) + pub horizontally: bool, + // whether tile is flipped vertically (should be the third operation) + pub vertically: bool, +} + +impl TileFlip { + fn new(diagonally: bool, horizontally: bool, vertically: bool) -> Self { + Self { + diagonally, + horizontally, + vertically, + } + } +} + +#[derive(Debug)] +pub struct LayerTile(u32); + +impl LayerTile { + /// Global Tile Id + pub fn gid(&self) -> u32 { + // Clear the flags + self.0 & !(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG) + } + + pub fn get_flip(&self) -> TileFlip { + // Read out the flags + TileFlip { + diagonally: (self.0 & FLIPPED_DIAGONALLY_FLAG) > 0, + horizontally: (self.0 & FLIPPED_HORIZONTALLY_FLAG) > 0, + vertically: (self.0 & FLIPPED_VERTICALLY_FLAG) > 0, + } + } +} + +#[cfg(test)] +mod test_layer_tile { + use crate::lowlevel::{layer_tile::*}; + + #[test] + fn test_get_flip() { + assert_eq!(LayerTile(13).get_flip(), TileFlip::default()); + assert_eq!( + LayerTile(3451 | FLIPPED_DIAGONALLY_FLAG).get_flip(), + TileFlip::new(true, false, false) + ); + } + + #[test] + fn test_gid() { + assert_eq!(LayerTile(5).gid(), 5); + assert_eq!( + LayerTile( + 52 | (FLIPPED_HORIZONTALLY_FLAG + | FLIPPED_VERTICALLY_FLAG + | FLIPPED_DIAGONALLY_FLAG) + ) + .gid(), + 52 + ); + assert_eq!(LayerTile(798 | (FLIPPED_DIAGONALLY_FLAG)).gid(), 798); + } +} -- cgit v1.2.3-60-g2f50