From 73e64946f7dab53bd75006882ddc1c4b16e2a486 Mon Sep 17 00:00:00 2001 From: gered Date: Fri, 26 May 2023 11:58:30 -0400 Subject: [PATCH] convenience method to clone single BitmapAtlas tiles as new bitmaps --- ggdt/src/graphics/bitmapatlas.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ggdt/src/graphics/bitmapatlas.rs b/ggdt/src/graphics/bitmapatlas.rs index 5f5daab..b22dbc0 100644 --- a/ggdt/src/graphics/bitmapatlas.rs +++ b/ggdt/src/graphics/bitmapatlas.rs @@ -2,13 +2,16 @@ use std::ops::Index; use thiserror::Error; -use crate::graphics::GeneralBitmap; +use crate::graphics::{GeneralBitmap, GeneralBlitMethod}; use crate::math::Rect; #[derive(Error, Debug)] pub enum BitmapAtlasError { #[error("Region is out of bounds for the Bitmap used by the BitmapAtlas")] OutOfBounds, + + #[error("Tile index {0} is invalid / out of range")] + InvalidTileIndex(usize), } #[derive(Debug, Clone, Eq, PartialEq)] @@ -118,6 +121,16 @@ where pub fn bitmap(&self) -> &BitmapType { &self.bitmap } + + pub fn clone_tile(&self, index: usize) -> Result { + if let Some(tile_rect) = self.get(index) { + let mut tile_bitmap = BitmapType::new(tile_rect.width, tile_rect.height).unwrap(); + tile_bitmap.blit_region(GeneralBlitMethod::Solid, &self.bitmap, tile_rect, 0, 0); + Ok(tile_bitmap) + } else { + Err(BitmapAtlasError::InvalidTileIndex(index)) + } + } } impl Index for BitmapAtlas