summaryrefslogtreecommitdiff
path: root/engine/src/texture.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-04-14 23:19:56 +0200
committerHampusM <hampus@hampusmat.com>2024-04-14 23:19:56 +0200
commitfffb401638ac08e395785eadb1ab0ae1d17613aa (patch)
treebe4ad64e2149b1a57349a83121ddd5e46f15881b /engine/src/texture.rs
parent2302a232de4132a25153071049d31bc59ae70b35 (diff)
feat(engine): create texture IDs automatically
Diffstat (limited to 'engine/src/texture.rs')
-rw-r--r--engine/src/texture.rs41
1 files changed, 31 insertions, 10 deletions
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<u32>,
@@ -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(),
@@ -78,6 +87,12 @@ impl Texture
}
#[must_use]
+ pub fn id(&self) -> Id
+ {
+ self.id
+ }
+
+ #[must_use]
pub fn properties(&self) -> &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<Id, Texture>,
+ pub list: Vec<Texture>,
}
-impl FromIterator<(Id, Texture)> for Map
+impl FromIterator<Texture> for List
{
fn from_iter<Iter>(iter: Iter) -> Self
where
- Iter: IntoIterator<Item = (Id, Texture)>,
+ Iter: IntoIterator<Item = Texture>,
{
- Self { inner: iter.into_iter().collect() }
+ Self { list: iter.into_iter().collect() }
}
}