summaryrefslogtreecommitdiff
path: root/engine/src/texture.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-05-25 17:34:45 +0200
committerHampusM <hampus@hampusmat.com>2025-06-03 14:08:40 +0200
commita109ac00e67129ddb358b6416a3241f22a441ad9 (patch)
treeb0d245c01ef4be675aacfcabd6ccfc6b7cd5889e /engine/src/texture.rs
parent68a97b0123da1f181d1c78a7f760c55c3f341014 (diff)
feat(engine): add asset management
Diffstat (limited to 'engine/src/texture.rs')
-rw-r--r--engine/src/texture.rs206
1 files changed, 15 insertions, 191 deletions
diff --git a/engine/src/texture.rs b/engine/src/texture.rs
index 4a4fe86..f119ac3 100644
--- a/engine/src/texture.rs
+++ b/engine/src/texture.rs
@@ -1,17 +1,7 @@
-use std::fmt::Display;
-use std::path::Path;
-use std::sync::atomic::{AtomicU32, Ordering};
-
-use image::io::Reader as ImageReader;
-use image::{DynamicImage, ImageError, Rgb, RgbImage};
-
-use crate::color::Color;
-use crate::data_types::dimens::Dimens;
-use crate::opengl::texture::PixelDataFormat;
+use crate::asset::Handle as AssetHandle;
+use crate::image::Image;
use crate::util::builder;
-static NEXT_ID: AtomicU32 = AtomicU32::new(0);
-
mod reexports
{
pub use crate::opengl::texture::{Filtering, Wrapping};
@@ -20,175 +10,32 @@ mod reexports
pub use reexports::*;
#[derive(Debug, Clone)]
+#[non_exhaustive]
pub struct Texture
{
- id: Id,
- image: DynamicImage,
- pixel_data_format: PixelDataFormat,
- dimensions: Dimens<u32>,
- properties: Properties,
+ pub asset_handle: AssetHandle<Image>,
+ pub properties: Properties,
}
impl Texture
{
- pub fn builder() -> Builder
- {
- Builder::default()
- }
-
- /// Opens a texture image.
- ///
- /// # Errors
- /// Will return `Err` if:
- /// - Opening the image fails
- /// - The image data is not 8-bit/color RGB
- pub fn open(path: &Path) -> Result<Self, Error>
- {
- Self::builder().open(path)
- }
-
- #[must_use]
- pub fn new_from_color(dimensions: &Dimens<u32>, color: &Color<u8>) -> Self
- {
- Self::builder().build_with_single_color(dimensions, color)
- }
-
- #[must_use]
- pub fn id(&self) -> Id
- {
- self.id
- }
-
- #[must_use]
- pub fn properties(&self) -> &Properties
- {
- &self.properties
- }
-
- pub fn properties_mut(&mut self) -> &mut Properties
- {
- &mut self.properties
- }
-
- #[must_use]
- pub fn dimensions(&self) -> &Dimens<u32>
- {
- &self.dimensions
- }
-
- #[must_use]
- pub fn pixel_data_format(&self) -> PixelDataFormat
- {
- self.pixel_data_format
- }
-
- #[must_use]
- pub fn image(&self) -> &DynamicImage
- {
- &self.image
- }
-}
-
-impl Drop for Texture
-{
- fn drop(&mut self)
+ pub fn new(asset_handle: AssetHandle<Image>) -> Self
{
- NEXT_ID.fetch_sub(1, Ordering::Relaxed);
- }
-}
-
-/// Texture builder.
-#[derive(Debug, Default, Clone)]
-pub struct Builder
-{
- properties: Properties,
-}
-
-impl Builder
-{
- pub fn properties(mut self, properties: Properties) -> Self
- {
- self.properties = properties;
- self
- }
-
- /// Opens a image as a texture.
- ///
- /// # Errors
- /// Will return `Err` if:
- /// - Opening the image fails
- /// - Decoding the image fails
- /// - The image data is in a unsupported format
- pub fn open(&self, path: &(impl AsRef<Path> + ?Sized)) -> Result<Texture, Error>
- {
- let image = ImageReader::open(path)
- .map_err(Error::OpenImageFailed)?
- .decode()
- .map_err(Error::DecodeImageFailed)?;
-
- let pixel_data_format = match &image {
- DynamicImage::ImageRgb8(_) => PixelDataFormat::Rgb8,
- DynamicImage::ImageRgba8(_) => PixelDataFormat::Rgba8,
- _ => {
- return Err(Error::UnsupportedImageDataFormat);
- }
- };
-
- let dimensions = Dimens {
- width: image.width(),
- height: image.height(),
- };
-
- let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
-
- Ok(Texture {
- id: Id::new(id),
- image,
- pixel_data_format,
- dimensions,
- properties: self.properties.clone(),
- })
+ Self {
+ asset_handle,
+ properties: Properties::default(),
+ }
}
- #[must_use]
- pub fn build_with_single_color(
- &self,
- dimensions: &Dimens<u32>,
- color: &Color<u8>,
- ) -> Texture
+ pub fn with_properties(
+ asset_handle: AssetHandle<Image>,
+ properties: Properties,
+ ) -> Self
{
- let image = RgbImage::from_pixel(
- dimensions.width,
- dimensions.height,
- Rgb([color.red, color.green, color.blue]),
- );
-
- let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
-
- Texture {
- id: Id::new(id),
- image: image.into(),
- pixel_data_format: PixelDataFormat::Rgb8,
- dimensions: *dimensions,
- properties: self.properties.clone(),
- }
+ Self { asset_handle, properties }
}
}
-/// Texture error.
-#[derive(Debug, thiserror::Error)]
-pub enum Error
-{
- #[error("Failed to open texture image")]
- OpenImageFailed(#[source] std::io::Error),
-
- #[error("Failed to decode texture image")]
- DecodeImageFailed(#[source] ImageError),
-
- #[error("Unsupported image data format")]
- UnsupportedImageDataFormat,
-}
-
builder! {
/// Texture properties
#[builder(name = PropertiesBuilder, derives=(Debug, Clone))]
@@ -229,26 +76,3 @@ impl Default for PropertiesBuilder
Properties::default().into()
}
}
-
-/// Texture ID.
-#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
-pub struct Id
-{
- id: u32,
-}
-
-impl Id
-{
- fn new(id: u32) -> Self
- {
- Self { id }
- }
-}
-
-impl Display for Id
-{
- fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
- {
- self.id.fmt(formatter)
- }
-}