From 7a7fab532c88c7ce7be5dc35f7cd70d0cb369a16 Mon Sep 17 00:00:00 2001 From: Micha White Date: Thu, 13 Oct 2022 21:02:34 -0400 Subject: You can load images now! --- src/texture.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/texture.rs b/src/texture.rs index ac2dd63..3b998a8 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -40,14 +40,26 @@ impl ImageFormat { } } -type PackError = impl std::fmt::Debug; +#[derive(Debug, Error)] +#[error("{:?}", .0)] +pub struct PackError(PackErrorInternal); + +type PackErrorInternal = impl std::fmt::Debug; #[derive(Error, Debug)] pub enum TextureError { #[error("{:?}", .0)] - TextureTooLarge(PackError), // use an error with a source + TextureTooLarge( + #[source] + #[from] + PackError, + ), #[error("{}", .0)] - BadImage(#[source] DecodingError), // TODO don't export this + BadImage( + #[source] + #[from] + DecodingError, + ), // TODO don't export this #[error("Unexpected Error (this is a bug in alligator_render): {}", .0)] Unexpected(#[source] Box), } @@ -55,7 +67,7 @@ pub enum TextureError { impl From for TextureError { fn from(ie: ImageError) -> Self { match ie { - ImageError::Decoding(de) => Self::BadImage(de), + ImageError::Decoding(de) => de.into(), _ => Self::Unexpected(Box::new(ie)), } } @@ -189,11 +201,12 @@ impl TextureAtlas { format: ImageFormat, ) -> Result { let img = image::load_from_memory_with_format(buf, format.format())?.into_rgba8(); - let id = TextureId::new(); - self.packer - .pack_own(id, img) - .map_err(TextureError::TextureTooLarge)?; + Ok(self.pack_rgba8(img)?) + } + pub fn pack_rgba8(&mut self, img: RgbaImage) -> Result { + let id = TextureId::new(); + self.packer.pack_own(id, img).map_err(PackError)?; Ok(id) } -- cgit v1.2.3