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/renderer/mod.rs | 10 +++++----- engine/src/texture.rs | 41 +++++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 15 deletions(-) (limited to 'engine') diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 304c38b..ef653a2 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -40,7 +40,7 @@ use crate::opengl::vertex_array::{PrimitiveKind, VertexArray}; use crate::opengl::{clear_buffers, enable, BufferClearMask, Capability}; use crate::projection::new_perspective; use crate::shader::Program as ShaderProgram; -use crate::texture::{Id as TextureId, Map as TextureMap, Texture}; +use crate::texture::{Id as TextureId, List as TextureMap, Texture}; use crate::transform::Transform; use crate::vector::{Vec2, Vec3}; use crate::vertex::Vertex; @@ -146,14 +146,14 @@ fn render( &shader_program_curr_bound, ); - for (texture_id, texture) in &texture_map.inner { + for texture in &texture_map.list { let gl_texture = gl_textures - .entry(*texture_id) + .entry(texture.id()) .or_insert_with(|| create_gl_texture(texture)); - let texture_unit = TextureUnit::from_texture_id(*texture_id) + let texture_unit = TextureUnit::from_texture_id(texture.id()) .unwrap_or_else(|| { - panic!("Texture id {texture_id} is a invalid texture unit"); + panic!("Texture id {} is a invalid texture unit", texture.id()); }); set_active_texture_unit(texture_unit); 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