From fffb401638ac08e395785eadb1ab0ae1d17613aa Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 14 Apr 2024 23:19:56 +0200 Subject: feat(engine): create texture IDs automatically --- engine/src/texture.rs | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'engine/src/texture.rs') diff --git a/engine/src/texture.rs b/engine/src/texture.rs index b0f0278..5cea9ff 100644 --- a/engine/src/texture.rs +++ b/engine/src/texture.rs @@ -1,6 +1,6 @@ -use std::collections::HashMap; use std::fmt::Display; use std::path::Path; +use std::sync::atomic::{AtomicU32, Ordering}; use ecs::Component; use image::io::Reader as ImageReader; @@ -10,6 +10,8 @@ use crate::color::Color; use crate::opengl::texture::PixelDataFormat; use crate::vector::Vec2; +static NEXT_ID: AtomicU32 = AtomicU32::new(0); + mod reexports { pub use crate::opengl::texture::{Filtering, Wrapping}; @@ -20,6 +22,7 @@ pub use reexports::*; #[derive(Debug, Clone)] pub struct Texture { + id: Id, image: DynamicImage, pixel_data_format: PixelDataFormat, dimensions: Vec2, @@ -52,7 +55,10 @@ impl Texture let dimensions = Vec2 { x: image.width(), y: image.height() }; + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + Ok(Self { + id: Id::new(id), image, pixel_data_format, dimensions, @@ -69,7 +75,10 @@ impl Texture Rgb([color.red, color.green, color.blue]), ); + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + Self { + id: Id::new(id), image: image.into(), pixel_data_format: PixelDataFormat::Rgb, dimensions: dimensions.clone(), @@ -77,6 +86,12 @@ impl Texture } } + #[must_use] + pub fn id(&self) -> Id + { + self.id + } + #[must_use] pub fn properties(&self) -> &Properties { @@ -107,6 +122,14 @@ impl Texture } } +impl Drop for Texture +{ + fn drop(&mut self) + { + NEXT_ID.fetch_sub(1, Ordering::Relaxed); + } +} + /// Texture error. #[derive(Debug, thiserror::Error)] pub enum Error @@ -152,9 +175,7 @@ pub struct Id impl Id { - /// Returns a new texture ID. - #[must_use] - pub fn new(id: u32) -> Self + fn new(id: u32) -> Self { Self { id } } @@ -173,19 +194,19 @@ impl Display for Id } } -/// Texture map. +/// Texture list. #[derive(Component)] -pub struct Map +pub struct List { - pub inner: HashMap, + pub list: Vec, } -impl FromIterator<(Id, Texture)> for Map +impl FromIterator for List { fn from_iter(iter: Iter) -> Self where - Iter: IntoIterator, + Iter: IntoIterator, { - Self { inner: iter.into_iter().collect() } + Self { list: iter.into_iter().collect() } } } -- cgit v1.2.3-18-g5258